用数组实现strstr函数


用数组实现strstr函数
char * mystrstr(char * dest, char *src)
{
int i = 0;
int j = 0;

//匹配个数
int count = 0;
int len = strlen(src);
char * p = NULL;
while (dest[i] != '\0')
{
//if (dest[i] == src[i]);

while (dest[i] == src[j] && dest[i])//匹配个数 = 字符串长度 l l l o
{
if (!count)
//如果匹配成功一个字符 需要记录位置
p = &dest[i];
count++;
i++;
j++;
//匹配成功
if (count == len)
{
return p;
}

}

//发生改变的值 i j count p
if (count < len)
{
i = i - count;
j = 0;
//count 归 0
count = 0;
//continue;
}

i++;
}

//返回值结果
//return p;
return NULL;
}

int main()
{

char *p = mystrstr("helllllo", "lllllo");
printf("%s\n", p);

system("pause");
return EXIT_SUCCESS;
}

posted on 2019-11-20 14:50  不冒泡的苏打水  阅读(387)  评论(0编辑  收藏  举报

导航