-
-
Save ardeshir/7325243 to your computer and use it in GitHub Desktop.
Stepping into states
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> | |
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