Check if a string is a palindrome in the C language.

Here is the code in C language to determine if a string is a palindrome.

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

int main() {
    char str[100];
    int i, len, flag = 0;

    printf("输入一个字符串:");
    scanf("%s", str);

    len = strlen(str);

    for (i = 0; i < len / 2; i++) {
        if (str[i] != str[len - i - 1]) {
            flag = 1;
            break;
        }
    }

    if (flag == 0)
        printf("%s 是一个回文字符串\n", str);
    else
        printf("%s 不是一个回文字符串\n", str);

    return 0;
}

This code first reads a string input from the user, then uses a loop to compare the first half of the string with the second half. If any mismatch is found, the flag variable is set to 1 and the loop is exited. Finally, the code determines if the string is a palindrome based on the value of the flag, and outputs the corresponding result.

bannerAds