answersLogoWhite

0

Program to implement strdup in C?

Updated: 12/18/2022
User Avatar

Wiki User

14y ago

Best Answer

char *strdup (const char *s)

{

size_t len;

char *p;

if ( !s )

return NULL;

len = strlen (s);

p = malloc (len+1);

if (p && len)

{

memcpy (p, s, len);

p[len] = '\0';

}

return p;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Program to implement strdup in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program of binary heap in c or c language?

to implement operations on binary heap in c


Write a program in c language to implement framing methods like character stuffing?

A program in c language to implement framing methods like character stuffing can be grave sizeCRC-32 and the variable c50.


How do you run graphics program in C?

pro c language to implement linear search using pointers


C program to implement arithmetic assignment operator?

y=2x2+3x+1


How can use Strdup function in c language?

char* duplicated strdup("Hello World"); duplicated will now contain the address to dynamically allocated memory of a C-style string that contains "Hello World" and a null at the end. Be sure to free duplicated when you're done with it or you'll have a memory leak.


How do you implement a program to display the Indian flag with colors using c graphics?

ANSI/ISO C does not and never has done graphics.


Write a program in C language to implement the apriori algorithm?

JavaScript is one program that has been written in C to implement the Apriori algorithm. There are also several other known programs available on the Internet that implement it as well.


C program to implement tower of hanoi using array implementation of stack abstract datatype?

stack abstract datatype


Write a program in c plus plus to implement macro processor?

Don't write, it is already written, google for 'cpp'.


How do you allocate memory dynamically for a string of unknown size in c?

With function strdup (here is an implementation, if you don't have one already)char *strdup (const char *f){size_t len;char *to;len= strlen (f);to = malloc (len+1);if (to) memcpy (to, f, len+1);return to;}


Why c is top down approach?

It doesn't. Programmers might use any approach they like.


Write a program to convert temperature degrees celsius to degrees fahrenheit?

Here's a full tutorial of how to implement and build the program. Great Stuff! See below