编写一个程序要求用户输入一段文字,然后按照每个单词的开头字母对这段文字分类统计单词的数量并排序输出。
编写一个程序要求用户输入一段文字,然后按照每个单词的开头字母对这段文字分类统计单词的数量并排序输出。例如,一次运行程序情况如下:
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
设计提示:
int main()
{
char passage[500];
int total, count[26], sort[26];
char *token;
int i, j;
for(i=0; i<26; i++)
count[i]=sort[i]=0;
total = 0;
printf("Please input a passage:\n");
gets(passage); //需编写
InitScanner(passage);//需编写
while (!AtEndOfLine()//需编写) {
token = GetNextToken();
if(isalpha(token[0])){
count[tolower(token[0])-'a']++;
total++;
}
}
for(i = 0; i<26; i++)
sort[i] = count[i];
SortIntegerArray(sort, 26);
for(i = 0; i<26; i++){
if(sort[i]!=0){
for(j=0; j<26; j++){
if(count[j]==sort[i]){
printf("Words begin with %c: %d\n", j+'a', sort[i]);
count[j] = 0;
}
}
}
}
printf("\nTotal words: %d\n", total);
return 0;
}
请各位兄弟帮忙填写完整,先谢谢了