C Data Structure Write Non Recursive Function Uses Stack Reverse Characters String Functio Q37143864

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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.