Requirements: Write a function that implements a string copy function similar to strcpy. The prototype is defined as char *mystrcpy(char *dest, char *src). The function is to copy the string pointed to by src to the array pointed to by dest, and return the dest pointer. (Hint: the areas pointed to by src and dest may overlap).
Simple version number:
char * mystrcpy (char * dest, char * src) { char * p = dest; while(*src != '\0'){ * dest ++ = * src ++; } return p; }Storage space analysis:
The above case does not consider the problem of overlapping memory regions. There is also no validity check for input parameters.
The revised version number is:
char * mystrcpy (char * dest, char * src) { char *d = dest;//input parameter backup char *s = src; int count = 0; assert(dest);//Pointer validity check assert(src); if(src == dest){ return src; } count = strlen(src) + 1; if(count<=1){ return 0; } if(dest < src || dest >= (src + count)){ while(count--){ *d++ = *s++; } } else{ d = dest + count; s = src + count; while(count--){ *d-- =*s--; } } return dest; }