Introduction to the usage of string common functions strcat and strcpy in C language _c language

Source: Internet
Author: User
strcpy Prototype declaration: extern Char *strcpy (char* dest, const char *SRC);
header file: #
Include <string.h>
function:Copy the string that starts with the SRC address and contains a null terminator to the address space starting with dest
Description:The memory regions referred to in SRC and dest cannot overlap and dest must have enough space to hold the SRC string.
Returns a pointer to Dest.
function Implementation:
Copy Code code as follows:

/**********************
* C Language standard library function strcpy A typical industrial-level minimalist implementation
* Return value: The address of the destination string.
* The ANSI-C99 standard is not defined for situations where exceptions occur, so the implementation determines the return value, usually null.
Parameters
* strdestination Target string
* Strsource Source string
***********************/

Char *strcpy (char *strdestination,const char *strsource)
{
ASSERT (Strdestination!=null && strsource!=null);
Char *strd=strdestination;
while ((*strd++=*strsource++)!= ' ");
return strdestination;
}

/*
Implementation in Gnu-c (excerpt):
*/
char* strcpy (char *d, const char *s)
{
Char *r=d;
while ((*d++=*s++));
return R;
}
/* while ((*d++=*s++)); Explanation: The two brackets are to take the value of an assignment expression,
The value of the assignment expression is the left operand, so after the copy null, the loop stops.

strcat Prototype
extern Char *strcat (char *dest,char *src);
Usage
#include <string.h>
In C + +, it exists in the <cstring> header file.
function
Add the string src refers to the end of the dest (cover the ' dest ' at the end of the ending) and add ' the '.
description
The memory regions referred to in SRC and dest cannot overlap and dest must have enough space to hold the SRC string.
Returns a pointer to Dest.
function Implementation:
Copy Code code as follows:

Add a const to the source string, indicating that it is an input parameter
Char *strcat (char *strdest, const char *STRSRC)
{
After the text return to address, it can not be placed in the assert assertion after the address
char *address = strdest;
Assert ((strdest!= null) && (STRSRC!= null)); Add a non 0 assertion to the source address and destination address
while (*strdest)//is a simplified form of while (*strdest!= ' ")
{
If you use while (*strdest++), there will be an error, because Strdest will also perform a + + after the loop finishes.
Then strdest will point to the next position of ' I '. /So be in the circulation body + +; Because if *strdest the last finger
To the end of the string '.
strdest++;
}

while (*strdest++ = *strsrc++)
{
NULL; The cyclic condition can be used with + +,
//Here you can add a statement *strdest= ' "; no need
return address; In order to achieve the chain operation, the destination address is returned
}

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.