?
Solved

Parsing string

Posted on 2006-04-20
6
Medium Priority
?
309 Views
Last Modified: 2010-04-01
I need to parse a string of integers like this:

string line = "123456..etc";
int result;

I need every single digit converted to a number so I am doing something like this:

for (int i = 0; i < line.length(); i++)
{
   result = atoi(&line[i]);
}

Instead of breaking the string up I get:

123456
23456
3456, Etc.

What is the best way to do this.  Thank you very much ahead of time.

Nick
0
Comment
Question by:nstefanov
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16501783
The easiest way would be to

for (int i = 0; i < line.length(); i++)
{
   result = line[i] - '0';
}
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 16503449
int main ()
{
      string line = "123456..etc";
      string sOneDigit = "";
      int result;

      for (int i = 0; i < line.length(); i++) {
            sOneDigit= line[i];                 // a one-character string
            result = atoi( sOneDigit.c_str() );
            cout << result << endl;
      }
      return 0;
}

This version lets you continue to use atoi()... but it breaks out each digit of the original line into a separate string.  That lets you avoid using the "little secret" that the ASCII value of digits '0' - '9' equate to a corresponding binary value when you subtract exactly 0x30 ('0').  I understand that in a future version of the the ANSI STL spec, the digits are not guaranteed to be in any particular order (just kidding).
0
 
LVL 86

Expert Comment

by:jkr
ID: 16504467
?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:nstefanov
ID: 16504473
jkr your method also works great i just thought Dan's is a little more complete.  Maybe should have split the points.  Will have to find out how to do that.  Thanks for your help.

Nick
0
 
LVL 86

Expert Comment

by:jkr
ID: 16504544
>>i just thought Dan's is a little more complete

To what extent? You wanted to convert chars into ints, not chars into strings that you then convert to ints.
0
 

Author Comment

by:nstefanov
ID: 16504596
I meant explanation.  Thanks again.

Nick
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question