C++中代码实现判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等。
时间: 2024-06-18 08:03:31 浏览: 16
在C++中,可以使用以下代码实现判断两个由大小写字母和空格组成的字符串在忽略大小写,且忽略空格后是否相等:
```c++
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
bool isEqual(string str1, string str2) {
// 去掉两个字符串中的空格
str1.erase(remove_if(str1.begin(), str1.end(), ::isspace), str1.end());
str2.erase(remove_if(str2.begin(), str2.end(), ::isspace), str2.end());
// 将两个字符串都转化为小写字母
transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
// 判断两个字符串是否相等
if (str1 == str2) {
return true;
}
else {
return false;
}
}
int main() {
string str1 = "Hello, World!";
string str2 = "hello world";
if (isEqual(str1, str2)) {
cout << "Two strings are equal." << endl;
}
else {
cout << "Two strings are not equal." << endl;
}
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)