Write an efficient function to implement strcpy() function in C. The standard strcpy() function copies a given C-string to another string.

The prototype of the 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 a 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 the above solution is O(n), where n is the length of the source string.

Shorter Version:

 
We can replace the above lines of code with the following 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

That’s all about strcpy() implementation in C.