-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strdup.c
51 lines (45 loc) · 1.44 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
46
47
48
49
50
51
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: xle-boul <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/03 11:37:22 by xle-boul #+# #+# */
/* Updated: 2021/10/19 12:11:01 by xle-boul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* duplicates a string into a new one, using malloc to assign memory */
char *ft_strdup(const char *s)
{
char *str;
int i;
i = 0;
str = (char *)malloc(sizeof(char) * ft_strlen(s) + 1);
if (!str)
return (NULL);
while (s[i] != '\0')
{
str[i] = s[i];
i++;
}
str[i] = '\0';
return (str);
}
/*
int main()
{
char s[] = "bl";
char *str;
int i = 0;
str = ft_strdup((char *)s);
printf("%d\n%s\n%s\n", strcmp(s, str), s, str);
while (str[i] != '\0')
{
printf("%c &str[i] = %p, &s[i] = %p\n", str[i], &str[i], &s[i]);
i++;
}
return (0);
}
*/