Implement strncpy

Answers were Sorted based on User's Feedback



Implement strncpy..

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

Implement strncpy..

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

Implement strncpy..

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

Post New Answer

More C++ General Interview Questions

What do you mean by persistent and non persistent objects?

1 Answers  


Can we use clrscr in c++?

0 Answers  


What is a constructor and how is it called?

0 Answers  


Is multimap sorted c++?

0 Answers  


What do you know about near, far and huge pointer?

0 Answers  






What are the advantages of using pointers in a program?

0 Answers  


Can we delete this pointer in c++?

0 Answers  


Can static member variables be private?

0 Answers  


What are smart pointers?

0 Answers  


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?

0 Answers  


Explain the concept of friend function in c++?

0 Answers  


Categories