-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
34 lines (31 loc) · 1.31 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: gmachado <gmachado@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/08 19:09:57 by gmachado #+# #+# */
/* Updated: 2022/04/20 15:19:49 by gmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub_str;
size_t len_s;
if (s == NULL)
return (NULL);
len_s = ft_strlen(s);
if (start >= len_s)
len = 0;
else if (len > len_s - start)
len = len_s - start;
sub_str = (char *)malloc((len + 1) * sizeof(char));
if (sub_str == NULL)
return (NULL);
sub_str[len] = '\0';
while (len-- > 0)
sub_str[len] = s[start + len];
return (sub_str);
}