问题:判断不包含空格且长度不超过1000的字符串是否是回文字符串。所谓回文字符串就是顺着念、倒着念完全相同的字符串。
分析:对于字符串\(str\),设其长度为\(n\),那么\(str\)是回文字符串必须要满足:str[0] == str[n-1]
、str[1] == str[n-2]
……(str[i] == str[n-1-i]
)等条件。反之,如果找到任意一个不相等的字符对,那么肯定不是回文字符串。


#include<iostream>
#include<cstring>
using namespace std;
char str[1010];
int main()
{
cin>>str;
int n = strlen(str);
int find = 0; //标记是否找到不相等的字符对
for(int i=0;i<n;i++){ //循环条件可以修改为i<=n/2
if(str[i] != str[n-1-i]){
find = 1;
break;
}
}
if(find) cout<<"NO";
else cout<<"YES";
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
char str[1010];
int main()
{
cin>>str;
int n = strlen(str);
int find = 0; //标记是否找到不相等的字符对
for(int i=0;i<n;i++){ //循环条件可以修改为i<=n/2
if(str[i] != str[n-1-i]){
find = 1;
break;
}
}
if(find) cout<<"NO";
else cout<<"YES";
return 0;
}
#include<iostream> #include<cstring> using namespace std; char str[1010]; int main() { cin>>str; int n = strlen(str); int find = 0; //标记是否找到不相等的字符对 for(int i=0;i<n;i++){ //循环条件可以修改为i<=n/2 if(str[i] != str[n-1-i]){ find = 1; break; } } if(find) cout<<"NO"; else cout<<"YES"; return 0; }
使用C++的string来解决问题参考代码如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
size_t n = str.length();
int find = 0; //标记是否找到不相等的字符对
for(size_t i=0;i<n;i++){ //循环条件可以修改为i<=n/2
if(str[i] != str[n-1-i]){
find = 1;
break;
}
}
if(find) cout<<"NO";
else cout<<"YES";
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
size_t n = str.length();
int find = 0; //标记是否找到不相等的字符对
for(size_t i=0;i<n;i++){ //循环条件可以修改为i<=n/2
if(str[i] != str[n-1-i]){
find = 1;
break;
}
}
if(find) cout<<"NO";
else cout<<"YES";
return 0;
}
#include<iostream> #include<string> using namespace std; int main() { string str; cin>>str; size_t n = str.length(); int find = 0; //标记是否找到不相等的字符对 for(size_t i=0;i<n;i++){ //循环条件可以修改为i<=n/2 if(str[i] != str[n-1-i]){ find = 1; break; } } if(find) cout<<"NO"; else cout<<"YES"; return 0; }
还可以用两个变量 \(i,j\) 来表示要判断相等字符对的下标:



#include<iostream>
#include<cstring>
using namespace std;
char str[1010];
int main()
{
cin>>str;
for(int i=0,j=strlen(str)-1;i<j;i++,j--){
if(str[i] != str[j]){
cout<<"NO";
return 0; //提前结束程序
}
}
cout<<"YES";
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
char str[1010];
int main()
{
cin>>str;
for(int i=0,j=strlen(str)-1;i<j;i++,j--){
if(str[i] != str[j]){
cout<<"NO";
return 0; //提前结束程序
}
}
cout<<"YES";
return 0;
}
#include<iostream> #include<cstring> using namespace std; char str[1010]; int main() { cin>>str; for(int i=0,j=strlen(str)-1;i<j;i++,j--){ if(str[i] != str[j]){ cout<<"NO"; return 0; //提前结束程序 } } cout<<"YES"; return 0; }
使用C++的string来解决问题参考代码如下:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
for(size_t i=0,j=str.length()-1;i<j;i++,j--){
if(str[i] != str[j]){
cout<<"NO";
return 0; //提前结束程序
}
}
cout<<"YES";
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
for(size_t i=0,j=str.length()-1;i<j;i++,j--){
if(str[i] != str[j]){
cout<<"NO";
return 0; //提前结束程序
}
}
cout<<"YES";
return 0;
}
#include<iostream> #include<string> using namespace std; int main() { string str; cin>>str; for(size_t i=0,j=str.length()-1;i<j;i++,j--){ if(str[i] != str[j]){ cout<<"NO"; return 0; //提前结束程序 } } cout<<"YES"; return 0; }