Java程序反转字符串中的每个单词

时间:2020-01-09 10:35:33  来源:igfitidea点击:

在本文中,我们将看到一个Java程序,该程序可以单独反转String中的每个单词,而不是反转整个字符串。

反转字符串中每个单词的步骤

为了编写Java程序来反转字符串中的每个单词,我们可以按照以下步骤操作

  • 使用split()方法拆分传递的String,这将为我们提供一个包含String中所有单词的数组。
  • 迭代数组,一次获取一个单词并反转。对于反向,我们可以使用递归和非递归方法编写自己的逻辑,也可以使用StringBuilder()类的反向方法。
  • 将每个反向单词添加到字符串。

使用递归反转字符串中的每个单词– Java程序

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);  
  }
}

输出:

Original String- This is a test string
Reversed String- sihT si a tset gnirts

反转字符串非递归中的每个单词– Java程序

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();
  }
}

输出:

Original String- This is non-recursive reverse method
Reversed String- sihT si evisrucer-non esrever dohtem

使用StringBuilder类的reverse()方法

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();
  }
}

输出:

Original String- This is a test String
Reversed String- sihT si a tset gnirtS