-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
39 lines (36 loc) · 1.32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ctokuyos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/01 19:21:16 by ctokuyos #+# #+# */
/* Updated: 2024/11/06 16:33:11 by ctokuyos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
size_t i;
size_t s_len;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start >= s_len)
return (ft_strdup(""));
if (len > s_len - start)
len = s_len - start;
substr = (char *)malloc((len + 1) * sizeof (char));
if (!substr)
return (NULL);
i = 0;
while (i < len)
{
substr[i] = s[start + i];
i++;
}
substr[i] = '\0';
return (substr);
}