-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strndup.c
35 lines (32 loc) · 1.19 KB
/
ft_strndup.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tpouget <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/19 15:28:54 by tpouget #+# #+# */
/* Updated: 2021/04/19 15:29:00 by tpouget ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strndup(const char *s, size_t n)
{
size_t i;
size_t len;
char *duplicate;
i = 0;
len = 0;
while (len < n && s[len])
len++;
duplicate = malloc(len + 1);
if (!duplicate)
return (NULL);
while (i < len)
{
duplicate[i] = s[i];
i++;
}
duplicate[len] = '\0';
return (duplicate);
}