data-structures · beginner · ~15 min

Name a colour

Map enum values to strings.

Challenge

Map a colour enum value to its lowercase name.

Task

Define enum Color { RED, GREEN, BLUE }; and implement const char *color_name(enum Color c) that returns "red", "green", or "blue". For any other value, return "unknown".

Input

One enum Color value c.

Output

A pointer to a string literal: "red", "green", "blue", or "unknown".

Example

RED                ->   "red"
BLUE               ->   "blue"
(enum Color)9      ->   "unknown"

Edge cases

  • A value outside the three defined constants returns "unknown".

Input format

One enum Color value c.

Output format

const char * literal: "red", "green", "blue", or "unknown".

Constraints

Return string literals; do not allocate.

Starter code

enum Color { RED, GREEN, BLUE };

const char *color_name(enum Color c) {
    /* TODO */
    return "unknown";
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.