0

When I am reversing a String using the recursive method it is giving me the stackOverflow Error.

public class ReverseString {

    public static void main(String[] args) {
        String str = "Juhi";
        System.out.println(recursiveString(str));
    }

    static String recursiveString(String str) {     
        return !str.equals("") ? recursiveString(str.substring(1) + str.charAt(0)) : str;

    }
}
3
  • 1
    You never pass anything but str.substring(1) + str.charAt(0) so the base case is never true (unless you pass in an empty String to begin with) and recursion never stops Commented Dec 11, 2018 at 16:46
  • 3
    Such simple bugs are trivial to figure out by using a debugger, executing the code step by step and inspecting the variable values. Learn to use your debugger, ASAP. It's really not difficult. Or at the very least add println() statements in the code. Commented Dec 11, 2018 at 16:48
  • There is a caveat with doing it this way. It won't work with all possible Unicode characters. See dzone.com/articles/the-right-way-to-reverse-a-string-in-java Commented Dec 11, 2018 at 16:56

3 Answers 3

3

This should fix the problem:

static String recursiveString(String str) {
    return !str.equals("") ? recursiveString(str.substring(1)) + str.charAt(0) : str;
}

In your code, you're not reducing the length of the string parameter, it's always the same input, so basically it's entering into an infinite loop. If we move the concatenation part after the recursive call, then the string will be shorter for each recursive call, until it's empty, signalling the end of the recursion.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for correcting it. I was giving the wrong input while recursive call.
@JuhiUdhale great! If this answer was useful, please consider accepting it, just click on the check mark to the left :)
Might be worth adding that this is not Unicode-aware, though this is probably not a requirement as it's just a learning exercise.
0
public static void main(String[] args) {
        String str = "Juhi";
        System.out.println(recursiveString(str));
    }

    static String recursiveString(String str) {
        return !str.equals("") ? recursiveString(str.substring(1) ) + str.charAt(0): str;

    }
}

Comments

0

A Unicode-aware recursive reverse

While the other answers have shown you where you went wrong and how to write a recursive string reversing algorithm that works for basic Unicode characters, they produce wrong results for supplementary Unicode characters. The following method works for all Unicode characters:

static String recursiveReverse(String str) {
    if (str.isEmpty())
        return "";
    int offsetToSecondCodePoint = str.offsetByCodePoints(0,1);
    return recursiveReverse(str.substring(offsetToSecondCodePoint))
            + str.substring(0, offsetToSecondCodePoint);
}

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.