-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
35 lines (32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: doley <doley@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 15:33:27 by doley #+# #+# */
/* Updated: 2024/10/09 19:22:45 by doley ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t s_len;
size_t substr_len;
char *substr;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start >= s_len)
return (ft_strdup(""));
if (s_len - start < len)
substr_len = s_len - start;
else
substr_len = len;
substr = ft_calloc(substr_len + 1, sizeof(char));
if (!substr)
return (NULL);
ft_strlcpy(substr, s + start, substr_len + 1);
return (substr);
}