码迷,mamicode.com
首页 > 其他好文 > 详细

笔试题目

时间:2014-10-15 09:38:10      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   os   ar   for   sp   2014   on   

找出一个字符串内某个子串的个数。

例如:

子串为:lan

输入:lanzhihui is a good boy lan la lalalan

输出:3

//检索子串的个数
#include<iostream>
#include<string>
using namespace std;


int find_str(string s,const char *str)
{
	if(str==NULL||s.empty())
		return 0;
	int len=s.size();
	const char *cp=str;
	int sum=0;
	int i=0;
	bool falg=false;
	while(i<len)
	{
	    falg=false;
		while(s[i]==*str)
		{
		    i++;
			str++;
			falg=true;
			if(*str=='\0')
			{
				sum++;
				str=cp;
			}
			if(i==len)
			{
				return sum;
			}
		}
		str=cp;
		if(!falg)
		{
		   i++;
		}
	}
	return sum;
}

int main()
{
	string s;

	for(;;)
	{
	getline(cin,s);

	int sum=find_str(s,"lan");
	cout<<sum<<endl;
	}
	system("pause");
	return 0;
}

bubuko.com,布布扣

笔试题目

标签:blog   http   io   os   ar   for   sp   2014   on   

原文地址:http://blog.csdn.net/lanzhihui_10086/article/details/40088647

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