strcpy in C Programming

The C Strcpy function is one of the String Functions, which is helpful to copy the user-specified string or content (a group of characters) from one string to another. The syntax of the strcpy in C Programming language 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 String function strcpy.

strcpy(destination, source);

strcpy in C Programming Example

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

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

//strcpy in C Programming example  
#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, str5, and we assigned string data to str3. The following statement will ask the user to enter the string he/she want to copy. Next, we are assigning the value to str1 using GETS Function

printf("\n Please enter the String you want to Copy: \n");
gets(str1);

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

strcpy(str2, str1);

The following C Programming statement will copy the character array (string data) from str3 to str4.

strcpy(str4, str3);

Next, we used the string data directly inside the strcpy function

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