-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
46 lines (42 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyalexan <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/22 10:28:02 by kyalexan #+# #+# */
/* Updated: 2021/12/15 14:01:02 by kyalexan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_min(int a, int b)
{
if (a < b)
return (a);
else
return (b);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
unsigned int s_len;
s_len = (unsigned int) ft_strlen(s);
len = ft_min(len, (int) s_len);
if (!s)
return (NULL);
if (start < s_len)
{
substr = (char *) ft_calloc(sizeof(char), len + 1);
if (!substr)
return (NULL);
ft_memcpy(substr, s + start, len);
}
else
{
substr = (char *) ft_calloc(sizeof(char), 1);
if (!substr)
return (NULL);
}
return (substr);
}