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.
s is a null-terminated string with at least one character (s[0] != '\0'). Never NULL in tests.
char first_char(const char *s) {
/* TODO */
return 0;
}
Trying to return a pointer (char*) instead of the character value (char).
Single-character string, string starting with whitespace, string starting with punctuation.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.