c 判断字符串是否相等
时间: 2023-11-15 09:56:27 浏览: 84
要判断两个字符串是否相等,可以使用strcmp函数。该函数的原型为:
```c
int strcmp(const char *str1, const char *str2);
```
其中,str1和str2分别为要比较的两个字符串。如果两个字符串相等,返回值为0;如果str1小于str2,返回值为负数;如果str1大于str2,返回值为正数。
示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello";
char str2[] = "world";
char str3[] = "hello";
if (strcmp(str1, str2) == 0) {
printf("str1和str2相等\n");
} else {
printf("str1和str2不相等\n");
}
if (strcmp(str1, str3) == 0) {
printf("str1和str3相等\n");
} else {
printf("str1和str3不相等\n");
}
return 0;
}
```
输出结果:
```
str1和str2不相等
str1和str3相等
```
相关问题
c语言如何判断字符串是否相等
在C语言中,判断字符串是否相等可以使用预定义函数strcmp()。该函数需要两个字符串作为参数,返回值是整型,如果两个字符串相等,返回值为0,否则返回其他整数值。下面是一个例子代码:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "Hello";
if (strcmp(str1, str2) == 0)
printf("两个字符串相等\n");
else
printf("两个字符串不相等\n");
return 0;
}
```
输出:
```
两个字符串相等
```
判断字符串是否相等c语言
在C语言中,可以使用strcmp()函数来判断两个字符串是否相等。该函数需要两个字符串作为参数,返回值为0表示两个字符串相等。例如:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello";
char str2[] = "world";
if (strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal\n");
} else {
printf("str1 and str2 are not equal\n");
}
return 0;
}
```
输出结果为:
```
str1 and str2 are not equal
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)