A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/cpp-strcpy/ below:

strcpy in C++ - GeeksforGeeks

strcpy in C++

Last Updated : 11 Jan, 2025

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.

The strcpy() function is used to copy strings in C++.

Example:

C++
// 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:


Function to copy string - strcpy implementation


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4