Introduction to strcat function in C with example

How to implement strcat function in C:

strcat is used to copy one string from one variable to another. This method is defined as below :

char* strcat(char* des, char* source)

It copies all the content or string pointed by the char pointer source to the char pointer des. The string used is null terminated i.e. we have one \0 at the end of the string.

In this post, we will learn how to implement strcat method, i.e. we will write one method that takes two parameters, and copy the string from one to another.

One more thing we need to keep in mind is that if we call strcat multiple times with the same destination, it keeps appending the strings to the destination.

Implementation of strcat using loop:

The below program shows how we can implement strcat in C using a while loop:

#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 100

void stringcat(char *dest, const char *source)
{
    char *currentPointer = dest + strlen(dest);

    while (*source != '\0')
    {
        *currentPointer = *source;
        *currentPointer++;
        *source++;
    }

    *currentPointer = '\0';
}

int main()
{
    char *destinationStr = (char *)malloc(MAX_LENGTH * sizeof(char *));

    stringcat(destinationStr, "Hello World");
    stringcat(destinationStr, "Hello World");

    puts(destinationStr);
}

Here,

  • stringcat is the method used in place of strcat.
  • It takes two pointers, the first one is the destination string pointer and the second one is the source string pointer.
  • At first, we are defining one variable currentPointer that points to the end of the destination string. At first, it is the start point of destination string.
  • We are using one while loop to iterate through the source string to the end. Inside the loop, we are copying the values of source to currentPointer i.e. destination.
  • Once the iteration is completed, we are assigning the value of currentPointer as \0 i.e. the end of the string.

It will print the below output :

Hello WorldHello World

As you can see, the second string is appended to the first string.

Method 2: By using strcpy :

strcpy method is used to copy string from source to destination. It is defined as below :

char* strcpy(char* dest, const char* source)

This method is similar to strcat. It is used to copy one string from a source pointer to a destination pointer. But the only difference is that it will not append the strings to the current string of destination pointer. For example :

#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 100

int main()
{
    char *destinationStr = (char *)malloc(MAX_LENGTH * sizeof(char *));

    strcpy(destinationStr, "Hello World");
    strcpy(destinationStr, "Hello World");

    puts(destinationStr);
}

This will print:

Hello World

We can write our own function that will work like strcat using strcpy as like below :

#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 100

void stringcat(char *dest, const char *source)
{
   strcpy(dest + strlen(dest), source);
}

int main()
{
    char *destinationStr = (char *)malloc(MAX_LENGTH * sizeof(char *));

    stringcat(destinationStr, "Hello World");
    stringcat(destinationStr, "Hello World");

    puts(destinationStr);
}

If you run this program, it will print :

Hello WorldHello World