#include <iostream>
#include <string>
#include <cctype>
using namespace std;
/*
void resort(char s[])
{
char temp;
for (int i=0,j=strlen(s)-1;i!=j;i++,j--)//对j的初始化是关键
{
temp=s[i];s[i]=s[j];s[j]=temp;
}
}*/
void resort(string &s)
{
char temp;
for (int i=0,j=s.size()-1;i<j;i++,j--)//对j的初始化是关键
{
temp=s[i];s[i]=s[j];s[j]=temp;
}
cout<<endl;
}
void tup(string &str)//增加一个toupper()函数(其头文件为<cctype>)
{
for (int i=0;i<str.size();i++)
str[i]=toupper(str[i]);
}
int main()
{
cout<<"请输入要判断的字符串;"<<endl;
string s;
string s0=s;
getline(cin,s);
tup(s); //转化为大写
string s1=s;
resort(s);//翻转
string s2=s;
if(s1==s2)
cout<<s0<<"是回文"<<endl;
else
cout<<s0<<"不是回文"<<endl;
return 0;
}3-3 要求不区分大小写,如abcBA仍认为是回文
最新推荐文章于 2022-01-12 15:44:48 发布
本文介绍了一个简单的C++程序,用于判断输入的字符串是否为回文。通过将字符串转换为大写并反转比较来实现,涉及字符串操作、字符大小写转换等基本编程技巧。

380

被折叠的 条评论
为什么被折叠?



