Implement strncat() function in C

Write an efficient function to implement strncat function in C. Standard strncat() function appends appends first num characters of given C-string to the another string.

 
 

The prototype of strncat is:

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

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 first num characters of null-terminated string pointed by source to the null-terminated string pointed to the destination. The first character of source overwrites the null-terminator of destination. The function returns the pointer to the destination string.

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

C

Download   Run Code

Output:

Techie Delight

Here is another version of strncat –

C

Download   Run Code

Output:

Techie Delight

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

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz