Implement strcat() function in C

Write an efficient function to implement strcat function in C. Standard strcat() function appends a copy of the given C-string to the another string.

 
 

The prototype of 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 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 – Coding made easy

Here is another version of strcat –

C

Download   Run Code

Output:

Techie Delight – Coding made easy

We can also use strcpy function to implement strcat as shown below –

C

Download   Run Code

Output:

Techie Delight – Coding made easy

The time complexity of above solution is O(n) where n is the length of source string.
 

 
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