[C]simple code of count input lines,words,chars

This is a simple C program which can count input lines, words and chars. But the number of words are not very strict. It likes simple wc command.
#include<stdio.h>

/*        ,    ,      my_wc.c by orangleliu */

int main()
{
    int c, nl, nw, nc, flag;
    nl = nw = nc =0;

    while((c = getchar()) != EOF)
    {
       ++nc;
       if( c == '
'
) ++nl; if( c == ' '|| c == '\t' || c == '
'
) flag = 1; else if ( flag == 1){ ++nw; flag = 0; } } printf("line %d words %d chars %d
"
, nl, nw, nc); }

res
lzz-rmbp|file # cat my_wc.c|./a.out
line 25 words 68 chars 447

좋은 웹페이지 즐겨찾기