1

So for homework this weekend we're learning about recursion so that means the dreaded string reversal with recursion problems. I have a simple piece of code that is correctly performing the recursion (according to the debug mode in eclipse) but since (?? this is where I'm confused) I'm calling it from a a different area of the program, it just keeps going on infinitely until it crashes or overflows or whatever it's called.

public static void main(String[] args) 
{
    System.out.println(reversePrint("Hello"));
}

public static String reversePrint(String s)
{
    if (s.length() <= 1)
        return s;

    return reversePrint(s.substring(1) + s.charAt(0));
}

I've been trying to Google-fu my way to figuring out why but I just can't, most sites explain recursion with pretty much the code I've written for the actual reversal of the string, but none seem to deal with any problems with getting it to print. I honestly don't know what I'm overlooking, been at this for a few hours, feels like I'm banging my head against a wall.

1
  • 1
    Your recursive call adds a character so the length can never be <= 1 Commented Apr 12, 2014 at 2:24

3 Answers 3

3

Try this:

public static String reversePrint(String s) {
    if (s.length() <= 1)
        return s;
    return reversePrint(s.substring(1)) + s.charAt(0);
}

Here was the problem in your code:

reversePrint(s.substring(1) + s.charAt(0))

You kept calling the reversePrint() method with a string of the same size - and as you know, a recursion must "reduce" the problem at each step until it hits the base case, or else it will never end.

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

3 Comments

Man I knew it was something small like that, obviously I could see in the debugger that my string was always the same size, which baffled me, but I couldn't figure out where I slipped up.
The devil is in the details :)
You'd think I'd be rewarded for staying IN on a Friday night but nooo, my brain has other plans I guess. Thanks, I can't accept this for another 5 minutes.
1

If you use reversePrint(s.substring(1) + s.charAt(0)), you always pass the full string into reversePrint.

You should make sure that the string passed to reversePrint is always getting smaller:

reversePrint(s.substring(1)) + s.charAt(0);

Notice how what I pass to reversePrint is one character smaller than s.

1 Comment

Yeah, thanks to the both of you, I should have picked up on that.
0

I Am Also Studying Recursion In Class Now, and The important Part Is To Remember Base Case And Proper Method Calls That Iterate Logically. Simple Syntax Errors Like Parentheses:

return reversePrint(s.substring(1) + s.charAt(0));

Won't Make Or Break It. Learning The Logic And Concept Of Recursion Is Much More Important. Stay Positive!

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.