Skip to content

Instantly share code, notes, and snippets.

@18z
Created August 25, 2015 18:12
Show Gist options
  • Save 18z/ff1fed576a6f6850ea04 to your computer and use it in GitHub Desktop.
Save 18z/ff1fed576a6f6850ea04 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
int main(){
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c = '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment