菜鸟求教,为什么会显示找不到,感激不尽

#include
#include
#include
char *mystrstr(char *string,char *findstring )
{
if (string == NULL || findstring == NULL)
{
return NULL;
}
int flag = 1;
while (*string != '\0')
{
char *p=string;
char *px = string;
char *now = findstring;
while (*now != '\0')
{
if (*px == *now)
{
px++;
now++;
}
else
{
flag = 0;
break;
}
}

if (flag == 1)
{
return p;
}
string++;
}
if (flag == 0)
{
return NULL;
}
}
void main()
{
char str1[20] = "i love you";
char str2[20] = "love";
char *p=mystrstr(str1, str2);
if (p == NULL)
{
printf("没有找到\n");
}
else
{
printf("%c\n", *p);
}

system("pause");

}

改成了如下这样:

 #include <stdio.h>
#include <iostream>
using namespace std;

char *mystrstr(char *string,char *findstring )
{
    if (string == NULL || findstring == NULL)
    {
        return NULL;
    }
    int flag = 1;
    while (*string != '\0')
    {
        char *p=string;
        char *px = string;
        char *now = findstring;
        while (*now != '\0')
        {
            if (*px == *now)
            {
                px++;
                now++;
            }
            else
            {
                flag = 0;
                break;
            }

            if(*now != '\0')
                flag = 1;
        }

        if (flag == 1)
        {
            return p;
        }
        string++;
    }
    if (flag == 0)
    {
        return NULL;
    }
}
void main()
{
    char str1[20] = "i love you";
    char str2[20] = "love";
    char *p=mystrstr(str1, str2);
    if (p == NULL)
    {
        printf("没有找到\n");
    }
    else
    {
        printf("%c\n", *p);
    }
    system("pause");
}

就多加了一下两句:
if(*now != '\0')
flag = 1;
while (*string != '\0')否则的话即使在某个位置两个字符串都匹配上了,但是没有把flag置1,无法跳出循环,
继续进行while (*string != '\0')里的循环,然后由于字符匹配不上,flag被置成0了

#include
#include
#define NULL 0
缺少头文件的引入,NULL在C/C++标准库中被定义为一个宏,一般为:
#define NULL ((void*)0) /*C中的“标准”写法,NULL被替换为一个void*类型的指针右值,值等于0;由于是void*类型,可以隐式转化为其它类型的指针。

#include
#include
#define NULL 0