Java

Java Program to Reverse the given String using Stack2 min read

Java Program to Reverse the given String using Stack

Given a String, write a java program to reverse the string using Stack

Java Code:




Output:

How to work

  • Initialize a StringBuffer, this will be our output.
  • Initialize a Stack.
  • Traverse through string, one character at a time and keep adding it to Stack.
  • While stack is not empty, keep popping out the characters from stack and add it to the output StringBuffer.
  • Print the output.

 

Java Program to Reverse the String entered by user using Stack

Java Code:

Output:

Leave a Comment