strstr函数(Brutr-Force算法)

来源:互联网 发布:星际淘宝系统 编辑:程序博客网 时间:2024/06/15 13:50

Brutr-Force算法:当比较字串失败后,返回到初始比较的下一字节

int strstr1(const char *str, const char *substr)

{

 assert(NULL != str || NULL != substr);

 

 int index = 0;

 int temp =0;

 int subIndex = 0;

 

 while(*str)

 {

 

  if(*str == *substr)

  {

   temp = index;

   subIndex = 0;

 

   while((*substr != NULL) && (*str != NULL) && (*str == *substr))

   {

    index++;

    subIndex++;

    substr++;

    str++;

   }

 

   if(*str -  *substr == 0)

   {

    break;

   }

 

   substr -= subIndex;

   str -= index-temp;

   index = temp;

   temp = -1;

  }

 

  index++;

  str++;

 }

 

 return temp;

}

 

 

 

原创粉丝点击