2010/2/14 Oscar Mederos <[email protected]>: > 2010/2/13 Charles A. Lopez <[email protected]>: >> your logic is a bit off.. >> you compare if the start is equal to the end and if true you return true. >> That is wrong. >> Recursion is a function/method/procedure that calls itself. It has a base >> case. The base case will ensure the recursion will actually stop. It also >> has a recursive case. >> Consider the recursion for factorial(n). >> If we use real values, then 5! is 5 * 4 * 3 * 2 * 1. >> The base case in that recursion is factorial(1). >> The recursive case is factorial(n-1). >> Explicitly, >> 5! = 5 * factorial(5-1) >> = 5 * factorial(4) >> = 5 * 4 * factorial(4-1) >> = 5 * 4 * 3 * factorial(3) >> .... >> = 5 * 4 * 3 * ..... * factorial(1) >> = 5 * 4 * 3 * 2 * 1 >> = 120 >> >> What is the base case for palindrome(...);?
I don't see why is that wrong. It just depends on the algorithm you design. In that case, his algorithm receives two numbers, which are the indexes of the two chars to compare. If those indexes are equals, of course, word[index1] == word[index2] and, string is a palindrome (You don't need to increment the first index and decrement the second, because you already compared all chars). It can just return 'true' because, if someone recursively called the method is because word[index1] == word[index2] before calling, so, if index1 == index2, it just finished the algorithm. >> -- >> Charles A. Lopez >> [email protected] >> >> Registered Microsoft Partner >> >> New York City, NY >> >> I'm running on Windows 7 Build 7100 >> >> Quality Software Works -- Oscar Mederos
