![]() |
VOOZH | about |
Given string str consisting of multiple words, the task is to reverse the entire string word by word.
Examples:
Input: str = "I Love To Code"
Output: Code To Love I
Input: str = "data structures and algorithms"
Output: algorithms and structures data
Approach: This problem can be solved not only with the help of the strtok() but also it can be solved by using Stack Container Class in STL C++ by following the given steps:
Below is the implementation of the above approach:
Code To Love I
Time Complexity: O(N), for traversing over the string.
Auxiliary Space: O(N), for storing the words in the string.
Another Approach: An approach without using stack is discussed here. This problem can also be solved using stack by following the below steps:
Below is the implementation of the above approach:
geeks for geeks
Time Complexity: O(N), for traversing over the string.
Auxiliary Space: O(N), for storing the words in the string.