Reverse the Sentence using Stack
Given a string str consisting of a sentence, the task is to reverse the entire sentence word by word.
Examples:
Input: str = “geeks for geeks”
Output: geeks for geeksInput: str = “data structures and algorithms”
Output: algorithms and structures data
Approach: An approach without using stack is discussed here. This problem can also be solved using stack by following the below steps:
- Create an empty stack.
- Tokenize the input string into words using spaces as separator with the help of strtok()
- Push the words into the stack.
- Pop the words from the stack until the stack is not empty which will be in reverse order.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to reverse the words // of the given sentence void reverse(char k[]) { // Create an empty character array stack stack<char*> s; char* token = strtok(k, " "); // Push words into the stack while (token != NULL) { s.push(token); token = strtok(NULL, " "); } while (!s.empty()) { // Get the words in reverse order cout << s.top() << " "; s.pop(); } } // Driver code int main() { char k[] = "geeks for geeks"; reverse(k); return 0; } |
Java
// Java implementation of the approach import java.util.Arrays; import java.util.Stack; class GFG { // Function to reverse the words // of the given sentence static void reverse(String k) { // Create an empty character array stack Stack<String> s = new Stack<>(); String[] token = k.split(" "); // Push words into the stack for(int i = 0; i < token.length; i++) { s.push(token[i]); } while (!s.empty()) { // Get the words in reverse order System.out.print(s.peek() + " "); s.pop(); } } // Driver code public static void main(String[] args) { String k = "geeks for geeks"; reverse(k); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach # Function to reverse the words # of the given sentence def reverse(k): # Create an empty character array stack s = [] token = k.split() # Push words into the stack for word in token : s.append(word); while (len(s)) : # Get the words in reverse order print(s.pop(),end= " "); # Driver code if __name__ == "__main__" : k = "geeks for geeks"; reverse(k); # This code is contributed by AnkitRai01 |
geeks for geeks
Recommended Posts:
- Stack | Set 3 (Reverse a string using stack)
- Reverse a number using stack
- Reverse a stack using recursion
- Reverse a stack without using extra space in O(n)
- Print Reverse a linked list using Stack
- Program to reverse a linked list using Stack
- Stack Permutations (Check if an array is stack permutation of other)
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Find maximum in stack in O(1) without using additional stack
- Sort a stack using a temporary stack
- Camel case of a given sentence
- Snake case of a given sentence
- gOOGLE cASE of a given sentence
- Split a sentence into words in C++
- Calculate the difficulty of a sentence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



