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 words in a given string
- Reverse middle words of a string
- Reverse String according to the number of words
- Reverse words in a given String in Java
- 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
- Possible Words using given characters in Python
- Split a sentence into words in C++
- Group words with same set of characters
- Count words in a given string
- Program to extract words from a given String
- Shortest substring of a string containing all given words
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