string copy() function
Last Updated : 17 Mar 2025
In the C++ programming language, the string::copy() function is a built-in member function of the string class, which is part of the Standard Template Library (STL). The string copy() function is commonly utilized to copy a specified number of characters from a string into a character array (C-style string). It is also particularly useful when we need to interface between C++ strings (std::string) and C-style character arrays. This function copies the contents of one string into another string.

Syntax:
It has the following syntax:
In this syntax,
- str2: It represents the destination string object, which retains the copied string.
- len: It defines the length of the substring.
- pos: It determines the position of the first character to include.
Return value
It returns the number of characters to be copied.
Simple C++ String Copy() function Example
Let's consider a simple example to demonstrate the string copy() function in C++.
Output:
The Source string is: C++ Tutorial The Destination string is: C++ Tutorialpool�g��
Explanation:
In this example, we have created a string named source with the value "C++ Tutorial". After that, we have utilized the copy() function to copy characters from the string into the character array destination. Finally, it shows how to transfer the data from a C++ string object to a C-style character array, and then it prints both strings.
C++ String Class Example using the copy() function with Offset
Let us consider an example to demonstrate the string copy() function with an offset in C++.
Output:
String contains elements: rogrammi
Explanation:
In this example, we have created a string str that contains the text "C++ programming language". After that, we have utilized the copy() function to copy 8 characters starting from index 5 of str into the character array str1. Next, we manually include a null terminator ('\0') to mark the end of the copied string. As a result, the output shows the substring "rogrammi".
C++ String Class Example using the copy() function to copy elements from an array to a vector
Let's consider an example to demonstrate how to copy an array of integers into a vector using the copy() function in C++.
Output:
Explanation:
In this example, we have created an integer array 'a' that contains five elements. After that, we have utilized the std::copy() function to copy all the elements from the array a into the vector vec using iterators. Finally, we use a loop to print the copied elements of the vector, which shows that the contents of the array have been successfully transferred to the vector.
C++ String Example using the copy() and substr() function
Let us take an example to demonstrate the string using the copy() and substr() functions in C++.
Output:
Using copy(): C++ Using substr(): C++
Explanation:
In this example, we have created a string str that contains the text "C++ Programming Language". After that, we use the copy() function to copy the first three characters ("C++") from the string into the character array buffer. Next, we manually add a null terminator to mark the end. After that, the same result is achieved using the substr() function that directly creates a substring. Finally, we print the output in the console for both functions.
Features of the String copy() function in C++
There are several features of the string copy() function in C++. Some of them are as follows:
- The string::copy() function in C++ is commonly utilized to copy a specified number of characters from a string into a character array (C-style string).
- It helps to ensure that no modification can happen to the original string, and the destination buffer should have enough space to store the string.
- The string copy() function allows us to copy a specific part of the string, without requiring the complete string.
- It is very useful when we want to work with APIs or libraries that need C-style strings.
- It is mainly utilized to directly write into an existing buffer without creating a new string object.
- The copy() function returns the character counts that are copied into the destination array in C++.
Conclusion
In conclusion, the string::copy() function in C++ is a simple and effective method that enables us to copy a portion of a string into a character array, which makes it highly useful for interoperability between C++ strings and C-style strings. It also gives better flexibility to handle substrings efficiently while ensuring safe data handling. It is commonly utilized with a null terminator and a sufficient buffer size effectively.
C++ String copy() function FAQs
1) What is the String copy() function in C++?
In the C++ programming language, the string copy() function is commonly utilized to copy a specified number of characters from a string into a character array (C-style string). It is very useful if we need to interface between C++ strings (std::string) and C-style character arrays.
2) Does the string copy() function automatically include a null character in C++?
No, it doesn't automatically include a null character in C++. We have to manually include \0 if we want a null-terminated C-string.
3) What is the return type of the string copy() function in C++?
In the C++ programming language, the string copy() returns the size_t that represents the number of copied characters.
4) Is the string copy() function faster than the substr() function in C++?
Yes, the string copy() function is faster than the substr() function because the copy() function directly writes into an existing buffer without making a new string object in C++.
5) Can the copy() function work with wide strings (wstring) in C++?
Yes, the copy() function can work with wide strings (wstring) because the wstring class has a similar copy() function for the wide characters in C++.