#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
char* my_strstr(const char* str1, const char* str2)
{
const char* cur = str1;
const char* s1 = NULL;
const char* s2 = NULL;
assert(str1 && str2);
if (str2 == '\0')
{
return (char*)str1;
}
while (*cur)
{
s1 = cur;
s2 = str2;
while(*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)cur;
}
cur++;
}
return NULL;
}
int main()
{
char arr1[] = "abcdef";
char arr2[] = "def";
char* ret = my_strstr(arr1, arr2);
if (ret != NULL)
{
printf("%s\n", ret);
}
else
{
printf("找不到\n");
}
return 0;
}
【C语言】每日一代码:模拟实现strstr
最新推荐文章于 2024-08-26 00:02:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)