/***************************************** * palindrome.c * * * * A program to check if a string is a * * palindrome. * *****************************************/ #include /* Function prototype */ int isPali(char []); int main() { /* Declare variables to store the palindrome */ char str1[100]; char str2[100]; /* Declare and initialize sentinel value */ char sent[] = "-999"; /* Loop variables */ int i, j; /* Boolean flag */ int flag = 0; /* Obtain input */ fgets(str1, sizeof(str1), stdin); str1[strlen(str1)-1] = '\0'; /* Begin sentinel loop */ while(strcmp(str1,sent) != 0) { /* Ignore all whitespace and special characters in the palindrome */ j = 0; for(i = 0; i < strlen(str1); i++) { if(str1[i] <= 'z' && str1[i] >= 'a') { str2[j] = str1[i]; j++; } else if (str1[i] <= 'Z' && str1[i] >= 'A') { str2[j] = str1[i] + 32; j++; } } str2[j] = '\0'; /* Use the function to check if the string is a palindrome */ if( isPali(str2) ) printf("This string is a palindrome\n"); else printf("This string is not a palindrome\n"); /* Read in the string again */ fgets(str1, sizeof(str1), stdin); str1[strlen(str1)-1] = '\0'; } return 0; } int isPali(char str[]) { int i, j; /* If we find mismatched characters return false */ for(i = 0, j = strlen(str)-1; i < j; i++, j--) if(str[j] != str[i]) return 0; /* If the loop finishes without returning zero then the string is a * palindrome, so return true. */ return 1; }