1

Today I am trying to convert String to reverse String e.g(Cat Is Running into Running Is Cat) word by word not Character

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:

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:

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

This code do not print (0) index why? can someone help me to solve this error please

3 Answers 3

1

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very help full
@user318974 No problem. If this answer helped you, please mark it as the accepted answer :)
@MuhammadUsama It now works correctly. Test case: Hello World From Here becomes Here From World Hello.
1

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

Comments

0

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 reverse_recursion(n==0?null:str.substring(0,(str.length()-Arry[n].length())-1));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.