You are on page 1of 2

RecursivePalindrome Assignment for C Programming

M. Beeson April 12, 2010

The Assignment

This assignment involves practice with writing a recursive function in C. A simple palindrome is a string that is equal to its reverse; for example racecar. The left half of a string of length n is the substring of the rst n/2 characters, truncating n/2 if n is odd. Thus the left half of abab is ab and the left half of abcab is ab. A string is called a recursive palindrome if either it is a single character or the empty string, or it is a simple palindrome and its left half is a recursive palindrome. Note that this denition is itself recursive. For example, the following are recursive palindromes: WOW WOWWOW WOWXWOW WOWXWOWWOWXWOW WOWXWOWZWOWXWOW Note that racecar is not a recursive palindrome, because its left half rac is not a recursive palindrome, since rac isnt even a simple palindrome. For this assignment you have to implement two functions: int isRecursivePalindrome(char *x) /* return 1 if the null-terminated string x is a recursive palindrome, 0 if not. long long countRecursivePalindromes(int n, char *alphabet) /* alphabet will be a null-terminated string. Return the number of recursive palindromes of length n, whose characters are chosen from the specified alphabet, provided that number will fit in a long long. If it will not fit, return -1. */ The required lename is RecursivePalindrome.c. You should, as usual, prepare a header le RecursivePalindrome.h and a le main.c for your own use, but submit only RecursivePalindrome.c */

Examples

Assume the alphabet is WOXZ. Then there is just one recursive palindrome of length 0 (the empty string), and just four of length 1, namely W , O, X , and Z . Since any string of length 1 is a recursive 1

palindrome, the recursive palindromes of length 2 are the strings with two identical characters, such as XX and OO. There are four of those. Now we come to length 3. Since any string of length 1 is a recursive palindrome, any string of length 3 whose rst and last characters agree is a recursive palindrome, such as WOW or XWX. There are four choices for the middle letter, and then there are four choices for the rst letter, and then the last letter is determined, so altogether there are 4 times 4 = 16 recursive palindromes of length 3. The number of length 4 palindromes will be the same as the number of length 2 palindromes, since the right half of an even-length recursive palindrome must be the reverse of the left half. So there are just 4 of them, namely XXXX, OOOO, WWWW, and ZZZZ. Now what about length 5? We can pick any middle letter, and then a recursive palindrome of length 2 for the left half, and then the right half is determined. So we get 4 times the number of recursive palindromes of length 2, which is 4 times 4 or 16. These are the strings XXXXX, XX0XX, XXWXX, XXZXX, OOXOO, OOWOO, etc. The number of recursive palindromes grows rapidly with n, so it will certainly exceed the capacity of a long long for some n; the exact point at which this happens depends on the length of the alphabet as well as on n. It is an important part of the assignment to detect when this occurs, or is about to occur, and correctly return -1.

Programming Hints

1. Start by writing a main() program to test your two functions. You should do this before writing any other code (except, of course, skeleton code for the two required functions that does nothing, but does at least compile, and your header le.) For starters, prepare an array of strings containing examples and non-examples of recursive palindromes, and a corresponding array of correct answers to test isRecursivePalindrome; and after that, have your main print out the rst few values of countRecursivePalindromes for the alphabet mentioned in the examples. 2. Then write isRecursivePalindrome. This function should be implemented recursively, directly following the denition given above. 3. Use the main you wrote in step 1 to test and debug isRecursivePalindrome. Dont go on until you are satised that isRecursivePalindrome is working perfectly on valid inputs. 4. Write a version of countRecursivePalindromes that does not attempt to check for overow, and test it on various examples. 5. Add code to countRecursivePalindromes that does test for overow. Do NOT assume that long longs have a particular size in bytes. Write this code so it will still work 40 years from now when (and if) all the C data types have larger capacities. To do this make use of sizeof(long long). Of course, I wont be able to test if you have done this correctly, but still, it is a thing you should aim for in all your code.

You might also like