soluces/c/semaine-4/palindrome/main.c
2023-11-22 23:07:21 +01:00

45 lines
766 B
C

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isPalindrome(char *str);
int main(void) {
FILE *file = fopen("palindrome.txt", "r");
char *token, line[512];
if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
while (fgets(line, sizeof(line), file)) {
token = strtok(line, ",");
while (token != NULL) {
if (isPalindrome(token)) {
printf("palindrome found: %s\n", token);
}
token = strtok(NULL, ",");
}
}
fclose(file);
return EXIT_SUCCESS;
}
int isPalindrome(char *str) {
int i, length = strlen(str);
for (i = 0; i < length / 2; i++) {
if (tolower(str[i]) != tolower(str[length - 1 - i])) {
return 0;
}
}
return 1;
}