PAT C入门题目-6-3 判断回文字符串 (20 分)

6-3 判断回文字符串 (20 分)

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

输入样例1:

thisistrueurtsisiht

输出样例1:

Yes
thisistrueurtsisiht

输入样例2:

thisisnottrue

输出样例2:

No
thisisnottrue
#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 len, i = 0;

    len = strlen(s);
    while (s[i] == s[len - 1 - i] && i <= len / 2) {
        i++;
    }
    if (i > len / 2) {
        return true;
    }
    else {
        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37503890/article/details/86600345