-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
50 lines (46 loc) · 2.21 KB
/
ft_substr.c
File metadata and controls
50 lines (46 loc) · 2.21 KB
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
43
44
45
46
47
48
49
50
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: miissa <miissa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 09:25:08 by miissa #+# #+# */
/* Updated: 2025/11/08 10:32:43 by miissa ### ########.fr */
/* */
/* ************************************************************************** */
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: miissa <miissa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 12:12:00 by miissa #+# #+# */
/* Updated: 2025/11/08 12:12:00 by miissa ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *dest;
size_t slen;
if (!s)
return (NULL);
slen = ft_strlen(s);
if (start >= slen)
{
dest = (char *)malloc(sizeof(char));
if (!dest)
return (NULL);
dest[0] = '\0';
return (dest);
}
if (len > slen - start)
len = slen - start;
dest = (char *)malloc((len + 1) * sizeof(char));
if (!dest)
return (NULL);
ft_strlcpy(dest, s + start, len + 1);
return (dest);
}