题解 | #回文字符串#
回文字符串
https://www.nowcoder.com/practice/df00c27320b24278b9c25f6bb1e2f3b8
设置两个“指针”:i、j,分别指向字符串的首尾;
判断两个指针所指的字符是否相等,相等则继续向中间靠拢,否则退出循环,输出false
#include <iostream> using namespace std; int main(){ string str; while(cin >>str){ bool flag = true; int i,j; for (i = 0,j = str.size()-1;i<=j;i++,j--){ if (str[i] != str[j]) flag = false; } if (flag) cout<<"Yes!"<<endl; else cout<<"No!"<<endl; } }