-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
34 lines (31 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jovicto2 <jovicto2@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/20 20:36:50 by jovicto2 #+# #+# */
/* Updated: 2023/05/20 20:39:37 by jovicto2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substring;
if (start >= ft_strlen(s))
return (ft_strdup(""));
if (ft_strlen(s) - start >= len)
substring = ft_calloc((len + 1), sizeof(char));
else
substring = ft_calloc((ft_strlen(s) - start + 1), sizeof(char));
if (substring)
{
if (len > ft_strlen(s) + 1)
len = ft_strlen(s) + 1;
ft_strlcpy(substring, (s + start), (len + 1));
}
else
return (NULL);
return (substring);
}