可以使用strcmp函数来比较两个字符串是否相等,并根据比较结果来进行赋值操作。
示例代码如下:
#include
#include
int main()
{
char str1[] = "hello";
char str2[] = "world";
//比较两个字符串是否相等
if(strcmp(str1, str2) == 0){
printf("str1和str2相等\n");
} else {
printf("str1和str2不相等\n");
}
//根据比较结果进行赋值
if(strcmp(str1, "hello") == 0){
printf("str1等于hello\n");
} else if(strcmp(str1, "world") == 0){
printf("str1等于world\n");
} else {
printf("str1不等于hello也不等于world\n");
}
return 0;
}
下一篇:比较字符串并删除字符串的一部分