How to fix strcpy so that it detects overlapping strings

后端 未结 9 1292
南旧
南旧 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 04:03

    if a > b; then
        copy a from the beginning
    else if a < b; then
        copy a from the ending
    else // a == b
        do nothing
    

    You can refer to an implementation of memmove, it's quite like what I said.

提交回复
热议问题