- 题目描述:
“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
- 思路:
对字符串进行下标操作,依次检验对应字符是否相同。 - 细节:
字符串的第一个字符下标为0;对于每一个字符串长度是不确定的,使用string更为合适。
- 源代码:
#include<iostream> #include<string> using namespace std; int main() { int n; string a; while(cin>>n) { for(int i=0;i<n;i++) { cin>>a; int b=1; int x=a.size(); for(int j=0;j<x/2;j++) if(a[j]!=a[x-1-j]) b=0; if(b) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; }