VC++求助啊!

来源:百度知道 编辑:UC知道 时间:2024/06/22 03:52:35
属入一段文字,然后按照每个单词的开头字母对这段文字分类统计单词的数量并排序输出:
例如: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

提示:
strtok()函数:
char * strtok( char *strToken, const char *strDelimit );
strToken: 包含着token的字符串(string)
strDelimit: 分隔符集合(seps)
大写字母转换成小写字母的函数tolower()

#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 << "