fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void reverse(string& word)
  8. {
  9. unsigned int end = word.size() - 1;
  10. for ( unsigned int i = 0; i < end; i++, end-- )
  11. {
  12. char c = word[end];
  13. word[end] = word[i];
  14. word[i] = c;
  15. }
  16. }
  17.  
  18. void reverse_rec( string& word )
  19. {
  20. if ( word.size() <= 1 ) return;
  21.  
  22. string tmp = word.substr( 1, word.size() - 2 );
  23. reverse_rec( tmp );
  24. word = word.substr( word.size() - 1 ) + tmp + word[0];
  25. }
  26.  
  27. int main()
  28. {
  29. string str( "This is a test");
  30. reverse(str);
  31. std::cout << str << std::endl;
  32.  
  33. reverse_rec(str);
  34. std::cout << str << std::endl;
  35.  
  36. std::reverse(str.begin(), str.end());
  37. std::cout << str << std::endl;
  38. return 0;
  39. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
tset a si sihT
This is a test
tset a si sihT