UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to study 'strcpy()' function

C program for how to copy a string using strcpy()

by Krishna viswambharan


Posted on 28 Sep 2017 Category: C Views: 282

Edited on 14 Jan 2018


C program to study 'strcpy()' function

In this article, we will see another string function.  Here we will see how to copy two strings using the string function ‘strcpy()’.  This string function is defined in the <string.h> header file

strcpy( ) function copies contents of one string into another string. The syntax for strcpy() function is given below.

char * strcpy ( char * destination, const char * source );

for example, strcpy ( string1, string2) – It copies contents of string2 into string1.

In the below program, the source string “Unclecoder”(input from the user) is copied to the destination string using the strcpy() function. C code for the program is written as below.

#include <stdio.h>
#include <string.h>
#include <conio.h>
 
void main()
{
   char source[100], destination[100];
 
   printf("Enter input string :");
   gets(source);    //read string
 
   strcpy(destination, source);   //copy operation
 
   printf("\nSource string:      %s”, source);
   printf("\nDestination string: %s ", destination);  //display result
 
   getch();
}

output



Leave a Comment:


Advertisement