Implement strncpy
Answers were Sorted based on User's Feedback
Answer / ada
char *my_strncpy( char *dst, char *src, size_t n)
{
int i = n;
char *p = dst;
if(!dst || !src)
return dst;
while( i != 0 && *src != '\0' )
{
*p++ = *src++;
i --;
}
while( i!=0 )
{
*p++ = '\0';
i --;
}
return dst;
}
| Is This Answer Correct ? | 6 Yes | 0 No |
Answer / shanmugavalli
char* strncpy(char* dest,const char* src,int n)
{
while(n>0)
{
if (!(*dest = *src)) break;
src++;
dest++;
n--;
}
if (n<=0) *dest = '\0';
return dest;
}
| Is This Answer Correct ? | 3 Yes | 4 No |
Answer / 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 |
What is do..while loops structure?
how many trys can we write in one class
Which one between if-else and switch is more efficient?
Give 10 points of differences between C & C++.
write the prime no program in c++?
What is exception handling? Does c++ support exception handling?
What are multiple inheritances (virtual inheritance)?
what is meaning of isa and hsa
what are the decision making statements in C++? Explain if statement with an example?
What is an adaptor class in c++?
Write about the use of the virtual destructor?
What is an Iterator class?