fork(17) download
  1.  
  2. #include <string> // for storing strings in a C++ way
  3. #include <sstream> // to easily separate sentences into words
  4. #include <vector> // to dynamically store arbitrary amounts of words
  5. #include <algorithm> // for std::reverse
  6. #include <iostream> // for printing the result
  7.  
  8. int main()
  9. {
  10. std::string sentence = "Your sentence which contains ten words, two of them numbers";
  11. std::stringstream stream(sentence);
  12. std::string word;
  13. std::vector<std::string> words;
  14. while ( stream >> word )
  15. {
  16. words.push_back(word);
  17. }
  18. std::reverse(words.begin(), words.end());
  19. for ( size_t i(0); i < words.size(); ++i )
  20. {
  21. std::cout << words[i] << " ";
  22. }
  23. std::cout << "\n";
  24. }
  25.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
numbers them of two words, ten contains which sentence Your