-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
56 lines (51 loc) · 1.49 KB
/
ft_strtrim.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: togauthi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/10 13:57:07 by togauthi #+# #+# */
/* Updated: 2024/10/15 13:48:26 by togauthi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int inside(const char c, const char *str)
{
size_t i;
i = 0;
while (str[i])
{
if (str[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t i;
size_t start;
size_t end;
char *trim;
if (!s1 || !set)
return (NULL);
i = 0;
start = 0;
while (inside(s1[start], set) && s1[start])
start++;
end = ft_strlen(s1);
while (inside(s1[end - 1], set) && end > start)
end --;
trim = malloc(end - start + 1);
if (!trim)
return (NULL);
while (start < end)
{
trim[i] = s1[start];
start++;
i++;
}
trim[i] = '\0';
return (trim);
}