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 String according to the number of words
- Reverse middle words of a string
- Reverse words in a given String in Python
- 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
- Group words with same set of characters
- Possible Words using given characters in Python
- Count words in a given string
- Split a sentence into words in C++
- Number of words in a camelcase sequence
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.