Strncpy( ) Function

Strncpy( ) Function :

Strncpy( ) function performs the same task as strcpy( ).The only difference between them is that the former function copies specified length of character from source to destination string. Whereas the latter function copies the whole source string to destination string. The format of the function is..
Strncpy ( destination,source,n)
Where,n is the argument.
A simple example s illustrated below.

Program :
#include<Stdio.h>
#include<conio.h>
#include<string.h>
void main( )
{
char str1[15],str2[15];
int n;
printf("Enter the source string :");
gets(str1);
printf("Enter the destination string:");
gets(str2);
printf("Enter the no of character to replace in destination string :");
scanf("%d",&n);
printf("Source string :%s ",str1);
printf("Destination string :%s",str2);
}

OUTPUT :
Enter source string : wonderful
Enter the destination string : beautiful

Enter number of character to replace in destination string :6

source string : wonderful
Destination string : wonderful

Explanation :
In the above program two strings are read from the terminal. Number of character to replace in in the destination string from source string is also entered. After obtaining these three arguments the strncpy( ) function replaces the destination string with number of characters ( arguments ). The source string character are "wonderful" and the destination "beautiful" before use of strncpy( ).After execution,the first six characters of the destination string ("beauti") are replaced with first six characters of source string ("wonder").