Implement strcpy() function in C

Write an efficient function to implement strcpy function in C. Standard strcpy() function copy given C-string to another string.


 
 

The prototype of strcpy is:

char* strcpy(char* destination, const char* source);

The C99 standard adds the restrict qualifiers to the prototype:

char* strcpy(char* restrict destination, const char* restrict source);

 
The strcpy() function copies the null-terminated C-string pointed to by source to the memory pointed to by destination. The memory allocated to destination should be large enough to copy the source string (including the terminating null character). Source and destination should not overlap with each other. The strcpy() function finally returns the pointer destination.

C

Download   Run Code

Output:

Techie Delight

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

 

Shorter Version:

 

We can replace above lines of code by below single line. It will also copy the C-string pointed by source into the array pointed by destination including the terminating null character.

 
Run Code

 
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