在 Java 中使用递归将字符串转换为反向字符串
Convert String to Reverse String Using Recursion in Java
今天我尝试将字符串转换为反向字符串 e.g(Cat Is Running into Running Is Cat)
逐字而不是字符
public class ReverseString_ {
public static void reverse(String str) {
String[] a = str.split(" ");
for (int i = a.length - 1; i >= 0; i--) {
System.out.println(a[i] + " ");
}
}
public static void main(String[] args) {
reverse("Cat Is Running");
}
}
显示以下输出:
Running Is Cat BUILD SUCCESSFUL (total time: 0 seconds)
我正在尝试将字符串转换为与上述相同的反向字符串,但通过递归方法,但它似乎太混乱了。并显示更多错误。有人可以帮我理解它吗?非常感谢
public static String reverse_recursion(String str) {
if (str == null)
return null;
else {
String Arry[] = str.split(" ");
int n = Arry.length - 1;
System.out.println(Arry[n] + "");
return reverse_recursion(Arry[n - 1]);
}
}
public static void main(String[] args) {
reverse_recursion("Cat Is Running");
}
此代码显示以下输出:
Running
Is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
这段代码不打印(0) index
为什么?有人可以帮我解决这个错误
这应该有效:
public static String reverse(String s) {
int idx = s.indexOf(" ");
if (idx < 0) {
// no space char found, thus, s is just a single word, so return just s itself
return s;
} else {
// return at first the recursively reversed rest, followed by a space char and the first extracted word
return reverse(s.substring(idx + 1)) + " " + s.substring(0, idx);
}
}
public static void main(String[] args) {
System.out.println(reverse("Cat Is Running"));
}
此解决方案可能会有所帮助。注释解释了代码。
public static String reverse_recursion(String str) {
String[] arry = str.split(" ", 2); //Split into a maximum of 2 Strings
if (arry.length > 1) { //If there is more than 1 word in arry
//Return the reverse of the rest of the str (arry[1])
//and concatenate together with the first word (arry[0])
return reverse_recursion(arry[1]) + " " + arry[0];
}
return arry[0]; //If less than or equal to 1 word, just return that word
}
您下次发送数组的最后一个元素而不是没有先前打印的字符串的字符串。
将您的 return 语句替换为它应该有效。
return reverse_recursion(n==0?null:str.substring(0,(str.length()-Arry[n].length())-1));
今天我尝试将字符串转换为反向字符串 e.g(Cat Is Running into Running Is Cat)
逐字而不是字符
public class ReverseString_ {
public static void reverse(String str) {
String[] a = str.split(" ");
for (int i = a.length - 1; i >= 0; i--) {
System.out.println(a[i] + " ");
}
}
public static void main(String[] args) {
reverse("Cat Is Running");
}
}
显示以下输出:
Running Is Cat BUILD SUCCESSFUL (total time: 0 seconds)
我正在尝试将字符串转换为与上述相同的反向字符串,但通过递归方法,但它似乎太混乱了。并显示更多错误。有人可以帮我理解它吗?非常感谢
public static String reverse_recursion(String str) {
if (str == null)
return null;
else {
String Arry[] = str.split(" ");
int n = Arry.length - 1;
System.out.println(Arry[n] + "");
return reverse_recursion(Arry[n - 1]);
}
}
public static void main(String[] args) {
reverse_recursion("Cat Is Running");
}
此代码显示以下输出:
Running
Is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
这段代码不打印(0) index
为什么?有人可以帮我解决这个错误
这应该有效:
public static String reverse(String s) {
int idx = s.indexOf(" ");
if (idx < 0) {
// no space char found, thus, s is just a single word, so return just s itself
return s;
} else {
// return at first the recursively reversed rest, followed by a space char and the first extracted word
return reverse(s.substring(idx + 1)) + " " + s.substring(0, idx);
}
}
public static void main(String[] args) {
System.out.println(reverse("Cat Is Running"));
}
此解决方案可能会有所帮助。注释解释了代码。
public static String reverse_recursion(String str) {
String[] arry = str.split(" ", 2); //Split into a maximum of 2 Strings
if (arry.length > 1) { //If there is more than 1 word in arry
//Return the reverse of the rest of the str (arry[1])
//and concatenate together with the first word (arry[0])
return reverse_recursion(arry[1]) + " " + arry[0];
}
return arry[0]; //If less than or equal to 1 word, just return that word
}
您下次发送数组的最后一个元素而不是没有先前打印的字符串的字符串。
将您的 return 语句替换为它应该有效。
return reverse_recursion(n==0?null:str.substring(0,(str.length()-Arry[n].length())-1));