-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
94 lines (86 loc) · 2.17 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jvico-ga <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/17 15:55:55 by jvico-ga #+# #+# */
/* Updated: 2021/09/24 20:16:52 by jvico-ga ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int found_initial(char const *s1, char const *set);
static int found_final(char const *s1, char const *set);
char *ft_strtrim(char const *s1, char const *set)
{
int t;
char *ptr;
t = 0;
if (s1 == NULL)
return (NULL);
if (found_final(s1, set) < found_initial(s1, set))
{
ptr = malloc(1 * sizeof(char));
if (ptr == NULL)
return (NULL);
ptr[0] = '\0';
return (ptr);
}
ptr = malloc((found_final(s1, set)
- found_initial(s1, set) + 2) * sizeof(char));
if (ptr == NULL)
return (NULL);
while (t <= (found_final(s1, set) - found_initial(s1, set)))
{
ptr[t] = s1[found_initial(s1, set) + t];
t++;
}
ptr[t] = '\0';
return (ptr);
}
static int found_initial(char const *s1, char const *set)
{
int t;
int v;
int boolean;
t = 0;
boolean = 0;
while (s1[t] != '\0')
{
v = 0;
boolean = 0;
while (set[v] != '\0' && boolean == 0)
{
if (s1[t] == set[v])
boolean = 1;
v++;
}
if (boolean == 0)
return (t);
t++;
}
return (t);
}
static int found_final(char const *s1, char const *set)
{
int t;
int v;
int boolean;
t = ft_strlen(s1) - 1;
while (t > 0)
{
v = 0;
boolean = 0;
while (set[v] != '\0' && boolean == 0)
{
if (s1[t] == set[v])
boolean = 1;
v++;
}
if (boolean == 0)
return (t);
t--;
}
return (t);
}