-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
51 lines (46 loc) · 1.52 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
42
43
44
45
46
47
48
49
50
51
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pmaryjo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/05 18:42:11 by pmaryjo #+# #+# */
/* Updated: 2021/09/05 18:42:12 by pmaryjo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *get_empty_str(void)
{
char *str;
str = (char *)malloc(1);
if (!str)
{
return (NULL);
}
str[0] = '\0';
return (str);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t counter;
size_t sub_str_size;
char *result;
if (!s)
return (NULL);
counter = 0;
if (start >= ft_strlen(s))
return (get_empty_str());
sub_str_size = ft_strlen(s) - start;
if (sub_str_size >= len)
sub_str_size = len;
sub_str_size++;
result = (char *)malloc(sub_str_size);
if (!result)
return (NULL);
s += start;
while (*s && len--)
result[counter++] = *s++;
result[counter] = '\0';
return (result);
}