Skip to content

Instantly share code, notes, and snippets.

@ardeshir
Created November 5, 2013 20:04
Show Gist options
  • Save ardeshir/7325243 to your computer and use it in GitHub Desktop.
Save ardeshir/7325243 to your computer and use it in GitHub Desktop.
Stepping into states
#include <stdio.h>
enum states { before, inside, after };
void step(enum states *state, int c)
{
if(c == '\n') {
putchar('\n');
*state = before;
} else
switch(*state) {
case before:
if(c != ' ') {
putchar(c);
*state = inside;
}
break;
case inside:
if(c == ' ') {
*state = after;
} else {
putchar(c);
}
break;
case after:
break;
}
}
int main(void)
{
int c;
enum states state = before;
while((c = getchar()) != EOF) {
step(&state, c);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment