This is essentially a small lexing task. The solution will never be pretty but if you write your code in the following way you can be quite confident of correctness:
curState <- NONE
while(c <- getChar)
switch(curState) {
case NONE:
switch(c) {
// ....
}
break;
// .....
}
}
You can also use a data structure to store the transition function (given a state and a character, what is the next state?) but for your case just writing the code is probably the best bet.
... don't forget about your text encoding! UTF-16, right? :)
You might want to look into implementations of the Unix wc
utility:
These will be more polished and featured than what you will do in an interview, but its nice to see regardless.