-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
74 lines (67 loc) · 1.62 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
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_strtrim.c :+: :+: */
/* +:+ */
/* By: jvan-hal <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/10/12 14:43:27 by jvan-hal #+# #+# */
/* Updated: 2022/10/26 10:09:59 by jvan-hal ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int getstart(const char *s, const char *set)
{
int i;
int j;
int inset;
i = 0;
while (s[i])
{
j = 0;
inset = 0;
while (!inset && set[j])
{
if (s[i] == set[j])
inset = 1;
++j;
}
if (!inset)
break ;
++i;
}
return (i);
}
static int getlast(const char *s, const char *set)
{
int i;
int j;
int inset;
i = ft_strlen(s) - 1;
while (i >= 0)
{
j = 0;
inset = 0;
while (!inset && set[j])
{
if (s[i] == set[j])
inset = 1;
++j;
}
if (!inset)
break ;
--i;
}
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int end;
i = getstart(s1, set);
end = getlast(s1, set);
if (end == -1)
i = 0;
return (ft_substr(s1, i, (end - i) + 1));
}