-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
41 lines (38 loc) · 1.34 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
38
39
40
41
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lstorey <lstorey@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/13 11:45:45 by lstorey #+# #+# */
/* Updated: 2023/11/21 14:46:00 by lstorey ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *nstr;
size_t m;
if (start > ft_strlen(s))
return ((char *) ft_calloc(1, 1));
if (ft_strlen(s) - (size_t)start < len)
m = ft_strlen(s) - start;
else
m = len;
nstr = (char *) malloc(m * sizeof(char) + 1);
if (!nstr)
return (NULL);
i = 0;
while (i < m)
{
nstr[i] = s[start];
i++;
start++;
if (!s)
return (nstr);
}
nstr[i] = '\0';
return (nstr);
}