-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
51 lines (47 loc) · 1.54 KB
/
ft_substr.c
File metadata and controls
51 lines (47 loc) · 1.54 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
47
48
49
50
51
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edegraev <edegraev@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 21:53:45 by edegraev #+# #+# */
/* Updated: 2023/11/11 12:17:54 by edegraev ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t i;
size_t size;
if (!s)
return (NULL);
if ((unsigned int)ft_strlen(s) < start)
return (ft_strdup(""));
size = ft_strlen(s + start);
if (size < len)
len = size;
str = (char *)malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
i = 0;
while (i < len)
{
str[i] = s[start + i];
i++;
}
str[i] = '\0';
return (str);
}
// #include <stdio.h>
// int main(void)
// {
// char *str = "test";
// char *out = ft_substr(str, 1, 2);
// printf("%s\n", out);
// free(out);
// out = ft_substr("tripouille", 1, 1);
// printf("%s\n", out);
// return (0);
// }