extracting word-per-word from strings

This is a discussion on extracting word-per-word from strings within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm creating a sea battle game for a project and at a certain point the user has to enter ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    extracting word-per-word from strings

    Hello,

    I'm creating a sea battle game for a project and at a certain point the user has to enter a string of the following form: 'ship x-coordinate y-coordinate orientation' as many times in a row as he wishsh, e.g. "S5 1 1 v S4 5 4 h S2 1 8 h".
    I create extra strings to save every item seperately for further use. But how can I extract them properly one by one? I've been trying for about 2 hours now and I'm starting to run out of ideas .
    Here's an example of what I tried. A bit silly code, but for illustration purposes of course.
    Code:
    std::string s("S5 1 1 v S4 5 4 h S2 1 8 h");
    
    while(! s.empty()){ std::string example(s,0,2); s.erase(0,2); }
    Obviously that's not really ideal, since the length of every word can vary.
    I thought using getline would be much better, since you can give a parameter when you'd like it to stop (in my case " "). However, I don't know how to get that to work for strings.

    Thanks a lot if you can help me out.
    Ed.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Use a stringstream container to parse your string:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    ...
    
    std::string s("S5 1 1 v S4 5 4 h S2 1 8 h");
    std::string temp;
    std::istringstream parser(s);
    while( parser >> temp )
        std::cout << temp << std::endl;
    Should output:

    Code:
    S5
    1
    1
    v
    S4
    5
    4
    h
    S2
    1
    8
    h
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    If you want to create your own parser, loop through the string until you hit a space and then store the word up to that point. Something like this in pseudocode:

    Code:
    while string is not empty
       x=0
       while s[x] is not a space and x is less than or equal to s length
             x++
       store substring from s[0] to s[x-1]
       erase s[0] to s[x-1]

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    One nice benefit of using string streams is that the code is already there for you and accounts for any kind of white space. For something like this I'd keep it simple. Plus it seems to me you may have a user type something like:

    Example:
    Code:
    S5  1 1 v S4 5 4 h S2    1 8	h
    Note: some of the above has multiple spaces or tabs

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Thanks a lot all of you. It works like a charm .
    And I think it's indeed more 'dummy-proof' to use stringstreams (they always pay attention to that in our projects), although creating my own parser was fun too.
    Last edited by tygernoot; 12-27-2004 at 01:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 04:38 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM
  5. strings
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 09-10-2001, 03:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21