-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strdup.c
38 lines (35 loc) · 1.22 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nmncube <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/30 09:58:14 by nmncube #+# #+# */
/* Updated: 2019/06/17 15:38:36 by nmncube ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s1)
{
int k;
int i;
char *s2;
k = 0;
i = 0;
while (s1[k] != '\0')
k++;
s2 = (char*)malloc(k + 1 * sizeof(char));
if (s2 == NULL)
return (NULL);
while (s1[i] != '\0' && s2 != NULL)
{
s2[i] = s1[i];
i++;
}
s2[i] = '\0';
if (s2 != NULL)
return ((char*)s2);
else
return (NULL);
}