-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
48 lines (43 loc) · 1.38 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
42
43
44
45
46
47
48
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apimikov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 09:33:34 by apimikov #+# #+# */
/* Updated: 2023/11/06 16:07:30 by apimikov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t min(size_t x, size_t y);
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t len_s;
size_t len_sub;
size_t i;
char *pnt;
if (!s)
return (NULL);
i = 0;
len_s = ft_strlen(s);
len_sub = 0;
if (len_s > start)
len_sub = min(len, len_s - start);
pnt = malloc(len_sub + 1);
if (!pnt)
return (NULL);
while (i < len_sub)
{
pnt[i] = s[start + i];
i++;
}
pnt[i] = '\0';
return (pnt);
}
size_t min(size_t x, size_t y)
{
if (x < y)
return (x);
return (y);
}