Home » C++ String copy() function

C++ String copy() function

by Online Tutorials Library

C++ String copy()

This function copies the contents of one string into another string.

Syntax

Suppose str1 and str2 are two string objects, len is the length of substring. We want to copy string str1 into the string object str2 then the syntax would look like:

Parameter

str2: str2 is the destination string object which retains the copied string.

len: It defines the length of substring.

pos: It determines the position of first character to include.

Return value

It returns the number of characters to be copied.

Example 1

Let’s see a simple example of copying the string into another string object.

Output:

source string is: TutorAspire tutorial  destination string is: TutorAspire tutorial   

In this example, source string contains the value “tutoraspire tutorial” and we copy the source string to the destination string using copy function.

Example 2

Let’s see a simple example of copying the string when position is passed in a parameter.

You may also like