networking · intermediate · ~15 min

SIP status code

Parse a SIP response status.

Challenge

A SIP response line mirrors HTTP: VERSION SP CODE SP REASON, e.g. SIP/2.0 200 OK. Extract the numeric status code.

Task

Implement int sip_status_code(const char *line) that returns the numeric status from a SIP response line — the number after the first space.

Input

A SIP response line line.

Output

Returns the numeric status code, or -1 if line does not start with SIP/.

Example

sip_status_code("SIP/2.0 200 OK")          ->   200
sip_status_code("SIP/2.0 404 Not Found")   ->   404
sip_status_code("garbage")                 ->   -1

Edge cases

  • A line not beginning with SIP/: return -1.

Input format

A SIP response line.

Output format

The numeric status code, or -1 if the line does not start with 'SIP/'.

Constraints

Require the 'SIP/' prefix; parse the integer after the first space.

Starter code

#include <string.h>
#include <stdlib.h>

int sip_status_code(const char *line) {
    /* TODO */
    return -1;
}

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