1000: 回文串问题
时间限制: 1 Sec 内存限制: 128 MB提交: 499 解决: 152
[提交] [状态] [讨论版] [命题人:外部导入]
题目描述
“回文串”是一个正读和反读都一样的字符串,字符串由数字和小写字母组成,比如“level”或者“abcdcba”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
输入
输入包含多个测试实例,每一行对应一个字符串,串长最多100字母。
输出
对每个字符串,输出它是第几个,如第一个输出为"case1:";如果一个字符串是回文串,则输出"yes",否则输出"no",在yes/no之前用一个空格。
样例输入
level
abcde
noon
haha
样例输出
case1: yes
case2: no
case3: yes
case4: no
提示
请使用scanf进行输入。
1 #include<iostream>
2 #include<string>
3 #include<string.h>
4
5 using namespace std;
6
7 char data[100];
8 int main(){
9 string str;
10 int index=0;
11 while(cin>>str){
12 index++;
13 bool flag=true;
14 int i=0,k=0;
15 int len=str.length();
16 for(i=0;i<len/2;i++){//levvel
17 data[i]=str.at(i);
18 }
19 k=i-1;
20 if(len%2!=0){
21 i++;
22 }
23 for(;i<len;i++){
24 if(str.at(i)!=data[k]){
25 flag=false;
26 break;
27 }
28 k--;
29 }
30 if(flag==false){
31 cout<<"case"<<index<<": no"<<endl;
32 }
33 else{
34 cout<<"case"<<index<<": yes"<<endl;
35 }
36 }
37 return 0;
38 }