K&R -- WC统计行数、单词数、字符数

近来无事,随手翻翻书吧。。。。。

#include<stdio.h>

#define OUT  1
#define IN   0

main()
{
    int c, nl, nc, nw, state;

    nl = nc = nw = 0;
    state = OUT;
    while ((c = getchar()) != EOF) {
        nc ++;

        if (c == '\n') {
            nl ++;
        }
        if (c == ' ' || c == '\t' || c == '\n') {
            state = OUT;
        } else if (state == OUT) {
            state = IN;
            nw ++;
        }
    }

    printf("%d\t%d\t%d\n", nl, nw, nc);
}

 

wc的

posted @ 2012-10-28 18:33  stallman  阅读(261)  评论(0编辑  收藏  举报