模拟实现strstr(c语言)

来源:互联网 发布:你凭什么看不起程序员 编辑:程序博客网 时间:2024/05/16 11:42
#include<stdio.h>#include<stdlib.h>#include<Windows.h>#include<string.h>#include<assert.h>char* my_strstr(const char* str, const char* substr)  {      const char* s1 = str;     const char* s2 = substr;      const char* ret = str;//ret就是用来保存首地址的      assert(str);      assert(substr);if(strlen(s1)<strlen(s2))return NULL;    while (*ret)      {          s1 = ret;s2 = substr;          while (*s1 && *s2 && (*s1 == *s2))          {              s1++;              s2++;          }          if (*s1 == '\0')          {              return NULL;          }          if(*s2 == '\0')return ret;         ret++;    }      return NULL;  }  int main()  {      char a[20] = "abbbcdef";      char b[10] = "bbcde";      printf("%s\n", my_strstr(a, b));      system("pause");      return 0;  }