Java Program to Reverse the String using Stack Implementation

Class Stackimple gives the static implementation of stack in java using methods. The methods are called from main class “StackMain”.

public class Stackimple
{
private int top;
char ch [];
int size;

Stackimple(int top1, int size)
{
this.size=size;
ch=new char[size];
top=top1;

}

public void push1(char ch1)
{
ch[++top]=ch1;

}

public char pop1()
{

return ch[top--];

}

public boolean isEmpty()
{
if(top==-1)return true;
else return false;
}
public boolean isFull()
{
if(top==(size-1))return true;
else return false;
}

}

public class stackMain {

public static void main(String[] args) {

Stackimple obj=new Stackimple(-1,6);
String str="Rajat";
int i=0;
char ch[]=str.toCharArray();

while(!obj.isFull())
{
obj.push1(ch[i]);
i++;

}

while(!obj.isEmpty())
{
char temp=obj.pop1();
System.out.print(""+temp);

}
}
}

Hope this helps 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *