| by Arround The Web | No comments

Strcpy C++

The C++ language has many inbuilt methods for programmers. We just need to access those methods by importing the required libraries. Strings play a vital role in coding and C++ provides plenty of built-in functions for strings. Strings are arrays of characters. It can store one or more than one characters. The difference between character arrays and strings is that character arrays can be of fixed size but mostly we do not define the size. And in strings, we do not need to limit the size. There are many methods used for strings. We can find the length of strings, we can concatenate two or more strings, and many more methods for strings are available for programmers to use. One of these built-in methods is string copy, denoted by the term strcpy(). This function is used to copy one string in another. These methods are pre-built in the C++ library so that we do not have to write the entire piece of code over and over again for the operations we carry out for the majority of the time when we code. To use this strcpy() function, we need to import the specific library that contains this method. For this function, we have to import any of the below command.

#include <cstring>

or

#include <string.h>

The first ‘cstring’ is a standard library and the second ‘string.h’ shows the header file. By using any of the two, we will be able to access the strcpy() method. Both of them deal with arrays of characters and have built-in methods for strings.

Syntax:

char* strcpy (char* destination, const char* source );

The syntax of string copy looks a little complex but it is easy once you understand it. The strings should be character strings (arrays of characters). Otherwise, this method will not work. That is why in syntax, we can see the ‘char’ data type is used.

Parameters:

Two parameters will be passed; one for the source and one for the destination of a string.

  • Source:

The source is the string we want to copy. The character pointer shows that it is a pointer to a string and it should be constant because by setting it to constant we ensure that function will not make any change in the string that is being copied. It is defined as a char pointer ‘char*’.

  • Destination:

The destination is where we will store the source string. It is not set to constant because in the destination string we want to make changes by adding the source string.

Return Type

The strcpy() function will return a pointer to the destination after copying the source string.

Using the C++ strcpy() Method

Let us look at how to use the strcpy() method to copy one string into another without writing the whole code from the scratch.

Code:

#include <iostream>

using namespace std;

#include<cstring>

int main ()
{
    char str_1[]="Hello!";
    char str_2[60];
    strcpy(str_2, str_1);
    cout<< "\nString two:  " << str_2;
    return 0;
}

Import two libraries; <iostream> for performing input/output operations and <cstring> to use the strcpy() function of this library. The <iostream> is integrated before the standard namespace and the second header file <cstring> is integrated after the standard namespace. Now, define two character arrays and initialize the first character array. We must have noticed that we have not set the size of the first array but we set the size of the second array. This is because when we initialize an array, we do not need to define the size after. But when we do declaration and initialization in one step, we have to define the size. Otherwise, the compiler will generate an error message. Now, use the strcpy() function to copy string 1 in string 2. This method will copy the first string from its location and then it will paste that into the second string. One thing to keep in mind is, when we store ‘str_1’ in ‘str_2’, the size of the string 2 variable is 60. So, the size of the string being copied should be 60 or less than 60. Now, display string 2 with a message on a console by using the ‘cout’ statement.

Output:

String two: Hello!

C++ Copying a String Into Another that Already has Some Text

Let’s talk about the strcpy() method and how it may be used to copy a string that contains a string variable.

Code:

#include <iostream>

using namespace std;

#include<cstring>

int main ()
{
    char string_1[]="I am string one";
    char string_2[]="I am string two";
    cout<< "Before strcpy() String_2: " << string_2;
    strcpy(string_2, string_1);
    cout<< "\nAfterstrcpy() String_2 : " << string_2;
    return 0;
}

First, integrate two libraries <iostream> and <cstring>. Then, call the main() method and inside it declares two character type arrays ‘string_1’ and ‘string_2’. Cout the second character array ‘string_2’ with a message. Next, apply the strcpy() function and copy the first string in the second. In the strcpy() method, the first parameter shows the destination and the second argument shows the source. After copying the ‘string_1’ in ‘string_2’, print a message on the terminal. The strcpy() method overwrites the already stored string with the string that is copied. We can say that this is the drawback of strcpy(): it replaces the string with the new string that we copy. Instead of appending, it overwrites the string. On the output screen, we will observe that the already stored data of ‘string_2’ will be replaced by ‘string_1’ data.

Output:

Before strcpy() String_2: I am string two

After strcpy() String_2 : I am string one

C++ strcpy() Using ‘string.h’ Library Instead of ‘cstring’

In this code, we will discuss how to call strcpy() function by importing a string header rather than a ‘cstring’.

Code:

#include <iostream>

using namespace std;

#include<string.h>

int main ()

{

   char st_1[]="One";

   char st_2[]="Two";

   cout << "Before strcpy() St_1: " << st_1;

   strcpy(st_1, st_2);

   cout << "\nAfter strcpy() St_1 : " << st_1;

   return 0;

}

We start the code by importing the <iostream> and <string.h> library. The first library is used to access the input/output method and the second library is used to access built-in methods of strings. In the next statement, the main() method is employed. The initialization of two strings of character types ‘st_1’ and ‘st_2’ is done within this function. Use the ‘cout’ statement so it will display data of the first string ‘st_1’ before performing strcpy(). Then, call the strcpy() method and copy ‘st_2’ in ‘st_1’. This function will replace the first string ‘st_1’ with the second string ‘st_2’. Then prints the ‘st_1’ on the screen to see the difference between the data of ‘st_1’ before and after using the strcpy() method.

Output:

Before strcpy() St_1: One

After strcpy() St_1 : Two

Conclusion

In this tutorial, the ‘strcpy()’ function is the main subject of discussion. In C++ this method is used to copy one string into another, the string should be a character array instead of using string as a class object because this method does not support string class objects to copy one in another. The article explains the strcpy() method with the background of the strings and libraries for inbuilt string methods. The topic is explained with the help of several coding examples which we call strcpy(). Furthermore, we implemented this method by importing two different libraries.

Share Button

Source: linuxhint.com

Leave a Reply