C++ String::copy() function



The C++ std::string::copy is used to copy a specified number of characters from a string object into a character array. It takes three parameters: a pointer to the destination character array, the number of characters to copy, and the starting position in the source string.

This function doesn't include a null character at the end of the copied content.

Syntax

Following is the syntax for std::string::copy() function.

size_t copy (char* s, size_t len, size_t pos = 0) const;

Parameters

  • s − It indicates the pointer to an array of characters.
  • len − It indicates the number of characters to copy.
  • pos − It indicates the position of the first character to be copied.

Return value

It does not have any return value.

Exceptions

If an exception is thrown, there are no changes in the string.

Example 1

Following is an example to find the std::string::copy using C++.

Open Compiler
#include <iostream> #include <string> int main() { char buffer[20]; std::string str("Sairamkrishna Mammahe..."); std::size_t length = str.copy(buffer, 6, 5); buffer[length] = '\0'; std::cout << "buffer contains: " << buffer << '\n'; return 0; }

Output

Let us compile and run the above program, this will produce the following result −

buffer contains: mkrish        

Example 2

In the program we have initialized the stringX1, stringX2 and stringX5 by assigning the value and declared the stringX3 and stringX4 tells the existence of entity and its location.

Open Compiler
#include <iostream> #include <cstring> using namespace std; int main() { char stringX1[] = " Good morning "; char stringX2[] = " Tutorialspont Company "; char stringX3[20]; char stringX4[20]; char stringX5[] = " TPC "; strcpy(stringX2, stringX1); strcpy(stringX3, " Copy successful "); strcpy(stringX4, stringX5); cout << "stringX1: " << stringX1 << "\nstrigX2: " << stringX2 << " \nstringX3: " << stringX3 << " \nstringX4: " << stringX4; return 0; }

Output

If we run the above code it will generate the following output.

stringX1:  Good morning 
strigX2:  Good morning  
stringX3:  Copy successful  
stringX4:  TPC 

Example 3

In this program, the src string contains the value "Tutorialspoint" and we copy the source string to the target string using the copy() function.

Open Compiler
#include <iostream> #include <string> using namespace std; int main() { string src = " Tutorialspoint"; char dest[15]; cout << " source string = " << src << '\n'; src.copy(dest, sizeof src); cout << " destination string = " << dest; return 0; }

Output

Following is the output of the above code.

source string = Tutorialspoint
destination string = Tutorialspoint                 

Example 4

In the below program, we are passing the position as a parameter. So it copies the substring of string x to the string x1 using copy() function.

Open Compiler
#include <iostream> #include <string> using namespace std; int main() { string x = "Tutorialspoint company"; char x1[22]; x.copy(x1, 12, 0); x1[10] = '\0'; cout << " String x1 = " << x1; return 0; }

Output

Following is the output of the above code.

String x1 = Tutorialsp               
string.htm
Advertisements