-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_substr.c
51 lines (46 loc) · 1.54 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: gamarcha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/15 21:20:46 by gamarcha #+# #+# */
/* Updated: 2021/04/15 21:20:46 by gamarcha ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *sub(unsigned int start, size_t len, size_t len_s)
{
char *str;
if (len_s < start)
str = (char *)malloc(1);
else if (len_s - start < len)
str = (char *)malloc(len_s - start + 1);
else
str = (char *)malloc(len + 1);
return (str);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t len_s;
size_t i;
if (s == 0)
return (0);
len_s = ft_strlen(s);
str = sub(start, len, len_s);
if (str == 0)
return (0);
i = 0;
if (start < len_s)
{
while (s[i + start] && i < len)
{
str[i] = s[i + start];
i++;
}
}
str[i] = 0;
return (str);
}