首页 > 编程开发 > C类语言    日期:2024-06-09 / 浏览

回文的概念:顺读和倒读是一样的

比如: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;
}

总结 

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章