Thread: split space sepparated string

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    52

    split space sepparated string

    Hi everyone,

    How do I split a string like this
    "500 57 - 33 + 17 * 22 -"

    into an array of strings
    "500" "57" "-" "33" ..

    What function can do that for me?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes. strtok() for example. sscanf() could also be used with some thinking about it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It should also be quite simple with stringstream and operator >>.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by anon View Post
    It should also be quite simple with stringstream and operator >>.
    yeah use a stringstream

    Code:
    std::stringstream ss(the_string);
    std::vector<std::string> strings;
    std::string tmp;
    
    while(ss>>tmp)
        strings.push_back(tmp);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM