码迷,mamicode.com
首页 > 编程语言 > 详细

【c语言】模拟库函数strstr

时间:2015-07-04 12:43:16      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

//  模拟库函数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

标签:

原文地址:http://blog.csdn.net/zhaoyaqian552/article/details/46754463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!