< Prev
Next >



C strncpy() method




The strncpy() is a string function defined in the string.h header file, which is used to copy the first n number of characters of a string to another string.




Signature of strcpy() function


char * strncpy ( char * destination, const char * source, size_t num )







Example of strncpy() function


In the upcoming code, we are calling strncpy() function, to copy the first 2 characters of one string named arr2 to another string named arr1.

/*  Program to append n number of characters of first string at the end of another string. */

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

int main()
{
char arr1[10] = "Keep Smiling";
char arr2[10] = "Always";

printf("The content of first string is : %s", arr1);
printf("\n");

printf("The content of second string is : %s", arr2);
printf("\n");

strncat(arr1,arr2,2); /* only first two characters from arr2 will be appended to the end of arr1 */

printf("The addition of two string is : %s", arr1);
printf("\n");
printf("The content of second string : %s", arr2);

return 0;
}


Output is -


The content of first string is : Keep a smile
The content of second string is : Always
The addition of two string is : Always smile
The content of second string : Always






  • Performing strncpy without using strncpy() function


  • Let's create a program copies the first n number of characters of one string to another string without using strncpy() function.


    /* Program to copy n, number of characters of one string to another string without strncpy() function */
    
    
    #include<string.h>
    #include<stdio.h>
    
    int main()
    {
    char dest[20] = "Hello";
    char src[10] = "World";
    
    printf("The value in dest string : %s", dest);
    printf("\n");
    
    printf("The second in src string : %s", src);
    printf("\n");
    
    int len1 = strlen(dest);
    int len2 = strlen(src);
    
    printf("Length of characters in dest string : %d \n", len1);
    
    int num; 
    printf("How many characters of the first string you to copy from str string : ");
    scanf("%d", &num);
    
    for(int i=0;i<num; i++)
    {
    	dest[i] = src[i];
    	len1 = len1 + 1 ;
    }
    dest[len1]='\0';
    
    printf("The new value in dest string : %s ", dest);
    
    return 0;
    }


    Output is :


    The value in dest string : Hello
    The second in src string : World
    Length of characters in dest string : 5
    How many characters of the first string you to copy from str string: 2
    The new value in dest string : Wollo





    Note: - The function strlen returns the total number of characters actually present in the char[] and not the total number of character it can hold.





    Please share this article -

    Facebook Google Pinterest Reddit Tumblr Twitter




    < Prev
    <Next >
    < C strncpy() function
    < C strcmp() function >
    Please subscribe our social media channels for notifications, we post a new article everyday.

    Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

    Coming Next
    -
    C++ and Python

    Ad2