Write a Java program to take an input string and exchange the first and last word and revers the middle word.
Input : Hello World GFG Welcomes You Output :You semocleW GFG dlroW Hello
- First we take two empty Strings and first String take the first word and second String takes the last word
- When we iterate each word, then we must take care about the variable pointing to next word apart from the last word.
- Now we reverse the left String in the given String.
- After the above process, we first print last word and reverse of left Strings and after that the first word.
You semocleW GFG dlroW Hello
- Java program to swap first and last characters of words in a sentence
- Reverse words in a given String in Java
- Counting number of lines, words, characters and paragraphs in a text file using Java
- Add index to characters and reverse the string
- How to swap or exchange objects in Java?
- Collections swap() method in Java with Examples
- TCS Coding Practice Question | Swap two Numbers
- Swap two variables in one line in C/C++, Python, PHP and Java
- Print all unique words of a String
- Swap two Strings without using third user defined variable in Java
- Swapping items of a list in Java : Collections.swap() with Example
- Java ArrayList to print all possible words from phone digits
- Print number of words, vowels and frequency of each character
- Reverse a LinkedList in Java
- Reverse an ArrayList in Java
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.