c how to check a palindrome string

Checking for Palindrome in C

To check if a string is a palindrome in C, you can compare the characters from the beginning and the end of the string. If they match, continue comparing the next set of characters until the entire string is checked. Here's a simple example of how you can achieve this in C:

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    int i, j;
    int isPalindrome = 1;

    printf("Enter a string: ");
    scanf("%s", str);

    for (i = 0, j = strlen(str) - 1; i < j; i++, j--) {
        if (str[i] != str[j]) {
            isPalindrome = 0;
            break;
        }
    }

    if (isPalindrome) {
        printf("%s is a palindrome\n", str);
    } else {
        printf("%s is not a palindrome\n", str);
    }

    return 0;
}

This code prompts the user to enter a string, then checks if it's a palindrome and prints the result accordingly.

Note: This code assumes that the input string contains no spaces. If you want to handle strings with spaces, you can use fgets to read the input instead of scanf.

[6]