basics · beginner · ~15 min
Index into a char array and handle the empty case.
Return the first character of a string, treating the empty string safely.
Implement char first_char(const char *s) that returns the first character of s, or '\0' if s is empty. No main — the grader calls it.
A NUL-terminated string s.
Returns the first character, or '\0' (0) when s is empty.
first_char("hi") -> 'h'
first_char("x") -> 'x'
first_char("") -> '\0'
'\0', so s[0] already covers it.A NUL-terminated string s.
The first character, or '\0' if s is empty.
char first_char(const char *s) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.