#include <iostream>
using namespace std;
#include <assert.h>
char* my_strstr(char *str1, char *str2)
{
assert(str1); //原串
assert(str2); //子串
while (*str1 != '\0')
{
char *p = str1;
char *q = str2;
char *res = NULL;
if (*p == *q)
{
res = p;
while (*p && *q && *p++ == *q++)
;
if (*q == '\0')
return res;
}
str1++;
}
return NULL;
}
int main()
{
char *str1 = "abdcddeffg";
char *str2 = "def";
//cout << my_strstr(str1, str2) << endl;
printf("%s\n",my_strstr(str1,str2));
system("pause");
return 0;
}
本文出自 “Vs吕小布” 博客,谢绝转载!