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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Reverse individual words with O(1) extra space
- 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
- Reverse words in a given String in Java
- Print words of a string in reverse order
- Reverse String according to the number of words
- Reverse middle words of a string
- Program to reverse words in a given string in C++
- Reverse words in a given string | Set 2
- Reverse words in a given string
- Reverse the Words of a String using Stack
- Reverse words in a given String in Python
- Given a sequence of words, print all anagrams together | Set 2
- Program to convert a given number to words
- Boggle (Find all possible words in a board of characters) | Set 1
- Print all words matching a pattern in CamelCase Notation Dictonary
- Print all valid words that are possible using Characters of Array
- Word formation using concatenation of two dictionary words
- Delete consecutive same words in a sequence
- Put spaces between words starting with capital letters
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.