-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
38 lines (34 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_substr.c :+: :+: */
/* +:+ */
/* By: jaberkro <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/10/22 20:14:58 by jaberkro #+# #+# */
/* Updated: 2021/12/01 13:08:26 by jaberkro ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *out;
size_t l;
if (!s)
return (NULL);
l = ft_strlen(s);
if (start > l)
return (ft_calloc(1, 1));
if (len > l)
len = l;
out = malloc(len + 1);
if (out == NULL)
return (NULL);
ft_strlcpy(out, s + start, len + 1);
return (out);
}
/* Allocates (with malloc(3)) and returns a substring
from the string ’s’.
The substring begins at index ’start’ and is of
maximum size ’len’.
*/