Thread: C++ string parsing delimited by 0's

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    10

    C++ string parsing delimited by 0's

    I have a number/string like this ( I am not sure how to convert int to and from strings)
    00000012231020000022334000070001222000001141100000 00110112710433340102200011271000033332010000010000 70005222500233400000000000000000000
    What I need to do is to separate the numbers between 0's, so I get strings like
    12231 22334 7 1222
    and so forth, and then I need to convert them into int. (basically I have searched to no avail of how to do the conversion)
    Can someone help out?
    Thanks!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Think about the general algorithm: ( one of many, of course )
    1) Split this string in several substrings, by making a new substring each time you encounter a zero( and ignoring the excess zeros )
    2) Convert each substring to int, preferably by means of "stoi()" or a stringstream.
    3) Profit
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,585
    You could also use std::replace_if() to replace the zeros with spaces, then use a stringstream to parse the string into the various integers.

    Jim

  4. #4
    Registered User
    Join Date
    Dec 2010
    Location
    Shamokin PA (middle of nowhere)
    Posts
    146
    How are you going to differentiate zeros that are delimiters from valid zeros contained in numbers such as 10 (ten), 101 (one hundred and one), etc?

  5. #5
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by freddie View Post
    How are you going to differentiate zeros that are delimiters from valid zeros contained in numbers such as 10 (ten), 101 (one hundred and one), etc?
    From what I see in the OPs sample, it looks like all zeros are delimiters.

  6. #6
    Registered User
    Join Date
    Apr 2016
    Posts
    10
    nailed it! thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing pipe-delimited linesfrom a text file
    By JOhn12341234 in forum C Programming
    Replies: 5
    Last Post: 07-03-2013, 08:51 AM
  2. Splitting a space delimited string
    By lukeaar in forum C Programming
    Replies: 5
    Last Post: 05-15-2010, 02:33 AM
  3. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  4. parsing delimited strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2001, 12:57 PM