Java使用递归反转字符串
示例
下面是递归代码以反转字符串
/*** Just a snippet to explain the idea of recursion
*
**/
public class Reverse {
public static void main (String args[]) {
String string = "hello world";
System.out.println(reverse(string)); //打印dlrow olleh
}
public static String reverse(String s) {
if (s.length() == 1) {
return s;
}
return reverse(s.substring(1)) + s.charAt(0);
}
}
以上是 Java使用递归反转字符串 的全部内容, 来源链接: utcz.com/z/343180.html