32 lines
811 B
C
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 );
|
|
} |