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


Download  Run Code

Output:

Techie Delight – Ace the Technical Interviews

 
Here’s another version of strcat():

C


Download  Run Code

Output:

Techie Delight – Ace the Technical Interviews

 
We can also use strcpy() function to implement strcat(), as shown below:

C


Download  Run Code

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.