/*
编写一个程序要求用户输入一段文字,然后按照每个单词的开头字母对这段文字分类统计单词的数量并排序输出。例如,一次运行程序情况如下:
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
单词必须为字母开头
以回车为输入结束标记
*/
#include <stdio.h>
#include <ctype.h>
int main(){
int i,j,k,max,flag = 0,sum = 0;
int word[26];
char c;
for (i = 0; i < 26; i++)
word[i] = 0;
printf("Please input a passage: \n");
while(!isalpha(c = getchar()))/* 滤掉开始的空格或其它非字母字符*/
;
while (c != '\n'){/* 输入的结束标记定为回车 */
if(flag == 0 && isalpha(c)){
flag = 1;
++word[(tolower(c))-'a'];
}
else if (c == ' ')
flag = 0;
c = getchar();
}
for (i = sum = 0; i < 26; i++){
for(max = j = 0; j < 26; j ++){
if(word[j] > max){
max = word[j];
k = j;
}
}
if(!max)
break;
printf ("Words begin with : %c %4d\n", k+'a', max);
sum += word[k];
word[k] = 0;
}
printf("\nTotal words : %d\n", sum);
return 0;
}
评论