strstr函數的實現

函數原型:extern char *strstr(const char *str1, const char *str2);函數

str1: 被查找字符串 spa

str2: 要查找字符串code

返回值:該函數返回str2第一次在str1中的位置,若是沒有找到,返回NULL字符串


函數實現原型

char * strstr(const char *s1,const char *s2)
{
	const char * p = s1;
	const int len = strlen(s2);

	for(;(p=strchr(p,*s2))!=0;p++)
	{
		if(strncmp(p,s2,len)==0)
			return (char *)p;
	}
	return 0;
}

實現中使用了另外兩個函數strchr和strncmp。class

另外兩篇文章中有介紹和實現co

相關文章
相關標籤/搜索