C++ Program to Reverse a String Using Stack Last Updated : 18 Dec, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a string, reverse it using stack. For example "GeeksQuiz" should be converted to "ziuQskeeG". Following is simple algorithm to reverse a string using stack. 1) Create an empty stack.2) One by one push all characters of string to stack.3) One by one pop all characters from stack and put them back to string.Recommended PracticeReverse a string using StackTry It!Following programs implements above algorithm. C++ // C++ program to reverse a string // using stack #include <bits/stdc++.h> using namespace std; // A structure to represent // a stack class Stack { public: int top; unsigned capacity; char* array; }; // function to create a stack of // given capacity. It initializes // size of the stack as 0 Stack* createStack(unsigned capacity) { Stack* stack = new Stack(); stack->capacity = capacity; stack->top = -1; stack->array = new char[(stack->capacity * sizeof(char))]; return stack; } // Stack is full when the top is equal // to the last index int isFull(Stack* stack) { return stack->top == stack->capacity - 1; } // Stack is empty when the top is equal to -1 int isEmpty(Stack* stack) { return stack->top == -1; } // Function to add an item to stack. // It increases the top by 1 void push(Stack* stack, char item) { if (isFull(stack)) return; stack->array[++stack->top] = item; } // Function to remove an item from // stack. It decreases the top by 1 char pop(Stack* stack) { if (isEmpty(stack)) return -1; return stack->array[stack->top--]; } // A stack-based function to reverse // a string void reverse(char str[]) { // Create a stack of capacity //equal to length of string int n = strlen(str); Stack* stack = createStack(n); // Push all characters of string // to stack int i; for (i = 0; i < n; i++) push(stack, str[i]); // Pop all characters of string and // put them back to str for (i = 0; i < n; i++) str[i] = pop(stack); } // Driver code int main() { char str[] = "GeeksQuiz"; reverse(str); cout << "Reversed string is " << str; return 0; } Java import java.util.Arrays; // A class to represent a stack class Stack { int top; int capacity; char[] array; // function to create a stack of // given capacity. It initializes // size of the stack as 0 public static Stack createStack(int capacity) { Stack stack = new Stack(); stack.capacity = capacity; stack.top = -1; stack.array = new char[stack.capacity]; return stack; } // Stack is full when the top is equal // to the last index public static boolean isFull(Stack stack) { return stack.top == stack.capacity - 1; } // Stack is empty when the top is equal to -1 public static boolean isEmpty(Stack stack) { return stack.top == -1; } // Function to add an item to stack. // It increases the top by 1 public static void push(Stack stack, char item) { if (isFull(stack)) return; stack.array[++stack.top] = item; } // Function to remove an item from // stack. It decreases the top by 1 public static char pop(Stack stack) { if (isEmpty(stack)) return '\0'; return stack.array[stack.top--]; } } // A stack-based function to reverse // a string class ReverseString { static void reverse(char str[]) { // Create a stack of capacity // equal to the length of the string int n = str.length; Stack stack = Stack.createStack(n); // Push all characters of the string // to the stack for (int i = 0; i < n; i++) Stack.push(stack, str[i]); // Pop all characters of the string and // put them back to str for (int i = 0; i < n; i++) str[i] = Stack.pop(stack); } // Driver code public static void main(String[] args) { char[] str = "GeeksQuiz".toCharArray(); reverse(str); System.out.println("Reversed string is " + Arrays.toString(str)); } } // Contributed by Gaurav_Arora Output: Reversed string is ziuQskeeGTime Complexity: O(n) where n is number of characters in stack. Auxiliary Space: O(n) for stack. Comment More infoAdvertise with us Next Article C++ Program to Reverse a String Using Stack kartik Follow Improve Article Tags : Strings Stack C++ Programs C++ DSA CPP Strings Programs +2 More Practice Tags : CPPStackStrings Similar Reads Stack in C++ STL In C++, stack container follows LIFO (Last In First Out) order of insertion and deletion. It means that most recently inserted element is removed first and the first inserted element will be removed last. This is done by inserting and deleting elements at only one end of the stack which is generally 5 min read stack empty() and stack size() in C++ STL The std::stack::size() and std::stack::empty() in C++ are built-in functions that are used to provide information about the size of the stack. They are the member functions of the std::stack container defined inside <stack> header file.stack::empty()The stack::empty() method is used to check w 2 min read stack::push() and stack::pop() in C++ STL The stack::push() and stack::pop() method in stack container is used to insert and delete the element from the top of stack. They are the member functions of std::stack container defined inside <stack> header file. In this article, we will learn how to use stack::push() and stack::pop() method 2 min read stack top() in C++ STL In C++, the std::stack::top() is used to find the top element of the std::stack container. It is a member function of std::stack class defined inside the <stack> header file. In this article, we will learn how to find the top element of stack using stack::top() in C++.Example:C++#include <b 2 min read stack emplace() in C++ STL Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. stack::emplace() This function is used to insert a new element into the stack container, the new element is added on top o 3 min read stack swap() in C++ STL Stacks are a type of container adaptors with LIFO(Last In First Out) type of work, where a new element is added at one end and (top) an element is removed from that end only.stack::swap()This function is used to swap the contents of one stack with another stack of same type but the size may vary. Sy 2 min read List of Stacks in C++ STL Prerequisite: List, Stack Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, list has slow traversal, but once a position has been found, insertion and deletion are quick. Syntax: list <Type> name_of_list; Stack are a type of container adaptor wit 3 min read Implementing Stack Using Class Templates in C++ The task is to implement some important functions of stack like pop(), push(), display(), topElement(), isEmpty(), isFull() using class template in C++. Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or 5 min read How to implement a Stack using list in C++ STL In this article, we will discuss how to implement a Stack using list in C++ STL. Stack is a linear data structure which follows. LIFO(Last In First Out) or FILO(First In Last Out). It mainly supports 4 major operations:1. Push: Push an element into the stack.2. Pop: Removes the element by following 3 min read Queue using Stacks Given a stack that supports push and pop operations, your task is to implement a queue using one or more instances of that stack along with its operations.Table of ContentBy Making Enqueue Operation CostlyBy Making Dequeue Operation Costly Queue Implementation Using One Stack and RecursionBy Making 11 min read Like