Write an efficient function to implement strncat() function in C.

The prototype of the strncat() is:

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

The standard strncat() function appends first num characters of a given C-string to another string. The C99 standard adds the restrict qualifiers to the prototype:

char* strncat(char* restrict destination, const char* restrict source, size_t num);

 
The strncat() function appends the first num characters of the null-terminated string pointed by the source to the null-terminated string pointed to the destination. The first character of the source overwrites the null-terminator of destination. The function returns the pointer to the destination string.

The source should not overlap with the destination, and the destination should be large enough to contain the concatenated resulting string, including the additional null-character.

C


Download  Run Code

Output:

Techie Delight

 
Here’s another version of strncat():

C


Download  Run Code

Output:

Techie Delight

 
The time complexity of the above solution is O(num).

That’s all about strncat() implementation in C.