Commonly used functions in C language string --strcat, strcpy

strcpy

Prototype declaration: extern char * strcpy (char * dest, const char * src);
Header file: #include < string.h >
Copy the string and containing src NULL terminator from the start address to start at dest: Function Address Space
Description: memory pointed to src and dest dest region may not overlap and must have sufficient space to accommodate the character string src.
Returns a pointer to the dest pointers .
Function to achieve:
 
  1.  
    /**********************
  2.  
     * A typical industrial-grade simplest C standard library functions strcpy implementation
  3.  
     * Returns: the target string address.
  4.  
     * In the case of the standard ANSI-C99 abnormality occurs is not defined, it returns the value determined by the implementor, usually NULL.
  5.  
     * Parameters:
  6.  
     * StrDestination target string
  7.  
     * StrSource source string
  8.  
     ***********************/
  9.  
     
  10.  
      char *strcpy(char *strDestination,const char *strSource)
  11.  
     {
  12.  
      assert(strDestination!= NULL && strSource!=NULL);
  13.  
      char * = strDestination solidified;
  14.  
      while ((* solidified ++ * ++ strSource)! = '\ 0');
  15.  
      return strDestination;
  16.  
     }
  17.  
     
  18.  
      /*
  19.  
     Implemented in GNU-C (excerpt):
  20.  
     */
  21.  
      char* strcpy(char *d, const char *s)
  22.  
     {
  23.  
      char *r=d;
  24.  
      while((*d++=*s++));
  25.  
      return r;
  26.  
     }
  27.  
      / * While ((* d ++ = * s ++)); explanation: two brackets to take the value of an assignment,
  28.  
    The left operand of assignment expression of value, so after copying NULL, the loop stops * /


strcat

prototype

extern char *strcat(char *dest,char *src);

usage

#include <string.h>
In C ++, it is present in the <cstring> header file.

Features

Added to the end of the string src referred dest (dest covered at the end of '\ 0') and adding '\ 0'.

Explanation

memory pointed to src and dest dest region may not overlap and must have sufficient space to accommodate the character string src.
Returns a pointer to the dest pointers .
Function to achieve:
  1.  
    // add the source string const, indicating that the input parameter
  2.  
    char *strcat(char *strDest, const char *strSrc)
  3.  
    {
  4.  
    // text after the return address, it can not be placed after the statement address assert assertion
  5.  
    char *address = strDest;
  6.  
    Assert ((strDest =! NULL) && (strSrc =! NULL)); // the source and destination addresses plus non-assertion 0
  7.  
    the while (* strDest) // a while (* strDest! = '\ 0') in a simplified form
  8.  
    {
  9.  
    // If you use while (* strDest ++), an error occurs, since the end of the cycle will be performed once strDest ++,
  10.  
    // it will point strDest '\ 0' next position. / ++ in circulation so to the body; because if * strDest last finger
  11.  
    // this marks the end of the string '\ 0'.
  12.  
    strDest++;
  13.  
    }
  14.  
     
  15.  
    while(*strDest++ = *strSrc++)
  16.  
    {
  17.  
    NULL; // can be used within the cycle conditions ++
  18.  
    } // This statement can be added * strDest = '\ 0'; not necessary
  19.  
    address return; // chaining order to achieve the object of the return address
  20.  

Guess you like

Origin www.cnblogs.com/qiumingcheng/p/11370310.html