C language – strcpy and strncpy programming implementation and summary

1. Strcpy and strncpy functions of strings

  1、Programming to achieve strcpy function (written test is easy to test)

     Request:

     Prototype: char *stpcpy (char *strDest, char *strSrc);

     Header file: #include < string.h>

     Function: copy the string that SRC refers to by NULL to the array referred to by dest.

        Note: Memory areas referred to by Src and dest cannot overlap and dest must have enough space to accommodate the SRC string.

     Returns a pointer to the character (NULL) at the end of dest.

 

    strcpyCode implementation:

     char * strCpy(char * strDest,const char * strSrc)   //[1]
     {
        assert((strDest != NULL)&&(strSrc != NULL));      //[2] 
        char * strDestCopy=strDest;                   //[3]
        while ((*strDest++ = *strSrc++) != '\0');         //[4]
        return strDestCopy;                            //[5]
     }

    Code analysis:

      Code [1]: you may forget the parameter list and return value in the code. 

        Code [2]: Many people forget to check the string pointer. This part reflects the programmer's rigor, mainly in the following three points.

           a、Does not check the validity of the pointer, indicating that the respondents do not pay attention to the robustness of the code.

           b、Use ((!) strDest | (! StrSrc)) or (!) (strDest & amp; & amp; strSrc) to check the validity of pointers, indicating that the respondent has no deep knowledge of implicit conversions of types in C.

           c、Use ((strDest = = 0) | (strSrc = = 0)) when checking the validity of pointers to show that the respondent does not know the benefits of using constants

        Code 2 can also be written as if ((strDest = = NULL)) (strSrc = = NULL).
                           throw "Invalid argument(s)"; //throwTo throw an exception

      Code [3]: forget to save the original strDest value, indicating that the respondent’s logical thinking is not strict.  

      Code [4]: There’s no hard requirement, this part of the functionality is easy to implement, considering the boundary problem

      Code [5]: The return value is for the chain call (in general, the pointer that the return value is a string type is for the chain call)

  

  2、Programming strncpy function

     Requirement:

      Prototype: char *strncpy (char *strDest, char *strSrc, int n);

      Header file: #include < string.h>

      Function: copy the first n byte of the string that SRC refers to by NULL to the array referred to by dest.

      Explain:

           1、If the first n byte of SRC does not contain NULL characters, the result will not end with NULL characters.

        2、If the length of SRC is less than n bytes, fill dest with NULL until n bytes are copied.

        3、srcAnd dest refers to the memory area can not be overlapped and dest must have enough space to accommodate SRC strings.

      Return value: pointer to dest.

 

      strncpyCode implementation:

         char * my_strncpy(char *strDest, const char *strSrc, int num)
         {
              assert((strDest != NULL) && (strSrc != NULL));
              //if (strDest == NULL || strSrc == NULL) return NULL;

              //Save the first address of the target string.
              char *strDestcopy = strDest;
              while ((num–)&&(*strDest++ = *strSrc++) != ‘\0’);
              //If num is greater than strSrc, the number of characters will be automatically supplementing’\0′.
              if (num > 0)
              {
                   while(–num)
                   {
                        *strDest++ = ‘\0’;
                   }
              }
              return strDestcopy;
        }

      Code analysis:

        1、There is little difference between code strncpy and code strcpy.

        2、Consider filling dest with NULL until n bytes are copied if the length of SRC is less than n bytes.

Leave a Reply

Your email address will not be published. Required fields are marked *