PTA 判断回文字符串 (字符串 函数)

判断回文字符串 (20 分)

本题要求编写函数,判断给定的一串字符是否为“回文”。所谓“回文”是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是回文。

函数接口定义:

bool palindrome( char *s );

函数palindrome判断输入字符串char *s是否为回文。若是则返回true,否则返回false。

裁判测试程序样例:

#include <stdio.h>
#include <string.h>

#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;
}

/* 你的代码将被嵌在这里 */

输入样例1:

thisistrueurtsisiht

输出样例1:

Yes
thisistrueurtsisiht

输入样例2:

thisisnottrue

输出样例2:

No
thisisnottrue

代码

bool palindrome(char *s)
{
	int i, len,count=0;
	len = strlen(s);
	if (len == 1)
	{
		return true;
	}
	int m = len - 1;
	for (i = 0; i < len / 2; i++)
	{
		if (*(s)==*(s+m))
		{
			count++;
			*s++;
			m -= 2;
		}
		else
		{
			return false;
		}
	}
	if (count == len / 2)
	{
		return true;
	}
}
  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值