java将字符串逆序递归方式输出

hello world my friend and now递归后
输出
now and friend my world hello

  public static String reverse(String s) {
    if (s == null) {
      return s;
    }
    int i = s.indexOf(" ");
    if (i == -1) {
      return s;
    }
    return reverse(s.substring(i + 1)) + " " + s.substring(0, i);
  }

追问

太棒了,亲,几年程序员?这么6.

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-04-08
public static String getLastChar(String str){
if(str == "" || str == null || str.length() <= 1)
return str;
int length = str.length();
return str.charAt(length -1) + getLastChar(str.substring(0,length-1));
}
相似回答