-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
37 lines (34 loc) · 1.3 KB
/
ft_substr.c
File metadata and controls
37 lines (34 loc) · 1.3 KB
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_substr.c :+: :+: */
/* +:+ */
/* By: nsterk <marvin@codam.nl> +#+ */
/* +#+ */
/* Created: 2020/09/24 21:24:38 by nsterk #+# #+# */
/* Updated: 2021/03/03 01:45:55 by nsterk ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
unsigned int i;
char *sub;
if (!s)
return (NULL);
if ((ft_strlen(s) - start) < len)
len = ft_strlen(s) - start;
if (start >= ft_strlen(s))
len = 0;
sub = (char *)malloc(sizeof(*s) * (len + 1));
if (!sub)
return (NULL);
i = 0;
while (s[start + i] != '\0' && i < len)
{
sub[i] = s[start + i];
i++;
}
sub[i] = '\0';
return (sub);
}