C++ strcpy()

StevenSwiniarski's avatar
Published Aug 13, 2022Updated Dec 21, 2022
Contribute to Docs

The strcpy() string function copies one string into another string and returns the newly copied string.

Note: strcpy() only works on C-style strings, not C++ style strings. So it only works with strings of type char str[]; not string str;

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

#include <string.h>

strcpy(string1, string2);

At the top of the file, the <string.h> header file needs to be included. The strcpy() copies the C-style string2 parameter into string1, including the terminating null character ('\0').

Codebyte Example

The following example prints a string, copies a second string over it, and prints it again:

Code
Output

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours