20分,完全不知道什么意思,自己试都是对的
查看原帖
20分,完全不知道什么意思,自己试都是对的
1119494
lele189楼主2024/2/29 22:02
#include<iostream>
#include<string>
#include<cctype>
#include <sstream>
using namespace std;

int main()
{
	string article1;
	string article2;
	string word1;
	string word2;

    getline(cin, word1);
	getline(cin, article1);
	
	//将article1变成小写
	for (char c : article1)
	{
		if (isalpha(c))
		{
			article2 += tolower(c);
		}
		if (c == ' ')
		{
			article2 += ' ';
		}
	}
	//将word1也变成小写
	for (char c : word1)
	{
		if (isalpha(c))
		{
			word2 += tolower(c);
		}
	}

	istringstream iss(article2);

	int counter = 0;//记录单词次数
	int site=0;//记录位置
	int firstsite;
	bool ifsite = false;//第一个出现的位置是否出现了

	string word;


	while (iss>>word)
	{
		if (word == word2)//如果单词匹配上了
		{
			counter++;
			if (ifsite == false)//如果还没有记录过初始位置
			{
				ifsite = true;
				firstsite=site;
			}
		}
		else//没有匹配上的情况
		{
			site+= word.length() + 1;//单词长度加空格
		}
	}

	if (ifsite == false)//说明没有这个单词
	{
		cout << "-1";
	}
	else//说明单词出现过
	{
		cout <<counter<<" "<<  firstsite;;
		
	}









}
2024/2/29 22:02
加载中...