-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
67 lines (58 loc) · 1.6 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
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rblondia <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/03 15:31:33 by rblondia #+# #+# */
/* Updated: 2021/11/09 17:05:16 by rblondia ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int contains(char c, char const *set)
{
int i;
if (!c || !set)
return (0);
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
int get_start(char const *s1, char const *set)
{
int i;
i = 0;
while (s1[i] && contains(s1[i], set))
i++;
return (i);
}
int get_end(char const *s1, char const *set)
{
int i;
i = ft_strlen(s1);
while (contains(s1[i - 1], set))
i--;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *new;
int start;
int end;
if (!s1)
return (NULL);
start = get_start(s1, set);
end = get_end(s1, set);
if (end < start)
return (ft_strdup(""));
new = ft_substr(s1, start, end - start);
if (!new)
return (NULL);
return (new);
}