习题8-8 判断回文字符串 (20point(s)).c

本题要求编写函数,判断给定的一串字符是否为“回文”。所谓“回文”是指顺读和倒读都一样的字符串。如“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
//   Date:2020/4/6
//   Author:xiezhg5
#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;
}

/* 你的代码将被嵌在这里 */
bool palindrome( char *s )
{
	int n=strlen(s)-1;   //测量数组长度判断有多少个元素
	int i=0,j=n;
	while(i<j)
	{
		if(s[i]!=s[j])  //判断同一数组次序不同的吧元素是否相同
		{
			break;      //不相同立即终止循环 
		}
		i++;        //从两头往中间缩进
		j--;
	}
	//循环完成后 
	if(i>=j)       //相同的元素比较多
			return true;
		else
			return false;
}
发布了208 篇原创文章 · 获赞 182 · 访问量 8640

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105350392
今日推荐