-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
41 lines (38 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
39
40
41
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aizsak <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/07 11:48:30 by aizsak #+# #+# */
/* Updated: 2022/11/07 18:06:05 by aizsak ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(const char *s, unsigned int start, size_t len)
{
char *str;
size_t i;
i = 0;
if (start >= ft_strlen(s))
{
str = malloc(1);
if (!str)
return (NULL);
str[i] = '\0';
return (str);
}
if (!s)
return (NULL);
if (len <= ft_strlen(s) - start)
str = malloc(sizeof(char) * (len + 1));
else
str = malloc(sizeof(char) * (ft_strlen(s) - start) + 1);
if (!str)
return (NULL);
while (start < ft_strlen(s) && i < len)
str[i++] = s[start++];
str[i] = '\0';
return (str);
}