pointers-memory · beginner · ~8 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. The terminating NUL is included in the search. 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.Drill: practice one contract before combining it with other skills.
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.
s is non-NULL and points to a live NUL-terminated string. Search includes the terminator. Return a borrowed pointer into s or NULL; do not modify or free input.
#include <stddef.h>
const char *find_char(const char *s, char c) {
/* TODO */
return NULL;
}
Stopping before checking whether the terminator itself is the requested character.
Empty string; missing character; repeated character returns first match; searching for the zero character returns the terminator.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.