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
- Count words that appear exactly two times in an array of words
- Check if the given string of words can be formed from words present in the dictionary
- Reverse words in a given String in Java
- Print words of a string in reverse order
- Reverse words in a given String in Python
- 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 the Words of a String using Stack
- Reverse words in a given string
- Given a sequence of words, print all anagrams together | Set 2
- Program to convert a given number to words
- Count words in a given string
- Print all possible words from phone digits
- Boggle (Find all possible words in a board of characters) | Set 1
- Print all words matching a pattern in CamelCase Notation Dictonary
- Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word
- Print all valid words that are possible using Characters of Array
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.