strstr function implementation

Strstr () function is used to find the first address of the substring, to achieve the following functions:

char* strstr(char * str1,char * str2)
{
	char *p1=NULL;
	char *p2=NULL;
	while(*str1)
	{
		p1=str1;
		p2=str2;
		while(*p1==*p2 && *p2!=NULL)
		{	
			p1++;
			p2++;
		}
		if(*p2==NULL)
		{
			return str1;
		}
		str1++;
	}
	
	return NULL;
}

Test Case:

#include<stdio.h>
#include<string.h>
char* strstr(char * str1,char * str2);
int main()
{
	char *str1="accdef";
	char *str2="cde";
	printf("%0x",strstr(str1,str2));
	
}
char* strstr(char * str1,char * str2)
{
	char *p1=NULL;
	char *p2=NULL;
	while(*str1)
	{
		p1=str1;
		p2=str2;
		while(*p1==*p2 && *p2!=NULL)
		{	
			p1++;
			p2++;
		}
		if(*p2==NULL)
		{
			return str1;
		}
		str1++;
	}
	
	return NULL;
}

Test Results:

Published 33 original articles · won praise 30 · views 20000 +

Guess you like

Origin blog.csdn.net/baidu_15547923/article/details/100764049