How to Reverse a Word Using Stack in C++? Last Updated : 12 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, we have a stack data structure that follows the Last In First Out (LIFO) principle. In this article, we will learn how to reverse a word using a stack in C++. Example: Input:word = "GeeksforGeeks"Output:Reversed Word: skeeGrofskeeGReverse a String Using Stack in C++To reverse a word using a stack in C++, we can push each character of the word onto the stack and then pop them off. Since a stack is a LIFO data structure, the characters will be popped off in the reverse order they were pushed on, effectively reversing the word. ApproachFirst, create an empty stack.Now, push all characters of the word to the stack.Start popping the characters from the stack and store them back in the reversed_word.Finally, we get the reversed word in reversed_word.C++ Program to Reverse a String Using StackThe below example demonstrates how we can reverse a word using a stack in C++. C++ // C++ Program to illustrate how to reverse a word using a // stack #include <iostream> #include <stack> using namespace std; int main() { // Declaring and initializing the original word string word = "GeeksforGeeks"; cout << "Original Word: " << word << endl; // Creating a stack to store characters stack<char> s; // Push each character of the word onto the stack for (char c : word) { s.push(c); } // Declaring a string to store the reversed word string reversed_word = ""; // Pop each character from the stack and append it to // the reversed word while (!s.empty()) { reversed_word += s.top(); s.pop(); } // Print the reversed word cout << "Reversed Word: " << reversed_word << endl; return 0; } OutputOriginal Word: GeeksforGeeks Reversed Word: skeeGrofskeeG Time Complexity: O(N), where N is the length of word.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Reverse a Stack in C++? G gauravgandal Follow Improve Article Tags : C++ Programs C++ STL cpp-stack CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Reverse a Vector Using a Stack in C++? In C++, the stack container follows the LIFO (Last In First Out) order of operation. It means that the element first inserted will come out at last while the element that was last inserted will come out first. In this article, we will learn how we can use stack to reverse a vector in C++ Example Inp 2 min read How to Reverse a Vector using STL in C++? Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 min read How to Reverse a Stack in C++? In C++, stacks are containers that store the elements in the last in-first out order(LIFO). In, this article, we will learn how we can reverse a stack in C++. Example Input: stack<int> S ={5,4,3,2,1} store Output: // Reversed Stack stack<int> S ={1,2,3,4,5}Reverse a Stack in C++We can re 2 min read How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read C++ Program to Reverse a String Using Stack 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 ba 4 min read Like