[Solved] how to split a string for equation


To extract characters from a string and to store them into a Character Array

Simple Method (Using string indexing):

#include<iostream>

using namespace std;
// Using the namespace std to use string.

int main(int argc, char** argv) {
    string input;
    cout << "Enter a string: ";
    cin>>input;     // To read the input from the user.
    
    int strLength = input.length(); // To compute the length of the string.
    char * charArray = new char[strLength]; //Dynamic memory allocation

    /**
     * The following for loop converts the string into an array.
    */
    for (int i=0; i<strLength; i++) {
        charArray[i] = input[i];
    }

    cout << endl << "The elements of the character array are as follows: {";
    for (int i = 0; i < strLength; i++) {
        if (i != strLength - 1) {
            cout << "\'" << charArray[i] << "\', ";
        } else {
            cout << "\'" << charArray[i] << "\'}";
        }   
    }

    delete [] charArray;
    return 0;
}

Sample Output:

Enter a string: Hello

The elements of the character array are as follows: {'H', 'e', 'l', 'l', 'o'}
RUN SUCCESSFUL (total time: 3s)

To handle more complex inputs or if you are interested in a more complex structure, the following program should help you:

#include<iostream>

using namespace std;
// Using the namespace std to use string.

int main(int argc, char** argv) {
    string input;
    cout << "Enter a string: ";
    cin>>input;     // To read the input from the user.
    
    int strLength = input.length(); // To compute the length of the string.
    char * charArray = new char[strLength]; //Dynamic memory allocation
    int loopVar = 0;

    /**
     * The following for loop converts the string into an array.
    */
    for (const char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
        charArray[loopVar] = *iterativeVar;
        ++loopVar;
    }

    cout << endl << "The elements of the character array are as follows: {";
    for (int i = 0; i < strLength; i++) {
        if (i != strLength - 1) {
            cout << "\'" << charArray[i] << "\', ";
        } else {
            cout << "\'" << charArray[i] << "\'}";
        }   
    }

    delete [] charArray;
    return 0;
}

Sample Output:

Enter a string: Hello

The elements of the character array are as follows: {'H', 'e', 'l', 'l', 'o'}
RUN SUCCESSFUL (total time: 2s)

Remember to use const char * in the for loop. Otherwise you will get an error as follows:

newmain.cpp: In function 'int main(int, char**)':
newmain.cpp:23:42: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
     for (char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {

Do not forget to deallocate the memory using delete which was allotted to the character array using new operator as shown below:

delete [] charArray;

Otherwise, you may encounter memory leaks in your program.

To extract characters which are integers from a string and to store them into an Integer Array:

The following program should help you:

#include<iostream>

using namespace std;
// Using the namespace std to use string.

int main(int argc, char** argv) {
    string input;
    cout << "Enter a number: ";
    cin>>input;     // To read the input from the user.
    
    int strLength = input.length(); // To compute the length of the string.
    int * intArray = new int[strLength]; //Dynamic memory allocation
    int loopVar = 0;

    /**
     * The following for loop converts the string into an array.
    */
    for (const char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
        intArray[loopVar] = (*iterativeVar-'0');
        ++loopVar;
    }

    cout << endl << "The elements of the integer array are as follows: {";
    for (int i = 0; i < strLength; i++) {
        if (i != strLength - 1) {
            cout << intArray[i] << ", ";
        } else {
            cout <<intArray[i]<<"}";
        }   
    }

    delete [] intArray;
    return 0;
}

Sample Output:

Enter a number: 123456789

The elements of the integer array are as follows: {1, 2, 3, 4, 5, 6, 7, 8, 9}
RUN SUCCESSFUL (total time: 4s)

solved how to split a string for equation