C – Strcpy() function
Strcpy() Fucntion – In C, strcpy() function is used to assign the contents of the one character array (string) to the other character array. The syntax for the strcpy() fucntion is–
strcpy(string1,string2);
It overwrites the string2 on the string1 and the string1 will now contain the same contents as the string2 contains. String2 may be a character array variable or a string constant. For example the following strcpy() statement is also valid –
strcpy(city,”DELHI”);
This will assign the string “DELHI” to the string variable city.
Sample program of the strcpy( ) in C programming langauge -
/* C program for strcpy( ) function*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char nm[15],nm2[15];
printf("Enter one string \t");
gets(nm);
printf("Enter another string \t");
gets(nm2);
printf("\n\nBoth string are - \n String1 : \n");
puts(nm);
puts("String2: ");
puts(nm2);
strcpy(nm,nm2);
/* Assigning string of nm2 to nm */
printf("First string after strcpy function is â \n");
puts(nm);
getch();
}
Input -
String1 – Hello
String2 – World
Ouput -
String1 – World
No comments yet