-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
34 lines (30 loc) · 1.46 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: gubranco <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/16 18:49:17 by gubranco #+# #+# */
/* Updated: 2022/12/03 21:56:03 by gubranco ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
if (!s)
return (0);
if ((unsigned int)ft_strlen(s) < start)
return (ft_strdup(""));
if (ft_strlen(s + start) < len)
len = ft_strlen(s + start);
str = (char *)malloc((len + 1) * sizeof(char));
if (!str)
return (NULL);
ft_strlcpy(&str[0], s + start, len + 1);
return (str);
}
/* Allocates (with malloc(3)) and returns a substring from the string ’s’.
The substring begins at index ’start’ and is of maximum size ’len’.
It returns the substring, and NULL if the allocation fails. */