Use C language to implement strncmp, strncat, memset, and memcmp
1. int strncmp (char * str1, char * str2, size_t num) usage: # include <string. h> function: Compare the first n characters of str1 and str2. return result: if the first n Bytes are completely equal, the returned value is 0. If str1 [n] And str2 [n] appear during the comparison of the First n Bytes, returns (str1 [n]-str2 [n]) # include <stdio. h> # include <assert. h> # include <stdlib. h> int strncmp (const char * str1, const char * str2, size_t num) {assert (str1); assert (str2 ); // In the next while function, // The first loop condition: -- num. If the first num character is compared, the loop is exited. // The second loop condition is: * str1 = * str2, if the two-character Ratio Otherwise, exit the loop while (-- num & * str1 = * str2) {if (* str1 = '\ 0') // * str1, if str1 points to the end of the string and exits the loop return 1; else str1 ++; // str1 pointer auto-increment 1, points to the next character str2 ++; // str2 pointer auto-increment 1, point to the next character} return * str1-* str2; // return comparison result} int main () {char * arr1 = "aaabbb", * arr2 = "aaaccc "; int m = 0; printf ("Enter the number to be compared:"); scanf ("% d", & m); int ret = strncmp (arr1, arr2, m ); if (ret = 1) {printf ("the two strings are the same! \ N ");} else {printf (" the difference between the two strings is % d \ n ", ret);} system (" pause "); return 0;} 2. char * strncat (char * s1, const char * s2.size _ t n); In the s2 string, only the first n characters are appended to the s1 string, the first character of the copied s2 string overwrites the null character at the end of the s1 string. The characters in the s2 string, including s2 [n], are not copied, and an empty character is appended to the end of the result. The return value is s1. # Include <stdio. h> # include <assert. h> # include <stdlib. h> char * strncat (char * dest, char const * src, size_t n) {assert (dest); assert (src); char * temp = dest; while (* dest) {dest ++;} while (n -- & * src) {* dest ++ = * src ++;} * dest = '\ 0'; return temp ;} int main () {char arr [50] = "I come from china !! "; Char * p =" me too !! "; Int m = 0; printf (" Enter the number of append: "); scanf (" % d ", & m); strncat (arr, p, m ); printf ("% s \ n", arr); system ("pause"); return 0;} 3. void * memset (void * s, int ch, size_t n); function explanation: Replace the first n Bytes (typedef unsigned int size_t) in s with ch and return s. Memset: fills in a given value in a memory block. It is the fastest way to perform the clearing operation on a large struct or array [1]. # Include <stdio. h> # include <stdlib. h> # include <assert. h> # include <string. h> void * my_memset (void * s, int ch, size_t n) {assert (s); void * temp = s; while (n --) {* (char *) s = (char) ch; s = (char *) s + 1;} return temp;} int main () {char arr [] = "abcdefg "; int m = 0; printf ("input content to be initialized:"); scanf ("% s", & m); my_memset (arr, m, strlen (arr )); puts (arr); system ("pause"); return 0;} 4.int memcmp (const void * buf1, const Void * buf2, unsigned int count); compare the first count bytes of buf1 and buf2 in the memory area. This function is compared by byte. # Include <stdio. h> # include <stdlib. h> int my_memcmp (const void * s1, const void * s2, size_t n) {char * arr1 = (char *) s1; char * arr2 = (char *) s2; while (-- n & * arr1 & * arr1 = * arr2) {arr1 ++; arr2 ++;} return * arr1-* arr2;} int main () {char * p = "abcdef"; char * q = "abcdef"; int r; r = my_memcmp (p, q, 3); if (r = 0) {printf ("two strings are equal! \ N ");} else if (r <0) {printf (" string p is less than string q! \ N ");} else printf (" string p is greater than string q! \ N "); system (" pause "); return 0 ;}