i need to tokenize an mathematical expression using strok..i have done something but i cannot get delimiters to my vector when i run the code i get 2x 4y 6 3 this output how can i get delimiters to my vector and how can i get my output like this 2x + 4y ^ 6 - 3 my code
int main()
{
vector<string> finalVector;
char input[1024]="2x+4y^6-3";
char *token = strtok(input, "^+-/()/t");
while (token != NULL) {
finalVector.push_back(token);
token = strtok(NULL, "^+-/()/t");
}
for (int i = 0; i < finalVector.size(); i++)
cout << finalVector.at(i) << " ";
return 0;
}
strtok
replaces the found delimiter with the null character. The delimiter is irretrievable gone.If you make a copy of your string before the first call to
strtok
, you can recover the delimiter:}