The recursive function is bolded, i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why. Can someone explain it to me?
Code:
#define MAX 100
#include <string.h>
#include <stdio.h>
int checkPalindrome(char string[MAX]);
int checkRecPalindrome(char string[MAX]);
int main(void){
char checkString[MAX], chr[5] = {"exit"};
do{
printf("input string to check if it is a palindrome, or input exit to end\n");
fflush(stdout);
gets(checkString);
if(strcmp(checkString, chr) != 0){
/*call copies and reverses string to copy and sends back result of strcmp*/
if(checkPalindrome(checkString) == 1)
printf("The word is a palindrome(iterative)\n");
else
printf("The word is not a palindrome(iterative)\n");
if (checkRecPalindrome(checkString))
printf("The word is a palindrome (recursive)\n");
else
printf("The word is not a palindrome(recursive)\n");
}
else
printf("Quitting...");
/*loop is continued until checkString matches chr with strcmp*/
}while(strcmp(checkString, chr) != 0);
return 0;
}
int checkPalindrome(char string[MAX]){ /*strrev could also be used for a shorter code*/
char revString[MAX];
int a, b = (strlen(string) - 1) ;
for(a = 0; a < strlen(string); ++a){
revString[b] = string[a];
--b;
}
revString[strlen(string)] = '\0';
if(strcmp(string, revString) == 0)
return 1;
else
return 0;
}
int checkRecPalindrome(char string[MAX]){
static int a = 0;
int strLgt = strlen(string) - a;
if(strLgt > a){
if(string[a] == string[strLgt - 1]){
a++;
return checkRecPalindrome(string);
}
else
return 0;
}
a = 0;
return 1;
}