fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. void recursive_reverse(std::string &s) {
  5. size_t pos = s.find_last_of(" ");
  6. // base case - there's no space
  7. if(pos == std::string::npos) {
  8. std::cout << s << std::endl;
  9. return;
  10. } else {
  11. std::string substring = s.substr(pos+1);
  12. std::cout << substring << std::endl;
  13. std::string rest = s.substr(0, pos);
  14. // recursive call
  15. recursive_reverse(rest);
  16. }
  17. }
  18.  
  19. int main() {
  20. std::string s("Hello World!");
  21. recursive_reverse(s);
  22. return 0;
  23. }
  24.  
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
World!
Hello