forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_a_string_using_stack.cpp
57 lines (53 loc) · 1.1 KB
/
reverse_a_string_using_stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Given a string,print reverse of the string using stack
Approach:-
In stack,we know LIFO principle works.
First,i create an empty stack,then i push each character of string in stack
then,using for loop i popped out every single character.
*/
#include<iostream>
#include<string>
#include<stack>
using namespace std;
//function to reverse the string
void reverse(string &s)
{
int i;
//Creating empty stack
stack<int> str;
//pushing each character of the string into the stack
for(char ch:s)
{
str.push(ch);
}
//popping each character one by one from the stack
for(i=0;i<s.size();i++)
{
s[i]=str.top();
str.pop();
}
}
//driver code
int main()
{
cout<<"Program to reverse a string."<<endl;
string s;
cout<<"Enter a string you want to reverse:";
cin>>s;
//calling function
reverse(s);
//printing result
cout<<s;
return 0;
}
/*
Test Case :
1.
Input : Rahul
Output : luhaR
2.
Input : Sajal
Output : lajaS
Time Complexity : O(n), where n is the length of the input string.
Space Complexity : O(n) for stack.
*/