#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
char *my_strstr(char *dest, char *src)
{
int i = 0;
int j = 0;
int len=strlen(src);
int count=0;
char * p = NULL;
while (dest[i]!='\0')
{
while (dest[i] == src[j])
{
//如果匹配成功了 获取首地址
if (!count){
p = &dest[i];
}
count++;
i++;
j++;
//匹配成功
if (count == len)
{
return p;
}
}
//helllo world", "llo"
if (count < len)
{
//退回到匹配相等的原位置
i = i- count;
//j也重新赋值为0
j = 0;
//计数器也为0
count = 0;
}
//加1后自动到下一个位置
i++;
}
return NULL;
}
int main(void)
{
char * ss = my_strstr("helllo world", "llo");
printf("%s\n", ss);
system("pause");
return 0;
}
strstr的实现
最新推荐文章于 2021-11-17 17:04:43 发布