How to fix strcpy so that it detects overlapping strings

后端 未结 9 1295
南旧
南旧 2021-02-14 03:08

In an interview, I was asked to write an implementation of strcpy and then fix it so that it properly handles overlapping strings. My implementation is below and it is very naiv

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-14 03:46

    You could probably use memmove() if you expect the strings to be overlapping.

    char* my_strcpy(char *a, char *b)
    {
        memmove(a, b, strlen(b) + 1);
        return a;
    }
    

提交回复
热议问题