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(...);? On 12 February 2010 18:27, newbiecoder <[email protected]> wrote: > I am having trouble mastering the concept of recursion...does anyone > here have any advice / excellent articles to share??? > > > I don't understand how to create a recursion for this palindrome > method given this format > Palindrome(string str, int start, int end) > > Not sure if the code below is the proper way. It does not work if > 'end' is an odd number.... > > > public static bool Palindrome(string str, int start, int end) > { > bool result = false; > > if (start == end) > > > { return true; } > > > else if (str[start] == str[end]) > > { result = Palindrome(str, start + 1, end - 1); } > > > > else > result = false; > > > return result; > > > } > -- Charles A. Lopez [email protected] Registered Microsoft Partner New York City, NY I'm running on Windows 7 Build 7100 Quality Software Works
