by R4R Team

-strncpy() is a string function that are able to copy the data of one string into the other string upto nth term.
-It is predefined in string.h header file.

Syntax-
strncpy(str1,str2,n)
-Here str1 and str2 is two string and data of str1 is copied in str2.
-n is the index value.

program-

#include< stdio.h>
#include< string.h>
int main()
{
char s1[20],s2[20];
//input of string
printf("Enter first string\n");
scanf("%s",&s1);
strncpy(s2,s1,5);
printf("Second string is %s",s2);
}


output-

Enter first string
Mystring
Second string is Mystr

-In this program, we take an input of first string, and pass in strncpy() function with the index value of 5.
-So this function only copy starting 5 character of the input string in other string.
-And at last we display that string on console




Leave a Comment: