String Concatenation in C – Custom Function and strcat

String concatenation in C joins two strings end-to-end into a single result. The standard library provides strcat() for this, but writing it manually teaches how C strings work: they are null-terminated char arrays, and concatenation means walking to the end of the destination string then copying source characters one by one until the null terminator. Understanding the manual approach makes strcat() easier to use correctly — and helps avoid the buffer-overflow mistakes that come from not sizing the destination array.

How It Works — Step by Step

To concatenate "Hello" and "World":

  1. Start with dest = "Hello\0" and src = "World\0".
  2. Walk i forward until dest[i] == '\0'. Now i = 5 (points past the 'o').
  3. Copy each character of src: dest[5]=’W’, dest[6]=’o’, dest[7]=’r’, dest[8]=’l’, dest[9]=’d’.
  4. Write dest[10] = '\0' to terminate the result.
  5. Result: "HelloWorld\0"

C Program for String Concatenation

/* String concatenation in C — custom function and strcat
 * Compile: gcc -ansi -Wall -Wextra concat.c -o concat */
#include <stdio.h>
#include <string.h>

#define MAX 100

void str_concat(char dest[], const char src[])
{
    int i = 0, j = 0;
    while (dest[i] != '\0')   /* find end of dest */
        i++;
    while (src[j] != '\0') {  /* append src */
        dest[i] = src[j];
        i++;
        j++;
    }
    dest[i] = '\0';           /* null-terminate */
}

int main(void)
{
    char s1[MAX], s2[MAX], buf[MAX * 2];

    printf("Enter first string:  ");
    scanf("%99s", s1);

    printf("Enter second string: ");
    scanf("%99s", s2);

    /* Method 1: custom function */
    strcpy(buf, s1);
    str_concat(buf, s2);
    printf("Custom concat: %s\n", buf);

    /* Method 2: standard strcat */
    strcpy(buf, s1);
    strcat(buf, s2);
    printf("strcat result: %s\n", buf);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra concat.c -o concat
./concat

Sample Input and Output

Test 1:

Enter first string:  Hello
Enter second string: World
Custom concat: HelloWorld
strcat result: HelloWorld

Test 2:

Enter first string:  C
Enter second string: Programming
Custom concat: CProgramming
strcat result: CProgramming

Code Explanation

  • First while loop in str_concat — advances i to the position of dest‘s null terminator. This is where the appended characters will start.
  • Second while loop — copies each character of src into dest starting at position i, incrementing both indices together.
  • dest[i] = ‘\0’ — manually terminates the result. Without this, the string would run into whatever garbage follows in memory.
  • buf[MAX * 2] — the destination buffer is sized to hold both strings. A common source of bugs is sizing the destination to only one string length; the result of concatenating two MAX-length strings needs at least MAX * 2 bytes.
  • scanf(“%99s”, s1) — the width limit prevents buffer overflow; %99s reads at most 99 characters into a 100-byte array, leaving room for the null terminator.

What This Program Teaches

  • Null-terminated strings — C strings end with '\0'; every string operation depends on finding or writing this terminator correctly.
  • Walking a string with an index — advancing an integer index until the null terminator is the fundamental C idiom for string traversal.
  • Buffer sizing — the destination must be large enough to hold the combined result; most string bugs come from undersized buffers.
  • Standard vs custom — comparing your own implementation against the library function verifies understanding and confirms the output is identical.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>