Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确比较C中的字符串?_C_String_Strcmp - Fatal编程技术网

如何正确比较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)
这是因为
=
==
将只比较这些字符串的基址。不是字符串本身的内容。

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

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

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

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

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

确定一些事项:并应替换为
fgets(输入,sizeof(输入),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;
打破
}
}
返回结果;
}

您不能像这样直接比较数组

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使用的教程

这在
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(“输入要检查的字符串
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;
}