Last active
August 29, 2015 14:02
-
-
Save cynx/de64319608d2d288068e to your computer and use it in GitHub Desktop.
C Program to count words, new line and characters
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
//count words, new line and characters | |
#include <stdio.h> | |
#define IN 1 | |
#define OUT 0 | |
int main() | |
{ | |
int c, nc, nl, nw, state; | |
state = OUT; | |
nc = nl = nw = 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",nc,nl,nw); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment