-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
41 lines (38 loc) · 1.36 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: jkang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 20:12:47 by jkang #+# #+# */
/* Updated: 2020/04/10 12:57:45 by jkang ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *temp;
size_t i;
size_t s_len;
s_len = ft_strlen(s);
if (s_len == 0)
return (0);
if (s_len < start)
{
temp = (char *)malloc(sizeof(char) * 2);
if (temp != 0)
temp[0] = '\0';
return (temp);
}
if (!(temp = (char *)malloc(sizeof(char) * (len + 1))))
return (0);
i = 0;
while (*s != '\0' && i < len)
{
temp[i] = *(s + start + i);
++i;
}
temp[i] = '\0';
return (temp);
}