c++, data structure
write a non-recursive function that uses a stack to reverse thecharacters in a string. the function’s prototype is stringreverse(const string& s);
Solution
#include <iostream>#include <string>#include <stack>using namespace std;string reverse(const string &s) { string reverse_string; // create an empty reverse string stack<char> st; for (int i = 0; i < s.length(); ++i) { // go through all characters of string st.push(s[i]); // open characters to stack } while (!st.empty()) { // while
OR
OR