求助!词频统计程序的修改
题目要求输入一段文字,统计每个单词的数量并输出,但是下面这段程序只能实现开头字母相同的单词的统计。希望有人能把核心比较部分修改下,实现对单词的统计
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main()
{
char str[500];
char *strToken;
char strDelimit[] = " ,.?!";
int wordCount = 0;
map<char, int> words;
map<char, int>::iterator iter;
cout << "Please input a passage:" << endl;
cin.getline( str, sizeof(str) );
strToken = strtok( str, strDelimit );
while ( strToken != NULL )
{
iter = words.find( tolower( *strToken ) );
if ( iter == words.end() ) {
words.insert( pair<char, int>( tolower( *strToken ), 1 ) );
}
else {
iter->second++;
}
++wordCount;
strToken = strtok( NULL, strDelimit );
}
for ( iter=words.begin(); iter!=words.end(); ++iter ) {
cout << "Words begin with " << iter->first << ": " << iter->second << endl;
}
cout << "Total words: " << wordCount << endl;
这样的运行结果是:
Please input a passage:
The topic of this assignment is about array, pointer and string. In particular, the goal of the assignment is to give you experience for dividing programs into modules and using the pointer for manipulation of string data.
Words begin with t: 7
Words begin with a: 6
Words begin with i: 4
Words begin with p: 4
Words begin with o: 3
Words begin with d: 2
Words begin with f: 2
Words begin with g: 2
Words begin with m: 2
Words begin with s: 2
Words begin with e: 1
Words begin with u: 1
Words begin with y: 1
Total words: 37
而我要的结果是:
the:n个
topic:m个
。。。。。