回文的概念:顺读和倒读是一样的
比如:12321
madam
上海自来水来自海上
1.法一
#include<stdio.h> #include<string.h> int main() { char s[100] ; gets(s); int start = 0, end = strlen(s) - 1; int flag = 1; while (start <= end && 1 == flag) { flag = (s[start] == s[end]); start++; end--; } if (1 == flag) printf("%s是回文",s); else printf("%s不是回文",s); return 0; }
2.法二
#include<stdio.h> #include<string.h> int main() { char s[100] ; gets(s); int start = 0, end = strlen(s) - 1; int flag = 1; while (start <= end && 1 == flag) { flag = (s[start++] == s[end--]); } 1 == flag ? printf("%s是回文", s) : printf("%s不是回文", s); return 0; }
3.法三
#include<stdio.h> #include<string.h> int main() { char s[100] ; gets(s); int start = 0, end = strlen(s) - 1; int flag = 1; while (start <= end && flag == (s[start++] == s[end--])); 1 == flag ? printf("%s是回文", s) : printf("%s不是回文", s); return 0; }