目录

2025-03-08-学习记录-CC-PTA-习题8-8-判断回文字符串

2025-03-08 学习记录–C/C++-PTA 习题8-8 判断回文字符串

合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。💪🏻

一、题目描述 ⭐️

https://i-blog.csdnimg.cn/direct/8929a3ff6ede4587961567bae476d161.png https://i-blog.csdnimg.cn/direct/9fd4b05c31a340adb6dc04d2567b4ae6.png https://i-blog.csdnimg.cn/direct/4602699c0ae948528b4ea40dc24a00cf.png

二、代码(C语言)⭐️

#include #include #define MAXN 20 typedef enum {false, true} bool; bool palindrome( char s ); int main() { char s[MAXN]; scanf("%s", s); if ( palindrome(s)==true ) printf(“Yes\n”); else printf(“No\n”); printf("%s\n", s); return 0; } / 你的代码将被嵌在这里 */ bool palindrome(char *s) { int len = strlen(s); // 计算字符串 s 的长度,存储在变量 len 中 // 使用双指针法: // i 从字符串开头向后移动,j 从字符串末尾向前移动 // 当 i < j 时,继续比较 for (int i = 0, j = len - 1; i < j; i++, j–) { // 比较 s[i] 和 s[j] 是否相等 if (s[i] != s[j]) { return false; // 如果发现不匹配的字符对,直接返回 false } } // 如果所有字符对都匹配,返回 true return true; } https://i-blog.csdnimg.cn/direct/173ae772efc847f8a77e55eabcdc7b38.png https://i-blog.csdnimg.cn/direct/cf91213f93fa445eb2bf855f485b8ec9.jpeg