strcpy() is a standard library function in C/C++ and is used to copy one string to another. In C it is present in string.h header file and in C++ it is present in cstring header file.
Syntax:
char* strcpy(char* dest, const char* src);
Paramters: This method accepts following paramters:
- dest: Pointer to the destination array where the content is to be copied.
- src: string which will be copied.
Return Value: After copying the source string to the destination string, the strcpy() function returns a pointer to the destination string.
Below program explains different usages of this library function:
C
// C program to illustrate // strcpy() function ic C/C++#include<stdio.h>#include<string.h>int main (){ char str1[]="Hello Geeks!"; char str2[] = "GeeksforGeeks"; char str3[40]; char str4[40]; char str5[] = "GfG"; strcpy(str2, str1); strcpy(str3, "Copy successful"); strcpy(str4, str5); printf ("str1: %s\nstr2: %s\nstr3: %s\nstr4: %s\n", str1, str2, str3, str4); return 0;} |
Output:
str1: Hello Geeks! str2: Hello Geeks! str3: Copy successful str4: GfG
Important Points
- This function copies the entire string to the destination string. It doesn’t append the source string to the destination string. In other words, we can say that it replaces the content of destination string by the content of source string.
- It does not affect the source string. The source string remains same after copying.
- This function only works with C style strings and not C++ style strings i.e. it only works with strings of type char str[]; and not string s1; which are created using standard string data type available in C++ and not C.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C program to copy string without using strcpy() function
- Why strcpy and strncpy are not safe to use?
- Bug Tracking System
- C Program to sort rows of the Matrix
- Order of execution in initializer list in C++
- Unusual behaviour with character pointers
- Header Guard in C++
- Advantages and Disadvantages of Array in C
- Can we write a print statement within if parentheses?
- If memory allocation using new is failed in C++ then how it should be handled?
- Draw an ellipse divided by straight line into two colored part in C++ Graphics
- Types of C files after its compilation
- What happens if we mix new and free in C++?
- VS Code | Build, Run and Debug in C++
Improved By : oliverelarsen

