c语言实现wc功能

本随笔对网站http://blog.chinaunix.net/uid-22566367-id-381958.html有所借鉴

#include <stdio.h>


#define BEGIN 1;

int main(int argc, char *argv[])
{
    int characters, lines, words, state;
    char c;

    state = characters = lines = words = 0;
    while((c = getchar()) != '0') {
        characters++;
        if(c == '\n') {
            lines++;
            state = 0;
            continue;
        } else if(c == ' ') {
            state = 0;
            continue;
        } else if(c == '\t') {
            state = 0;
            continue;
        } else {
            if(state == 0) {
                state = BEGIN;
                words++;
            }
            continue;
        }
    }

    printf("%d characters. %d words. %d lines.\n", characters, words, lines);
}

posted on 2017-09-28 23:37  vonzc  阅读(452)  评论(0编辑  收藏  举报