Strcpy C++

We know about making copies of one file or folder to another in various operating systems. But, many of us may not know about making a copy of string value. Many languages support one string copy to another string, i.e. C++, java, etc. The C++ language supports the string copy concept using its strcpy() built-in function in programs. If you are looking for something like this, then this article is meant for you as it will explain the use of the strcpy() function in C++. Let’s start this guide with the inaugural of the Linux terminal application utilizing the “Ctrl+Alt+T” shortcut key.

After opening the shell terminal, we immediately need some C++ files to start our code. If you don’t have one earlier, you can make one with the “touch” query in the shell. This query is quite simple to use as it only takes the name of a file to be created and nothing else. We used it to create one and used the “nano” instruction to open it. The “nano” instruction has been used to open it within the GNU Nano editor of Ubuntu, a built-in editor. This built-in editor will open your file within the shell terminal. You can also use the other way to open your file, i.e. go to the home folder and open it with a text editor.

Example 01:

Our empty C++ file will be opened in the nano editor and ready to be used. Within our first example, we will be using a simple method to copy some string to some other variable string using the “strcpy” function. Before that, you need to include some necessary header files of C++ in the code at the top. The first header is the iostream library that must be utilized to use the input and output object statements. Without iostream, we will not be able to get a result from cin and cout statements of C++. After this, we have included the “cstring” library of C++. This library is required to use string variables in the code along with string values or inputs. You must add the standard namespace of C++, i.e. “std” using the “using” keyword.

We have to perform overall copy operations within the main() method of C++. The execution of this code will start from the main() and ends on the main() function regardless of how many user-defined functions are used. We have stated a character type variable “str” of size 10. It is of size 10; it will be string at the end. Here comes the main copy part using the strcpy() function. We have been copying a string “Hello” into the “str” variable via the strcpy() function. Another cout statement has been used to display the updated variable “str” on the shell. The main() function and the overall program are now complete and ready for use after saving by the Ctrl+S shortcut.

Before execution, we must compile the code in Ubuntu’s shell. For C++, ubuntu makes us bound to install the g++ compiler, and we did so. We compiled the C++ code file with the g++ installed compiler, which was easy to do. After that, the “./a.out” instruction was used by us to execute the compiled C++ script. We have got the result for the original empty string as nothing. After that, the updated string “str” value is displayed after using the strcpy() function to copy the string into it.

Example 02:

We have seen the example of copying a random string text value to some variable string. We will be using one string variable value for another empty string variable. Thus, we have updated the original code file, and the updated code is shown below. Within a main() function, we have declared a string “s1” with the string value “Hello World”. Another string “s2” of size 20 has been declared. The first cout statement displays the value of string s1, and the other shows the empty value of string s2. We have used the strcpy() function to copy s1 to s2. In the end, the cout statement shows the new value of string s2.

The original values of s1 and s2 were displayed in the first 2 lines of code compilation and execution. The third line shows the copied value of s1 into s2, i.e. s1=s2.

Within C++, it doesn’t have an issue with the strcpy() function to add the large size of a string variable into the small size of a string variable. To see this, we have updated the code and declared string s2 of size 5 while the string s1 already contains the value of size 11. We have copied the value of s1 into s2 and displayed the old and updated value for s2 using the cout object.

On compilation of this code, we got no errors. After running this code, the original value of s1 and s2 were displayed first. After that, the new value of s2 has also been displayed. This shows that the strcpy() function will not pay attention to the size of a string while copying.

Example 03:

Let’s take a look at another simple yet outstanding example of C++ using the strcpy() function in the Ubuntu 20.04 system. Thus, we have been using the same code file with quite a change in its script. So, open the same file and write the below-shown code into it. After adding the same iostream, cstring libraries, and standard namespace, we have used the main() function to start the code implementation. Within the start of a main() function, we have initialized two strings, s1 and s3, with string values and defined two string variables, s2 and s4, of size 20 as well.

The 4 cout statements are used to display the values of the original 4 string variables. After this, we have used the strcpy() function to copy s1 to s2 and the cout statement to display the new value of s2. Then the strcpy() function is utilized to copy s4 to s1 and the cout statement to display the new value of s1. Again, the strcpy() function is copying s3 to s4, and the cout shows the new value of s4. At last, the strcpy() function copies the empty string to s3, and the cout statement displays a new value of s3. The code is completed and saved to compile.

We have compiled the code and executed it after that. The First 4 output lines show the value before using the strcpy() function. The last 4 output lines show the result of using the strcpy() function, i.e. switch of values.


Finally! This article has covered all the possible methods to copy one string to another via the strcpy() function. We have tried to use the simplest and easiest examples for this purpose. So, our users can understand the code well and learn the concept without any difficulties.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.