pointers-memory · intermediate · ~15 min

Implement atoi

String → integer conversion, the classic interview problem.

Challenge

Implement int my_atoi(const char *s) that converts a string of optional spaces, an optional +/- sign, and decimal digits to an int. Stop at the first non-digit. Do not handle overflow (assume the input is in range).

Starter code

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

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