Given a string str consisting of a sentence, the task is to reverse the entire sentence word by word.
Input: str = “geeks for geeks”
Output: geeks for geeks
Input: str = “data structures and algorithms”
Output: algorithms and structures data
- 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:
geeks for geeks
- Stack | Set 3 (Reverse a string using stack)
- Reverse a stack using recursion
- Reverse a number using stack
- 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
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Stack Permutations (Check if an array is stack permutation of other)
- Find maximum in stack in O(1) without using additional stack
- Sort a stack using a temporary stack
- Split a sentence into words in C++
- gOOGLE cASE of a given sentence
- Camel case of a given sentence
- Snake case of a given sentence
- 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 firstname.lastname@example.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.