Javatpoint Logo
Javatpoint Logo

Java Program to Reverse a String Using the Stack Data Structure

The Stack is a sequential data structure that operates according to the LIFO (Last In First Out) tenet, meaning that the one that was added last is the one that is extracted first.

Approach:

  1. Insert each character one at a time into the datatype character stack.
  2. Pop each character from the stack one at a time until the stack is empty.
  3. Increase the character array by one popped element.
  4. Create a string from a character array.
  5. Provide the reversed string.

The application of the aforementioned strategy is shown below.

File name: ReverseStringUsingStack.java

Output:

 JavaTpoint  <- Reverse ->  tniopTavaJ
 Welcome to JavaTpoint  <- Reverse ->  tniopTavaJ ot emocleW

Time Complexity: O(n), where n is the characters count in the stack.

Space Complexity: O(n) for the 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