-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
63 lines (58 loc) · 1.52 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
56
57
58
59
60
61
62
63
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pboucher <pboucher@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/11 16:30:17 by pboucher #+# #+# */
/* Updated: 2024/10/14 21:20:21 by pboucher ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count(char const *s, unsigned int start, size_t len)
{
unsigned int i;
size_t j;
i = 0;
j = 0;
while (s[i] != 0)
{
if (i == start)
{
while (j < len && s[i] != 0)
{
i++;
j++;
}
break ;
}
i++;
}
return (j + 1);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
int count;
unsigned int i;
size_t j;
count = ft_count(s, start, len);
str = ft_calloc(count, 1);
if (!str)
return (NULL);
i = 0;
j = 0;
while (s[i])
{
if (i == start)
{
while (j < len && s[i])
str[j++] = s[i++];
break ;
}
i++;
}
str[j] = 0;
return (str);
}