-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
42 lines (39 loc) · 1.35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: arsobrei <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 13:37:59 by arsobrei #+# #+# */
/* Updated: 2023/08/02 17:03:14 by arsobrei ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub_str;
size_t len_s;
size_t index;
len_s = ft_strlen(s);
if (start >= len_s)
{
return (ft_strdup(""));
}
if (len > len_s - start)
{
len = len_s - start;
}
sub_str = (char *)malloc((len + 1) * sizeof(char));
if (sub_str == NULL)
return (NULL);
index = 0;
while (index < len)
{
sub_str[index] = s[start];
index++;
start++;
}
sub_str[index] = '\0';
return (sub_str);
}