Advertisement
Guest User

C++ split sentence ez

a guest
May 5th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9. int main(int argc, const char * argv[])
  10. {
  11.     string sentence;
  12.     string tmp;
  13.     vector<string> v;
  14.  
  15.     cout << "Enter a sentence: ";
  16.     getline(cin, sentence);
  17.  
  18.     for(int i = 0; i < sentence.length(); i++) {
  19.  
  20.         tmp += sentence[i];
  21.  
  22.         // Check for whitespace or if we are at the end of the string
  23.         if((sentence[i] == ' ') || (i == sentence.length() - 1)) {
  24.             // push back current tmp value
  25.             v.push_back(tmp);
  26.             // clear tmp string
  27.             tmp = "";
  28.         }
  29.     }
  30.  
  31.     for (int i = 0; i < v.size(); i++)
  32.     {
  33.         cout << v[i] << " ";
  34.     }
  35.     cout << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement