首页 > 其他 > 详细

在父字符串中查找子字符串

时间:2016-03-26 08:15:25      阅读:238      评论:0      收藏:0      [点我收藏+]

在父字符串中查找子字符串(指针控制,也可选择标控制)

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
char* StrStr(char* source, char* dest)
{
	assert(source&&dest);
	if (strlen(source) < strlen(dest))
		return NULL;
	char* newSrc = NULL;
	char* newDest = dest;
	while (*source)
	{
		newSrc = source;
		while (*source&&*dest&&*source == *dest)
		{
			source++;
			dest++;
		}
		if (*dest == ‘\0‘)
		{
			return newSrc;
		}
		dest = newDest;
		source = newSrc + 1;
	}
}
void Test1()
{
	char* src = "abcbcdef";
	char* dest = "bcd";
	cout << StrStr(src, dest) << endl;
}

指针追踪截图

技术分享


技术分享


技术分享



技术分享


本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1755336

在父字符串中查找子字符串

原文:http://10541556.blog.51cto.com/10531556/1755336

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!