Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Arrays and Strings
  5. Strings
  6. String Copy

String Copy

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

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments