C语言 比较C中的两个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14232990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:58:28  来源:igfitidea点击:

Comparing two strings in C?

cstringcomparison

提问by user1955438

This code is not working as the comparison is not being done. Why?

由于未进行比较,此代码不起作用。为什么?

All names get past the if.

所有名称都超过了if.

printf("Enter Product: \n");
scanf("%s", &nameIt2);
printf("Enter Description: \n");
scanf("%s", &descriptionI);
printf("Enter Quantity: \n");
scanf("%d", &qtyI);
printf("Enter Order Quantity: \n");
scanf("%s", &ordqtyI);

while (fscanf(fp4, "%s %s %d %s\n", &namet2, &description2, &qty2, &ordqty2) != EOF){
    if(namet2 != nameIt2)
        fprintf(fpt2, "%s %s %d %s\n", &namet2, &description2, qty2, &ordqty2);
}

回答by Veger

To compare two C strings (char *), use strcmp(). The function returns 0when the strings are equal, so you would need to use this in your code:

要比较两个 C 字符串 ( char *),请使用strcmp(). 该函数0在字符串相等时返回,因此您需要在代码中使用它:

if (strcmp(namet2, nameIt2) != 0)

If you (wrongly) use

如果您(错误地)使用

if (namet2 != nameIt2)

you are comparing the pointers (addresses) of both strings, which are unequal when you have two different pointers (which is always the case in your situation).

您正在比较两个字符串的指针(地址),当您有两个不同的指针时,它们是不相等的(在您的情况下总是如此)。

回答by Aakash

For comparing 2 strings, either use the built in function strcmp()using header file string.h

要比较 2 个字符串,请使用strcmp()使用头文件的内置函数string.h

if(strcmp(a,b)==0)
    printf("Entered strings are equal");
else
    printf("Entered strings are not equal");

OR you can write your own function like this:

或者您可以像这样编写自己的函数:

int string_compare(char str1[], char str2[])
{
    int ctr=0;

    while(str1[ctr]==str2[ctr])
    {
        if(str1[ctr]=='
 if (strcmp(namet2, nameIt2) != 0)
'||str2[ctr]=='
if(strcmp(sr1,str2)) // this returns 0 if strings r equal 
    flag=0;
else flag=1; // then last check the variable flag value and print the message 

                         OR

char str1[20],str2[20];
printf("enter first str > ");
gets(str1);
printf("enter second str > ");
gets(str2);

for(int i=0;str1[i]!='
strcmp(namet2, nameIt2)
';i++) { if(str[i]==str2[i]) flag=0; else {flag=1; break;} } //check the value of flag if it is 0 then strings r equal simple :)
') break; ctr++; } if(str1[ctr]=='##代码##' && str2[ctr]=='##代码##') return 0; else return -1; }

回答by simonc

You are currently comparing the addresses of the two strings.

您当前正在比较两个字符串的地址。

Use strcmpto compare the values of two char arrays

使用strcmp比较两个字符数组的值

##代码##

回答by fge

You try and compare pointers here, not the contents of what is pointed to (ie, your characters).

您尝试在这里比较指针,而不是所指向的内容(即您的字符)。

You must use either memcmpor str{,n}cmpto compare the contents.

您必须使用memcmpstr{,n}cmp来比较内容。

回答by Rajesh

The name of the array indicates the starting address. Starting address of both namet2and nameIt2are different. So the equal to (==) operator checks whether the addresses are the same or not. For comparing two strings, a better way is to use strcmp(), or we can compare character by character using a loop.

数组名表示起始地址。同时启动的地址namet2nameIt2不同。所以等于 ( ==) 运算符检查地址是否相同。为了比较两个字符串,更好的方法是使用strcmp(),或者我们可以使用循环逐个字符地比较。

回答by Amjad

To answer the WHY in your question:

要回答您的问题中的原因:

Because the equality operator can only be applied to simple variable types, such as floats, ints, or chars, and notto more sophisticated types, such as structures or arrays. To determine if two strings are equal, you must explicitly compare the two character strings character by character.

因为相等运算符只能应用于简单的变量类型,例如floats、ints 或chars,而不能应用于更复杂的类型,例如结构或数组。要确定两个字符串是否相等,必须逐个字符地显式比较两个字符串。

回答by vaibhav sharma

##代码##

回答by jgr

You need to use strcmp:

您需要使用strcmp

##代码##