I have a problem with my program, in this code, I need to check if a string is a palindrome with recursion but for some reason, if the string length is even and every other char but the middle are
similar then it should still work (like aaaaaabCaaaaaa
)
#include <stdio.h>
#include <string.h>
#define EMPTY_STRING -1
#define FALSE 0
#define TRUE !FALSE
#define MAX_STR_LEN 500
int isPalindrome(char str[], int start, int end);
int main(void)
{
char str[MAX_STR_LEN] = "some random words that will get over-drived like eggs, plane, chair and a young unicorn that jumps on the bad";
int end = 0;
int start = 0;
printf("please enter the string you want to check: ");
fgets(str, MAX_STR_LEN, stdin); //getting the string
str[strcspn(str, "\n")]; //removeing the \n from the end
end = strlen(str) - 2; //removing the null (And another char that i dont know how it got there)
if (end == EMPTY_STRING) //checking if its an empty string, if it is then printing yes
{
printf("palindrome");
getchar();
return 0;
}
if (isPalindrome(str, start, end)) //checking if the string is a polindrom
{
printf("palindrome");
}
else
{
printf("not palindrome");
}
getchar();
return 0;
}
/*
function gets a string and checks if its a polindrom
input: the string, the first char index. the last char index
return value: true if its a polindrom, false if its not
*/
int isPalindrome(char str[], int start, int end)
{
int isPalindromeTrue = FALSE;
if (str[start] == str[end]) //if the chars are matching , continue
{
isPalindrome(str, start + 1, end - 1); //callingn the function again
isPalindromeTrue = TRUE; //will be used after all itertions
}
return isPalindromeTrue;
}
You may use this code. Here I deleted this redundant line str[strcspn(str, "\n")];
and added a new if condition for the case of an "even" string:
#include <stdio.h>
#include <string.h>
#define EMPTY_STRING -1
#define FALSE 0
#define TRUE !FALSE
#define MAX_STR_LEN 500
int isPalindrome(char str[], int start, int end);
int main(void)
{
char str[MAX_STR_LEN] = "some random words that will get over-drived like eggs, plane, chair and a young unicorn that jumps on the bad";
int end = 0;
int start = 0;
printf("please enter the string you want to check: ");
fgets(str, MAX_STR_LEN, stdin); //getting the string
//str[strcspn(str, "\n")]; //removeing the \n from the end
end = strlen(str) - 2; //removing the null (And another char that i dont know how it got there)
if (end == EMPTY_STRING) //checking if its an empty string, if it is then printing yes
{
printf("Yes");
getchar();
return 0;
}
if (isPalindrome(str, start, end)) //checking if the string is a polindrom
{
printf("palindrome");
}
else
{
printf("not palindrome");
}
getchar();
return 0;
}
/*
function gets a string and checks if its a polindrom
input: the string, the first char index. the last char index
return value: true if its a polindrom, false if its not
*/
int isPalindrome(char str[], int start, int end)
{
int isPalindromeTrue = FALSE;
if (str[start] == str[end]) //if the chars are matching , continue
{
isPalindrome(str, start + 1, end - 1); //calling the function again
isPalindromeTrue = TRUE; //will be used after all itertions
}
if ((strlen(str) - 1) % 2 == 0) //checking if the string is even
{
if (str[(strlen(str) - 1) / 2 - 1] != str[(strlen(str) - 1) / 2])
{
isPalindromeTrue = FALSE; //will be used after all itertions
}
}
return isPalindromeTrue;
}