-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
37 lines (35 loc) · 1.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fraqioui <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/07 07:40:41 by fraqioui #+# #+# */
/* Updated: 2022/10/18 17:14:48 by fraqioui ### ########.fr */
/* */
/* ************************************************************************** */
#include"libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
size_t i;
i = 0;
if (!s)
return (NULL);
if (start >= ft_strlen(s))
return ((char *)ft_calloc(1, sizeof(char)));
if (ft_strlen(s) <= start + len)
substr = malloc(sizeof(char) * (ft_strlen(s) - start + 1));
else
substr = malloc(sizeof(char) * (len + 1));
if (!substr)
return (NULL);
while (s[start] && i < len)
{
substr[i] = s[start + i];
i++;
}
substr[i] = '\0';
return (substr);
}