Converting string to stack<char>

Hello everyone,

I'm working on a c++ assignment that requires me to push a string to a function, which pushes that string into a local stack<char>. Then the characters are removed from the stack and pushed to a local string. The goal is to reverse the characters of the string using
bool empty() const;
void pop();
void push(char& c);
char& top() const;
Right now I'm having difficulty just converting the string to stack<char>, so I haven't progressed much. That is the main part I need help with, but I wouldn't mind some tips for the actual reversal either. Thank you!


1
2
3
4
5
6
7
string stringReversal1(string input)
{
     stack <char> local;
     string locst;

     return locst;
}
you have to push every char to from the original string to the stack. Then pop every character from the stack to the reversed string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main()
{
	stack<char> stk;
	string str("abc");
	
	for(int i = 0; i < str.length(); i++)
		stk.push(str.at(i));
		
	string reverse;
	while(!stk.empty())
	{
		reverse.push_back(stk.top());
		stk.pop();
	}
		
	cout<<str<<" "<<reverse<<endl;
	return 0;
}


But i guess your prof wants you to implement your own stack
That seems to have worked, thank you!
Topic archived. No new replies allowed.