fork download
  1. #include <iostream> // cout
  2. #include <string> // string
  3. #include <sstream> // string stream
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string testString = "Hello how are you.";
  9.  
  10. istringstream iss(testString); // not istringstream NOT sstringstream
  11.  
  12. char c; // this will read the delima (space in this case)
  13. string firstWord;
  14.  
  15. iss>>firstWord>>c; // read the first word and end after the first ' '
  16.  
  17. cout << "The first word in \"" << testString << "\" is \"" << firstWord << "\""<<endl;
  18.  
  19. cout << "The rest of the words is \"" <<testString.substr(firstWord.length()+1) << "\""<<endl;
  20. return 0;
  21. }
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
The first word in "Hello how are you." is "Hello"
The rest of the words is "how are you."