-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
39 lines (36 loc) · 1.3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vduriez <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/19 16:10:08 by vduriez #+# #+# */
/* Updated: 2020/09/04 15:55:52 by vduriez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
size_t j;
char *res;
if (!s || len < 0)
return (NULL);
i = 0;
j = 0;
while (s[start + j])
j++;
j = (len > j) ? j : len;
if (start >= (unsigned int)ft_strlen(s))
j = 0;
if (!(res = malloc(sizeof(char) * (j + 1))))
return (NULL);
while (s[start + i] && i < j)
{
res[i] = s[start + i];
i++;
}
res[i] = '\0';
return (res);
}