简体   繁体   English

从 string.h 库中复制 strcmp() function

[英]Replicating the strcmp() function from string.h library

I am trying to replicate the strcmp() function from the string.h library and here is my code我正在尝试从 string.h 库中复制 strcmp() function,这是我的代码

/**
 * string_compare - this function compares two strings pointed
 * by s1 and s2. Is a replica of the strcmp from the string.h library
 * @s1: The first string to be compared
 * @s2: The second string to be compared
 *
 * Return: On success, it returns:
 * 0 if s1 is equal to s2
 * negative value if s1 is less that s2
 * positive value if s1 is greater than s2
 */

int string_compare(char *s1, char *s2)
{
        int sum = 0, i;

        for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++)
                sum += (s1[i] - s2[i]);
        for ( ; s1[i] != '\0'; i++)
                sum += (s1[i] - 0);
        for ( ; s2[i] != '\0'; i++)
                sum += (0 - s2[i]);

  

        return (sum);
}

I tried my function using this sample code:我使用以下示例代码尝试了我的 function:

#include <stdio.h>

int main(void)
{
    char s1[] = "Hello";
    char s2[] = "World!";

    printf("%d\n", string_compare(s1, s2));
    printf("%d\n", string_compare(s2, s1));
    printf("%d\n", string_compare(s1, s1));
    return (0);
}

And I get the following output,我得到以下 output,

-53
-500
0

But I should be getting:但我应该得到:

-15
15
0

Why am I getting such a result??为什么我会得到这样的结果??

This approach is incorrect.这种方法是不正确的。

Let's assume that the first string is "B" and the second string is "AB" .假设第一个字符串是"B" ,第二个字符串是"AB"

It is evident that the first string is greater than the second string in the lexicographical order.很明显,按字典顺序,第一个字符串大于第二个字符串。

But the result will be negative due to this for loop但是由于这个 for 循环,结果将是负面的

    for ( ; s2[i] != '\0'; i++)
            sum += (0 - s2[i]);

though the function shall return a positive value.尽管 function 应返回正值。

Moreover there can occur an overflow for the variable sum of the type int .此外,类型int的变量sum可能会发生溢出。

Also the function should be declared at least like function 也应至少声明为

int string_compare( const char *s1, const char *s2);

because passed strings are not changed within the function.因为在 function 中传递的字符串不会改变。

The function can be defined the following way function 可以通过以下方式定义

int string_compare( const char *s1, const char *s2 )
{
    while ( *s1 && *s1 == *s2 )
    {
        ++s1;
        ++s2;
    }

    return ( unsigned char )*s1 - ( unsigned char )*s2;
} 

You are overcomplicating very simple function.您使非常简单的 function 过于复杂。

#define UC unsigned char

int mystrcmp(const char *s1, const char *s2)
{
    int result;
    while(!(result = (UC)*s1 - (UC)*s2++) && *s1++);
    return result;    
}

Strings in C are arrays of characters terminated with a null character ( \0 ). C 中的字符串是以 null 字符 ( \0 ) 结尾的字符的 arrays 字符。

When you pass a string to a function, you are passing a pointer to its first element.当您将字符串传递给 function 时,您传递的是指向其第一个元素的指针。 That pointer is passed by value .该指针是按值传递的。 You can modify that pointer within the function without any side-effects on the string it points to, as long as you don't dereference and assign to the address it points to.您可以在 function 中修改该指针,而不会对它指向的字符串产生任何副作用,只要您不取消引用并分配给它指向的地址。

That's why the pointer math from 0___________'s answer works.这就是0___________ 答案中的指针数学有效的原因。

 int mystrcmp1(const char *s1, const char *s2) { int result = 0; while(;(result = *s1 - *s2++) && *s1++); return result; }

*s1++ could be rewritten as *(s1++) to disambiguate. *s1++可以重写为*(s1++)以消除歧义。 s1++ returns the current pointer to the beginning of the first string, and then increments the pointer so it points to the next character. s1++将当前指针返回到第一个字符串的开头,然后将指针递增,使其指向下一个字符。 That pointer is then dereferenced to give us the character.然后取消引用该指针以给我们字符。 The same happens with the s2 pointer. s2指针也是如此。

Then we're comparing them by subtraction.然后我们通过减法比较它们。 If they're the same, we get 0 , which in C is false in a boolean context.如果它们相同,我们得到0 ,在 C 中,在 boolean 上下文中为假。 This result is assigned to result .此结果分配给result

We can now see that the loop continues while corresponding characters in the two strings are equal and while dereferencing s1 does not give us the null terminator.我们现在可以看到循环继续,而两个字符串中的对应字符相等而取消引用s1并没有给我们 null 终止符。

When the loop continues it means there was either a difference or we reached the end of the first string.当循环继续时,这意味着要么存在差异,要么我们到达了第一个字符串的末尾。

The difference will be stored in result , which the function returns.差值将存储在 function 返回的result中。

I'm not sure what approach you are using, but here's the code which gives the output you asked for (I've used arrays instead of pointers since I'm not good with pointers).我不确定您使用的是什么方法,但这是给出您要求的 output 的代码(我使用 arrays 而不是指针,因为我不擅长使用指针)。 The output is based on the difference in ASCII values of characters in the string. output是基于字符串中字符的ASCII值的差异。

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

int string_compare(char s1[100], char s2[100])
{
        int i,flag=0;
        
        if(strlen(s1)==strlen(s2)){
            for(i=0;i<strlen(s1);i++){
                if(s1[i]!=s2[i]){
                    flag=1;
                    break;
                }
            }
        }
        
        if(flag){
            return 0;
        }

        for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++){
                if(s1[i]>s2[i]){
                        //printf("%d",s1[i]-s2[i]);

                    return (s1[i]-s2[i]);
                }
                
                else if (s1[i]<s2[i]){
                        //printf("%d",s1[i]-s2[i]);

                    return (s1[i]-s2[i]);
                }
        }

  }

int main(void)
{
    char s1[] = "Hello";
    char s2[] = "World!";

    printf("%d\n", string_compare(s1, s2));
    printf("%d\n", string_compare(s2, s1));
    printf("%d\n", string_compare(s1, s1));
    return (0);
}

In your code the last 2 for loops in string_compare function are completely unnecessary.在您的代码中,string_compare function 中的最后 2 个 for 循环是完全没有必要的。 And you are not supposed to sum up the difference in ASCII values of each character of the strings, that's a completely wrong concept.而且您不应该总结字符串中每个字符的 ASCII 值的差异,这是一个完全错误的概念。

strcmp() function compares characters of both strings starting from 0th index, and if both characters are same, their ASCII values are also same so the difference in their ASCII values is 0, so it'll keep scanning for dissimilar characters. strcmp() function 从第 0 个索引开始比较两个字符串的字符,如果两个字符相同,则它们的 ASCII 值也相同,因此它们的 ASCII 值的差异为 0,因此它将继续扫描不同的字符。

If all characters are same, the difference 0 is returned.如果所有字符都相同,则返回差 0。 If at some point a dissimilar pair of characters is found, their difference in ASCII value is returned and it stops scanning the rest of the characters.如果在某个时候发现了不同的字符对,则返回它们在 ASCII 值中的差异,并停止扫描字符的 rest。 So if character of string 1 has greater ASCII value than that of the corresponding character in string 2, the difference is positive.因此,如果字符串 1 的字符的 ASCII 值大于字符串 2 中相应字符的 ASCII 值,则差异为正。 Otherwise it is negative.否则为负数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM