#include
#include
#include
using namespace std;
bool R1(string s);
int main()
{
string s1;
while(getline(cin,s1)&&s1 !="q")
{
if(R1(s1))
cout<<s1<<"是回文.\n";
else
cout<<s1<<"不是回文.\n";
cout<<"请输入下一个字符串,输入q将退出\n";
}
return 0;
}
bool R1(string s)
{
string::iterator it;
for(it=s.begin();it!=s.end();++it)
{
*it=tolower(*it);
if(!isalnum(*it))
{s.erase(it);
}
}
cout<<s<<endl;
string val(s.rbegin(),s.rend());
if(val==s)
return true;
else
return false;
}
c++ string类的, 后的空字符无法消除
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答
- _cycycyc 2015-09-21 03:32关注
你在for(it=s.begin();it!=s.end();it++)循环内部调用it.erase(it)会使迭代器失效的。
it指向逗号时erase,则之后it指向了空格,此时循环结束it++,就到了空格的下一字符,因此空格就去不掉本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥50 树莓派安卓APK系统签名
- ¥15 maple软件,用solve求反函数出现rootof,怎么办?
- ¥65 汇编语言除法溢出问题
- ¥15 Visual Studio问题
- ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
- ¥20 求一个html代码,有偿
- ¥100 关于使用MATLAB中copularnd函数的问题
- ¥20 在虚拟机的pycharm上
- ¥15 jupyterthemes 设置完毕后没有效果
- ¥15 matlab图像高斯低通滤波