Prove capacity before strcpy and strcat

Write int join(char out[], size_t cap, const char left[], const char right[]). Inputs are valid null-terminated strings; out is a separate writable array that does not overlap either input, and cap is its actual capacity. Concatenate left and right with no separator only when both strings plus one terminator fit. Return 1 on success. On failure return 0 and, when cap>0, leave out as the empty string; with cap=0 do not access out. The function must also handle empty strings. Use strlen, strcpy and strcat only after the relevant capacity reasoning. Avoid adding two potentially large size_t lengths in the check. For a full driver, read two tokens of 1..15 characters using bounded input, join into char out[8], and print OK|result or NO ROOM. The driver’s stated input contract excludes longer tokens. Explain the exact-fit boundary.


