Kodewick
Start learning →
Basic←0 / 3

Count a letter

#003BasicStringsLoops4 min

Write count_char(s, c) to count how many times the character c is in the string s. Upper and lower case are different letters. A C string ends with '\0'.

Examples
count_char("banana", 'a')→3
count_char("", 'x')→0
The functionint count_char(const char *s, char c)
main.c
int count_char(const char *s, char c) {
    int n = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        // your code here: if this character is c, add 1 to n
    }
    return n;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.