Implement strncpy

Answer Posted / lylez00

#include <string.h>
/* strncpy */
char *(strncpy)(char *restrict s1, const char *restrict s2,
size_t n)
{
char *dst = s1;
const char *src = s2;
/* Copy bytes, one at a time. */
while (n > 0) {
n--;
if ((*dst++ = *src++) == '\0') {
/* If we get here, we found a null character at
the end
of s2, so use memset to put null bytes at
the end of
s1. */
memset(dst, '\0', n);
break;
}
}
return s1;
}

Is This Answer Correct ?    1 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Define private, protected and public access control.

609


What data structure is fastest, on average, for retrieving data: a) Binary Tree b) Hash Table c) Stack

591


What is linked list in c++?

695


Differentiate between a constructor and a destructor in c++.

574


What does the linker do?

597






what kind of projects are suitable for c and c++

631


Define namespace in c++?

652


Can union be self referenced?

580


Are php strings immutable?

566


What is pair in c++?

631


Explain linear search.

635


What will happen if a pointer is deleted twice?

726


Is linux written in c or c++?

560


How can you say that a template is better than a base class?

587


Do you know what are the new features that iso/ansi c++ has added to original c++ specifications?

562