-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
34 lines (31 loc) · 1.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aabduvak <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/02 00:22:06 by aabduvak #+# #+# */
/* Updated: 2022/01/02 00:22:06 by aabduvak ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *str, unsigned int start, size_t leng)
{
char *res;
size_t i;
i = 0;
if (!str)
return (NULL);
if (start >= ft_strlen(str))
return (ft_strdup(""));
if (start + ft_strlen(str) < leng)
leng = start + ft_strlen(str);
res = (char *) malloc(sizeof(char) * (leng + 1));
if (!res)
return (NULL);
while (i < leng && str[start] && start < ft_strlen(str))
res[i++] = str[start++];
res[i] = '\0';
return (res);
}