本文主要是介绍单词识别C++(getline()、for(auto [k,v]:hash),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
输入一行英文句子,统计句子中出现的各个单词(不区分大小写)及其出现次数。要求能识别英文句号和逗号,即是说单词由空格、句号和逗号隔开。
输入格式
共一行,包含一个长度不超过 1000 的字符串。
字符串中只包含大小写字母,空格,英文句号和逗号。
输出格式
按字典顺序,输出每个单词及其出现次数。单词在输出时,应将字母全部转化为小写。每个单词的输出占一行。
具体格式为:word:times
数据范围
输入字符串长度不超过 1000。
至少存在一个有效单词,单词一定完全由字母构成。
输入样例:
A blockhouse is a small castle that has four openings through which to shoot.
输出样例:
a:2
blockhouse:1
castle:1
four:1
has:1
is:1
openings:1
shoot:1
small:1
that:1
through:1
to:1
which:1
#include<iostream>
#include<map>
using namespace std;
bool check(char x)
{if(x>='a'&&x<='z'||x>='A'&&x<='Z') return true;return false;
}
int main()
{string str;getline(cin,str);map<string,int> hash;for(int i=0;i<str.size();i++){if(check(str[i])){int j=i;string word;while(j<str.size()&&check(str[j]))word+=tolower(str[j++]);hash[word]++;i=j;}}for(auto [k,v]:hash)cout<<k<<":"<<v<<endl;return 0;
}
这篇关于单词识别C++(getline()、for(auto [k,v]:hash)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!