-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
38 lines (35 loc) · 1.39 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: solariscode <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/02 11:17:48 by melkholy #+# #+# */
/* Updated: 2022/02/13 08:20:57 by solariscode ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
size_t count;
if (!s)
return (NULL);
if ((size_t)start > ft_strlen(s))
return (ft_strdup(""));
if (len > ft_strlen(&s[start]))
sub = (char *)malloc((ft_strlen(&s[start]) + 1) * sizeof(char));
else
sub = (char *)malloc((len + 1) * sizeof(char));
if (!sub)
return (NULL);
count = 0;
while (count < len && *(s + start + count))
{
sub[count] = *(s + start + count);
count ++;
}
sub[count] = '\0';
return (sub);
}