Reverse a string using stack

1 min read

https://practice.geeksforgeeks.org/problems/reverse-a-string-using-stack/1

char *reverse(char *S, int len) {
  stack<char> st;

  for (int i = 0; i < len; i++) {
    st.push(S[i]);
  }

  char *rev = new char(len + 1);

  int i = 0;
  while (!st.empty()) {
    rev[i++] = st.top();
    st.pop();
  }
  rev[i] = '\0';

  return rev;
}
Implement N stacks in an array
Design a stack that supports getmin() in O(1) time and O(1) extra space