C language: Simulate Implementation of strncmp
The strncmp () function is used to simulate the implementation of the string comparison function. You can view the blog http://41040184.blog.51cto.com/41030184/1714512. Return Value: if the first n characters of str1 and str2 are the same, 0 is returned. If arr1 is greater than arr2, a value greater than 0 is returned. If arr1 is less than arr2, returns a value smaller than 0. [Parameter] str1 and str2 are the two strings to be compared, and n is the number of characters to be compared (which cannot be done by the strcmp function ). The len I set here is the length of arr1 .,
# Define _ CRT_SECURE_NO_WARNINGS 1 # include <stdio. h> # include <stdlib. h> # include <assert. h> int my_strcmp (const char * dest, const char * src, int len) {assert (dest); assert (src); while (len --) {if (* dest = * src) {dest ++; src ++ ;}} if (len = 0) return 0; else return (* dest-'\ 0')-(* src-' \ 0');} int main () {char arr1 [] = "abcdeff "; char arr2 [] = "abcd"; int len = sizeof (arr1)/sizeof (arr1 [0]); int ret = my_strcmp (arr1, arr2, len ); printf ("% d", ret); system ("pause"); return 0 ;}