Java Program to Reverse Each Word of a String

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:
  1. Split the input String using the split() method, the split method returns a string array.
  2. Iterate over string array and which internally makes a call to reverseWithStringConcat() method
  3. The reverseWithStringConcat() method is responsible to reverse each word and return the same
  4. Concatenated each reverse word using the + operator (in this program we are using only String, not StringBuilder or StringBuffer)
  5. 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

Comments