fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void ReverseString(string &S, int start, int end)
  7. {
  8. if ( start < end )
  9. {
  10. swap(S[start], S[end - 1]);
  11. ReverseString(S, start+1, end - 1);
  12. }
  13. }
  14.  
  15. void ReverseString(string &S)
  16. {
  17. ReverseString(S, 0, S.size());
  18. }
  19.  
  20. int main()
  21. {
  22. string s = "The string to reverse" ;
  23.  
  24. cout << "Before Reversing" << endl;
  25. cout << s << endl;
  26.  
  27. ReverseString(s);
  28. cout << "After Reversing" << endl;
  29. cout << s << endl;
  30.  
  31. ReverseString(s, 0, 7);
  32. cout << "After Reversing a subset" << endl;
  33. cout << s << endl;
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 4188KB
stdin
Standard input is empty
stdout
Before Reversing
The string to reverse
After Reversing
esrever ot gnirts ehT
After Reversing a subset
reverse ot gnirts ehT