Details and implementation of string functions --- strcpy () and strncpy ()

Source: Internet
Author: User

1. strcpy () and strncpy ()

Strcpy (): strcpy (DEST, Src); strcpy copies the string pointing to SRC ending with '\ 0' to the array indicated by DEST, and returns the pointer pointing to DeST.

When sizeof (DEST)> = sizeof (SRC), the copy is correct and '\ 0' is added to the end of the Dest string ';

When sizeof (DEST) <sizeof (SRC), an error occurred while copying.


 

Strncpy (): strncpy (DEST, SRC, n); strncpy copies the first n characters of the string that SRC points to ending with '\ 0' to the array specified by DeST, returns the pointer to DeST.

When N> = sizeof (SRC), the copy is correct and '\ 0' is added to the end of the Dest string ';

When n <sizeof (SRC), only the first n-1 strings of SRC are copied to DEST, and '\ 0' is not added to the end of the Dest string ';


 

Sample Code:

# Include <iostream> # include <string> using namespace STD; int main () {char a [] = "lanzhihui is a good boy! "; // The following strcpychar B [30]; char C [30]; char d [30]; strcpy (B, A); // strcpy for simple copying, in addition to copying the string of array A, the '\ 0' following array A is also copied to array B. cout <"B:" <B <Endl; // when the storage space sizeof (B)> = sizeof (A) of array B is copied correctly, add '\ 0' after array B string '. // An error occurred while copying the storage space sizeof (B) <sizeof (A) of array B. Cout <"strlen (c):" <strlen (strcpy (D, strcpy (C, A) <Endl; // copy A to C, strcpy returns the c Address, copies C to D, returns the D address, and returns the length of the string D. // This is the reason why the target string address is returned after the strcpy () function is copied. It supports chained expression. Cout <"C:" <C <Endl; cout <"D:" <D <Endl; // The following strncpychar E [10]; char f [30]; char G [30]; strncpy (F, A, sizeof (f); // sizeof (f)> = sizeof (, copy strncpy (G, A, 10); // copy the first nine characters in A to G, you must manually add '\ 0' G [9] =' \ 0' to the G array; // n <sizeof (, otherwise, the output fails. strncpy (E, A, sizeof (e); e [sizeof (E)-1] = '\ 0'; // sizeof (E) <sizeof (a), this statement must exist, otherwise the output has an error cout <"E:" <e <Endl; cout <"F: "<F <Endl; cout <" G: "<G <Endl; System (" pause "); Return 0 ;}



 

For example, the copy operation between strcpy () and strncpy () ends with '\ 0:

# Include <iostream> # include <string> using namespace STD; int main () {char s [] = "lanzhihui \ 0 is a good boy! "; Char name [30]; strcpy (name, S); // copy the cout ended with '\ 0' <name <Endl; // The output end strncpy (name, S, sizeof (name) with '\ 0'; // The copy end cout with' \ 0' <name <Endl; // The output ends system ("pause") with '\ 0'; return 0 ;}


II. Implementation of strcpy () and strncpy ()

Write the implementation code based on the test of strcpy () and strncpy () functions:

# Include <iostream> # include <string> # include <assert. h> using namespace STD; char * strcpy_m (char * DEST, const char * Str) {assert (DEST! = NULL) & (STR! = NULL); char * CP = DEST; while (* CP ++ = * STR ++ )! = '\ 0') {//} return DEST;} Char * strncpy_m (char * DEST, const char * STR, int N) {assert (DEST! = NULL) & (STR! = NULL); char * CP = DEST; while (N & (* CP ++ = * STR ++ )! = '\ 0') {n --;} If (n) {While (-- N) * CP ++ =' \ 0';} return DEST;} int main () {char a [] = "lanzhihui is a good boy! "; // The following strcpy_mchar B [30]; char C [30]; char d [30]; strcpy_m (B, A); cout <" B: "<B <Endl; cout <" strlen (c): "<strlen (strcpy_m (D, strcpy_m (C, A) <Endl; cout <"C:" <C <Endl; cout <"D:" <D <Endl; // The following strncpy_mchar E [10]; char f [30]; char G [30]; strncpy_m (F, A, sizeof (f); strncpy_m (G, A, 10 ); G [9] = '\ 0'; strncpy_m (E, A, sizeof (e); e [sizeof (E)-1] =' \ 0 '; cout <"E:" <e <Endl; cout <"F:" <F <Endl; cout <"G: "<G <Endl; System (" pause "); Return 0 ;}

It can be seen that the strcpy_m () function and strncpy_m () function are the same as the running result of the library function.

Details and implementation of string functions --- strcpy () and strncpy ()

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.