In this exercise, we will take a look at the strcpy() function which is used to copy a string into an empty character array (empty string). The syntax for this function is this:

strcpy(dst, src);

Given a string src and an empty string (empty character array) dst, the strcpy() function copies the contents of the src string into the dst string. It is important that the size of the empty character array, dst, be greater than or equal to the length of the string src plus one for the null character (‘\0’) in order to accommodate all incoming characters. If the dst char array is too short, the program will exhibit undefined behavior. Similar to the strcat() function, a new string will not be created.

The following example illustrates the use of strcpy():

#include<stdio.h> #include<string.h> int main() { char s1[] = "ABCD"; char s2[5]; // Empty string of five characters strcpy(s2, s1); printf("%s", s2); // Prints: ABCD }



Copy the string from src to dst.


The variable len represents the size of an empty char array that will be used to receive a copied string. Initially, it contains the number zero.

Change the variable len so that the empty char array, dst2, has enough space to store the letters of the string pan.


Copy the letters of pan into dst2.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?