考研机试真题--回文字符串--华中科技大学

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Void_worker/article/details/82357209

题目:
题目描述
给出一个长度不超过1000的字符串,判断它是不是回文(顺读,逆读均相同)的。
输入描述:
输入包括一行字符串,其长度不超过1000。
输出描述:
可能有多组测试数据,对于每组数据,如果是回文字符串则输出”Yes!”,否则输出”No!”。
示例1
输入
hellolleh
helloworld
输出
Yes!
No!

链接:
https://www.nowcoder.com/practice/df00c27320b24278b9c25f6bb1e2f3b8?tpId=40&tqId=21559&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

代码:

#include<fstream>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int maxn = 1010;

int main(){
//    freopen("a.txt", "r", stdin);
    char s[maxn];
    int len, i, j;
    bool flag;
    while(scanf("%s", s) != EOF){
        len = strlen(s);
        flag = true;
        i = 0;
        j = len - 1;
        while(i < j){
            if(s[i] != s[j]){
                printf("No!\n");
                flag = false;
                break;
            }
            ++i;
            --j;
        }
        if(flag) printf("Yes!\n");
    }
}

猜你喜欢

转载自blog.csdn.net/Void_worker/article/details/82357209
今日推荐