“The difference between strcmp() and strncmp()” is full of dry goods! ! !

Personal homepage: Xiaoguang Sikai C++
My introduction: A mechanical student who wants to learn computers by death, a c++ enthusiast, is exploring on the road of c++, and is willing to spend one hundred and twenty energy to fight with C++ to the end!
Difficulty level of the article: My article is not as beautiful as Diao Chan, not as strong as Lu Bu. In layman’s terms, Xiaoguang’s article is not as magical as those big cows. Summarize and sort out the content you have mastered, and finally write it out step by step. So whether you are a novice who is getting started, or a big guy who wants to review, it is very easy to understand. I hope you don’t think I am long-winded!
End: Finally, Xiaoguang’s articles may be a little different, just to make you happy when you are reading materials with a lot of text!

Article directory

  • 1. strcmp
    • Dry goods one:
    • A simulation implementation of strcmp()
    • Dry goods two:
    • Dry goods three:
  • strncmp()
    • Dry goods one:
    • Analog implementation of strncmp()

1.strcmp

int strcmp (const char * str1, const char * str2)

int strcmp indicates that an integer value is returned at the end

const char * str1 means to accept the first address of the string to be compared

const char * str2 means to accept the first address of the second string to be compared (note: because we only compare two spaces and do not modify them, adding const makes the logic more rigorous)

Return value:

1. If the first string is greater than the second string, a value greater than 0 will be returned

2. If the first string is less than the Dargo string, a value less than 0 will be returned

3. If the first string is equal to the second string, a 0 will be returned

How is it compared?

First realize the function of strcmp

#include <stdio.h>
#include <string.h>
int main()
{<!-- -->
char arr1[] = "abcdef";
char arr2[] = "abdef";
int ret = strcmp(arr1, arr2);
printf("%d\
", ret);
return 0;
}

Because the ASCII code value of c is smaller than the ASCII code value of d, arr1 is smaller than arr2, and finally returns a value less than 0

Dry goods one:

The strcmp() function first compares the first character, if the first character is equal, then compares the second character, and so on! Until a different character is encountered, if there is no different character, the two characters are equal and return 0

strcmp() simulation implementation

int my_strcmp(const char* str1, const char* str2)
{<!-- -->
assert(str1 & amp; & amp; str2);
while (*str1 == *str2)
{<!-- -->
if (*str2 == '\0')
{<!-- -->
return 0;
}
str1++;
str2++;
}
return *str1 - *str2;
}
int main()
{<!-- -->
char arr1[] = "abc";
char arr2[] = "abd";
int ret = my_strcmp(arr1, arr2);
printf("%d\
", ret);
return 0;
}

Dry goods two:

Please see the diagram:

Therefore, don’t just return 1 and -1 just because it is greater than and less than, so write the following code:>

If there is a judgment condition, it should be >0 or <0

Dry goods three:

Summarize the three previously learned functions starting with str

1.strcpy()

2.strcat()

3.strcmp()

==Knowledge points:== These three functions seem to have nothing to do with the length of the string, right? No matter which function you use, they all end with ‘\0’, and they will go directly to ‘\0’ , It doesn’t matter how long you are, whether it is enough to save it or not, and I am only responsible for executing it until ‘\0’, so you should pay more attention when using such a function! ! ! ,

For example: strcpy()

There is simply no room for 6 characters in the array arr2, but strcpy() will copy 6 characters into it. Although an error is reported, it is still copied. If you join the VS platform and do not report an error to a function, then Can make a big mistake! ! ! The other two functions also make sense!

Therefore, some length-limited functions are introduced into the library function

1. strncpy

2.strncat

3. strncmp

The first two have been introduced, the third strncmp() is introduced below

strncmp()

It is roughly the same as the strcmp() function, except that the parameter part has an extra number of characters to be compared size_t num

Let’s use this code first

int main()
{<!-- -->
char arr1[] = "abcdef";
char arr2[] = "defg";
int ret = strncmp(arr1, arr2, 3);
printf("%d\
", ret);
return 0;
}

Dry goods one:

The temper of this function is that if you ask me to compare a few characters, I will compare a few characters for you, and I will never compare more! ! ! It is very obedient, and other details are the same as the strcmp() function, please learn together!

strncmp() simulation implementation

int my_strncmp(const char* str1, const char* str2, size_t num)
{<!-- -->
assert(str1 & amp; & amp; str2);
while (num)
{<!-- -->
if (*str1 == *str2)
{<!-- -->
str1++;
str2++;
num--;
}
else
return *str1 - *str2;
}
return 0;

}
int main()
{<!-- -->
char arr1[] = "abcdef";
char arr2[] = "abcdf";
int ret = my_strncmp(arr1, arr2, 5);
printf("%d\
", ret);
return 0;
}