Stack Example

Stack Example 1: Reverse a string or word

In our previous article Stack Introduction and Implementation we had learnt how to implement stack in java. In this first example we will see how to reverse a word or a string using a stack.

When you run the following code, it will ask you to type a string. Once you type the string, press Enter, then it will print the string with letters in reverse order.

Stack Example 1: Reverse a string

Output

Observation

A stack is used to reverse the letters in a string.  Because stack follows Last In First Out (LIFO) principle, the stack reverses the order of characters.

Recommended Posts

Reference

Leave a Reply

avatar
  Subscribe  
Notify of