P1308 统计单词数

#include<iostream>
#include<cstdio>
#include <iomanip>
#include<cmath>
#include<string>
using namespace std;

int main()
{
	string word, sentence;
	getline(cin, word);
	getline(cin, sentence);
	int w = word.size();
	int s = sentence.size();//不能用int s=strlen(word)
	//把单词 句子全部转换成大写
	for(int i=0;i<w;i++)
		word[i]= toupper(word[i]);//toupper转换成大写字母函数
	for (int j = 0; j < s; j++)
		sentence[j] = toupper(sentence[j]);
	
	int t = 0;//记录第一个出现的相同的字母
	int count = 0;//记录相同个数的单词
	for (int k = 1; k <= s - w; k++)//指向句子的
	{
		//q指向单词
		int q = 0;
		for ( q; q < w; ++q)
		{
			if (sentence[k + q] != word[q] )
				break;
			if (k>0&&sentence[k - 1] != ' ')
				break;//不是空格开头不是一个单词
			
			
		}
		if (q == w && (sentence[k +w] == ' '||k+w+q==s))//刚开始写的是sentence[k +1] == ' ',没有考虑到k其实根本没变
		{
			count++;
			if (count == 1)
				t = k;
		}
		
		
	}
	if (count != 0)
		cout << count <<" "<< t;
	else cout << -1;
		return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值