fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. string lastWord(string line)
  8. {
  9. return line.substr(line.rfind(" ")+1);
  10. }
  11. int main() {
  12. string poem = "Roses are red\n\
  13. Violets are blue\n\
  14. Sugar is sweet\n\
  15. And so are you";
  16. stringstream ss(poem);
  17. while (ss) {
  18. string line;
  19. getline(ss, line);
  20. cout << lastWord(line) << endl;
  21. }
  22. return 0;
  23. }
Success #stdin #stdout 0.01s 2860KB
stdin
Standard input is empty
stdout
red
blue
sweet
you