Determine palindrome characters in C language.
Here is an example in C language to check if a string is a palindrome.
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[]) {
int len = strlen(str);
int i, j;
for (i = 0, j = len - 1; i < j; i++, j--) {
if (str[i] != str[j]) {
return 0;
}
}
return 1;
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
if (isPalindrome(str)) {
printf("%s是回文字符串。\n", str);
} else {
printf("%s不是回文字符串。\n", str);
}
return 0;
}
In this example, the isPalindrome function is used to determine if a given string is a palindrome. It does so by using two pointers to traverse from the ends of the string towards the middle, comparing the characters at corresponding positions. If no unequal characters are found after traversing the entire string, it means the string is a palindrome and returns 1; otherwise, it returns 0.
In the main function, the user input a string is obtained first and then the isPalindrome function is called to determine whether it is a palindrome string, and corresponding information is output based on the result of the determination.