how would you seperate a character in a string.

For example :

string ="2x^2+8"

how would i seperate the string into '2' 'x' '^' '2' '8'.

so I could find its derivative.

Recommended Answers

All 3 Replies

you can treat it as an array if you want.

std::string somestring;
somestring = "2x^2+8";
cout << somestring[0] << endl; // Shows 2
cout << somestring[1] << endl; // shows x
cout << somestring[2] << endl; // shows ^
cout << "you get the point..." << endl; // ;-)
Member Avatar for iamthwee

you can treat it as an array if you want.

std::string somestring;
somestring = "2x^2+8";
cout << somestring[0] << endl; // Shows 2
cout << somestring[1] << endl; // shows x
cout << somestring[2] << endl; // shows ^
cout << "you get the point..." << endl; // ;-)

Indeed but that is hardly generic.

You need to look at the core components.

Finding the coefficients (these precede the 'x' and go all the way back to the first operator {+,-} )
Find the powers to x if it is multiplied by x. (these come after the 'x^' and go all the way forward to the first operator.)


That's just a general rule though as I haven't included parenthesis etc.

As you get deeper into this you might want to look at my code snippet for evaluating derivatives etc.

http://www.daniweb.com/code/snippet891.html

Indeed but that is hardly generic.

You need to look at the core components.

While I fully agree with you here, his question was about getting access to the individual characters in the string... not about how to proceed with the rest of his project. ;)

commented: Yeah true. +18
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.