# Thread: Parsing a String

1. ## Parsing a String

I am going to have to parse a polynomial string to break it down into its different elements.

Lets say I have this polynomial: 5x^3+-23x^12

What my thoughts were was to do something like this as an example:

Code:
```    string polyString = "5x^3+-23x^12";
char thisChar;
for( int count = 0; count < polyString.size(); count++ ){
thisChar = polyString.at(count);
//then some if statements in here to detect if it is a number
}```

I am curious if anyone has a good solution getting the numbers out of the polynomial. All I need to get out is the coefficient and exponent.

My idea is to take the each char and detect what it is, but I was wondering if anyone had a good way of doing this that did not involve if statements that would be incredibly long.

2. use a for loop like you are and check each character, use the isdigit() function
edit: search cplusplus.com reference if you dont know how to use it, its easy though

3. sscanf perhaps?
Code:
```#include <cstdio>
.
.
string polyString = "5x^3+-23x^12";
int c[10], e[10];

int n = sscanf(polyString.c_str(), "%dx^%d%+%dx^%d+%dx^%d",
&c[0], &e[0], &c[1], &e[1], &c[2], &e[2]);
for (int i=0; i<n/2; ++i)
{
cout << c[i] << " " << e[i] << endl;
}```

4. I just used that polynomial as an example. The program needs to be able to handle a polynomial of any size. That would work great, but that above solution wont work in this case.

5. are the coefficients only numerical? if so isdigit() and for loop should work.

6. >That would work great, but that above solution wont work in this case.
Can you explain better why not? This code runs fine for me:
Code:
```#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

int main()
{
string polyString = "5x^3+-23x^12";
int c[10], e[10];

int n = sscanf(polyString.c_str(),
"%dx^%d%+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d+%dx^%d",
&c[0],&e[0],&c[1],&e[1],&c[2],&e[2],&c[3],&e[3],&c[4],&e[4],&c[5],&e[5],
&c[6],&e[6],&c[7],&e[7],&c[8],&e[8],&c[9],&e[9]);
for (int i=0; i<n/2; ++i)
{
cout << c[i] << " " << e[i] << endl;
}

cin.get();
return 0;
}```

7. Originally Posted by swoopy
Can you explain better why not? This code runs fine for me:

What happens if the polynomial is something like:

"5x^3+-23x^12+3x^1+2x^1+5x^1"

Would that code be able to be expandable for any length polynomial that the user could input?

8. can you answer my previous question?

9. Since you have a string you can also use the string's find functions. Another option is to place the string into a stringstream and parse it that way.

10. Originally Posted by nadroj
can you answer my previous question?

That will work fine, but it will get somewhat sticky when I get a polynomial like:

"345x^4"

Code:
```    string polyString = "345x^4";
char thisChar;
int temp;

for( int count = 0; count < polyString.size(); count++ ){
thisChar = polyString.at(count);
if(thisChar.isDigit() == true){
temp = (int)thisChar;
//how would i take into account a number greater than one digit though?
}
}```
I need to get the whole number (345 in this case) as an int so I can use it, any idea how to get that?

11. you could read if a character is digit, if it is, scan ahead and see if the next one also is, and so on. if they are consecutive then it is the same number

12. Originally Posted by nadroj
you could read if a character is digit, if it is, scan ahead and see if the next one also is, and so on. if they are consecutive then it is the same number

Ok, I see what you are saying. Thanks for all the help everyone, I am going to try to get this working now.

13. Originally Posted by mas0nite
What happens if the polynomial is something like:

"5x^3+-23x^12+3x^1+2x^1+5x^1"

Would that code be able to be expandable for any length polynomial that the user could input?
The array has 10 elements, so it will work for a polynomial of up to 10 terms. However I get the feeling that you would be more comfortable using a loop to parse the expression. Or another option is to find a regular expression library. The Boost library may have something you could use.

14. And what about 3x+1? Will I see 1?

15. Try boost

Popular pages Recent additions