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

Write the program for fibonacci in c++?

20 Answers   TATA, Wipro,


Please post the model question paper of hal?

2 Answers  


What is the use of volatile variable?

0 Answers  


Write a corrected statement in c++ so that the statement will work properly. if (x = y) x = 2*z;

2 Answers  


How do you generate a random number in c++?

0 Answers  






Mention the ways in which parameterized can be invoked.

0 Answers  


Difference between inline functions and macros?

0 Answers  


How do you print for example the integers 3,2,1,5,4 in a binary tree within the console in format where it looks like an actual binary tree?

0 Answers  


1)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 * sizeof(int)); for (i=0; i<4; i++) *a++ = 10 * i; for (i=0; i<4; i++) { printf("%d\n", *savea); savea += sizeof(int); } return 0; } 2)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 * sizeof(int)); for (i=0; i<4; i++) *a++ = 10 * i; for (i=0; i<4; i++) { printf("%d\n", *savea); savea ++; } return 0; } The output of this two programs will be different why?

5 Answers  


Floating point representation and output seems to be compiler dependent?

1 Answers  


Is there structure in c++?

0 Answers  


How do I run c++?

0 Answers  


Categories