Skip to content

Instantly share code, notes, and snippets.

@cynx
Last active August 29, 2015 14:02
Show Gist options
  • Save cynx/de64319608d2d288068e to your computer and use it in GitHub Desktop.
Save cynx/de64319608d2d288068e to your computer and use it in GitHub Desktop.
C Program to count words, new line and 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