-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
40 lines (37 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbartos <mbartos@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/24 11:52:38 by mbartos #+# #+# */
/* Updated: 2023/10/30 13:39:53 by mbartos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub_str;
char *sub_str_first;
size_t i;
size_t length_malloc;
if (start > ft_strlen(s))
length_malloc = 0;
else if (start <= ft_strlen(s) && ft_strlen(s) <= len)
length_malloc = ft_strlen(s) - start;
else
length_malloc = len;
sub_str = (char *) malloc((length_malloc + 1) * sizeof(char));
if (sub_str == NULL)
return (NULL);
sub_str_first = sub_str;
i = 0;
while (i < length_malloc && *(s + start + i))
{
*sub_str++ = *(s + start + i);
i++;
}
*sub_str = '\0';
return (sub_str_first);
}