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 do you mean by persistent and non persistent objects?
Can we use clrscr in c++?
What is a constructor and how is it called?
Is multimap sorted c++?
What do you know about near, far and huge pointer?
What are the advantages of using pointers in a program?
Can we delete this pointer in c++?
Can static member variables be private?
What are smart pointers?
Difference between Overloading and Overriding?
35 Answers Appnetix Techno, GameLoft, HP, IBM, NIIT, Rohde and Schwarz,
What happens when the extern "c" char func (char*,waste) executes?
Explain the concept of friend function in c++?