pointers-memory · beginner · ~15 min
Return a pointer into a string.
Find the first occurrence of a character in a string and return a pointer to it, exactly like the standard strchr.
Implement const char *find_char(const char *s, char c) that returns a pointer to the first c in s, or NULL if c does not appear. No main — the grader calls it.
s — a NUL-terminated string. c — the character to search for.
Returns a pointer into s at the first match, or NULL if c is absent.
find_char("hello", 'l') -> pointer to "llo" (index 2)
find_char("hello", 'h') -> pointer to start
find_char("hello", 'z') -> NULL
s itself.s — a NUL-terminated string; c — the character to find.
A pointer to the first occurrence, or NULL if absent.
#include <stddef.h>
const char *find_char(const char *s, char c) {
/* TODO */
return NULL;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.