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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- 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
- Print the middle character of a string
- Generate Random Numbers Using Middle Square Method in Java
- Swap the first and last character of a string in Java
- How to swap or exchange objects in Java?
- Swap two Strings without using third user defined variable in Java
- Swapping items of a list in Java : Collections.swap() with Example
- Collections swap() method in Java with Examples
- TCS Coding Practice Question | Swap two Numbers
- Java Program to Swap two Variables
- Java Program to Swap Two Numbers Using Bitwise XOR Operation
- Swap two variables in one line
- Java Program to Swap Two Numbers
- Print number of words, vowels and frequency of each character
- Print all unique words of a String
- Java ArrayList to print all possible words from phone digits
- Find the count of M character words which have at least one character repeated
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.