-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
80 lines (73 loc) · 1.79 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
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htopa <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/27 16:40:04 by htopa #+# #+# */
/* Updated: 2024/05/03 16:41:42 by htopa ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_start_index(char const *s1, char const *set)
{
int i;
int j;
int found;
i = 0;
while (s1[i] != '\0')
{
j = 0;
found = 0;
while (set[j] != '\0')
{
if (s1[i] != set[j])
j++;
else
{
i++;
found = 1;
}
}
if (set[j] == '\0' && found == 0)
return (i);
}
return (-1);
}
static int get_end_index(char const *s1, char const *set)
{
int i;
int j;
int found;
i = ft_strlen(s1) - 1;
while (i >= 0)
{
j = 0;
found = 0;
while (set[j] != '\0')
{
if (s1[i] != set[j])
j++;
else
{
i--;
found = 1;
}
}
if (set[j] == '\0' && found == 0)
return (i);
}
return (-1);
}
char *ft_strtrim(char const *s1, char const *set)
{
int start;
int end;
start = get_start_index(s1, set);
end = get_end_index(s1, set);
if (start == -1 && end == -1)
return (ft_strdup("\0"));
else
return (ft_substr(s1, start, end - start + 1));
}