-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
39 lines (36 loc) · 1.36 KB
/
ft_substr.c
File metadata and controls
39 lines (36 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgalan-n <fgalan-n@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 13:35:39 by fgalan-n #+# #+# */
/* Updated: 2022/10/04 11:51:07 by fgalan-n ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *str;
if (!s)
return (NULL);
if (start >= ft_strlen(s))
return ((char *)ft_calloc(1, sizeof(char)));
if (ft_strlen(s) <= start + len)
str = malloc(sizeof(char) * (ft_strlen(s) - start + 1));
else
str = malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
i = 0;
while (s[start] && (i < len))
{
str[i] = s[start];
i++;
start++;
}
str[i] = '\0';
return (str);
}