Answer: compare characters from both ends moving inward; if all pairs match, the string is a palindrome.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len, isPal = 1;
printf("Enter a string: ");
scanf("%99s", str);
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
isPal = 0;
break;
}
}
if (isPal)
printf("%s is a palindrome\n", str);
else
printf("%s is not a palindrome\n", str);
return 0;
}
Explanation: a palindrome reads the same forwards and backwards. The loop compares the first character with the last, the second with the second-last, and so on, for half the length. One mismatch is enough to say it is not a palindrome.
Sample: "madam" is a palindrome; "hello" is not. (The comparison is case-sensitive, so "Madam" would need converting to lower case first.)