Skip to content

Instantly share code, notes, and snippets.

@axayjha
Last active August 25, 2017 19:46
Show Gist options
  • Save axayjha/1189b279d9cf624ca3eff923aa2b56d8 to your computer and use it in GitHub Desktop.
Save axayjha/1189b279d9cf624ca3eff923aa2b56d8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define WORD 1
#define PUNC 0
int is_punc(char c)
{
if(c==' ' || c==',' || c=='\t' || c==';' || c=='.')
return 1;
return 0;
}
int main()
{
int c, wordcount=0, state=WORD;
c=getchar();
if( (!(is_punc(c))) && c!=EOF) wordcount++;
while(c!=EOF)
{
printf("%c %d %d\n", c, state, wordcount);
c=getchar();
if(is_punc(c) && state==WORD)
{
state=PUNC;
}
if( (!(is_punc(c))) && state==PUNC && c!=EOF)
{
wordcount++;
state=WORD;
}
}
printf("%d\n", wordcount);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment