Given a string str, we need to print reverse of individual words.
Input : Hello World Output : olleH dlroW Input : Geeks for Geeks Output : skeeG rof skeeG
Method 1 (Simple): Generate all words separated by space. One by one reverse words and print them separated by space.
Method 2 (Space Efficient): We use a stack to push all words before space. As soon as we encounter a space, we empty the stack.
skeeG rof skeeG
Using stringstream in C++ :
Time complexity : O(n)
Space complexity : O(n)
- Reverse individual words with O(1) extra space
- Reverse words in a given string
- Reverse words in a given String in Java
- Reverse words in a given String in Python
- Reverse middle words of a string
- Reverse String according to the number of words
- Print words of a string in reverse order
- Check if the given string of words can be formed from words present in the dictionary
- Count words that appear exactly two times in an array of words
- Acronym words
- Split a sentence into words in C++
- Possible Words using given characters in Python
- Group words with same set of characters
- Count words in a given string
- Count palindrome words in 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.
Improved By : shrikanth13