basics · intermediate · ~15 min

Count words

Track transitions between two states while scanning.

Challenge

Implement int count_words(const char *s) returning the number of whitespace-separated words. Use a small in-word/out-of-word state machine.

Starter code

int count_words(const char *s) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.