-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strdup.c
45 lines (40 loc) · 1.31 KB
/
ft_strdup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tpouget <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/14 17:04:37 by tpouget #+# #+# */
/* Updated: 2020/05/26 14:29:25 by tpouget ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s)
{
size_t i;
char *duplicate;
i = 0;
duplicate = malloc((ft_strlen(s) + 1) * sizeof(char));
if (!duplicate)
return (NULL);
while (s[i])
{
duplicate[i] = s[i];
i++;
}
duplicate[i] = '\0';
return (duplicate);
}
/*
char *ft_strdup(const char *s)
{
size_t len;
char *ptr;
len = ft_strlen(s);
ptr = malloc(len + 1);
if (!ptr)
return NULL;
return ft_memcpy(ptr, s, len + 1);
}
*/