C ++ string copy function (interview question)

Source: Internet
Author: User

1. It is known that the prototype of the strcpy function is:
Char * strcpy (char * strDest, const char * strSrc );
Here, 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.
Answer:
C code

Char * strcpy (char * strDest, const char * strSrc)
{
If (strDest = NULL | strSrc = NULL)
Return NULL;
If (strDest = strSrc)
Return strDest;
Char * tempptr = strDest;
While (* strDest ++ = * strSrc ++ )! = '/0 ')
;
Return tempptr;
}

Important points,

(1) The string content referred to by the passed source pointer strSrc cannot be modified in this function, so it should be declared as the const type.

(2) When judging whether the source pointer or target pointer is null, the thinking is rigorous and exception handling should be considered.

(3) consider the situation where the passed source and destination pointers point to the same memory area. If they point to the same memory area, the source pointer = destination pointer and one of them can be directly returned.

(4) For a function prototype, note that the return value is the first address of the target string. You must copy the return value of the function as a parameter of other functions.

C ++ code. Therefore, the first address of the target pointer must be saved in the function.

(5) When copying a string, you can either determine whether the character is null or not before copying the string. At this time, you must add the '/0' character to the target string outside the recycling; you can either copy the file before determining whether the character is null.

The above code is the last form.

The previous format is

While (* strDest! = '/0 ')

{

* StrDest ++ = * strSrc ++;

}

* StrDest = '/0 ';

C ++ code:

Char * strcpy (char * strDest, const char * strSrc)
{
If (strDest = NULL) | (strSrc = NULL ))
Throw "Invalid argument (s )";

// Note that it cannot be written as throw new String ("invalid argument (s)"), because the dynamically applied memory in the function,

// Returning the address is a bad programming habit. It is easy for users to release the memory after using the function.

If (strDest = strSrc)
Return strDest;

Char * strDestCopy = strDest;
While (* strDest ++ = * strSrc ++ )! = '/0 ');
Return strDestCopy;
}

From the code, we can see that an exception is handled in the C ++ code, and throw an exception.

 

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.