操作系统大型实验进展(9)-----strlen()和sizeof(三)

2014-11-23 23:20:59 · 作者: · 浏览: 19
---
  -------------------------------------------------4:start----------------------------------------
  int strlen(const char *str)
  {
  assert(str);
  if (*str==NULL)
  return 0;
  else
  return (1 + strlen(++str));
  }
  -----------------------------------------------4:end----------------------------------------
  -------------------------------------------------5:start---------------------------------------- /**
  * strlen - Find the length of a string
  * @s: The string to be sized
  */
  size_t strlen(const char *s)
  {
   const char *sc;
   for (sc = s; *sc != '\0'; ++sc)
   /* nothing */;
   return sc - s;
  }
  -----------------------------------------------5:end----------------------------------------
  以上各种实现的方式都是大同小异的,有的用的是变量,有的用的是指针。
  其中,最后一个用的是递归的方式。其实,在实现库函数的时候,是规定不可以
  调用其他的库函数的,这里只是给大家一个方法,不用变量就可以实现strlen。