Last active
August 25, 2017 19:46
-
-
Save axayjha/1189b279d9cf624ca3eff923aa2b56d8 to your computer and use it in GitHub Desktop.
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
#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