Fall 2004
Due: Thursday, 4 Nov
Emphasis
on: Using
Recursion and Templates to Detect Palindromes
In this
assignment you will be asked to write a recursive function that tests whether
an array of characters is a palindrome.
You will then rewrite the function as a function template so that you
can determine if an array of any type (e.g. arrays of int, float, bool) are
palindromes.
A
palindrome in the context of english words is a word that reads the same
forward and backward. For instance, the
following are palindromes:
dad
level did madamimadam
A
couple of hints to get you started. An
array with 0 or 1 characters will be defined to be a palindrom (e.g. stopping
condition). If the first and last
characters of the array are equal, then it is a palindrome provided the string
in between the ends is also a palindrome.
Part A:
Write a
recursive function that accepts an array of characters (and possibly other
parameters) and determines if the character array is a palindrome as defined
above. The function should return a bool
result that is true if it is a palindrome and false otherwise.
Part B:
Modify
your function from part A to turn it into a function template. Thereby allow your palindrome testing
function to test not only if an array of characters is a palindrome, but also
arrays of ints, floats, etc.
Part C:
Write a
test driver program that will read in character strings from a file. The input file will have 1 word per
line. You should read in each line as a
character array then test if it is a palindrome. If the line is a palindrome write it back out
to standard output.
Requirements
1)
You
must create and use a function that solves the palindrome testing using true
recursion
2)
The
palindrome function must be made into a template that can test for palindromes
in any type of array.
3) Your test driver program should read in all lines in the input file and only output those lines that are palindromes.