-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
36 lines (32 loc) · 1.31 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: youkim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/09 18:48:38 by youkim #+# #+# */
/* Updated: 2021/05/12 14:20:08 by youkim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t st_min(size_t first, size_t last)
{
if (first >= last)
return (last);
return (first);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
if (!s)
return (0);
if (ft_strlen(s) < start)
return (ft_strdup(""));
len = st_min(len, ft_strlen(s + start));
sub = malloc((len + 1) * sizeof(char));
if (!sub)
return (0);
ft_strlcpy(sub, s + start, len + 1);
return (sub);
}