Reverse the given String using Stack

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


Input: “tutorial horizon”
Output: “noziroh lairotut”

Recommended: Please read Stack Data Structure before continue to read.


  • 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 Code:


Original String: tutorial horizon
Reverse String: noziroh lairotut

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.