1 /* Reads a string of chars from stdin and check if it's 2 a palindrome. */ 3 4 #include <stdio.h> 5 #include <ctype.h> 6 7 #define MAX_LEN 100 8 9 int 10 main (void) 11 { 12 char str[MAX_LEN]; 13 int len, i, j; 14 15 if(!fgets(str, MAX_LEN, stdin)) 16 return 1; 17 18 /* Converts all the string to lowercase; */ 19 for(i = 0; str[i] != '\0'; i++) 20 if(isupper(str[i])) 21 str[i] = tolower(str[i]); 22 23 /* Erases all the blank spaces between words */ 24 for(i = 0; str[i]; i++) 25 if(str[i] == ' ') 26 for(size_t j = i; str[j]; j++) 27 str[j] = str[j + 1]; 28 29 /* Counts the length of the string; */ 30 for(i = 0; str[i] != '\n'; i++) 31 ; 32 33 /* Stored the lengh of the string for later, */ 34 /* and subtract one from i because of the last char; */ 35 len = i; 36 i = (i - 1); 37 38 /* This part compares one by one the chars from the */ 39 /* string, first with last, second with before last, */ 40 /* and so on, only comparing the next one if the previous */ 41 /* one was equal; */ 42 for(j = 0; (str[j] == str[i]) && str[j]; j++, i--) 43 ; 44 45 /* If the string or sentence its palindrome, then in the previous */ 46 /* block j would end with the value of the lengh, stored previously; */ 47 if(j == len) 48 printf("%s\n", "La cadena es capicua."); 49 50 return 0; 51 }