some-usable-scripts/C_Language/strdup.c

21 lines
409 B
C
Raw Normal View History

2011-02-20 18:52:23 +01:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * strdup2(char *);
int main(int argc, char **argv)
{
char * chaine1 = "Coucou c'est moi";
char * copie = strdup2(chaine1);
printf("Chaîne originale : %s \nChaîne copie : %s\n", chaine1, copie);
exit(0);
}
char * strdup2(char * t)
{
char *s, *save;
save = s = malloc((strlen(t) + 1)* sizeof(char));
while(*s++ = *t++);
return save;
}