-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
37 lines (34 loc) · 1.29 KB
/
ft_substr.c
File metadata and controls
37 lines (34 loc) · 1.29 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ychihab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 11:32:58 by ychihab #+# #+# */
/* Updated: 2022/10/24 08:20:08 by ychihab ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *ptr;
if (!s)
return (NULL);
if (start > ft_strlen(s))
return (ft_calloc(1, 1));
i = 0;
if (len > ft_strlen(s + start))
len = ft_strlen(s + start);
ptr = malloc(len + 1);
if (ptr == NULL)
return (0);
while (start + i < ft_strlen(s) && i < len)
{
ptr[i] = s[i + start];
i++;
}
ptr[i] = '\0';
return (ptr);
}