正文

单词统计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;
}


 

阅读(3518) | 评论(1)


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

评论

评论人: 发布时间: 2010-01-13 14:43:00
如果这个题目稍微改一下,这个程序应该怎么改啊?题目是:在盘中,存有一篇英语文章(该文章不存在一个单词跨两行的现象,每行最多80个字符)统计每个单词的开头字母出现的次数,再把结果存入文件中。请问这个程序用c语言应该怎么编?????
您需要登录后才能评论,请 登录 或者 注册