# Thread: Extract complete integers from string

1. ## Extract complete integers from string

Hi

I have strings that look like

etc., and I need to compute the value of the fraction by e.g. extracting the top and bottom as substrings.

My first idea was to modify the two functions below, which search for the first and the last digit, to search for the first and the last series of digits. But this does not quite work. Are there simple ways to do this?

Code:
```int top_frac(const std::string& x)
{
std::string temp;
int number = 0;

for ( unsigned int i = 1; i < x.size(); i++ ) {
if ( isdigit( x[i] ) ) {
for ( unsigned int a = i; a < x.size(); a++ ) {
temp += x[a];
}
break;
}
}
std::istringstream stream( temp );
stream >> number;
return number;
}

int bot_frac(const std::string& x)
{
std::string temp;
int number = 0;

for ( unsigned int i = x.size(); i--; ) {
if ( isdigit( x[i] ) ) {
for ( unsigned int a = i; a < x.size(); a++ ) {
temp += x[a];
}
break;
}
}
std::istringstream stream( temp );
stream >> number;
return number;
}```
serge

2. I suggest to start by removing the "Answer: " substring. A fairly direct way to do this is to observe that "Answer: " has a length of 8, so you take the substring starting from index 8 (remember to use named constants rather than magic numbers, or otherwise compute the length of "Answer: ").

With this substring, e.g., "132/643", you have a few ways by which you can extract 132 and 643. One approach is to turn it into a stringstream, and then use formatted input to read into an int, then a char, then an int.

Another approach is to use the find member function of std::string to find the '/', then get two substrings "132" and "643", and then convert them to int, e.g., by using a stringstream as in your current code.

3. Originally Posted by laserlight
I suggest to start by removing the "Answer: " substring. A fairly direct way to do this is to observe that "Answer: " has a length of 8, so you take the substring starting from index 8 (remember to use named constants rather than magic numbers, or otherwise compute the length of "Answer: ").

With this substring, e.g., "132/643", you have a few ways by which you can extract 132 and 643. One approach is to turn it into a stringstream, and then use formatted input to read into an int, then a char, then an int.

Another approach is to use the find member function of std::string to find the '/', then get two substrings "132" and "643", and then convert them to int, e.g., by using a stringstream as in your current code.
Thanks for the inspiration. This is what I put together after finding a nice split function on the internet:

Code:
```#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <stdio.h>

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}

int main(int argc, char* argv[])
{

if ( str.find(lookup) != std::string::npos ) {
std::string numstr = str.substr( numpos + 9 );
std::vector<std::string> x = split(numstr, '/');
// for ( std::vector<std::string>::iterator it1 = x.begin(); it1 != x.end(); ++it1 ) std::cout << *it1 << " ; ";
std::cout << std::endl;
std::cout << (double) atoi( x[0].c_str() ) / atoi( x[1].c_str() ) << std::endl;
}

return 0;
}```

4. The standard library really lacks some practical algorithms such as splitting a string, even in this modern age...
But wait, there's a solution. Enter boost.

5. That's not bad, serge, but is using magic values (like 9).

Personally, I'd check if it contains the string "Answer" at the beginning, form a string by discarding anything other than digits, the '/' characters, and (maybe) sign characters ('+' or '-'). Then parse what's left as if it's a set of integral values separated by '/'. Depending on needs, it may be a good idea to cope with "Answer 2" (i.e. the absence of a '/') rather than barfing, as well as cope with "answer 3/4" (case insensitive).

The necessary amount of error checking, and ability to cope with extraneous characters, depend on how reliable the source of input is. It could, if needed, cope with "AnSwer 6/-2/3 from winnie the poo" (by giving a result of -1, reporting presence of extraneous characters, etc etc).