#include < stdio.h> #include < stdlib.h> #include < string.h>
int est_palidrome(char *str) { bool result = false; //if the word contains no more than 1 character if(strlen(str) <= 1) result = true; //otherwise if the first character matches the latest else if(str[0] == str[strlen(str)-1]) { //le First and the last character are checked so they are eliminated char temp[strlen(str)-2]; //memcpy allows you to copy a part str in temp -2: the '\0' + the last character eliminated memcpy( temp, & str[1], strlen(str) - 2 ); // '\0' indicates the boundary temp[strlen(str) - 2] = '\0'; //recursive call result = est_palidrome(temp); } return result; }
int main() { char str[20]; printf("Enter a string.\n"); scanf("%s",str);
if(est_palidrome(str)) printf("It's a palindrome!\n"); else printf("It's not a palindrome! \n"); system("pause"); } |