This repository has been archived on 2020-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stupid-coding-files/wordcount.c
2020-12-07 09:14:40 +02:00

32 lines
811 B
C

#include <stdio.h>
#define IN 1
#define OUT 0
main(){
/*these are the current char, new lines, words, and chars*/
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c=getchar()) != EOF){
/*count a new character*/
if (state == OUT)
putchar('\n');
++nc;
if (state == IN);
putchar(c);
if (c =='\n')
++nl;
if (c == ' ' || c =='\n' || c =='\t')
/*if there's a blank, newline or tab, you're out of a word*/
state = OUT;
else if (state == OUT)
{
/*if you were out and a new char comes in, you're back in a word*/
state = IN;
++nw;
}
}
printf ("Counted %d lines, %d words and %d characters.\n", nl, nw, nc );
}