Reversing a String Using a Stack

This problem asks you to reverse a string using a stack. An example input would be "abc" and the corresponding output would be "cba".

Problem

Given a string, reverse it using a stack.
Example input: "abc"
Example output: "cba"

Solution

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: ";
    cin >> input;
    for (int i = 0; i < input.length(); i++)
    {
        s.push(input[i]);
    }
    cout << "Reversed string: ";
    while (!s.empty())
    {
        cout << s.top();
        s.pop();
    }
    cout << endl;
    return 0;
}

/*

The solution is optimal because it uses a stack to reverse the string.

*/

A.I. Evaluation of the Solution

The candidate's solution is optimal because it uses a stack to reverse the string. The candidate's approach is also clear and easy to follow.

Evaluated at: 2022-11-26 04:16:20