如何正确比较C中的字符串?

如何正确比较C中的字符串?,c,string,strcmp,C,String,Strcmp,我试图得到一个程序,让用户输入一个单词或字符,存储它,然后打印它,直到用户再次键入它,退出程序。我的代码如下所示: #include <stdio.h> int main() { char input[40]; char check[40]; int i=0; printf("Hello!\nPlease enter a word or character:\n"); gets(input); /* obsolete

我试图得到一个程序,让用户输入一个单词或字符,存储它,然后打印它,直到用户再次键入它,退出程序。我的代码如下所示:

#include <stdio.h>

int main()
{
    char input[40];
    char check[40];
    int i=0;
    printf("Hello!\nPlease enter a word or character:\n");
    gets(input);   /* obsolete function: do not use!! */
    printf("I will now repeat this until you type it back to me.\n");

    while (check != input)
    {
        printf("%s\n", input);
        gets(check);   /* obsolete function: do not use!! */
    }

    printf("Good bye!");
    

    return 0;
}
#包括
int main()
{
字符输入[40];
字符检查[40];
int i=0;
printf(“你好!\n请输入一个单词或字符:\n”);
获取(输入);/*已过时的函数:不要使用*/
printf(“我现在将重复此操作,直到您将其键入给我为止。\n”);
while(检查!=输入)
{
printf(“%s\n”,输入);
获取(检查);/*已过时的函数:不要使用*/
}
printf(“再见!”);
返回0;
}
问题是,我一直在打印输入字符串,即使用户的输入(检查)与原始输入(输入)匹配。我是否对这两个字符串进行了错误比较?

您不能(有效地)使用
比较字符串=
==
,您需要使用
strcmp

while (strcmp(check,input) != 0)
这是因为
=
==
将只比较这些字符串的基址。不是字符串本身的内容。

确定一些事情:并且应该用
fgets(input,sizeof(input),stdin)
替换,这样就不会出现缓冲区溢出

接下来,要比较字符串,必须使用strcmp,其中返回值0表示两个字符串匹配。使用相等运算符(即,
!=
)比较两个字符串的地址,而不是其中的单个
字符


还要注意的是,虽然在本例中它不会引起问题,
fgets
也将换行符
'\n'
存储在缓冲区中<代码>获取()。如果将来自
fgets()
的用户输入与字符串文本(如
“abc”
)进行比较,则将永远不会匹配(除非缓冲区太小,以致
“\n”
无法放入其中)。

不能像这样直接比较数组

array1==array2
你应该逐字符比较它们;为此,可以使用函数并返回布尔值(True:1,False:0)。然后可以在while循环的测试条件中使用它

试试这个:

#include <stdio.h>
int checker(char input[],char check[]);
int main()
{
    char input[40];
    char check[40];
    int i=0;
    printf("Hello!\nPlease enter a word or character:\n");
    scanf("%s",input);
    printf("I will now repeat this until you type it back to me.\n");
    scanf("%s",check);

    while (!checker(input,check))
    {
        printf("%s\n", input);
        scanf("%s",check);
    }

    printf("Good bye!");

    return 0;
}

int checker(char input[],char check[])
{
    int i,result=1;
    for(i=0; input[i]!='\0' || check[i]!='\0'; i++) {
        if(input[i] != check[i]) {
            result=0;
            break;
        }
    }
    return result;
}
#包括
int checker(字符输入[],字符检查[]);
int main()
{
字符输入[40];
字符检查[40];
int i=0;
printf(“你好!\n请输入一个单词或字符:\n”);
扫描频率(“%s”,输入);
printf(“我现在将重复此操作,直到您将其键入给我为止。\n”);
扫描频率(“%s”,勾选);
while(!checker(输入,检查))
{
printf(“%s\n”,输入);
扫描频率(“%s”,勾选);
}
printf(“再见!”);
返回0;
}
int checker(字符输入[],字符检查[])
{
int i,结果=1;
对于(i=0;输入[i]!='\0'| |检查[i]!='\0';i++){
如果(输入[i]!=检查[i]){
结果=0;
打破
}
}
返回结果;
}
使用

这在
string.h
库中,非常流行<如果字符串相等,则code>strcmp
返回0。有关
strcmp
返回内容的更好解释,请参阅

基本上,你必须做:

while (strcmp(check,input) != 0)


您可以查看一个关于strcmp的教程,每当您试图比较字符串时,请将它们与每个字符进行比较。为此,您可以使用名为strcmp(input1,input2)的内置字符串函数;您应该使用名为
#include

请尝试以下代码:

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h>  

int main() 
{ 
    char s[]="STACKOVERFLOW";
    char s1[200];
    printf("Enter the string to be checked\n");//enter the input string
    scanf("%s",s1);
    if(strcmp(s,s1)==0)//compare both the strings  
    {
        printf("Both the Strings match\n"); 
    } 
    else
    {
        printf("Entered String does not match\n");  
    } 
    system("pause");  
} 
#包括
#包括
#包括
int main()
{ 
字符s[]=“堆栈溢出”;
chars1[200];
printf(“输入要检查的字符串\n”);//输入输入字符串
scanf(“%s”,s1);
if(strcmp(s,s1)==0)//比较两个字符串
{
printf(“两个字符串都匹配\n”);
} 
其他的
{
printf(“输入的字符串不匹配\n”);
} 
系统(“暂停”);
} 
#包括
#包括
int main()
{
字符s1[50],s2[50];
printf(“输入字符串的字符:”);
获取(s1);
printf(“\n输入要重复的字符串的不同字符:\n”);
而(strcmp(s1,s2))
{
printf(“%s\n”,s1);
获取(s2);
}
返回0;
}
这是一个非常简单的解决方案,您可以根据需要获得输出

如何正确比较字符串


让我们深入了解为什么
检查!=输入不足

在C语言中,字符串是标准的库规范

字符串是以第一个空字符结尾并包括第一个空字符的连续字符序列。
C11§7.1.1 1

上面的输入不是字符串<代码>输入不可用

input
的内容可以变成字符串

在大多数情况下,当在表达式中使用数组时,它将转换为其第一个元素的地址

下面将
check
input
转换为第一个元素各自的地址,然后比较这些地址

check != input   // Compare addresses, not the contents of what addresses reference
要比较字符串,我们需要使用这些地址,然后查看它们指向的数据。
strcmp()
执行此任务。§7.23.4.2

int strcmp(常量字符*s1,常量字符*s2)

strcmp
函数将
s1
指向的字符串与
s2
指向的字符串进行比较

strcmp
函数返回大于、等于或小于零的整数, 因此,
s1
指向的字符串大于、等于或小于
s2
指向的字符串

代码不仅可以发现字符串是否具有相同的数据,还可以发现当它们不同时哪个字符串更大/更小

当字符串不同时,以下为真

strcmp(check, input) != 0


有关详细信息,请参见欢迎使用指针的概念。几代初学编程的人都发现了这个概念
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char s1[50],s2[50];
        printf("Enter the character of strings: ");
        gets(s1);
        printf("\nEnter different character of string to repeat: \n");
        while(strcmp(s1,s2))
        {
            printf("%s\n",s1);
            gets(s2);
        }
        return 0;
    }
char input[40];
char check[40];
strcpy(input, "Hello"); // input assigned somehow
strcpy(check, "Hello"); // check assigned somehow

// insufficient
while (check != input)

// good
while (strcmp(check, input) != 0)
// or 
while (strcmp(check, input))
check != input   // Compare addresses, not the contents of what addresses reference
strcmp(check, input) != 0
----------     ----------
| 0x4000 |     | 0x4004 |
|    1   |     |    7   |
----------     ----------
----------     ----------
| 0x4000 |     | 0x4004 |
|    1   |     |    1   |
----------     ----------
while (strcmp(check, input))
#include <stdio.h>
#include <string.h>

int main()
{
    char input[40];
    char check[40] = "end\n"; //dont forget to check for \n

    while ( strcmp(check, input) ) //strcmp returns 0 if equal
    {
        printf("Please enter a name: \n");
        fgets(input, sizeof(input), stdin);
        printf("My name is: %s\n", input);
    }

    printf("Good bye!");
    return 0;
}