C Programming

Strncat Function in C

We use the strncat function for the concatenation of two strings. Concatenation is the concept of appending two strings together. We append one string and can add another string to the end of the string and make them one string using the strncat function. For the string concatenation, we generally use two strings – the first string represents the source string that we want to combine and the other string is the target or destination string where we store the combination of the earlier string as per our requirement. The size of the target is always kept greater than the source string. For example, if we have string 1 with memory size as “4” that has the characters stored in it as “hi” and a second string with size “12” that contains the characters “people”, if we want these strings together, we will use strncat() function. Hence, the combination “hi people“ is stored in the target string that has memory size “12” more as compared to the earlier string1.

Procedure

This article presents the sequential procedure related to the implementation of the strncat() function. We first deal with the syntax of this function and then we implement the syntax in the hypothetical examples. The procedure is represented in the following steps:

Syntax
The syntax for the strncat representation is as follows:

Strncat (x, y, size_n_characters)

The “x” in the given strncat function is the destination string. It is the string where the characters are added or included. While the “y” is the source string. We take the characters that we want to append in the destination string from this source string. The last “n” in the function represents the number of characters that we want to append in the destination string from the source string. Now that we have seen and understood the syntax for the strncat() function, let’s not wait any further and let’s implement some examples for the strncat().

Example 1:
For the very first step of the article, we continue to follow the previous heading which is “syntax” and use this syntax to execute the simplest example to verify if the strncat(), as per the previous explanation, works or not.

Open the Microsoft Visual Studios on your operating systems and start creating a new project by selecting the create “new project” from “File” in the toolbar. Since this article is meant to be written for the strncat function in the C language, we create and add the project to the C files repository to import some important libraries that C offers.

Keep in mind that we import the libraries in our program so that we may utilize the built-in functions that are offered by those libraries later in the program. After the new project is successfully created, it is time to import the two main header files – one for the implementation of functions related to strings as “string. h” and the other one for the input and output reading and displaying respectively as “stdio. h”.

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

In the next step, we declare the two strings with the data type as the “Char” character. The first string is the source string with the memory size “8” which contains the “people” characters. The other string is the destination string that has the more memory size “12” as compared to the source string which contains the “hi” characters. We want the character to be stored in the source file to append to the destination file. So, we do this by making a function call to the strncat() function. We declare the name of the source and destination files in the arguments of the function and specify the size of the characters that we need to append that would be “5” in our example.

We execute the following code in the compiler. The output looks like this:

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

int main()
{
    char a[50] = "hi";
    char b[50] = "people";
    strncat(b, a, 5);
    printf("Source string : %s\n", a);
    printf("Destination string : %s",b);

    return 0;
}

The output first displays the source files. Then, after the implementation of the strncat() method, the output displays the destination string that appended the 5 characters from the source file.

Example 2:
For the previous example, we declared and initialized both the source and the destination string with their character values. In this example, we initialize the strings with the user input. Create a C project in the C compiler and import certain header files from the C’s libraries as follows:

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

Once we import these header files, we can now easily deal with the strings and their multiple operations. The first step for the program is to declare both the source and destination strings, keeping in view that the destination file must have a memory size that is greater as compared to the source file. To initialize both these destination and source strings, we call the gets() method. After taking the user-defined strings (for both destination and source), we append the source file to the destination file. To do this, we call the strncat() method and pass it both the destination and source strings with the size of “n” characters that are needed to be appended. Then, we display this destination string. This example can be executed in the C program in the following way:

#include<stdio.h>
#include<string.h>
void main() {
    char sou[45], dest[50];
    printf("Enter the source string :");
    gets(sou);
    printf("Enter the destination string :");
    gets(dest);
    dest[2] = '\0';
    strncat(dest, sou, 2);
    strncat(dest, &sou[4], 1);
    printf(" updated destination string :");
    puts(dest);
}

The output first takes the user-defined strings and then display them. Then, it concatenates both the strings and display it again in the form of the updated destination string.

Conclusion

This article represents the core explanation of the concept of the string concatenation function as strncat() in the C language. We covered the syntax for this function and executed two different examples representing the two different cases of string declaration. This article will help you with understanding about strncat().

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.