C++ Standard Library C++ STL Library

C++ <cstring> - strcpy() Function



The C++ <cstring> strcpy() function is used to copy the character string pointed to by source, including the null character, to the character array whose first element is pointed to by destination.

The behavior is undefined if the destination is not large enough for the content of source (including the null character), or if the strings overlap.

Syntax

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

Parameters

destination Specify pointer to the character array where the content is to be copied.
source Specify pointer to the null-terminated byte string to copy from.

Return Value

Returns destination.

Example:

The example below shows the usage of strcpy() function.

#include <iostream>
#include <cstring>
using namespace std;
 
int main (){
  char str1[50] = "xyz";
  char str2[50];
  char str3[50] = "Hello World!";

  //replacing the content of str1
  //and str2 with str3
  strcpy(str1, str3);
  strcpy(str2, str3);

  cout<<"str1 is: "<<str1<<"\n";
  cout<<"str2 is: "<<str2<<"\n";   
  return 0;
}

The output of the above code will be:

str1 is: Hello World!
str2 is: Hello World!

❮ C++ <cstring> Library