June 22, 2022

Java Program to Reverse Each Word in a String

In this post we’ll see a Java program to reverse each word in a String individually rather than reversing the whole string.

Steps to reverse each word in a String

In order to write a Java program to reverse each word in a String you can follow the steps as given below-

  1. Split the passed String using split() method, that gives you an array having all the words in a String.
  2. Iterate the array, taking one word at a time and reverse it. For reversing you can write your own logic using both recursive and non-recursive methods or use reverse method of the StringBuilder() class.
  3. Append each reversed word to a string.

Reverse each word in a String using recursion – Java Program

public class ReverseWord {
  public static void main(String[] args) {
    // /Using recursive logic
    String str = "This is a test string";
    StringBuilder sb = new StringBuilder();
    //Split String on spaces
    String[] strArr = str.split("\\s+");
    // Iterate word by word
    for(String s : strArr) {
      // reverse and append
      sb.append(reverseString(s)).append(" ");
    }
    System.out.println("Original String- " + str);
    System.out.println("Reversed String- " + sb.toString());
  }
	  
  private static String reverseString(String str) {
    // base case
    if((str == null) || (str.length() <= 1)){
      return str;
    }
    // recursive call
    return reverseString(str.substring(1)) + str.charAt(0);  
  }
}
Output
Original String- This is a test string
Reversed String- sihT si a tset gnirts

Reverse each word in a String non-recursive – Java Program

public class ReverseWord {
  public static void main(String[] args) {
    // /Using recursive logic
    String str = "This is non-recursive reverse method";
    StringBuilder sb = new StringBuilder();
    //Split String on spaces
    String[] strArr = str.split("\\s+");
    // Iterate word by word
    for(String s : strArr) {
      // reverse and append
      sb.append(reverseString(s)).append(" ");
    }
    System.out.println("Original String- " + str);
    System.out.println("Reversed String- " + sb.toString());
  }
	  
  private static String reverseString(String str){
    // validate String
    if((str == null) || (str.length() <= 1)){
      return str;
    }
    // reverse one char at a time
    StringBuilder sb = new StringBuilder();
    for(int i = str.length() - 1; i >= 0; i--){
      sb.append(str.charAt(i));
    }
    return sb.toString();
  }
}
Output
Original String- This is non-recursive reverse method
Reversed String- sihT si evisrucer-non esrever dohtem

Using reverse() method of StringBuilder class

public class ReverseWord {
  public static void main(String[] args) {
    // /Using recursive logic
    String str = "This is a test String";
    StringBuilder sb = new StringBuilder();
    //Split String on spaces
    String[] strArr = str.split("\\s+");
    // Iterate word by word
    for(String s : strArr) {
      // reverse and append
      sb.append(reverseString(s)).append(" ");
    }
    System.out.println("Original String- " + str);
    System.out.println("Reversed String- " + sb.toString());
  }
	  
  private static String reverseString(String str){
    // validate String
    if((str == null) || (str.length() <= 1)){
      return str;
    }
    StringBuilder sb = new StringBuilder(str);
    return sb.reverse().toString();
  }
}
Output
Original String- This is a test String
Reversed String- sihT si a tset gnirtS

That's all for the topic Java Program to Reverse Each Word in a String. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment