-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
39 lines (37 loc) · 1.38 KB
/
ft_substr.c
File metadata and controls
39 lines (37 loc) · 1.38 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: belkarto <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 11:06:56 by belkarto #+# #+# */
/* Updated: 2022/10/24 12:16:48 by belkarto ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t s_len;
size_t i;
i = 0;
if (!s)
return (0);
s_len = ft_strlen(s);
if (start > s_len)
str = (char *)malloc(1);
else if (len > s_len - start)
str = (char *)malloc(sizeof(char) * (s_len - start + 1));
else
str = (char *)malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
while (i < len && i + start < s_len && s[i])
{
str[i] = s[i + start];
i++;
}
str[i] = '\0';
return (str);
}