单词计数

单词计数

1. 统计行数、单词数与字符数

2. 单词认为是任何其中不包含空格、制表符或换行符的字符序列

#include <stdio.h>
#include <Conio.h>
#define IN 1
#define OUT 0
main()
{
    int nl,nw,nc,input,state;
    nl = nw = nc = 0;
    state = OUT;
    while((input=getchar())!=EOF){
        ++nc;
        if(input== '
'){
            ++nl;
        }
        if(input ==' ' || input == '	' || input == '
'){
            state = OUT;
        }
        else if(state == OUT){
            state = IN;
            ++nw;
        }
    }
    printf("number of line is: %d, number of character is: %d, number of new word is: %d", nl, nc, nw);
    getch();
}

tips:

赋值由右向左,n1 = (nw = (nc = 0));