-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
44 lines (41 loc) · 1.51 KB
/
ft_substr.c
File metadata and controls
44 lines (41 loc) · 1.51 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simarcha <simarcha@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 20:11:25 by simarcha #+# #+# */
/* Updated: 2024/01/20 20:52:31 by simarcha ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
//we copy s into a malloc string from start up to start + len
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
if (!s)
return (NULL);
if (start > ft_strlen(s))
return (ft_calloc(1, sizeof(char)));
if (start + len > ft_strlen(s))
len = ft_strlen(s + start);
str = ft_calloc((len + 1), sizeof(char));
if (!str)
return (NULL);
while (len-- > 0)
str[len] = s[start + len];
return (str);
}
/*
#include <stdio.h>
int main(void)
{
char *res;
char const *s = "This is a test";
unsigned int start = 3;
size_t len = 5;
res = ft_substr(s, start, len);
printf("%s\n", res);
return (0);
}*/