Thread: Tokenizing string

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Tokenizing string

    How would you cut this line in 4 parts?

    "The quick brown fox jumps over the lazy dog"

    Like this:

    The
    quick
    brown
    fox jumps over the lazy dog


    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    #include <istream>
    #include <ostream>
    #include <iterator>
    #include <sstream>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        std::string str = "The quick brown fox jumps over the lazy dog";
        stringstream ss;
        string s;
    
        ss << str;
    
        for(int i=0; i<4; i++)
        {
          (getline(ss, s, ' '));
            cout << s << endl;
        }
    
    }
    I cant seem to get the "fox jumps over the lazy dog" part.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well since you're only looping four times what do you expect?

    If you want to process the whole stringstream then perhaps a while() loop would be a better option.

    Code:
    #include <string>
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
        std::string str;
        stringstream ss("The quick brown fox jumps over the lazy dog");
    
        while(ss >> str)
        {
            cout << str << endl;
        }
    
    }
    And note that you should only need three #include files for this program.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Probably like this:
    Code:
    #include <string>
    #include <iostream>
    #include <sstream>
    
    int main()
    {
      std::string str = "The quick brown fox jumps over the lazy dog";
      std::stringstream ss(str);
      std::string s;
    
      for (int i = 0; i < 3; i++)
      {
        if(!std::getline(ss, s, ' ')) // in case there are less words
          break;
        std::cout << s << "\n";
      }
      if(std::getline(ss, s))
        std::cout << s;
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Quote Originally Posted by jimblumberg View Post
    Well since you're only looping four times what do you expect?
    Yes but as I stated and dont want to tokenize the whole sentence, I want to keep "fox jumps over the lazy dog" together.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Quote Originally Posted by OldGuy2 View Post
    Probably like this:
    Thank you, this is the solution. Happy new year, soon...
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tokenizing a string
    By sigur47 in forum C Programming
    Replies: 3
    Last Post: 05-07-2012, 07:16 PM
  2. Tokenizing a string
    By BdON003 in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2009, 10:45 PM
  3. Tokenizing a string
    By chinesepirate in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2008, 11:32 AM
  4. Tokenizing a C++ string
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2004, 06:31 AM
  5. String Tokenizing
    By irncty99 in forum C++ Programming
    Replies: 21
    Last Post: 05-08-2003, 07:47 AM

Tags for this Thread