C Program to Copy The First String into Second String

Below is a simple C program that copies the content of the first string into the second string

Method 1 :

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char input[20],output[20];
printf(“Enter the String : “);
gets(input);
printf(“Input : %s\n”,input);
strcpy(output,input);
printf(“Output : %s\n”,output);
strcpy(output,”students”);
printf(“output : %s\n”,output);
}

Output :

Method 2 :

#include<stdio.h>
void stringCopy(char[],char[]);
int main()
{
char str1[100],str2[100];
printf(“Enter any string :”);
gets(str1);
stringCopy(str1,str2);
printf(“After copying :%s”,str2);
return 0;
}
void stringCopy(char str1[],char str2[])
{
int i=0;
while(str1[i]!=’\0′)
{
str2[i]=str1[i];
i++;
}
str2[i]=’\0′;
}

Output :

Leave a Comment

Your email address will not be published.

Shopping Basket
Verified by MonsterInsights