Reverse a string using Stack
PROBLEM :
An string of words is given, the task is to reverse the string using stack.
Input:
The first line of input will contains an integer T denoting the no of test cases . Then T test cases follow. Each test case contains a string s of words without spaces.
Output:
For each test case ,print the reverse of the string in new line.
Constraints:
1<=T<=100
1<=length of the string <=100
Example:
Input:
2
GeeksQuiz
GeeksforGeeks
Output:
ziuQskeeG
skeeGrofskeeG
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
void reverse(char *str, int len)
{
stack<char> sta ;
for(int i=0;i<len;i++)
sta.push(str[i]) ;
for(int i=0;i<len;i++){
str[i]=sta.top() ;
sta.pop() ;
}
}
--------------------------------------------------------------------------------
An string of words is given, the task is to reverse the string using stack.
Input:
The first line of input will contains an integer T denoting the no of test cases . Then T test cases follow. Each test case contains a string s of words without spaces.
Output:
For each test case ,print the reverse of the string in new line.
Constraints:
1<=T<=100
1<=length of the string <=100
Example:
Input:
2
GeeksQuiz
GeeksforGeeks
Output:
ziuQskeeG
skeeGrofskeeG
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
void reverse(char *str, int len)
{
stack<char> sta ;
for(int i=0;i<len;i++)
sta.push(str[i]) ;
for(int i=0;i<len;i++){
str[i]=sta.top() ;
sta.pop() ;
}
}
--------------------------------------------------------------------------------
Comments
Post a Comment