Strncpy Implementation in C

STRNCPY – copy characters from string.
strncpy() copies n bytes and it adds null termination at the end of the target string.
Syntax: ptr = strncpy( s1, s2, N );
char *s1 – It points to an area of memory that is to receive the copied characters. This must be able to hold at least N characters.
const char *s2 : Itpoints to the string from which characters will be copied.
size_t N : The number of characters to copy.
char *ptr : Points to the copied string (i.e. “s1”)
.

char *(strncpy)(char * s1, const char *, size_t n)
{
   char *dst = s1;
   const char *src = s2;
   while (n > 0) {
      n–;
      if ((*dst++ = *src++) == ‘\0’) {
         memset(dst, ‘\0’, n);
         break;
         }
   }
   return s1;
}


About mohan

This is Mohan Jayapalan author of TechPages Blog.I am a part time blogger and Currently working for private Software Concern.

© Copyright 2010-2016 www.techpages.org · All Rights Reserved · Powered by WordPress ·