A string copy interview question

Source: Internet
Author: User

Many technical tests are written into the string copy function (which database function cannot be used ),
Is there any question about where to take the function test site and when writing it?
It is best to write rigorous routines.
Function declaration:
Char * strcpy (char * strdest, const char * strsrc)
So far, I have concluded that:
1. Test whether strdest and strsrc are not empty.
2. Check whether strdest and strsrc overlap
3. Pay attention to the writing structure
4. Function robustness
In addition, I think if the Data Length of the strsrc buffer exceeds the value of the strdest buffer
But how to check the length of the strdest buffer?
Who else can add?

 

 

 

Strncpy () function: This Program Written by Linus Torvalds, a real cool man.

Char * strncpy (char * DEST, const char * SRC, size_t count)
{
Char * TMP = DEST;

While (count -- & (* DEST ++ = * SRC ++ )! = '\ 0 ')
/* Nothing */;

Return TMP;
}

Someone may say, "No, why not add an end sign to the target string to this:
While (count -- & (* DEST ++ = * SRC ++ )! = '\ 0 ')
/* Nothing */;
* DEST = '\ 0 ';"

As a strncpy function, it just needs to copy the specified length of characters to the target string, without any self-Attention, maybe the function caller does not want this end sign at all. If needed, he will add it himself.

Therefore, a function can only do what you want. This is simplicity. Other functions should understand what the called function can do. This is consistency; unix/Linux itself is a wonderful product with simple consistency principles.

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.