Program to reverse a string of characters using a Stack class in Java
class CharacterStack { private char[] stackArray; private int topOfStack; public CharacterStack( int capacity ) { stackArray = new char[capacity]; topOfStack = -1; } public void push( char element ){ stackArray[++topOfStack] = element; } public char pop(){ return stackArray[topOfStack--]; } public char peek(){ return stackArray[topOfStack]; } public boolean isEmpty(){ return topOfStack < 0; } public boolean isFull(){ return topOfStack == stackArray.length - 1; } } public class Client { public static void main(String[] args) { CharacterStack stack = new CharacterStack(50); String str = "!tol a sknahT.moc.topsrebbuh detisiv uoy"; int length = str.length(); System.out.println("Initial String : " + str); for(int i = 0; i < length ; i++ ){ stack.push(str.charAt(i)); } System.out.println(); System.out.print("Reversing the String : "); while(!stack.isEmpty()){ System.out.print(stack.pop()); } System.out.println(); } }Output of the program :