GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java Program to Reverse a String using Stack - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java Program to Reverse a String using Stack

Last Updated : 22 Aug, 2026

A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle. This property makes a stack useful for reversing a string because the last character inserted into the stack is the first character removed.

  • Stack follows the LIFO principle.
  • Each character is pushed into the stack.
  • Characters are popped in reverse order.

Example

Input: GeeksForGeeks
Output: skeeGroFskeeG

Approach

  • Create a Stack<Character> to store the characters of the string.
  • Traverse the string and push each character into the stack.
  • Pop characters from the stack until it becomes empty.
  • Store each popped character in a character array.
  • Convert the character array into a String and return it.
Java
import java.util.Stack;

public class GFG {

    // Method to reverse a string using Stack
    static String reverseString(String str) {

        Stack<Character> stack = new Stack<>();

        // Push each character into the stack
        for (int i = 0; i < str.length(); i++) {
            stack.push(str.charAt(i));
        }

        // Store the reversed characters
        char[] reversed = new char[str.length()];
        int i = 0;

        // Pop characters from the stack
        while (!stack.isEmpty()) {
            reversed[i++] = stack.pop();
        }

        return new String(reversed);
    }

    public static void main(String[] args) {

        String str1 = "GeeksForGeeks";
        String str2 = "Hello World";

        System.out.println("Original: " + str1);
        System.out.println("Reversed: " + reverseString(str1));

        System.out.println("Original: " + str2);
        System.out.println("Reversed: " + reverseString(str2));
    }
}

Output
Original: GeeksForGeeks
Reversed: skeeGroFskeeG
Original: Hello World
Reversed: dlroW olleH

Explanation

  • Create a Stack<Character> to store the string characters.
  • Push each character into the stack.
  • Pop characters one by one to get them in reverse order.
  • Store the popped characters in a character array.
  • Convert the array into a String and return it.
Try It Yourself
redirect icon
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.