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

Difference between strdup and strcpy?

0 Answers  


What are files in c++?

0 Answers  


what is c++

0 Answers  


What is the size of integer variable?

0 Answers  


Which is better turbo c++ or dev c++?

0 Answers  


What is an iterator class in c++?

0 Answers  


Describe private, protected and public – the differences and give examples.

0 Answers  


Explain the scope resolution operator?

2 Answers  


What is the disadvantage of using a macro?

0 Answers  


How to access a variable of the structure?

0 Answers  


Write a program in c++ to print the numbers from n to n2 except 5 and its multiples

0 Answers   Cankaya University,


What is a storage class used in c++?

0 Answers  


Categories