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

write a programme to get a character and thier ASCII value

2598


What is the prototype of printf function?

658


Is c++ a good beginners programming language?

584


Explain stack unwinding.

640


What is stack unwinding?

606






What is this weird colon-member (" : ") syntax in the constructor?

547


Can manipulators fall in love?

566


What is iterator c++?

551


Can we run c program in turbo c++?

588


Who calls main function?

587


How to implement is-a and has-a class relationships?

589


What are static variables?

619


What is basic if statement syntax?

566


Which is most difficult programming language?

582


What is data structure in c++?

663