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