Copy String

char* copy(char *d, char *s)
{
    int n = strlen(s);
    char *ret = d;
    if ((d - s) >= 0) {
        d += n;
        s += n;
        for ( ; n >= 0; n--)
            *d-- = *s--;
    }
    else
        while (*d++ = *s++);
 
    return ret;
}
/* this version doesn't consider the case that dst array and src array is overlap */
void copy(char *dst, char *src)
{
    // copy characters until reaching '\0';
    while (*dst++ = *src++) ;
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License