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++ :
skeeGrofskeeG si doog ot nrael
Time complexity : O(n)
Space complexity : O(n)
Using Java 8 Streams
emocleW ot GFG
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Reverse individual words with O(1) extra space
- Reverse words in a given 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
- Program to reverse words in a given string in C++
- Reverse middle words of a string
- 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++
- Count words in a given string
- Group words with same set of characters
- Print all the non-repeating words from the two given sentences
- Number of words in a camelcase sequence
- Shortest substring of a string containing all given words
- Remove all the palindromic words from the given sentence
- Generating Lyndon words of length n
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.