-
-
Save fallenleavesguy/7fd6b4127da2036bd9d262f0f08bd122 to your computer and use it in GitHub Desktop.
Word Count in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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