Javatpoint Logo
Javatpoint Logo

Java Program to Reverse a String Using Stack

The stack is a linear data structure that is utilized to store the collection of objects. It depends on Last in First Out (LIFO). Java Collections structure gives numerous points of interaction and classes to store objects. One of them is the Stack class which gives various activities like push, pop, search, and so forth.

Approach to reverse a string using stack

  1. Push the elements/characters of the string individually into the stack of datatype characters.
  2. Pop the elements/characters individually from the Stack until the stack becomes vacant.
  3. Add a popped component to the character array.
  4. Convert character array to string.
  5. Return turned around the string.

The implementation of the above approach is as shown below

Output:

Java Program to Reverse a String Using Stack

Time Complexity: O(n), where n is the Size of the String

Space Complexity: O(n), Extra space of the n size stack







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA