X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TP4%2Fpalindrome.c;fp=TP4%2Fpalindrome.c;h=a84c08f3c0ef0b50b0a6701d4ff0aaea627099a9;hb=4aea40a102357e642edaf4315a1f6ca2630b2e18;hp=0000000000000000000000000000000000000000;hpb=faffdc37debb7507e2b1b321f2a05061c2738afd;p=Algorithmic_C.git diff --git a/TP4/palindrome.c b/TP4/palindrome.c new file mode 100644 index 0000000..a84c08f --- /dev/null +++ b/TP4/palindrome.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +#define LEN_MAX 40 + +bool palindrome_iter(char* mot, int longeur) { + int i = 0; + int j = longeur - 1; + + while (i <= j) { + if (mot[i] != mot[j]) { return false; } + i++; + j--; + } + return true; +} + +bool palindrome_rec_aux(char* mot, int i, int j) { + if (i >= j) { return true; } + if (mot[i] != mot[j]) { return false; } + return palindrome_rec_aux(mot, i+1, j-1); +} + +bool palindrome_rec(char* mot, int longueur) { + return palindrome_rec_aux(mot, 0, longueur-1); +} + + +int main() { + char mot[LEN_MAX]; + + printf("Saisir un mot\n"); + scanf("%s", mot); + if (palindrome_rec(mot, strlen(mot))) + printf("%s est un palindrome\n", mot); + else + printf("%s n'est pas un palindrome\n", mot); + + return 0; +}