Write a function isPalindrom() that takes as input a C string and its size, and recursively tests whether it is a palindrome (a word or phrase which remains the same if reversed. e.g. radar, kayak etc). The function returns the Boolean data type which is either true or false. The required header “stdbool.h” has already been included in tasks.h.
The function has the following prototype:
bool isPalindrom(char * ptr_array, int size);
The recursive definition of a palindrome is:
• The string is a palindrome if it has only one character or is an empty string.
• The string is a palindrome if the first and the last characters are the same and the characters in between form a palindrome.
#include <stdio.h>
int main()
{
char text[100];
int begin, middle, end, length = 0;
gets(text);
while (text[length] != '\0')
length++;
end = length - 1;
middle = length/2;
for (begin = 0; begin < middle; begin++)
{
if (text[begin] != text[end])
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if (begin == middle)
printf("Palindrome.\n");
return 0;
}
Comments
Leave a comment