标签:
(1). 实现一个控制台程序,给定一段英文字符串,统计其中各个英文单词(4字符以上含4字符)的出现频率。
file: 3
word: 2
case: 1
considered: 1
insensitive: 1
same: 1
(2). 性能分析:
(1).github: 代码签入github
(2).博客:提交博客
输入
Word is case insensitive, i.e. “file”, “FILE” and “File” are considered the same word.
输出
file: 3
word: 2
case: 1
considered: 1
insensitive: 1
same: 1
还可以参考下列链接:
C++ 正则表达式介绍链接
5.代码
#include <iostream> using std::cout; using std::endl;
//函数: 查找子串sub在str中出现的次数
int fun(const std::string& str, const std::string& sub)
{
int num = 0;
for (size_t i=0; (i=str.find(sub,i)) != std::string::npos; num++, i++);
return num;
}
void main()
{
std::string str("Many of my classmates have a computer, I have one too. My father bought it for me as a present when my first year in middle school. He said I can study English with computer. Most of the time, I use computer to search study materials on the internet. I also have some foreign friends on the internet, we can talk in English. Sometimes I play video game with computer after I finish my homework. My computer helps me a lot, it is a good friend to me.");
std::string sub("computer");
cout<<fun(str,sub)<<endl;
}
6.结果
7.总结
我一开始想用KMP匹配来做的,后来没调试好,而且计数器也加不进去就放弃了,最后就用了这个蠢办法<<**@**>>
标签:
原文地址:http://www.cnblogs.com/Vpygamalion/p/5559074.html