经典面试题之手撕字符串函数

  • 2019 年 10 月 6 日
  • 筆記

经典面试题之手撕字符串函数

最近看到一些面试C/C++的一个问题:手撕字符串函数,包括:

  • strcpy
  • memcpy
  • memmove
  • strcat
  • strcmp
  • strstr

下面一起来手撕。

1.strcpy

功能:把 src 所指向的字符串复制到 dest。

看到这个需求你一想,这还不简单,就写出了下面代码,可惜啊,你挂了!

char *strcpy(char *dest, const char *src) {      if (!dest || !src)          return NULL;      char *d = dest;      while (*src != '