-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
107 lines (96 loc) · 1.99 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
95
96
97
98
99
100
101
102
103
104
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sahafid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/05 08:50:17 by sahafid #+# #+# */
/* Updated: 2021/11/07 12:31:09 by sahafid ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *nullcase(void)
{
char *c;
c = (char *)malloc(sizeof(char));
if (!c)
return (NULL);
c[0] = '\0';
return (c);
}
static int reversedepart(int j, char *set, char *a)
{
int o;
o = 0;
while (j > 0)
{
o = 0;
while (set[o])
{
if (set[o] == a[j])
break ;
o++;
}
if (set[o] != a[j])
break ;
j--;
}
return (j);
}
static int depart(int i, char *b, char *a)
{
int o;
o = 0;
while (a[i])
{
o = 0;
while (b[o])
{
if (b[o] == a[i])
break ;
o++;
}
if (b[o] != a[i])
break ;
i++;
}
return (i);
}
static char *newarray(int i, int j, char *a)
{
int o;
char *c;
o = 0;
c = NULL;
c = (char *)malloc((j - i + 2) * sizeof(char));
if (!c)
return (NULL);
while (i <= j)
{
c[o] = a[i];
o++;
i++;
}
c[o] = '\0';
return (c);
}
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int j;
char *a;
char *b;
i = 0;
j = 0;
a = (char *)s1;
b = (char *)set;
if (!(s1) || !(set))
return (NULL);
i = depart(i, b, a);
j = ft_strlen(a);
j = reversedepart(j, b, a);
if (i > j)
return (nullcase());
return (newarray(i, j, a));
}