Open In App

strcpy in C++

strcpy() is a standard library function in C++ and is used to copy one string to another. In C++ it is present in the <string.h> and <cstring> header files. 

Syntax: 



char* strcpy(char* dest, const char* src);

Parameters: This method accepts the following parameters:  

Return Value: After copying the source string to the destination string, the strcpy() function returns a pointer to the destination string.



Example:




// C++ program to illustrate
// strcpy() function in C/C++
#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
    // Strings Declared
    char str1[] = "Hello Geeks!";
    char str2[] = "GeeksforGeeks";
 
    char str3[40];
    char str4[40];
 
    char str5[] = "GfG";
 
    // String copy used
    strcpy(str2, str1);
    strcpy(str3, "Copy successful");
    strcpy(str4, str5);
 
    // Strings Printed
    cout << "str1: " << str1 << "\nstr2: " << str2
         << "\nstr3: " << str3 << "\nstr4: " << str4;
   
    return 0;
}

Output
str1: Hello Geeks!
str2: Hello Geeks!
str3: Copy successful
str4: GfG

Time Complexity: O(n)
Auxiliary Space: O(1)

Important Points:

Article Tags :
C++