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

Source: Internet
Author: User
Tags assert expression
The following is a detailed analysis of the use of the common functions of strcat and strcpy in C language, the need for friends can refer to the following

strcpy Prototype declaration: extern Char *strcpy (char* dest, const char *SRC);
header files: #
include <string.h>
function: Copy the string containing the null terminator starting from the SRC address to the address space starting with dest
Description:the memory area of SRC and dest can not 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.


* Parameter:


* strdestination Target string


* strsource Source string


***********************/





Char *strcpy (char *strdestination,const char *strsource)


{


assert (Strdestination!=null &amp;&amp; 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-layer bracket is to take the value of the 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 (covering the end of the Dest ") and add '.
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:


///The source string is added to the const, indicating that it is an input parameter
Char *strcat (char *strdest, const char *strsrc)
{
 //After text return address, so Cannot be placed after assert assertion declare 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 while (*strdest!= ') simplified form
  {
   //If using while (*strdest++), there will be an error, Because Strdest also performs a + + at the end of the loop,
   /Then strdest will point to the next location. /So be in the loop body + +, because if *strdest finally refers to the
   //To the end of the string ".
    strdest++;
 }

  while (*strdest++ = *strsrc++)
  {
    null;     & nbsp;      //The cycle condition can be used with + +,
 }                   //Here you can add a statement *strdest= '; no need
  return address;    //To implement a chained operation, return the destination address to
}

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.