basics · beginner · ~15 min

Copy a string

Copy bytes until and including the NUL.

Challenge

Copy one C string into a destination buffer, terminator included.

Task

Implement void str_copy(char *dst, const char *src) that copies the string src into dst, including the terminating NUL byte. dst is guaranteed large enough. No main — the grader calls it.

Input

A destination buffer dst (big enough), and a NUL-terminated source string src.

Output

No return value. After the call, dst holds a copy of src.

Example

str_copy(dst, "hello")   ->   dst == "hello"
str_copy(dst, "")        ->   dst == ""
str_copy(dst, "abc")     ->   dst == "abc"

Edge cases

  • The empty string copies just the NUL byte.

Rules

  • Copy the terminating '\0' too, or dst won't be a valid string.

Input format

A destination buffer dst (large enough), and a NUL-terminated source src.

Output format

No return value; dst is filled with a copy of src.

Constraints

dst is large enough; copy the terminating NUL.

Starter code

void str_copy(char *dst, const char *src) {
    /* TODO */
}

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