寻找字符串

版权声明:小简原创 https://blog.csdn.net/qq_43469554/article/details/87619059

某天,蒜头君和花椰妹在公园里散步,走着走着,我的天!他们各自都捡到了一串漂亮的字符串,然而蒜头君好奇心比较重,他想知道自己的字符串在花椰妹的字符串中出现了多少次,例如花椰妹的字符串为abababa,蒜头君的字符串为aba,那么蒜头君的字符串在花椰妹的字符串中出现了3次。蒜头君一向比较傲娇,于是向你请教,你可以帮帮他么?

输入格式

输入包含两行,第- -行为花椰妹捡到的字符串,第二行为蒜头君捡到的字符串。两个字符串可能包含除换行、回车、\o 外的任何字符。两个字符串长度均不大于1000。

输出格式

请你帮花椰妹找出他的字符串里出现了多少次蒜头君的字符串。

样例输入:
i miss you!
you
样例输出:
1

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char s1[1005], s2[1005];
	int ans = 0;
	gets(s1);
	gets(s2);
	int len1 = strlen(s1);
	int len2 = strlen(s2);
	for (int i = 0; i + len2 - 1<= len1 - 1; i++)
	{
		bool matched = true;
		for (int j = 0; j <= len2 - 1; j++)
		{
			if (s1[i + j] != s2[j])
			{
				matched = false;
				break;
			}
		}
		if (matched)
		{
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43469554/article/details/87619059