-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
40 lines (37 loc) · 1.33 KB
/
Copy pathft_substr.c
File metadata and controls
40 lines (37 loc) · 1.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: obouizi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/26 15:02:09 by obouizi #+# #+# */
/* Updated: 2024/11/02 11:33:21 by obouizi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *ptr;
size_t i;
size_t s_len;
if (s == NULL)
return (NULL);
s_len = ft_strlen(s);
if (start >= s_len)
return (ft_calloc(1, sizeof(char)));
if (len > s_len - start)
len = s_len - start;
ptr = (char *) malloc(len + 1);
if (ptr == NULL)
return (NULL);
i = 0;
while (i < len && s[start])
{
ptr[i] = s[start];
start++;
i++;
}
ptr[i] = '\0';
return (ptr);
}