Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Functions and Pointers
  5. Built-in functions
  6. Copy n Chars

Copy n Chars

Use of strncpy( ) function

#include<stdio.h>
#include<string.h>
int main()
{
	char src[50];
	char dest[50];
	int n;
	puts("Enter string");
	gets(src);
	puts("Enter the number of characters to copy");
	scanf("%d",&n);
	puts("Source string is:");
	puts(src);
	strncpy(dest,src,n);
	dest[n] = '\0';
	puts("Destination string :");
	puts(dest);
	return 0;
}

Output

Enter string
Welcome all
Enter the number of characters to copy
3
Source string is:
Welcome all
Destination string :
Wel

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