Given a string str, print reverse all words except the first and last words.
Input : Hi how are you geeks Output : Hi woh era uoy geeks Input : I am fine Output : I ma fine
- Print the first word.
- For remaining middle words, print reverse of every word after reaching end of it. This will print reverse of all words except the last word.
- Print last word.
Hi woh era uoy geeks
Time Complexity : O(n)
Auxiliary Space : O(L) where L is length of the longest word in the string.
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.
- Check if the given string of words can be formed from words present in the dictionary
- Sorting array with reverse around middle
- Reverse Middle X Characters
- 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
- 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
- Count words that appear exactly two times in an array of words
- Reverse individual words
- Reverse individual words with O(1) extra space
- Lexicographically middle string
- String formed with middle character of every right substring followed by left sequentially
- Print the middle character of a string
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Check if a given string is a Reverse Bitonic String or not
- Replace the middle element of matrix with sum of surrounding elements
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.