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 are the various oops concepts in c++?
What is a forward referencing and when should it be used?
What do you mean by storage classes?
Write about the members that a derived class can add?
Can a constructor throw a exception? How to handle the error when the constructor fails?
What ANSI C++ function clears the screen a) clrscr() b) clear() c) Its not defined by the ANSI C++ standard
Which algorithm do you like the most? Why?
Difference between overloaded functions and overridden functions
What happens if an exception is throws from an, object's constructor and object's destructor?
write a programming using for loop in c++ to generate diamond trangel?
What is the best c c++ compiler for windows?
What is the basic structure of c++ program?