C++判断两个字符串相等
时间: 2023-08-13 10:05:58 浏览: 100
在C++中,可以使用`==`运算符来判断两个字符串是否相等。例如:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "world";
if(str1 == str2) {
cout << "str1 is equal to str2" << endl;
} else {
cout << "str1 is not equal to str2" << endl;
}
return 0;
}
```
输出结果为:
```
str1 is not equal to str2
```
相关问题
C++怎么判断两个字符串相等
在C语言中,可以使用strcmp函数来判断两个字符串是否相等。strcmp函数的原型为:
```c
int strcmp(const char* string1, const char* string2);
```
该函数会比较两个字符串string1和string2,并返回一个整数值:
- 如果string1等于string2,返回0
- 如果string1小于string2,返回一个负数
- 如果string1大于string2,返回一个正数
因此,可以通过判断strcmp的返回值是否为0,来判断两个字符串是否相等。例如:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello";
char str2[] = "hello";
char str3[] = "world";
if (strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal\n");
} else {
printf("str1 and str2 are not equal\n");
}
if (strcmp(str1, str3) == 0) {
printf("str1 and str3 are equal\n");
} else {
printf("str1 and str3 are not equal\n");
}
return 0;
}
```
输出结果为:
```
str1 and str2 are equal
str1 and str3 are not equal
```
c++判断两个字符串是否相等
在C++中判断两个字符串是否相等可以使用字符串比较函数`strcmp()`,该函数接受两个字符串参数,如果两个字符串相等则返回0,否则返回非0值。示例代码如下:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[] = "Hello";
char str2[] = "world";
char str3[] = "Hello";
if (strcmp(str1, str2) == 0) {
cout << "str1 and str2 are equal" << endl;
} else {
cout << "str1 and str2 are not equal" << endl;
}
if (strcmp(str1, str3) == 0) {
cout << "str1 and str3 are equal" << endl;
} else {
cout << "str1 and str3 are not equal" << endl;
}
return 0;
}
```
输出结果为:
```
str1 and str2 are not equal
str1 and str3 are equal
```
另外,如果使用`std::string`类型存储字符串,则可以使用`==`运算符判断两个字符串是否相等,示例代码如下:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello";
string str2 = "world";
string str3 = "Hello";
if (str1 == str2) {
cout << "str1 and str2 are equal" << endl;
} else {
cout << "str1 and str2 are not equal" << endl;
}
if (str1 == str3) {
cout << "str1 and str3 are equal" << endl;
} else {
cout << "str1 and str3 are not equal" << endl;
}
return 0;
}
```
输出结果为:
```
str1 and str2 are not equal
str1 and str3 are equal
```
阅读全文
相关推荐
![](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)