Write Function Called Mystrncpy Copies String Maxsize Characters String Src Char Buffer De Q37032712

a) Write a function called my_strncpy that copies a string of upto maxsize characters from string src to a char buffer dest:

int my_strncpy(const char *src, char *dest, int maxsize);

The function returns the number of characters copied. To get anycredit do not use any functions from string.h.


Solution


#include<stdio.h>

int my_strncpy(const char*src, char*dest, intmaxsize)

{

    // if the dest array is not initialized

    if( dest== NULL )

       return 0;

       

    int i, count= 0;

   

    for( i= 0 ; *src!=&& i< maxsize ;src++, i++ )

    {

       dest[i] =*src;

       count++;

    }

    dest[i]=;

   

    return0;

}

int main()

{

    char src[]= { “abcdef”};

    chardest[5];

   

   my_strncpy(src,dest, 5);

   

    printf(“String :%s”, dest);

   

    return0;

}

Sample Output

String abcde

String

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.