Project-Scylla/KNR C/ex1_10-5.c

27 lines
581 B
C
Raw Permalink Normal View History

#include <stdio.h>
/*Rewriting the wc program from pge 21/22 of KNR C, because the book does it in a way that I think is silly, and because I can.*/
int nl, nw, nc, state = 0;
#define IN 1 /*State when selected character is in a word*/
#define OUT 0 /*State when selected character is a newline, space, or tab (outside a word)*/
int main(){
for(int a = getchar(); a != EOF; a = getchar()){
++nc;
if(a == '\t' || a == ' ' || a == '\n'){
state = OUT;
if (a == '\n')
++nl;
} else {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
return 0;
}