Table of Contents
show
strcpy()
Copy one string to another
strcpy(destination,source);
Program: copy string using string function
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100];
char s2[100];
puts("Enter string");
gets(s1);
strcpy(s2,s1);
puts("After copying s2 contains");
puts(s2);
return 0;
}
Output
Enter string
bag
After copying s2 contains
Bag
Program: copy string without using string function
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100];
char s2[100];
int i = 0;
puts("Enter string");
gets(s1);
while(s1[i] != '\0')
{
s2[i] = s1[i];
i++;
}
s2[i] = '\0';
puts("After copying s2 contains");
puts(s2);
return 0;
}
Output
Enter string
comp
After copying s2 contains
comp
Views: 0