Admittedly not the best code for checking whether a string is a palindrome but it's throwing up a side problem nevertheless. While it correctly identifies and reports palindromic strings, if the input string is not a palindrome then the code does not return the printf "The string is not a palindrome". Instead it does nothing beyond reporting the strlen. I've tried using both boolean (match) and counter (sum) as described in code below. Any suggestions would be much appreciated. Thanks
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int len, i;
int j = 0;
// int sum = 0;
int match;
/* Inputs string and gets string length */
printf("Enter the string: \n");
gets(str);
len = strlen(str);
printf("Length of the string is %d\n", len);
/* For checking whether an even string is palindromic */
if (len%2==0)
{
for (i = len - 1; i>= len/2; i--)
{match = 1;
if(str[j] != str[i])
{
//sum = sum + 1;
//j++;
match = 0;
break;
j++;
}
if (match == 1)
{
printf("The string is a palindrome\n");
}
else
printf("The string is not a palindrome\n");
}
}
/* For checking whether odd string is palindromic*/
else if (len%2 !=0)
{
for (i = len -1; i > len/2; i--)
{match = 1;
{
if(str[i] != str[j])
{
// sum = sum + 1;
// j++;
match = 0;
break;
j++;
}
if (match == 1)
{
printf("The string is a palindrome");
}
else
{
printf("The string is not a palindrome\n");
}
}
}
}
}