且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【c语言】模拟库函数strstr

更新时间:2022-09-16 21:01:30

//  模拟库函数strstr

#include <stdio.h>
#include <assert.h>

const char* my_strstr(const char *parent, const char *child)
{
	const char *pgo = parent;
	const char *cgo = child;
	const char *pgos = parent;
	assert(parent != NULL && child != NULL);
	if (!*child)
	{
		return NULL;
	}
	while (*pgo)
	{
		pgo = pgos;
		cgo = child;
		while (*pgo == *cgo && *pgo && *cgo)
		{
			pgo++;
			cgo++;
		}
		if (*cgo == '\0')
			return pgos;
		pgos++;
	}
	return NULL;
}
int main()
{
	printf("%s\n", my_strstr("hhhww", "hhww"));
	printf("%s\n", my_strstr("zhaoyaqian","aoya"));
	printf("%s\n", my_strstr("abc", "aefg"));
	return 0;
}




【c语言】模拟库函数strstr






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5081516.html,如需转载请自行联系原作者