STRNCPY() Function – C Program

STRNCPY() Function – C Program

Write a  C Program to Copy string to another string using STRNCPY string function.

STRNCPY() Function – The STRNCPY() function used to copy some portion of contains second string into first string.

The Syntax of STRNCPY() Function :

strncpy (str1, str2, 5) – It copies first 5 character of str2 into str1.
strncpy(str2, str1, 3) – It copies first 3 character of str1 into str2.

Example of STRNCPY() Function C Program

#include<stdio.h>
#include<string.h>

int main()
{
char str1[100], str2[100];
printf("Please enter the first string = ");
gets(str1);
 
printf("\nPlease enter the second string = ");
gets(str2);

strncpy(str1,str2, 6);
 
printf("\nThe str1 after copy = %s", str1);
 
return 0;
}

The output of STRNCPY() Function C Program is :

Please enter the first string =
Please enter the second string = Academy

The str1 after copy = Academ

 

Leave a Reply

Your email address will not be published. Required fields are marked *