Implement strncat() function in C
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <stdio.h> #include <string.h> #include <stdlib.h> // Function to implement `strncat()` function in C char* my_strncat(char* destination, const char* source, size_t num) { // make `ptr` point to the end of the destination string char* ptr = destination + strlen(destination); // Appends characters of the source to the destination string while (*source != '\0' && num--) { *ptr++ = *source++; } // null terminate destination string *ptr = '\0'; // destination string is returned by standard `strncat()` return destination; } // Implement `strncat()` function in C int main() { char dest[30]; strcpy(dest, "Techie "); char src[] = "Delight – Implement strncat"; my_strncat(dest, src, 7); puts(dest); return 0; } |
Output:
Techie Delight
Here’s another version of strncat():
C
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <stdio.h> #include <string.h> #include <stdlib.h> // Function to implement `strncat()` function in C char* my_strncat(char* destination, const char* source, size_t num) { int i, j; // move to the end of the destination string for (i = 0; destination[i] != '\0'; i++); // `i` now point to terminating null character in the destination // appends `num` characters of the source to the destination string for (j = 0; source[j] != '\0' && j < num; j++) { destination[i + j] = source[j]; } // null terminate destination string destination[i + j] = '\0'; // the destination is returned by standard `strncat()` return destination; } // Implement `strncat()` function in C int main() { char dest[30]; strcpy(dest, "Techie "); char src[] = "Delight – Implement strncat"; my_strncat(dest, src, 7); puts(dest); return 0; } |
Output:
Techie Delight
The time complexity of the above solution is O(num).
That’s all about strncat() implementation in C.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)