strcat and strncat in C: solved exercise

strcat and strncat in C: solved exercise

If you searched for a solved strcat and strncat exercise in C, here are the essential patterns: concatenating two strings with strcat, safe bounded concatenation with strncat, and building strings step by step without buffer overflow.

strncat(dst, src, n) appends at most n characters from src to the end of dst and always null-terminates. The n parameter is the number of characters to copy from the source, not the total size of the destination buffer.

Problem statement

  1. Build the string "Hello, world!" by concatenating parts with strcat.
  2. Repeat using strncat with a character limit.
  3. Show how to correctly calculate remaining space before calling strncat.

C solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <string.h>

int main(void) {
    char buf[32];

    /* 1. Basic strcat */
    strcpy(buf, "Hello");
    strcat(buf, ", ");
    strcat(buf, "world");
    strcat(buf, "!");
    printf("strcat:  \"%s\"\n", buf);

    /* 2. strncat with limit */
    char dst[16] = "C is ";
    strncat(dst, "powerful and fast", 8); /* append at most 8 chars */
    printf("strncat: \"%s\"\n", dst);

    /* 3. Safe remaining-space calculation */
    char result[20] = "";
    const char *parts[] = {"one", "-", "two", "-", "three"};
    for (int i = 0; i < 5; i++) {
        size_t remaining = sizeof(result) - strlen(result) - 1;
        strncat(result, parts[i], remaining);
    }
    printf("safe:    \"%s\"\n", result);

    return 0;
}

Expected output

1
2
3
strcat:  "Hello, world!"
strncat: "C is powerful"
safe:    "one-two-three"

Common mistakes

  • Passing the total buffer size as n to strncat: n is the number of characters to copy from the source, not the available space. You must compute sizeof(dst) - strlen(dst) - 1 to get the real remaining capacity.
  • Not reserving enough space in dst before strcat: if dst is too small, a buffer overflow occurs.
  • Confusing strncat with strncpy: strncat always appends \0; strncpy does not if n < strlen(src).
  • Chaining many strcat calls instead of using snprintf: snprintf is cleaner, safer, and more efficient for building composite strings.

Practical use

strncat is used to incrementally build strings with fixed-size buffers: filesystem paths, protocol messages, or HTML fragments. In new code, snprintf is preferred for building the entire string at once.

Guided practice and full book

If you want a complete path with progressive difficulty:

FAQ

What is the exact difference between strcat and strncat?

strcat(dst, src) copies all characters from src up to and including its \0 onto the end of dst, with no limit. strncat(dst, src, n) copies at most n characters from src and always appends \0. The difference is safety: strncat lets you control how much is appended.

Why is strncat’s n parameter not the destination buffer size?

By historical C design. n tells how many characters to take from the source. To compute the real available space in dst, calculate sizeof(dst) - strlen(dst) - 1 and pass that as n.

When is snprintf better than strncat?

Almost always. snprintf(buf, sizeof(buf), "%s%s", part1, part2) is clearer, safer, and more efficient than multiple strncat calls. Prefer strncat only when appending a fragment to an existing string and the overhead of snprintf is unacceptable.