strcpy in C Programming

The C Strcpy function is one of the String Functions, which helps copy the user-specified string or content (a group of characters) from one string to another. The syntax of the strcpy is as shown below.

The following function will accept two character arrays as the parameters. And it copies string data from source to destination using the built-in function strcpy.

strcpy(destination, source);

strcpy in C Programming Example

The strcpy function is used to copy the content from the source to the destination string. This program will help you to understand the string copy with an example.

TIP: You must include the #include<string.h> header before using this strcpy function.

#include <stdio.h> 
#include<string.h>
int main()
{
   char str1[50];
   char str2[50];
   char str3[] =  " C Programming Language";
   char str4[50], str5[50];
 
   printf("\n Please enter the String you want to Copy: \n");
   gets(str1);
 
   strcpy(str2, str1); 
   //puts(str1);
   puts(str2);
  
   strcpy(str4, str3);
   puts(str4);
   //puts(str3); 
 
   strcpy(str5, " we provide free tutorials");
   puts(str5);
}
strcpy in C Programming 1

Within this program, First, we declared three character arrays str1, str2, str3, str4, and str5, and we assigned text data to str3. The first two statements will ask the user to enter the text he/she wants to copy. Next, we assign the value to str1 using GETS Function.

The following statement will copy the string data from str1 to str2.

strcpy(str2, str1);

The following C Programming statement will copy the character array from str3 to str4.

strcpy(str4, str3);

Next, we used the text directly inside the function.

strcpy(str5, " we provide free tutorials");