Use of strncat() in C++ with examples

In this discussion, let’s clearly understand the primary purpose of the strncat() function in C++ and in which scenarios we are going to use it, with some examples.

strncat() in C++ :

C++ has a large number of built-in functions that make programming easier. One of these is the strncat() function. strncat() is a predefined function in the string.h header file that is used for string operations. It’s used to append a certain number of characters from one string(source)  to the end of another string(destination).

Syntax :

char * strncat ( char * destination, const char * source, size_t number );

When calling the strncat() function, we must pass three arguments, two of which are character arrays and one of which is an integer.

The first of these parameters is the destination string; the second is the source string, which is of the const type, which means its value does not change; and the third is a number of unsigned integral type representing the maximum number of characters from the source string that can be appended to the destination string.

Here we have 2 cases:

  1. If the length of the source string is less than the “number”, then the complete source string is appended to the end of the destination string
  2. or else only the “number” of characters from the source string is appended to the destination string.

In both scenarios, the destination string’s terminating null character is removed, and a terminating null character is appended at the end after the concatenation.

strncat() function returns a pointer to the destination string.

Examples illustrating the strncat() function :

Lets us see an example to understand how strncat() function internally works. For this let’s consider two character arrays src[50], dest[50], and a number num.

Code : 

#include <iostream>
#include <cstring>
using namespace std;
int main() 
{
    char dest[50] = "code" ;
    char src[50] = "speedy.com";
    int num = 6;
    cout<<"Before using strncat() function"<<endl;
    cout<<"destination string : "<<dest<<endl;
    cout<<"Source string : "<<src<<endl;
    strncat(dest,src,num); 
    cout<<"After using strncat() function with number as "<<num<<endl;
    cout<<"destination string : "<<dest<<endl;
    cout<<"Source string : "<<src<<endl;
    return 0;
}

 

Output : 

Before using strncat() function
destination string : code
Source string : speedy.com
After using strncat() function with number as 6
destination string : codespeedy
Source string : speedy.com

The length of the source string in this example is 10, and the maximum number of characters to be appended to the destination is 6. As a result, the first 6 characters of the source string src are appended to the destination string by replacing the destination string’s terminating null character with the first character of the source string. Finally, the function will return a pointer to the dest string, with the src string appended at the end and an additional null character at the end.

Let’s look at another example to better understand another scenario.

#include <iostream>
#include <cstring>
using namespace std;
int main() 
{
    char dest[50] = "code" ;
    char src[50] = "speedy.com";
    int num = 15;
    cout<<"Before using strncat() function"<<endl;
    cout<<"destination string : "<<dest<<endl;
    cout<<"Source string : "<<src<<endl;
    strncat(dest,src,num); 
    cout<<"After using strncat() function with number as "<<num<<endl;
    cout<<"destination string : "<<dest<<endl;
    cout<<"Source string : "<<src<<endl;
    return 0;
}

output :

Before using strncat() function
destination string : code
Source string : speedy.com
After using strncat() function with number as 15
destination string : codespeedy.com
Source string : speedy.com

The length of the source string in this example is 10, and the maximum number of characters to be appended to the destination is 15. Since the num is greater than the length of the source string the complete source string src until the terminating null character is appended to the destination string by replacing the destination string’s terminating null character with the first character of the source string.

The behavior of strncat() is undefined in two cases. They are as follows:

  • When the strings overlap.
  • When dest character array is too small to append the contents of src.

Another similar function is strcat(), but it has a small difference.

Leave a Reply

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