【C/C++】模拟实现strcpy

函数原型:char * strcpy ( char * destination, const char * source );

模拟实现:

char *my_strcpy(char *dest, const char *src)
{
    assert(dest != NULL && src != NULL);
    char *res = dest;
    while ((*res++ = *src++) != '\0');
    return dest;
}

Example:

//http://www.cplusplus.com/reference/cstring/strcpy/?kw=strcpy
/* strcpy example */
#include 
#include 

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

//Output
//str1: Sample string
//str2: Sample string
//str3: copy successful

你可能感兴趣的:(C/C++)