forked from sushantvaidkar/HacktoberFest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive_palindrome.cpp
57 lines (50 loc) · 1.25 KB
/
recursive_palindrome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define bool short
#define true 1
#define false 0
#define TAMLINHA 1000
#define NUMENTRADA 1000
bool isFim(char *s){
return (strlen(s) >= 3 && s[0] == 'F' && s[1] == 'I' && s[2] == 'M');
}
/*
*Recursively checks whether the input String matches a palindrome
*@param s input string
*@param i int counter to cycle through input string
*@return resp bool <true> if input string is a palindrome <false> if it's not
*/
bool isPalindromo(char *s, int i){
bool resp;
int j = strlen(s) - i - 2;
if(i > ((int)strlen(s)/2)){
resp = true;
}else{
if(s[i] != s[j]){
resp = false;
}else{
resp = isPalindromo(s, i+1);
}//fim if
}//fim if
return resp;
}
int main(){
char entrada[NUMENTRADA][TAMLINHA];
int numEntrada = 0;
bool resp;
//the input ends with the string 'FIM'
do{
fgets(entrada[numEntrada], TAMLINHA, stdin);
}while(isFim(entrada[numEntrada++]) == false);
numEntrada--;
for(int i = 0; i < numEntrada; i++){
resp = isPalindromo(entrada[i], 0);
if(resp){
printf("YES\n");
}else{
printf("NO\n");
}//fim if
}//fim for i
return 0;
}