Simple implementation of strcpy and strcmp of standard library functions in C language

Source: Internet
Author: User
Tags strcmp

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.