-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
55 lines (50 loc) · 1.46 KB
/
ft_substr.c
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
52
53
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emdi-mar <emdi-mar@student.42roma.it> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/08 20:27:22 by emdi-mar #+# #+# */
/* Updated: 2024/11/08 20:27:28 by emdi-mar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t adj_len(char const *s, unsigned int start, size_t len)
{
size_t a_len;
size_t s_len;
a_len = 0;
s_len = ft_strlen(s);
if (start < s_len)
{
a_len = s_len - start;
if (a_len > len)
a_len = len;
}
return (a_len);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *p;
size_t i;
size_t len_s;
p = NULL;
if (s)
{
len = adj_len(s, start, len);
p = ft_calloc(len + 1, sizeof(*p));
}
if (p)
{
len_s = ft_strlen(s);
i = 0;
while (start < len_s && i < len)
{
p[i] = s[start];
start++;
i++;
}
}
return (p);
}