Reverse a String Using a Stack

This problem asks you to reverse a string using a stack. An example input and output are provided.


Given a string, reverse it using a stack.
Example input: "Hello, world!"
Example output: "!dlrow ,olleH"


This solution is in C++. Our A.I. can create solutions in multiple languages.
by kayakane
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main()
    string input;
    stack<char> s;
    cout << "Enter a string: ";
    getline(cin, input);
    for (int i = 0; i < input.length(); i++)
    while (!s.empty())
        cout <<;
    cout << endl;
    return 0;


The solution is optimal because it uses a stack, which is a LIFO data structure.
This means that the last character entered into the stack will be the first one
to be popped out. This is the same as reversing a string.


A.I. Evaluation of the Solution

The candidate's solution is correct and uses a stack to reverse the string as required. The candidate's code is clean and easy to read. Well done.

Evaluated at: 2022-11-27 10:15:47