basics · intermediate · ~15 min

Bounded concatenation

Append safely the way strlcat does.

Challenge

Implement int safe_concat(char *dst, int dstsz, const char *src) that appends src to the string already in dst, never exceeding dstsz-1 bytes, always NUL-terminating. Return 0 if everything fit, -1 if it was truncated.

Starter code

#include <string.h>

int safe_concat(char *dst, int dstsz, const char *src) {
    /* TODO */
    return -1;
}

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