Select Git revision
palindrome_refactored.c
Forked from
algorithmique / cours
Source project has a limited visibility.
palindrome_refactored.c 961 B
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
bool is_palindrome_1(char mot[]);
bool is_palindrome_2(char mot[]);
void main() {
printf("Entrez une chaîne de caractères: ");
char mot[30];
scanf("%s",mot);
printf("Le mot %s",mot);
if (is_palindrome_1(mot)) {
printf(" est un palindrome\n");
} else {
printf(" n'est pas un palindrome\n");
}
}
bool is_palindrome_1(char mot[]) {
int lg = strlen(mot);
bool palindrome = true;
int first_idx = 0, last_idx = lg-1;
while (first_idx < last_idx) {
if (mot[first_idx] != mot[last_idx]) {
palindrome = false;
break;
}
first_idx += 1;
last_idx -= 1;
}
return palindrome;
}
bool is_palindrome_2(char mot[]) {
int lg = strlen(mot);
char inverse[lg+1];
for (int i=0;i<lg;i++) {
inverse[i] = mot[lg-1+i];
}
inverse[lg] = '\0';
return (strcmp(inverse,mot) == 0);
}