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 are the various oops concepts in c++?

0 Answers  


What is a forward referencing and when should it be used?

0 Answers  


What do you mean by storage classes?

1 Answers  


Write about the members that a derived class can add?

0 Answers  


Can a constructor throw a exception? How to handle the error when the constructor fails?

1 Answers  






What ANSI C++ function clears the screen a) clrscr() b) clear() c) Its not defined by the ANSI C++ standard

0 Answers  


Which algorithm do you like the most? Why?

2 Answers   Google,


Difference between overloaded functions and overridden functions

0 Answers  


What happens if an exception is throws from an, object's constructor and object's destructor?

4 Answers   Wipro,


write a programming using for loop in c++ to generate diamond trangel?

1 Answers   NIIT,


What is the best c c++ compiler for windows?

0 Answers  


What is the basic structure of c++ program?

0 Answers  


Categories