Skip to content

Instantly share code, notes, and snippets.

@foobra
Created May 22, 2017 05:12
Show Gist options
  • Save foobra/5a134dd841d2b698fff80da8383e2c05 to your computer and use it in GitHub Desktop.
Save foobra/5a134dd841d2b698fff80da8383e2c05 to your computer and use it in GitHub Desktop.
word-count without FSM
#include <stdio.h>
#include <map>
#include <utility>
#include <functional>
enum STATE
{
IN,
OUT,
};
int main()
{
char c;
int count = 0; // word count
STATE state = OUT;
while (scanf("%c", &c) != EOF && c != '\n') {
if (c == ' ' || c == '\t') {
switch (state) {
case IN: {
state = OUT;
}
break;
case OUT: {
state = OUT;
}
break;
}
}
else {
switch (state) {
case IN: {
state = IN;
}
break;
case OUT: {
state = IN;
count++;
}
break;
}
}
}
printf("word count: %d\n", count);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment