-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
40 lines (37 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
36
37
38
39
40
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgata-va <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/14 13:24:48 by fgata-va #+# #+# */
/* Updated: 2021/10/21 12:43:03 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
size_t total_len;
size_t i;
size_t j;
if (!s)
return (NULL);
total_len = ft_strlen(s);
if (start >= total_len)
return (ft_strdup(""));
substr = (char *)malloc(sizeof(char) * len + 1);
if (!substr)
return (NULL);
i = start;
j = 0;
while (j < len && s[i] != '\0')
{
substr[j] = s[i];
j++;
i++;
}
substr[j] = '\0';
return (substr);
}