basics · intermediate · ~15 min

Parse two integers

Tokenise a small input line.

Challenge

Parse a line containing two space-separated non-negative integers.

Task

Implement int parse_pair(const char *s, int *a, int *b) that parses two non-negative integers from s, separated by a single space (e.g. "12 34"). On success, store them in *a and *b and return 1. Return 0 if the format is wrong (wrong separator, negative numbers, missing values, or trailing junk). No main — the grader calls it.

Input

A NUL-terminated string s, and two pointers a and b for the results.

Output

Returns 1 on success (with *a and *b set); 0 on any invalid input.

Example

parse_pair("12 34", &a, &b)   ->   1,   a == 12, b == 34
parse_pair("0 7", &a, &b)     ->   1,   a == 0,  b == 7
parse_pair("12-34", &a, &b)   ->   0    (wrong separator)

Edge cases

  • Both numbers must be non-negative.
  • Anything other than a single space between them, or trailing characters, fails.

Rules

  • Enforce the exact format — reject malformed input rather than guessing.

Input format

A NUL-terminated string s, and two int pointers a and b.

Output format

1 on success (with *a and *b set); 0 on invalid input.

Constraints

Two non-negative ints separated by a single space; reject anything else.

Starter code

int parse_pair(const char *s, int *a, int *b) {
    /* TODO */
    return 0;
}

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