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.
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:
str1: Hello Geeks! str2: Hello Geeks! str3: copy successful str4: GfG
- 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 firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Why strcpy and strncpy are not safe to use?
- C program to copy string without using strcpy() function
- Learn C++ Programming Step by Step - A 20 Day Curriculum!
- Difference between C and Dart
- Difference between Sentinel and Counter Controlled Loop in C
- Bitonic string
- Check given string is oddly palindrome or not | Set 2
- Why do we need reference variables if we have pointers
- What are the differences between C and Embedded C?
- Similarities and Difference between Java and C++
- Macros and its types in C/C++
- How to find index of a given element in a Vector in C++
- Introduction to Complex Objects and Composition
- Draw a smiley face using Graphics in C language