-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathft_strndup.c
32 lines (29 loc) · 1.13 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbelpaum <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/17 18:30:35 by tbrowang #+# #+# */
/* Updated: 2021/07/18 12:11:02 by dbelpaum ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft.h"
char *ft_strndup(const char *s, int n)
{
char *dup;
int i;
i = 0;
dup = (char *)malloc((n + 1) * sizeof(char));
if (!dup)
return (NULL);
while (s[i] != '\0' && n > 0 )
{
dup[i] = s[i];
i++;
n--;
}
dup[i] = '\0';
return (dup);
}