Implement strcat() function in C
Write an efficient function to implement strcat() function in C. The standard strcat() function appends the copy of a given C-string to another string.
The prototype of the strcat() is:
char* strcat(char* destination, const char* source);
The C99 standard adds the restrict qualifiers to the prototype:
char* strcat(char* restrict destination, const char* restrict source);
The strcat() function appends a copy 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 37 38 39 |
#include <stdio.h> #include <string.h> #include <stdlib.h> // Function to implement `strcat()` function in C char* my_strcat(char* destination, const char* source) { // 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') { *ptr++ = *source++; } // null terminate destination string *ptr = '\0'; // the destination is returned by standard `strcat()` return destination; } // Implement `strcat()` function in C int main() { char* str = (char*)calloc(100, 1); my_strcat(str, "Techie "); my_strcat(str, "Delight "); my_strcat(str, "– "); my_strcat(str, "Ace "); my_strcat(str, "the "); my_strcat(str, "Technical "); my_strcat(str, "Interviews"); puts(str); return 0; } |
Output:
Techie Delight – Ace the Technical Interviews
Here’s another version of strcat():
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 41 42 |
#include <stdio.h> #include <stdlib.h> // Function to implement `strcat()` function in C char* my_strcat(char* destination, const char* source) { 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 characters of the source to the destination string for (j = 0; source[j] != '\0'; j++) { destination[i + j] = source[j]; } // null terminate destination string destination[i + j] = '\0'; // the destination is returned by standard `strcat()` return destination; } // Implement `strcat()` function in C int main() { char* str = (char*)calloc(100, 1); my_strcat(str, "Techie "); my_strcat(str, "Delight "); my_strcat(str, "– "); my_strcat(str, "Ace "); my_strcat(str, "the "); my_strcat(str, "Technical "); my_strcat(str, "Interviews"); puts(str); return 0; } |
Output:
Techie Delight – Ace the Technical Interviews
We can also use strcpy() function to implement strcat(), as shown below:
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 |
#include <stdio.h> #include <string.h> #include <stdlib.h> // Function to implement `strcat()` function in C char* my_strcat(char* destination, const char* source) { strcpy(destination + strlen(destination), source); return destination; } // Implement `strcat()` function in C int main() { char* str = (char*)calloc(100, 1); my_strcat(str, "Techie "); my_strcat(str, "Delight "); my_strcat(str, "– "); my_strcat(str, "Ace "); my_strcat(str, "the "); my_strcat(str, "Technical "); my_strcat(str, "Interviews"); puts(str); return 0; } |
Output:
Techie Delight – Ace the Technical Interviews
The time complexity of the above solution is O(n), where n is the length of the source string.
That’s all about strcat() 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 :)