Let's create a function that checks if a string is a palindrome by comparing characters from the beginning and end, moving towards the center.
#include <stdio.h>
#include <string.h>
int isPalindrome(char *str) {
int start = 0;
int end = strlen(str) - 1;
while (start < end) {
if (str[start] != str[end]) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1; // Is a palindrome
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\\n")] = 0; // Remove newline character if present
if (isPalindrome(str))
printf("\"%s\" is a palindrome.", str);
else
printf("\"%s\" is not a palindrome.", str);
return 0;
}
This function uses a while loop to check characters from both ends of the string, providing an efficient way to determine palindromicity.