-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
46 lines (37 loc) · 1.42 KB
/
ft_substr.c
File metadata and controls
46 lines (37 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/19 21:51:13 by marvin #+# #+# */
/* Updated: 2022/05/19 22:11:09 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
unsigned int size;
char *ret;
size = ft_strlen(s);
if (!s)
return (NULL);
if (len <= 0 || start > size)
return (ft_strdup(""));
if (len > size - start)
len = size - start;
ret = malloc(sizeof(char) * (len + 1));
if (ret == NULL)
return (NULL);
ft_strlcpy(ret, (char *)(s + start), len + 1);
return (ret);
}
/*int main()
{
char src[50];
strcpy(src, "Hola como estas");
printf("%s\n", src);
printf("%s\n", ft_substr(src, 5, 6));
return 0;
}*/