首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

,string字符串内部比较

2014-01-06 
求助,string字符串内部比较本帖最后由 u012941710 于 2014-01-02 16:26:11 编辑#includeiostream#includ

求助,string字符串内部比较
本帖最后由 u012941710 于 2014-01-02 16:26:11 编辑

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
bool isnot(const string&s);
int main()
{
cout << "Enter  a string: ";
string str;
getline(cin, str);
if (isnot(str))
cout << "Yes,it is ";
else
cout << "No,it is not";
return 0;
}
bool isnot(const string&s)
{
string s1(s);
int x = 0;
for (int i = 0; i < s.size(); i++)
{
if (isalpha(s[i]))
{
s1[x] = tolower(s[i]);
x++;
}
}//我用cout<<s1看了下,貌似到这步都是正确的,但是之后得出的结论就没对
for (int i = 0; i <=x/2; i++)
{
if (s1[i] != s1[x-i])
return false;
}
return true;
}
题目的要求是,输入一个字符串,然后忽略空格标点大小写,然后要求判断是否是顺读和逆读都一样。
[解决办法]

http://www.cplusplus.com/reference/string/string/compare/???
[解决办法]
引用:
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
bool isnot(const string&s);
int main()
{
cout << "Enter  a string: ";
string str;
getline(cin, str);
if (isnot(str))
cout << "Yes,it is ";
else
cout << "No,it is not";
return 0;
}
bool isnot(const string&s)
{
string s1(s);
int x = 0;
for (int i = 0; i < s.size(); i++)
{
if (isalpha(s[i]))
{
s1[x] = tolower(s[i]);
x++;
}
}//我用cout<<s1看了下,貌似到这步都是正确的,但是之后得出的结论就没对
for (int i = 0; i <=x/2; i++)
{
if (s1[i] != s1[x-i-1])return false;
}
return true;
}
题目的要求是,输入一个字符串,然后忽略空格标点大小写,然后要求判断是否是顺读和逆读都一样。



附上

热点排行