basics · beginner · ~15 min

Count a character in a string

Scan a string accumulating a count.

Challenge

Implement int char_count(const char *s, char c) returning how many times c appears in s.

Starter code

int char_count(const char *s, char c) {
    /* TODO */
    return 0;
}

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