# Recursion help

Page 1 of 1

## 7 Replies - 761 Views - Last Post: 26 April 2009 - 07:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=101766&amp;s=033352d2d4dbc3902d1c527bb63c350a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Hikaroshi

Reputation: 8
• Posts: 42
• Joined: 26-April 09

# Recursion help

Posted 26 April 2009 - 02:37 PM

I have no idea how to do recursion but apparently it's on the APCS test... I don't even know why they have recursion on it because I realized that not that many people even use it.

Well, I was working on some questions my teacher found on some site and I was having difficulty on this question:
```public static void method0910(String s)
{
int n = s.length();
if (n > 0)
{
String temp = s.substring(0,n-1);
method0910(temp);
System.out.println(s);
}
}

```

Which of the following is printed as a result of the call method0910("TANGO") ?

I looked at the solution and it says it's TA TAN TANG TANGO... But, how? What does the 0,n-1 do? The ',' I never understood either. Can somebody please go over this for me?

Is This A Good Question/Topic? 0

## Replies To: Recursion help

### #2 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Recursion help

Posted 26 April 2009 - 03:36 PM

The comma -- , -- is just used to separate arguments passed to the substring() method.

The 0 is the first argument. That is the argument of the index to start at. 0 means the first character in the string.

The n - 1 is what character index to go to. The first method call it will go to the last letter of the string. In the next call, it will go to the second to last character in the string, and so on, while the length of the string is greater than 0. The variable n changes with every call of the method, which is how the recursion actually operates.

This post has been edited by Locke: 26 April 2009 - 03:37 PM

### #3 Skyd

Reputation: 6
• Posts: 27
• Joined: 09-April 09

## Re: Recursion help

Posted 26 April 2009 - 05:46 PM

method0910("TANGO") performs two major tasks and they are: method0910("TANG") and System.out.println("TANGO"). Here's why:

First, the input s is equal to "TANGO".
n is the length of s which is = 5
temp is a substring of s from 0 to (5-1) which is "TANG"

Therefore, method0910 eventually gets to:
1. method0910("TANG")
2. System.out.println("TANGO")

Now lets try to see the entire process visually. we call method0910("TANGO") and it does this:
1. method0910("TANG")
2. System.out.println("TANGO")

now, what does method0910("TANG") do? it calls method0910("TAN") and System.out.println("TANG"). So lets replace method0910("TANG") with method0910("TAN") and System.out.println("TANG"):

1. method0910("TAN")
2. System.out.println("TANG")
3. System.out.println("TANGO")

Now replace method0910("TAN") with method0910("TA") and System.out.println("TA"):

1. method0910("TA")
2. System.out.println("TAN")
3. System.out.println("TANG")
4. System.out.println("TANGO")

Replace method0910("TA")

1. method0910("T")
2. System.out.println("TA")
3. System.out.println("TAN")
4. System.out.println("TANG")
5. System.out.println("TANGO")

Finally, now we replace method0910("T")

1. method0910("")
2. System.out.println("T")
3. System.out.println("TA")
4. System.out.println("TAN")
5. System.out.println("TANG")
6. System.out.println("TANGO")

Notice we can't replace method0910("") anymore. This is becase the length of "" is 0, and inside the method0910, it says only to perform its two main tasks if the length n is more than 0. Thus, method0910("") does absolutely nothing. Therefore we can now we can evaluate the print statements:

T
TA
TAN
TANG
TANGO

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Recursion help

Posted 26 April 2009 - 06:23 PM

Why everybody having to wite a recursion program wrote them and then ask us to show them how it works ?

### #5 Hikaroshi

Reputation: 8
• Posts: 42
• Joined: 26-April 09

## Re: Recursion help

Posted 26 April 2009 - 07:08 PM

Thank you very much, I understand how it works now.

@pbl, this isn't my code.

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Recursion help

Posted 26 April 2009 - 07:10 PM

Hikaroshi, on 26 Apr, 2009 - 06:08 PM, said:

Thank you very much, I understand how it works now.

@pbl, this isn't my code.

so you want us to debug code that you cut and pasted from the web ?

### #7 Hikaroshi

Reputation: 8
• Posts: 42
• Joined: 26-April 09

## Re: Recursion help

Posted 26 April 2009 - 07:17 PM

pbl, on 26 Apr, 2009 - 06:10 PM, said:

Hikaroshi, on 26 Apr, 2009 - 06:08 PM, said:

Thank you very much, I understand how it works now.

@pbl, this isn't my code.

so you want us to debug code that you cut and pasted from the web ?

No, I just wanted to know how does the result come out to be TA TAN TANG TANGO. If I didn't come here I would never figure out why the answer is "TA TAN TANG TANGO". I know my teacher wouldn't be capable of telling me.

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Recursion help

Posted 26 April 2009 - 07:20 PM