Java Program to Reverse Each Word in a Sentence



Java Program to Reverse Each Word in a Sentence

Sample Code

public class Example {
  public static void main(String[] args) {
    String str = "the sky is blue";
    System.out.println("The original string is: " + str);
    String strWords[] = str.split("\s");
    String rev = "";
    for (String sw: strWords) {
      StringBuilder sb = new StringBuilder(sw);
      sb.reverse();
      rev += sb.toString() + " ";
    }
    System.out.println("The modified string is: " + rev.trim());
  }
}

Output

The original string is: the sky is blue
The modified string is: eht yks si eulb

Related Searches to Java Program to Reverse Each Word in a Sentence