Rewrite C Function: strncat in C

Last Updated : 10 July, 2022 · 4 min read

articles banner image

In C programming language, string manipulation is not an easy task. Concatenating or adding strings together can prove to be a challenge, too. Luckily, there are implemented library functions in C language that make programmers life a bit easier. In this article, we will review the strncat function. This function is defined in the standard string.h library. Below you will find a non-standard yet effective implementation of the strncat function, too.

After reading this article, you will:

Structure of strncat:

Declaration: char *strncat(char *dest, const char *src, size_t n);

Return: Pointer to destination array (char *dest).

Parameter list:
(char *dest) - destination array to which elements are copied.
(char *src) - source array from which elements are copied.
(size_t n) - number of elements to be copied.

Header file: <string.h>.

1. The strncat function in C

The problem that this function helps to solve is of string (or character array) concatenation. If you have a character array that has more memory than it uses. And you would like to use this memory instead and replace unused bytes with desired characters, then strncat is a perfect candidate. To see this in a more concrete example, consider this code:

Example 1: Usage of the strncat in C.

# include <stdio.h>
# include <string.h>
int main ()
{
    
char dest[24] = "Practice makes " ;
    
char dest2[24] = "Practice makes " ;
    
char *src = "perfect!" ;
    
strncat (dest, src, 5);
    
strncat (dest2, src, 8);
    
printf ( "When n is 5: %s\n" , dest);
    
printf ( "When n is 8: %s\n" , dest2);
    
return 0;
}
					

Possible output:

When n is 5: Practice makes perfe
When n is 8: Practice makes perfect!

From the example above, we can already make a couple of deductions about the behavior of this function. (1) The strncat function modifies the destination array that we pass as the first argument. (2) Number of elements that are copied from the source string depend on the third argument. When we passed 5 as an argument (on line 8), we copied perfe. When we passed 8 as an argument (on line 9), we copied perfect!.

How lovely this behavior may seem, there are a couple of drawbacks that are worthy of knowing before you add the strncat function to your project. The drawbacks manifest when (1) the destination and the source arrays overlap in memory, (2) when the destination array does not have enough memory to contain characters from the source string.

2. How to implement the strncat function in C ?

In order to implement the strncat function, we would need to replicate the following behaviors:

Example 2: Implemented strncat function in C.

# include <stdio.h>
# include <string.h>
char *our_strncat ( char *dst, const char *src, size_t n)
{
    
if (dst == 0 || src == 0)
    
{
        
return 0;
    
}
    
size_t i = strlen (dst);
    
int a = 0;
    
size_t b = i;
    
while (a < n && src[a] != '\0' )
    
{
        
dst[i++] = src[a++];
    
}
    
dst[i] = '\0' ;
    
return (dst);
}
					
int main ()
{
    
char dest[24] = "Practice makes " ;
    
char dest2[24] = "Practice makes " ;
    
char *src = "perfect!" ;
    
our_strncat (dest, src, 5);
    
our_strncat (dest2, src, 8);
    
printf ( "When n is 5: %s\n" , dest);
    
printf ( "When n is 8: %s\n" , dest2);
    
return 0;
}
					

Possible output:

When n is 5: Practice makes perfe
When n is 8: Practice makes perfect!

Code snippet above demonstrates one of many ways how the strncat function can be implemented and rewritten. With this implementation you can concatenate character arrays, given, of course, that appropriate arguments are passed to this function.

3. Quiz of reader's knowledge

Quiz : strncat in C

1What is the return type of the strncat function?

2What causes undefined behaviour in strncat?

3What does the strncat function help with?

quiz completed!

Congrats!

Feel free to share your achievement below!