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 below steps:
  1. Split the input String using split() method, 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 words using + 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 
----------------------------
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.

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