TP6: n-ary tree: add insert and destroy functions
[Algorithmic_C.git] / TP4 / palindrome.c
CommitLineData
4aea40a1
JB
1#include <stdlib.h>
2#include <stdio.h>
3#include <stdbool.h>
4#include <string.h>
5
6#define LEN_MAX 40
7
8bool palindrome_iter(char* mot, int longeur) {
9 int i = 0;
10 int j = longeur - 1;
11
12 while (i <= j) {
13 if (mot[i] != mot[j]) { return false; }
14 i++;
15 j--;
16 }
17 return true;
18}
19
20bool palindrome_rec_aux(char* mot, int i, int j) {
21 if (i >= j) { return true; }
22 if (mot[i] != mot[j]) { return false; }
23 return palindrome_rec_aux(mot, i+1, j-1);
24}
25
26bool palindrome_rec(char* mot, int longueur) {
27 return palindrome_rec_aux(mot, 0, longueur-1);
28}
29
30
31int main() {
32 char mot[LEN_MAX];
33
34 printf("Saisir un mot\n");
35 scanf("%s", mot);
36 if (palindrome_rec(mot, strlen(mot)))
37 printf("%s est un palindrome\n", mot);
38 else
39 printf("%s n'est pas un palindrome\n", mot);
40
41 return 0;
42}