字符串相等的判断(函数忽略大小写,标点符号及空白字符)
2014-09-12 14:08
477 查看
个人比较喜欢的一个题目:
编写一个函数,比较两个字符串的相等性,如果字符串相等,函数应该返True,不等则返回False。函数应该忽略大小写,标点符号及空白字符。用各种输入测试你的函数。(摘自Absolute C++ 编程题第9.9题)
分析:
1.应该写一个忽略大小写的函数,一般的作法是,无论大小写都转换为小写(个人的习惯吧),过程大概是遍历字符串的每一个字符,然后对其进行tolower()操作,tolower和toupper函数的注意事情在之前有介绍过了(当然此处和之前没有什么关系)。
2.忽略标点字符和空格,目前还没学习正则表达式,而且也不知道C++有没有(我所知道的python有),但是吃出我的作法是:将标点符号和空格放在一个字符串(punct)里面,然后在遍历字符串的时候,对每个字符,判断是否出现在punct中即(punct.find()) ,如果不在其中说明则不是标点或空格,故将此字符push_back();
3.最后,把上述的两个函数整合到题目要求的函数中,这个比较简单,将直接在程序中体现。
源程序:
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
string removePunct(const string &s,const string &punct);
string makelower(const string &s);
bool Isequal(const string &s1,const string &s2);
int main()
{
string s1;
string s2;
getline(cin,s1);
getline(cin,s2);
cout<< Isequal(s1,s2);
return 0;
}
string removePunct(const string &s,const string &punct)
{
string noPunct;
int slength=s.length();
int i;
int punctlength=punct.length();
for(i=0;i<slength;i++)
{
string achar=s.substr(i,1);
int location=punct.find(achar,0);//在punct里面找,如果是,则location返回正常值
//若超出或者《0 ,则说明不在nopunct中,故保存下来
if(location<0||location>=punctlength)
{
noPunct=noPunct+achar;//之所以achar选择为string的原因是在这可以做更安全的连接
}
}
return noPunct;
}
string makelower(const string &s)
{
int i;
string temp(s);
for( i=0;i<s.length();i++)
{
temp[i]=tolower(s[i]);
}
return temp;
}
bool Isequal(const string &s1,const string &s2)
{
string punct(",.;?!'\" ");
string str1(s1);
string str2(s2);
str1=makelower(str1);
str2=makelower(str2);
string lowerstr1=removePunct(str1,punct);
string lowerstr2=removePunct(str2,punct);
return lowerstr1==lowerstr2;
}
测试用例:(欢迎补充)
编写一个函数,比较两个字符串的相等性,如果字符串相等,函数应该返True,不等则返回False。函数应该忽略大小写,标点符号及空白字符。用各种输入测试你的函数。(摘自Absolute C++ 编程题第9.9题)
分析:
1.应该写一个忽略大小写的函数,一般的作法是,无论大小写都转换为小写(个人的习惯吧),过程大概是遍历字符串的每一个字符,然后对其进行tolower()操作,tolower和toupper函数的注意事情在之前有介绍过了(当然此处和之前没有什么关系)。
2.忽略标点字符和空格,目前还没学习正则表达式,而且也不知道C++有没有(我所知道的python有),但是吃出我的作法是:将标点符号和空格放在一个字符串(punct)里面,然后在遍历字符串的时候,对每个字符,判断是否出现在punct中即(punct.find()) ,如果不在其中说明则不是标点或空格,故将此字符push_back();
3.最后,把上述的两个函数整合到题目要求的函数中,这个比较简单,将直接在程序中体现。
源程序:
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
string removePunct(const string &s,const string &punct);
string makelower(const string &s);
bool Isequal(const string &s1,const string &s2);
int main()
{
string s1;
string s2;
getline(cin,s1);
getline(cin,s2);
cout<< Isequal(s1,s2);
return 0;
}
string removePunct(const string &s,const string &punct)
{
string noPunct;
int slength=s.length();
int i;
int punctlength=punct.length();
for(i=0;i<slength;i++)
{
string achar=s.substr(i,1);
int location=punct.find(achar,0);//在punct里面找,如果是,则location返回正常值
//若超出或者《0 ,则说明不在nopunct中,故保存下来
if(location<0||location>=punctlength)
{
noPunct=noPunct+achar;//之所以achar选择为string的原因是在这可以做更安全的连接
}
}
return noPunct;
}
string makelower(const string &s)
{
int i;
string temp(s);
for( i=0;i<s.length();i++)
{
temp[i]=tolower(s[i]);
}
return temp;
}
bool Isequal(const string &s1,const string &s2)
{
string punct(",.;?!'\" ");
string str1(s1);
string str2(s2);
str1=makelower(str1);
str2=makelower(str2);
string lowerstr1=removePunct(str1,punct);
string lowerstr2=removePunct(str2,punct);
return lowerstr1==lowerstr2;
}
测试用例:(欢迎补充)
相关文章推荐
- C语言strcasecmp()函数:判断字符串是否相等(忽略大小写)
- 忽略2个字符串的大小写,再判断他们是否相等的
- 【leetcode】对撞指针应用之回文字符串判断(忽略大小写,以及出数字外其他字符)
- 判断连个字符串是否相等忽略其字符顺序
- 判断两个由大小写字母和空格组成的字符串在忽略大小写和压缩掉空格后是否相等?--POJ--字符串处理
- ios忽略2个字符串的大小写,再判断他们是否相等的
- php教程判断字符串是否大小写字母函数
- c++ 忽略大小写字符串比较函数
- 判断字符串中的字符大小写和其他字符
- C#忽略大小写的字符串替换(Replace)函数
- 使用tolower()和toupper()函数转换字符串中字符的大小写
- 判断特殊字符函数(特殊字符串,判断的字符串)
- rtrim() 函数 从字符串的末端开始删除空白字符!
- shell中判断变量是否为整型,字符,字符串等的函数
- iPhone中基础字符判断函数收集(如判断大小写、数字等)
- [C/C++]_[初级]_[标准库之几种算法库函数的运用,如字符串查找(字符个数查找)、分隔、删除、替换、排序、字符交换位置、大小写转换等等]
- 用MASM32来DIY判断字符串1是否为字符串2的子串的函数演示代码(源码+exe)(应用lstrcpyn()和lstrcmp(),区分大小写)
- 1.4 写一个函数判断两个字符串是否使用相同的字符构成。
- 高效的忽略大小写的字符串替换(Replace)函数(多种方法比较)
- js 判断两字符串是否相等, 不区分大小写