c语言如何判断字符串是否相等
时间: 2023-10-18 15:07:40 浏览: 64
在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函数。该函数的原型为int strcmp(const char *s1, const char *s2),其中s1和s2分别为要比较的两个字符串。如果两个字符串相等,则返回0,否则返回非0值。可以使用该函数来实现判断字符串相等的功能。另外,也可以使用循环遍历两个字符串的每个字符进行比较,如果有不同的字符则返回1,否则返回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
```