1 //a simple implementation of the C-Language standard library function strcpy. 2 3 //return value: The address of the target string. 4 5 //The ansi-c99 standard is undefined for cases where an exception occurs, so the implementation determines the return value, which is usually null. 6 7 //parameter: des is the target string and source is the original string. 8 9 Char* STRCPY (Char* DES,Const Char*source)Ten { One Char* r=des; A -ASSERT ((des! = NULL) && (Source! =NULL)); - the while((*des++ = *source++)! =' /'); - - returnR; - } + - //While ((*des++=*source++)); Explanation: The assignment expression returns the left operand, so the loop stops after the assignment of NULL.
1 //a simple implementation of C-language standard library function strcmp2 3 //Returns a negative number when worth s1<s2 is returned, the return value = 0 when s1=s2, and the return positive when S1>s24 5 //parameters: String str1,str26 7 intstrcmpConst Char*STR1,Const Char*str2)8 {9 /*Do not use while (*str1++==*str2++) to compare, when not equal will still be executed once + +,Ten The comparison value returned by return is actually the next character. The + + should be placed in the loop body. */ One while(*STR1 = = *str2) A { - if(*STR1 = =' /') - Return0; the -str1++; -str2++; - } + return*STR1-*str2; -}
Simple implementation of strcpy and strcmp of standard library functions in C language