Re: Is this recursion?
- From: "Marion" <mchew02@xxxxxxxxxxx>
- Date: 30 Jan 2007 08:17:15 -0800
The recursive step is the "reversal(aWord,position-1)" call.
The base case is position equal to 0, which is handled differently from
non-zero. Specifically, the recursive step is not done in the base case.
They have the correct relation to each other, if position is initially
non-negative. The recursive step then uses a function of position,
position-1, that ensures that it must reach the base case, position==0.
Incidentally, you should look at your handling for empty strings.
Patricia
Thanks Patricia and Gordon,
I thought my base case was there but U wasn't confident about my
reasoning until you two explained it a bit to me.
Patricia, I learned from Gordon in another program that strings were
compared using ".equal" and not "==". I'm afraid I will never forget
this even after I am 101 years old . . . with senility. It's been
burned into my brain, permanently. :p)
In the simplified example code I provided, the empty string does not
exist since the program initialized before calling the recursive
method. I understand that calling a recursive method when a negative
number condition exists is not a happy event. In my current code that
is not shown here, I do check for the empty string condition before
proceeding.
Thanks very much for your analysis, Patricia and Gordon, btw. Much
appreciated!
.
- Follow-Ups:
- Re: Is this recursion?
- From: Patricia Shanahan
- Re: Is this recursion?
- References:
- Is this recursion?
- From: Marion
- Re: Is this recursion?
- From: Patricia Shanahan
- Is this recursion?
- Prev by Date: Re: New line in a JTextArea (student learning here)
- Next by Date: Re: Is there an easy way to find the right class?
- Previous by thread: Re: Is this recursion?
- Next by thread: Re: Is this recursion?
- Index(es):
Relevant Pages
|