data-structures · beginner · ~15 min
Count a character by walking the string.
Implement:
int count_char(const char *s, char c);
Return how many times c occurs in s, computed recursively.
A string and a character.
Occurrence count.
One character per call.
#include <stddef.h>
/* Count occurrences of character c in string s, computed recursively. */
int count_char(const char *s,char c){ (void)s;(void)c; return 0; }
Forgetting the NUL base case; not advancing the pointer.
Empty string returns 0.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.