正文

单词统计2007-03-20 11:06:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/24122.html

分享到:

/*
编写一个程序要求用户输入一段文字,然后按照每个单词的开头字母对这段文字分类统计单词的数量并排序输出。例如,一次运行程序情况如下:
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;
}


 

阅读(3459) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册