In this post, we will write a Java program to reverse each word of an input String.
The logic is very simple. just follow the below steps:
- Split the input String using the split() method, the 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 word using the + 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
----------------------------
Using StringBuilder
Now, let's use StringBuilder to rewrite the above program ( We suggest you use StringBuilder for concatenation)
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 the above program logic.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Related Java String Programs with Output
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a 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
- Java Program to Swap Two Strings Without Using Third Variable
Comments
Post a Comment
Leave Comment