如何比较字符串中的"若"声明? [英] How to compare strings in an "if" statement?
本文介绍了如何比较字符串中的"若"声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要测试,看看类型字符的变量可以用正常的字符串,如奶酪像比较比较:
I want to test and see if a variable of type "char" can compare with a regular string like "cheese" for a comparison like:
#include <stdio.h>
int main()
{
char favoriteDairyProduct[30];
scanf("%s",favoriteDairyProduct);
if(favoriteDairyProduct == "cheese")
{
printf("You like cheese too!");
}
else
{
printf("I like cheese more.");
}
return 0;
}
(我真正想要做的是比这更长的时间,但是这是我卡上的主要部分。)
因此,如何将一个是C比较两个字符串?
(What I actually want to do is much longer than this but this is the main part I'm stuck on.) So how would one compare two strings in C?
推荐答案
您正在寻找的功能 STRCMP
或 STRNCMP
从文件string.h
。
You're looking for the function strcmp
, or strncmp
from string.h
.
由于字符串数组只是,你需要每个字符比较,所以这个功能会为你做的:
Since strings are just arrays, you need to compare each character, so this function will do that for you:
if (strcmp(favoriteDairyProduct, "cheese") == 0)
{
printf("You like cheese too!");
}
else
{
printf("I like cheese more.");
}
延伸阅读: STRCMP在cplusplus.com
这篇关于如何比较字符串中的&QUOT;若&QUOT;声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文