basics · intermediate · ~15 min

Validate a number string

Encode acceptance rules as states.

Challenge

Implement int valid_number(const char *s) returning 1 if s is an optional '+'/'-' sign followed by one or more decimal digits and nothing else, else 0. Empty string is invalid.

Starter code

int valid_number(const char *s) {
    /* TODO */
    return 0;
}

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