Skip to content

Instantly share code, notes, and snippets.

@fallenleavesguy
Last active September 10, 2017 16:36
Show Gist options
  • Save fallenleavesguy/7fd6b4127da2036bd9d262f0f08bd122 to your computer and use it in GitHub Desktop.
Save fallenleavesguy/7fd6b4127da2036bd9d262f0f08bd122 to your computer and use it in GitHub Desktop.
Word Count in c
/**
* simple word count in c
*/
#include <stdio.h>
#define IN 1
#define OUT 0
int main(int argc, const char * argv[]) {
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);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment