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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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)
- Program to reverse a linked list using Stack
- Print Reverse a linked list using Stack
- Frequency of smallest character in first sentence less than that of second sentence
- 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
- Calculate the difficulty of a sentence
- Split a sentence into words in C++
- gOOGLE cASE of a given sentence
- Remove all the palindromic words from the given sentence
- Check if a word is present in a sentence
- Count palindrome words in a sentence
- Find the word with most anagrams in a given sentence