The C library strcpy() function accepts two parameter which copies the string pointed to, by src to dest. This function is essential for maintaining and upgrading of older system.
SyntaxFollowing is the syntax of C library strcpy() function −
char *strcpy(char *dest, const char *src)Parameters
This function accepts the following parameters −
dest − This is the pointer to the destination array where the content is to be copied.
src − This is the string to be copied.
Ensure that the destination array has enough space to hold the source string, containing the null terminator.
Return ValueThis returns a pointer to the destination string dest.
Example 1Following is the basic C program that shows the usage of strcpy() function.
#include <stdio.h> #include <string.h> int main () { char src[40]; char dest[100]; memset(dest, '\0', sizeof(dest)); strcpy(src, "This is tutorialspoint.com"); strcpy(dest, src); printf("Final copied string : %s\n", dest); return(0); }Output
On execution of above code, we get the following output −
Final copied string : This is tutorialspoint.comExample 2
Below the example shows the usage of custom string copy without the use of strcpy().
#include <stdio.h> void customStrcpy(char* dest, const char* src) { while (*src) { *dest = *src; dest++; src++; } *dest = '\0'; } int main() { char source[] = "string Copy!"; char destination[20]; customStrcpy(destination, source); printf("The custom copied string: %s\n", destination); return 0; }Output
After executing the above code, we get the following output −
The custom copied string: string Copy!Example 3
Here, we define the two strings variable − source and destination. Then apply strcpy() which accepts both these variable to determine the result of copy string.
#include <stdio.h> #include <string.h> int main() { char source[] = "Hello, World!"; char destination[20]; strcpy(destination, source); printf("The result of copied string: %s\n", destination); return 0; }Output
The above code produces the following output −
The result of copied string: Hello, World!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4