C语言判断回文字符串:算法实现及代码解析
#include <stdio.h> #include <string.h> #include <stdbool.h>
bool isPalindrome(char string[]) { int length = strlen(string); for (int i = 0; i < length / 2; i++) { if (string[i] != string[length - i - 1]) { return false; } } return true; }
int main() { char string[100]; printf('Enter a string:'); fgets(string, sizeof(string), stdin);
// 去除字符串末尾的换行符
string[strcspn(string, '\n')] = '\0';
if (isPalindrome(string)) {
printf('It is a palindrome\n');
} else {
printf('It is not a palindrome\n');
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/obk 著作权归作者所有。请勿转载和采集!