您的位置:首页 > 其它

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

2015-06-04 23:55 288 查看

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 == '\n' )
++nl;

if( c == ' '|| c == '\t' || c == '\n')
flag = 1;
else if ( flag == 1){
++nw;
flag = 0;
}
}

printf("line %d words %d chars %d \n", nl, nw, nc);
}


res

lzz-rmbp|file # cat my_wc.c|./a.out
line 25 words 68 chars 447
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: