酷代码 AI
菜单

如果一串字符从左至右读和从右至左读相同,那么这串字符就是回文(Palindrome)。例如,ABCBA是回文,而ABCD则不是。本题要求编写一个判断字符串是否为回文的函数,并且通过函数参数获取字符串的中间字符(如果中间是两个字符,则获取靠左的那一个,例如字符串是ABCDEF,则中间字符是C)。题目保证输入的字符串长度在1至2000之间且不含空格、换行等分隔符。 函数接口定义: int isPalindrome(const char* str, char* pch); 其中 str 和 pch 都是用户传入的参数。 str 是字符串的首地址; pch 是传入的要存放字符串中间字符的变量的地址。如果字符串是回文,函数须返1,否则返回0。 裁判测试程序样例: /* 测试程序仅为示例,实际的测试程序可能不同 */ #include <stdio.h> #include <string.h> #define MAXLENGTH 100 int isPalindrome(const char* str, char* pch); int main(){ char str[MAXLENGT

```c #include <string.h> int isPalindrome(const char* str, char* pch) { int len = strlen(str); // 获取中间字符 *pch = str[(len - 1) / 2]; int left = 0; int right = len - 1; // 判断是否为回文 while (left < right) { if (str[left] != str[right]) { return 0; } left++; right--; } return 1; } ``` [2025-12-10 23:12:19 | AI问答 | 136点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多