C ++ manual Implementation of library functions

Source: Internet
Author: User

It is known that the strcpy function prototype is char * strcpy (char * strdest, const char * strsrc), where strdest is the destination string and strsrc is the source string. Do not call the string library function of C ++/C. Compile the strcpy function.

1 # include <cassert> 2 3 char * strcpy (char * strdest, const char * strsrc) 4 {5 assert (strdest & strsrc ); // 2 points 6 char * CP = strdest; // 2 points 7 while (* CP ++ = * strsrc ++) // 2 points 8; 9 return strdest; // 2 points 10}

A: To implement a chained expression. // 2 points

For example, int length = strlen (strcpy (strdest, "Hello World "));

In addition, the strlen function is as follows:

1 int strlen (const char * Str) 2 {3 assert (STR); 4 int Len = 0; 5 while (* STR ++) 6 {7 Len ++; 8} 9 return Len; 10}

 

Strcat function implementation:

1 char * strcat (char * strdest, const char * strsrc) 2 {3 assert (strdest & strsrc); 4 char * Pd = strdest; 5 while (* PD ++); 6 While (* PD ++ = * strsrc ++); 7 return strdest; 8}

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.