首先,字符串查找函数是在目的字符串中查找源字符串的首次出现的具体位置,若找到了便返回该位置的地址,若没有找到,则返回空指针NULL
char* strstr(const char*arr1 , const char* arr2);
#include<stdio.h>
#include<assert.h>
char* my_strstr(const char* arr1,const char* arr2)//查找字符串,目的地字符串和源头字符串的内容不发生改变(const)
{
assert(arr1 != NULL);
assert(arr2 != NULL);//确保指针的有效性
char* s1 = arr1;
char* s2 = arr2;
char* cur = arr1;//将arr1的地址赋给cur,用cur来控制查找的起始位置
if (*arr2 =='\0')//说明arr2字符串是空串,则返回arr1的地址
{
return arr1;
}
while (*cur)//循环继续的条件是*cur!=NULL
{
s1 = cur;//将cur赋给S1
s2 = arr2;
while ((*s1 != '\0') && (*s2 != '\0') && (*s1 == *s2))//满足这三个条件,则继续查找下一个字符,判断是否相同