In C++, we often need to manipulate strings, including copying one string into another. This tutorial aims to teach you the various methods of copying strings in C++.
Understanding Strings in C++
Before we get into how to copy strings, it's essential first to grasp the concept of what strings are. In C++, a string is essentially a collection of characters organized as an array in memory. This sequence of characters concludes with a unique character, the null character ('\0
'). To illustrate, the string "Hello" is stored in memory as an array {'H', 'e', 'l', 'l', 'o', '\0'}
.
C++ Program to Copy Strings
There are various techniques for copying a string from one variable to another in C++. Let's look at some of the most widely used methods.
Using C-style Character Arrays
The simplest way to copy a string in C++ is to manually copy each character from the source string to the destination string. This method can be ineffective but is a good way to understand the concept.
Here's an example of how to do this:
#include<iostream>
#include<cstring> // for strlen()
using namespace std;
int main() {
char source[] = "Hello, world!";
int len = strlen(source); // getting length of source string
// destination string length should be one more than source
// to hold the null character at the end
char destination[len + 1];
// copying character by character
for(int i=0; i<len; i++) {
destination[i] = source[i];
}
// append null character at the end
destination[len] = '\0';
cout << "Copied string is: " << destination << endl;
return 0;
}
The above program first determines the length of the "source
" string using the strlen()
function from the <cstring>
library. Next, it creates a new character array called "destination
", one character longer than the "source
" to accommodate the null
character. Lastly, the program utilizes a loop to transfer each character from the "source
" to the "destination
".
Using the strcpy Function
A more efficient and quicker way to copy strings in C++ is to utilize the strcpy()
function, which is part of the <cstring>
library. The strcpy()
function accepts two arguments: a "destination
" string and a "source
" string, and it copies the "source
" string into the "destination
" string.
Here's an example of how to use the strcpy()
function to copy a string:
#include<iostream>
#include<cstring> // for strcpy()
using namespace std;
int main() {
char source[] = "Hello, world!";
// destination string should be large enough to hold the source string
char destination[50];
strcpy(destination, source);
cout << "Copied string is: " << destination << endl;
return 0;
}
In the above program, we created a large character array called "destination
" to ensure it can hold both the "source
" string and the null
character. Next, we utilized the function "strcpy()
" to copy the source into the destination.
Using the Assignment Operator
If you are using the string class in C++, you can use the assignment operator (=
) to copy one string to another. This method is simple and easy to use but unavailable if you're working with C-style character arrays.
Here's an example of how to use the assignment operator to copy a string:
#include<iostream>
#include<string> // for string class
using namespace std;
int main() {
string source = "Hello, world!";
string destination;
destination = source;
cout << "Copied string is: " << destination << endl;
return 0;
}
In the program above, we initialized a string object named "source
" and gave it the value "Hello, world!". Then, we created another string object named "destination
" and used the assignment operator to duplicate the content of "source
" into "destination
".
In this guide, we've explored different ways to copy strings in C++. Although copying each character manually may seem tedious, it provides a fundamental understanding of how strings work in C++. We can use the strcpy()
function or assignment operator for a more efficient approach that delivers simplicity and speed.