-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsub.c
executable file
·37 lines (34 loc) · 1.32 KB
/
ft_strsub.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vportell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/10/31 08:08:14 by vportell #+# #+# */
/* Updated: 2016/11/04 15:40:36 by vportell ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strsub(char const *s, unsigned int start, size_t len)
{
int i;
char *sub;
i = 0;
if (!s)
return (NULL);
while (s[i])
i++;
if ((int)start + (int)len > i || (int)len < 0)
return (NULL);
if (len == 0)
return ("");
sub = (char *)malloc(sizeof(char) * (len + 1));
if (!sub)
return (NULL);
i = start - 1;
while (s[++i] && i < (int)start + (int)len)
sub[i - start] = s[i];
sub[i - start] = '\0';
return (sub);
}