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

  • C++ Tutorial
  • Interview Questions
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • OOP
  • Exception Handling
  • STL
  • DSA C++
  • Introduction2 min read
  • Data Types4 min read
  • Variables4 min read
  • Operators9 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — C++ Program to Reverse a String Using Stack - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • C++ Tutorial
  • Interview Questions
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • OOP
  • Exception Handling
  • STL
  • DSA C++

C++ Program to Reverse a String Using Stack

Last Updated : 22 Aug, 2026

Reversing a string using a stack means pushing all characters onto the stack and then popping them one by one. Since a stack follows the Last In, First Out (LIFO) principle, the characters are retrieved in reverse order.

  • Push each character of the string onto the stack.
  • Pop the characters and place them back into the string to obtain the reversed string.

Examples:

Input: GeeksQuiz
Output: ziuQskeeG

Input: HelloWorld
Output: HelloWorld

Approach

The approach follows these steps:

  1. Create an empty stack.
  2. Push each character of the string onto the stack.
  3. Pop each character from the stack.
  4. Store the popped characters back into the string.
  5. The resulting string is the reverse of the original string.
C++
#include <iostream>
#include <stack>
#include <string>
using namespace std;

void reverseString(string& str)
{
    stack<char> st;

    // Push all characters onto the stack
    for (char ch : str)
        st.push(ch);

    // Pop characters and store them back
    for (char& ch : str) {
        ch = st.top();
        st.pop();
    }
}

int main()
{
    string str = "GeeksQuiz";

    reverseString(str);

    cout << "Reversed string is " << str;

    return 0;
} 

Output
Reversed string is ziuQskeeG

Explanation: The string "GeeksQuiz" is pushed into the stack from left to right. The last character, 'z', is pushed last and therefore popped first. Similarly, the remaining characters are popped in reverse order and placed back into the string.

Thus, the original string:

G e e k s Q u i z

becomes:

z i u Q s k e e G

and the final result is:

ziuQskeeG

Try It Yourself
redirect icon
Comment

Explore

    C++ Basics
    • Introduction2 min read
    • Data Types4 min read
    • Variables4 min read
    • Operators9 min read
    • Basic Input / Output2 min read
    • Control Flow Statements15+ min read
    • Loops7 min read
    • Functions5 min read
    • Arrays5 min read
    Core Concepts
    • Pointers and References4 min read
    • new and delete Operators5 min read
    • Templates in C++7 min read
    • Struct Union Enum3 min read
    • Exception Handling5 min read
    • File Handling9 min read
    • Multithreading6 min read
    • Namespace5 min read
    OOP
    • OOP Concepts10 min read
    • Inheritance6 min read
    • Polymorphism5 min read
    • Encapsulation3 min read
    • Abstraction4 min read
    Standard Template Library(STL)
    • STL3 min read
    • Container Library3 min read
    • Iterators10 min read
    • Algorithm Library3 min read
    Practice & Problems
    • Interview Questions15+ min read
    • Programming Examples4 min read
    Courses
    • Placement 360 Course2 min read
    • DSA and System Design 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.