Write a program to reverse a string using stack data structure ?
- Given a string, reverse it using stack. For example “Wikitechy” should be converted to “yhcetikiW”.
- Following is simple algorithm to reverse a string using stack.
-
- Empty stack to be created.
- String to stack put one by one push all characters.
- From the stack and put them back to string one by one pop all characters.
Sample Code in Java
import java.util.*;
//stack
class Stack
{
int size;
int top;
char[] a;
//function to check if stack is empty
boolean isEmpty()
{
return (top < 0);
}
Stack(int n)
{
top = -1;
size = n;
a = new char[size];
}
//function to push element in Stack
boolean push(char x)
{
if (top >= size)
{
System.out.println("Stack Overflow");
return false;
}
else
{
a[++top] = x;
return true;
}
}
//function to pop element from stack
char pop()
{
if (top < 0)
{
System.out.println("Stack Underflow");
return 0;
}
else
{
char x = a[top--];
return x;
}
}
}
// Driver code
class Main
{
//function to reverse the string
public static void reverse(StringBuffer str)
{
// Create a stack of capacity
// equal to length of string
int n = str.length();
Stack obj = new Stack(n);
// Push all characters of string
// to stack
int i;
for (i = 0; i < n; i++)
obj.push(str.charAt(i));
// Pop all characters of string
// and put them back to str
for (i = 0; i < n; i++)
{
char ch = obj.pop();
str.setCharAt(i,ch);
}
}
//driver function
public static void main(String args[])
{
//create a new string
StringBuffer s= new StringBuffer("Wikitechy");
//call reverse method
reverse(s);
//print the reversed string
System.out.println("Reversed string is " + s);
}
}
Output
Time Complexity:
- O(n) where n is number of characters in stack.
Categorized in:
Tagged in:
Accenture interview questions and answers, algorithm for palindrome using stack, Altimetrik India Pvt Ltd interview questions and answers, Applied Materials interview questions and answers, Bharti Airtel interview questions and answers, BMC Software interview questions and answers, Capgemini interview questions and answers, CASTING NETWORKS INDIA PVT LIMITED interview questions and answers, CGI Group Inc interview questions and answers, Chetu interview questions and answers, Ciena Corporation interview questions and answers, Collabera Te interview questions and answers, data structure to reverse string ( other than stack), Dell International Services India Pvt Ltd interview questions and answers, Flipkart interview questions and answers, geekyants interview questions and answers, Genpact interview questions and answers, Globallogic India Pvt Ltd interview questions and answers, how to push a string into a stack in c, how to reverse a stack in c++, IBM interview questions and answers, Indecomm Global Services interview questions and answers, java input string, Mphasis interview questions and answers, NetApp interview questions and answers, Oracle Corporation interview questions and answers, rereverse a string using stack in cstack string java, reverse a linked list using stack in c, reverse a list using stack in c, reverse a number using stack, reverse a number using stack in c, reverse a string in java, reverse a string using stack c++, reverse a string using stack in c#, reverse a string using stack in java, reverse a string using stack in python, reverse string using stack data structure in c, reverse string using stack in java, reverse words in a string using stack, reverse words in a string using stack c++, samsung interview questions and answers, SAP Labs India Pvt Ltd interview questions and answers, Sapient Consulting Pvt Ltd interview questions and answers, stack string java, Tech Mahindra interview questions and answers, Tracxn Technologies Pvt Ltd interview questions and answers, UnitedHealth Group interview questions and answers, use a stack to reverse the words of a sentence, Wipro Infotech interview questions and answers, WM Global Technology Services India Pvt.Ltd Limited (WMGTS) interview questions and answers, write a program to reverse a string using stack data structu, Xoriant Solutions Pvt Ltd interview questions and answers, Yodlee Infotech Pvt Ltd interview questions and answers