-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
38 lines (35 loc) · 1.34 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_substr.c :+: :+: */
/* +:+ */
/* By: tclement <tclement@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/04/13 13:29:22 by tclement #+# #+# */
/* Updated: 2020/05/20 10:48:22 by tclement ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
size_t index;
if (s == NULL)
return (NULL);
if (start > ft_strlen(s))
return (ft_strdup(""));
if (start + len > ft_strlen(s))
len = ft_strlen(s) - start;
sub = ft_calloc(len + 1, sizeof(char));
if (sub == NULL)
return (NULL);
index = 0;
while (index < len && s[index] != '\0')
{
sub[index] = s[start];
index++;
start++;
}
sub[index] = '\0';
return (sub);
}