C++ Program to Copy Strings



In this article, we'll show how to write a C++ program to copy strings. A string in C++ is a sequence of characters, such as letters, numbers, symbols, or anything enclosed in double quotes (e.g., "Hello"). Copying a string means transferring all characters from one string to another or making an exact copy.

For example, here's how copying works:

Input: "Learning C++ is fun!" Output: "Learning C++ is fun!" (This is the copied string)

We can copy a string in C++ using the following methods:

Copying Strings Using strcpy() Function

In this approach, we use the built-in strcpy() function from the <cstring> library to copy the contents of one string into another. This function handles the copying internally, so the program stays simple and efficient.

Example

In this example, we first define a source string and create an empty destination array. Then, we use strcpy() to copy the contents of the source string into the destination.

Open Compiler
#include <iostream> #include <cstring> using namespace std; int main() { // Original string to be copied char source[] = "Welcome to tutorialspoint!"; // Destination array to store copied string char destination[50]; // Copy source string into destination strcpy(destination, source); cout << "Source String: " << source << endl; cout << "Destination String: " << destination << endl; return 0; }

The output of the above program shows that the source and destination strings are the same after copying.

Source String: Welcome to tutorialspoint!
Destination String: Welcome to tutorialspoint!

Time Complexity: O(n), because the strcpy copies each character from the source to the destination.

Space Complexity: O(n) because memory is used for storing both the source and destination strings, where n is the length of the string.

Copying Strings Using a Custom Loop

In this approach, we manually copy each character from one string to another using a loop until we reach the null terminator.

Example

Here's a complete C++ program where we loop through each character of the source string and copy it to the destination string. Finally, we add the null terminator at the end of the string.

Open Compiler
#include <iostream> using namespace std; int main() { // Source string to copy char source[] = "Learn C++ at tutorialspoint!"; // Destination array for the copied string char destination[50]; int i = 0; // Loop through each character and copy it while (source[i] != '\0') { destination[i] = source[i]; i++; } // Add null terminator at the end destination[i] = '\0'; cout << "Source String: " << source << endl; cout << "Destination String: " << destination << endl; return 0; }

Once you run the code, the following output shows the source string copied to the destination.

Source String: Learn C++ at tutorialspoint!
Destination String: Learn C++ at tutorialspoint!

Time Complexity: O(n) because we loop through each character of the source string and copy it to the destination.

Space Complexity: O(n) because we store both the source and destination strings, where n is the length of the string.

Copying Strings Using std::string Assignment

In this approach, we use the std::string class and the assignment operator(=) to easily copy strings. It simplifies the process by automatically taking care of memory and null terminators for us.

Example

In this example, we declare two std::string variables, source and destination. The source string is assigned to the destination using the = operator, which handles the copying process.

Open Compiler
#include <iostream> #include <string> using namespace std; int main() { // Source string to be copied string source = "C++ is fun!"; // Destination string where the source will be copied string destination; // Copy the source string to the destination destination = source; cout << "Source String: " << source << endl; cout << "Destination String: " << destination << endl; return 0; }

Below is the output of the above program, showing the copied string from the source to the destination:

Source String: C++ is fun!
Destination String: C++ is fun!

Time Complexity: O(n) because each character from the source is copied to the destination.

Space Complexity: O(n) because we store both the source and destination strings.

Copying Strings Using Pointer-Based Copying

In this approach, we use pointers to copy strings. A pointer stores the memory address of a string's characters, and we copy each character from the source to the destination using pointer arithmetic.

Example

In this example, we create two character pointers, one for the source string and one for the destination. We use a while loop to copy each character from the source to the destination and add the null terminator at the end.

Open Compiler
#include <iostream> using namespace std; int main() { // Initialize the source and destination strings char source[] = "Hello, tutorialspoint!"; char destination[50]; // Pointers to traverse the source and destination strings char *srcPtr = source; char *destPtr = destination; // Copy each character from source to destination while (*srcPtr != '\0') { *destPtr = *srcPtr; srcPtr++; destPtr++; } *destPtr = '\0'; // Add null-terminator to destination string // Output the source and destination strings cout << "Source String: " << source << endl; cout << "Destination String: " << destination << endl; return 0; }

The output below shows the copied string from the source to the destination using pointer.

Source String: Hello, tutorialspoint!
Destination String: Hello, tutorialspoint!

Time Complexity: O(n) because we go through each character in the source string once to copy it.

Space Complexity: O(n) because we store both the source and destination strings.

Updated on: 2025-05-15T19:46:46+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements