Commit | Line | Data |
---|---|---|
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 | ||
c31b68a4 JB |
8 | bool palindrome_iter(char *mot, int longeur) |
9 | { | |
4aea40a1 JB |
10 | int i = 0; |
11 | int j = longeur - 1; | |
12 | ||
13 | while (i <= j) { | |
c31b68a4 JB |
14 | if (mot[i] != mot[j]) { |
15 | return false; | |
16 | } | |
17 | i++; | |
18 | j--; | |
4aea40a1 JB |
19 | } |
20 | return true; | |
21 | } | |
22 | ||
c31b68a4 JB |
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); | |
4aea40a1 JB |
32 | } |
33 | ||
c31b68a4 JB |
34 | bool palindrome_rec(char *mot, int longueur) |
35 | { | |
36 | return palindrome_rec_aux(mot, 0, longueur - 1); | |
4aea40a1 JB |
37 | } |
38 | ||
39 | ||
c31b68a4 JB |
40 | int main() |
41 | { | |
4aea40a1 JB |
42 | char mot[LEN_MAX]; |
43 | ||
44 | printf("Saisir un mot\n"); | |
45 | scanf("%s", mot); | |
46 | if (palindrome_rec(mot, strlen(mot))) | |
c31b68a4 | 47 | printf("%s est un palindrome\n", mot); |
4aea40a1 | 48 | else |
c31b68a4 | 49 | printf("%s n'est pas un palindrome\n", mot); |
4aea40a1 JB |
50 | |
51 | return 0; | |
52 | } |