本文介绍了我似乎无法得到正确的strcmp工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想很简单的东西;比较用户输入字符串为Hello,但STRCMP不想工作。我知道我缺少明显的东西,我认为这与我宣布我的字符串的方式做。所有帮助是极大的AP preciated。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;INT主要(无效)
{
字符命令[4555],比较[] =你好;
与fgets(命令的sizeof(命令),标准输入);
的printf(%S \\ n%S \\ n,命令,比较);
如果(STRCMP(命令,比较)== 0)
{
输出(字符串相等);
}其他{
输出(字符串不等于);
}
}
解决方案
与fgets
将留在缓冲区中的换行,然后空终止,同时命令就没有换行,只是被空终止。
I'm trying something very simple; comparing a user inputted string to "hello" but strcmp does not want to work. I know I'm missing something obvious and I think it has to do with the way I declared my string. All help is greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char command[4555], compare[] = "hello";
fgets (command, sizeof (command), stdin);
printf ("%s\n%s\n", command, compare);
if (strcmp (command, compare) == 0)
{
printf ("The strings are equal");
} else {
printf ("The strings are not equal");
}
}
解决方案
fgets
will leave the newline in the buffer and then null terminate while command will have no newline and just be null terminated.
这篇关于我似乎无法得到正确的strcmp工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!