fork(1) download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. int main()
  6. {
  7. string a;
  8. getline(cin, a);
  9.  
  10. size_t first_start = 0;
  11. size_t first_len = a.find(' ');
  12.  
  13. size_t last_start = a.rfind(' ') + 1;
  14. size_t last_len = a.size() - last_start;
  15.  
  16. size_t mid_start = first_len;
  17. size_t mid_len = last_start - first_len;
  18.  
  19. string b = a.substr(last_start, last_len)
  20. + a.substr(mid_start, mid_len)
  21. + a.substr(first_start, first_len);
  22.  
  23. std::cout << b << '\n';
  24. }
  25.  
Success #stdin #stdout 0.01s 2864KB
stdin
This is an example of that program that swaps first and last word.
stdout
word. is an example of that program that swaps first and last This