-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_substr.c
44 lines (40 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adiaz-lo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/13 10:35:25 by adiaz-lo #+# #+# */
/* Updated: 2020/01/13 10:49:30 by adiaz-lo ### ########.fr */
/* */
/* ************************************************************************** */
/*
** This function allocates (using 'malloc()') and returns a substring from the
** string 's'.
** The substring begins at index 'start' and is as maximum size 'len'.
*/
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t count;
size_t size;
char *tab;
if (!s)
return (NULL);
if ((unsigned int)ft_strlen(s) < start)
return (ft_strdup(""));
size = ft_strlen(s + start);
if (size < len)
len = size;
if (!(tab = (char *)malloc((len + 1) * sizeof(char))))
return (NULL);
count = 0;
while (count < len)
{
tab[count] = s[start + count];
count++;
}
tab[count] = '\0';
return (tab);
}