fork download
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4.  
  5. string reverse(string s) {
  6. stack<char> st;
  7.  
  8. // Push all characters onto the stack
  9. for (char c : s)
  10. st.push(c);
  11.  
  12. // Pop characters to form reversed string
  13. string res;
  14. while (!st.empty()) {
  15. res += st.top();
  16. st.pop();
  17. }
  18. return res;
  19. }
  20.  
  21. int main() {
  22. string s = "geeksforgeeks";
  23. cout << s << endl;
  24. cout << reverse(s) << endl;
  25. return 0;
  26. }
Success #stdin #stdout 0s 5292KB
stdin
abide
stdout
geeksforgeeks
skeegrofskeeg