简体   繁体   English

在 Java 中使用递归将字符串转换为反向字符串

[英]Convert String to Reverse String Using Recursion in Java

Today I am trying to convert String to reverse String eg(Cat Is Running into Running Is Cat) word by word not Character今天我试图将字符串转换为反向字符串, eg(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");
    }
}

The following output is shown:显示以下 output:

Running Is Cat BUILD SUCCESSFUL (total time: 0 seconds)

I am trying to convert String into reverse String same as above but through Recursion method but it seems too confusing.我正在尝试将字符串转换为与上面相同的反向字符串,但通过递归方法,但它似乎太混乱了。 and display more errors.并显示更多错误。 Can someone please help me understanding it.有人可以帮我理解它。 Many thanks非常感谢

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

This code show following output:此代码显示以下 output:

Running
Is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

This code do not print (0) index why?这段代码不打印(0) index为什么? can someone help me to solve this error please有人可以帮我解决这个错误吗

This should work:这应该有效:

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

This solution might be helpful.此解决方案可能会有所帮助。 The comments explain the code pretty much.注释几乎解释了代码。

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
}

You are sending the last element of the Array next time instead of the String without the previously printed String.下次您将发送数组的最后一个元素,而不是没有先前打印的字符串的字符串。

Replace your return statement with this it should work.用它替换你的 return 声明它应该可以工作。

return reverse_recursion(n==0?null:str.substring(0,(str.length()-Arry[n].length())-1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM