-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
40 lines (37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rnarciso <rnarciso@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/22 17:39:09 by rnarciso #+# #+# */
/* Updated: 2022/12/01 22:11:03 by rnarciso ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
size_t i_2;
size_t i_3;
unsigned char *res;
i = ft_strlen(s);
if (start > i)
len = 0;
if (len > i - start)
len = i - start;
res = (unsigned char *)malloc(len + 1);
if (!s || !res)
return (NULL);
i_2 = 0;
i_3 = 0;
while (s[i_2])
{
if (i_2 >= start && i_3 < len)
res[i_3++] = s[i_2];
i_2++;
}
res[i_3] = '\0';
return ((char *)res);
}