-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
41 lines (38 loc) · 1.32 KB
/
ft_substr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wboutzou <wboutzou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/24 18:07:57 by wboutzou #+# #+# */
/* Updated: 2021/11/26 21:24:27 by wboutzou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *p;
size_t i;
size_t j;
i = start;
j = 0;
if (!s)
return (0);
if (start > ft_strlen(s) + 1)
return (ft_strdup(""));
if (ft_strlen(s) - start < len)
len = ft_strlen(s) - start;
p = (char *) malloc(len +1);
if (!p)
return (NULL);
if (start < ft_strlen(s))
{
while (s[i] != '\0' && j < len)
{
p[j++] = s[i++];
}
p[j] = '\0';
}
return (p);
}