Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

南开百题难题破解(3)

$
0
0

题目要求:

如:大字符串asasd asa 小字符串as 则n=3;

解答如下:

这个不难看懂,就不加注释了。

nt findStr(char *str, char *substr)
{
	char *p=str,*q=substr,*tem1=NULL,*tem2=NULL;
	int slen=strlen(str),sublen=strlen(substr);
	int count=0;
	while(p-str<slen)
	{
		tem1=p;
		tem2=q;
		while(*tem1==*tem2 && tem2-substr<sublen)
		{
			tem1++;
			tem2++;
			if(tem2-substr==sublen)
			{
				count++;
				break;
			}
		}
		p++;
	}
	return count;
}
用数组 实现
int findStr(char *str, char *substr)
{
	int i=0,j=0;
	int tem1=i,tem2=j;
	int count=0;
	while(str[i]!='\0')
	{
		tem1=i;
		tem2=j;
		while(str[tem1]==substr[tem2] && substr[tem2]!='\0')
		{
			tem1++;
			tem2++;
			if(substr[tem2]=='\0')
			{
				count++;
				break;
			}
		}
		i++;
	}
	return count;
}


作者:huangsir2011 发表于2013-3-9 16:56:23 原文链接
阅读:53 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>