-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strdup.c
More file actions
42 lines (39 loc) · 1.26 KB
/
ft_strdup.c
File metadata and controls
42 lines (39 loc) · 1.26 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_strdup.c :+: :+: */
/* +:+ */
/* By: cherrewi <cherrewi@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2022/10/19 15:12:20 by cherrewi #+# #+# */
/* Updated: 2022/10/23 12:18:40 by cherrewi ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
Duplicates a string, and reseves memory via ft_calloc.
*/
char *ft_strdup(const char *s1)
{
size_t len;
size_t i;
const char *s_iter;
char *s2;
i = 0;
len = 0;
s_iter = s1;
while (*s_iter)
{
len++;
s_iter++;
}
s2 = (char *)ft_calloc(len + 1, sizeof(char));
if (s2 == NULL)
return (s2);
while (i < len)
{
*(s2 + i) = *(s1 + i);
i++;
}
return (s2);
}