Updated April 15, 2023
Definition of C++ String Copy
String copy is a function in C++ which is used for copying one entire string to another string seamlessly without making many changes and efforts. string copy in C++ is part of the standard library function which makes the content of one string, in fact, the entire string copied into another string. Unlike C where the string copy function is present in the string. h header file, In C++ the string copy function is present in the cstring header file. All the contents present within the source string gets fully copied in the destination string using string copy easily.
Syntax:
The syntax flow for the C++ String Copy is as shown:
string_1.copy(string_2,len_gth);
string_1.copy(string_2,len_gth,posi_tion);
string_1 and string_2 are the two objects which are considered as the source and the destination strings. Let’s see how the Sting copy functions with this.
Parameters:
The parameters passed signifies the following:
- String_2 is the destination string which is the destination object comprising of the data and it persists the copies string from the source string.
- String_1 represents the source string.
- len_gth represents the length of the substring in the entire string.
- posi_tion tells for the position of the very first position of the character to include its value.
How to Copy String in C++?
Copy string functionality is the same as other functionality in C++ despite being the fact that in C standard library containing the Copy functionality is present within the string. header whereas in C ++ the header function comes with the standard library function of cstring.h. The method must mandatorily contain the source string and the destination string as part of the argument for manipulation and for copying the content entirely from source to destination. The source string is the string that will get copied into the destination string. Destination string is the string that will comprise of the content that will get copied or the entire string from the source string. Even the parameters include the length which means the substring for the string defined in the source or the destination string. Also, the parameter may include the position from where the string value will get fetched.
There is a misconception that the source string gets appended to the destination string, but the fact is completely different it never works in this fashion rather it copies the entire content without any appending from the source string to the destination string. Even the source string does not get altered or changed which means it not at all get affected and it remains the same as earlier. The return value for the copied string in C++ includes a number of characters that get copied. The behaviors remain undefined if in case the memory allocated for the destination pointer pointing towards the destination string is not that much larger to accommodate all the content from the source string. Moreover, the string should not be in a format where they will overlap each other. Both string functions must be kept in mind while the content copy or content manipulation.
There is a myth being created by many programmers that the strncpy and strcpy function can be used alternatively but it is not at all true in case the memory allocation size gets increased or decreased then both the strcpy and strncpy functions will be used simultaneously but this is wrong. The strcpy() function satisfies the condition where the source string gets copied to the destination string and if the memory allocation size of the destination string is more than the source string then the source string easily gets copied to the destination string including terminating null characters and thus another case gets arise where the destination string might get less memory allocation comparatively then, in this case, the terminating null character will be skipped and the content from the source string to the destination string will be copied which means the terminating null character will get neglected. Also, the string will not overlap the destination string thus the destination string must have more space with proper memory allocation.
Examples of C++ String Copy
Following are the examples are given below:
Example #1
This program demonstrates the copying of the contents of the source string to the destination string using the strcpy() function which is part of the standard library function of string.h header file as shown in the output.
Code:
using namespace std;
intmain()
{
string src_str1 = "Learning_New_Language";
char dest_on[30];
cout<<"Present_source_strng : "<<src_str1<<'\n';
src_str1.copy(dest_on,sizeof src_str1);
cout<<"Final_string_dest:"<<dest_on;
return 0;
}
Output:
Example #2
This program demonstrates the copying of the string when the position of the character within the string gets passed from the parameter. It copies all the content from the source to destination and gets the value even after passing the parameter as shown in the output.
Code:
#include<iostream>
using namespace std;
intmain()
{
string str = "copy_str_func";
char str_p[19] ;
str.copy(str_p,3,7);
cout<<"Content_Of_String : " <<str_p;
return 0;
}
Output:
Example #3
This program demonstrates the strcpy() function which represents that the memory allocation for the destination string should be more so that the entire content for the source string gets copied to the destination string seamlessly as shown in the output.
Code:
#include <cstring>
#include <iostream>
using namespace std;
intmain()
{
char sr_1[] = "Welcome_Everyone !";
char dest_n[50];
strcpy(dest_n,sr_1);
cout<<dest_n;
return 0;
}
Output:
Note: There is a major problem with strcpy() function which is like the destination string array does not specify the size of the entire array which is risky for the memory allocation as the array with large character array will not get accommodated properly and then it will create a problem for the buffer and other functioning of the array related to the memory because most often this situation arises.
Conclusion
The strcpy() function in C++ is used mostly for copying of the content of the source string to the destination string without hampering the content of the source string and then putting all the content without changing any content in the source string. It provides flexibility and eases to the programmers.
Recommended Articles
This is a guide to C++ String Copy. Here we also discuss the definition and How to Copy String in C++? along with different examples and its code implementation. You may also have a look at the following articles to learn more –