Thread: extracting specify int in a string

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    7

    extracting specify int in a string

    I am writing a program which require me to extract certain integers in the input string.
    For example,
    input string "this is 10th line of page 234" i want to extract the int 10 and 234. which function should i use to this?(find_first_of(), find_last_of(), substr() or atoi() )and how do i do it? I am not really know how to use the above functions.
    Please help, thank you.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    you "could" use a string container, using find()

    Code:
    #include <iostream>
    #include <string>
    
    int main ( void )
    {
    
    std::string phrase = "this is the 10th line on page 234";
    
    if (phrase.find("234"))
    {
        std::cout << "The phrase is in the string" << std::endl;
    }
    
    std::cin.get();
    
    return 0;
    }
    This is just one way, if you know STL constianers, you could use a iterator to search througth
    the string and indicate if it can find the numbers or words you are after, remeber to use a constant iterartor though, it is much safer.

    Please keep in mind this is only one way to acheive the result, other members may or will have
    better ways to solve the problem, I hope I was helpful.

    ps - Iterator set up:

    Code:
    std::vector < std::string > phrase;
    std::vector < std::string >::const_iterator myIterator;
    std::vector < std::string >::iterator iter;

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Since these integers could be in the same word as regular characters (like 10th), and if you don't know where or what they will be, I would search one character at a time for a digit (see isdigit), and then make a new string with that digit and each following one until a non-digit is found. You can then convert that new string to an int using your favorite conversion tool - stringstreams, lexical_cast, atoi.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    7
    The int i am looking for is not fix at all times, like the cumulative of set of following data,

    File1-0(71%) File2-0(63%) 177
    File1-0(71%) File2-0(63%) 177
    File1-1(59%) File2-1(63%) 167

    I will like to extra the value 71 (percentage) 63(percentage) and 177 for everyline. and the values is not fix, so find() wont work for this case. Hmm.. for isdigit, not the all int in the input i am interested to extract. ermm... i was thinking to use find_first_of(), but not very sure how to use it in this case

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could find an opening paranthesis, '(', and then convert a number from the next character onwards as described in the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    7
    okie. Greatly appreciate everyone's help. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM