Loading RustViz Engine...
Implement a lexical scanner for string tokens using a transition enum.
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
pub enum State { Start, InWord, InWhitespace }
pub fn count_words(input: &str) -> usize {
let mut state = State::Start;
let mut words = 0;
for c in input.chars() {
match state {
State::Start | State::InWhitespace => {
if !c.is_whitespace() {
state = State::InWord;
words += 1;
}
}
State::InWord => {
if c.is_whitespace() {
state = State::InWhitespace;
}
}
}
}
words
}