In this post, we will write a Java program to reverse each word of an input String.
The logic is very simple. just follow below steps:
- Split the input String using split() method, split method returns a string array.
- Iterate over string array and which internally makes a call to reverseWithStringConcat() method
- The reverseWithStringConcat() method is responsible to reverse each word and return the same
- Concatenated each reverse words using + operator (in this program we are using only String not StringBuilder or StringBuffer)
- Call display() method to print original and reverse strings
Java Program To Reverse Each Word Of A String
/** * Java Program To Reverse Each Word Of A String. * * @author javaguides.net * */ public class ReverseEachWord { public static void main(String[] args) { reverseEachWord("java guides"); reverseEachWord("I am string not reversed"); reverseEachWord("Reverse Me"); } private static String reverseEachWord(String originalStr) { // split the sentence by words using split method String[] words = originalStr.split(" "); String reverseStr = ""; // Iterate over String array for (String word: words) { // reverse each word by calling reverseWithStringConcat method reverseStr = reverseStr + reverseWithStringConcat(word) + " "; } display(originalStr, reverseStr); return reverseStr; } private static final void display(String original, String reverse) { System.out.println(original); System.out.println(reverse); System.out.println("----------------------------"); } private static final String reverseWithStringConcat(String string) { String reverseWord = ""; for (int i = (string.length() - 1); i >= 0; i--) { reverseWord = reverseWord + string.charAt(i); } return reverseWord; } }
Output:
java guides
avaj sediug
----------------------------
I am string not reversed
I ma gnirts ton desrever
----------------------------
Reverse Me
esreveR eM
----------------------------
Now, let's use StringBuilder to rewrite above program ( We suggest you to use StringBuilder for concatenation)
Using StringBuilder
public class ReverseEachWord {
public static void main(String[] args) {
reverseEachWord("java guides");
reverseEachWord("I am string not reversed");
reverseEachWord("Reverse Me");
}
private static String reverseEachWord(String originalStr) {
// split the sentence by words using split method
String[] words = originalStr.split(" ");
StringBuilder reverseStr = new StringBuilder();
// Iterate over String array
for (String word : words) {
// reverse each word by calling reverseWithStringConcat method
StringBuilder temp = new StringBuilder(word);
reverseStr.append(temp.reverse());
reverseStr.append(" ");
}
display(originalStr, reverseStr.toString());
return reverseStr.toString();
}
private static final void display(String original , String reverse){
System.out.println(original);
System.out.println(reverse);
System.out.println("----------------------------");
}
}
Output:
java guides
avaj sediug
----------------------------
I am string not reversed
I ma gnirts ton desrever
----------------------------
Reverse Me
esreveR eM
----------------------------
You can optimize above program logic.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Related String Programs
Note that these programs are asked in interviews.- Java program to Count Number of Duplicate Words in String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains only Digits
- How to Check if the String Contains only Letters
- How to Check If the String Contains Only Letters or Digits
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course