-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
84 lines (77 loc) · 1.76 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oakoudad <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 16:04:30 by oakoudad #+# #+# */
/* Updated: 2021/11/12 14:15:21 by oakoudad ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char const *ft_start(char const *s1, char const *set)
{
int i;
int status;
while (*s1)
{
i = 0;
status = 0;
while (set[i])
{
if (set[i] == *s1 && status == 0)
{
s1++;
status = 1;
}
i++;
}
if (status == 0)
break ;
}
return ((char *)s1);
}
int ft_end(char *s1, char const *set)
{
int i;
int status;
int l;
l = ft_strlen (s1) - 1;
while (l >= 0)
{
i = 0;
status = 0;
while (set[i])
{
if (set[i] == s1[l] && status == 0)
{
l--;
status = 1;
}
i++;
}
if (status == 0)
break ;
}
return (l + 1);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *p;
int i;
if (!s1 || !set)
return (NULL);
s1 = ft_start(s1, set);
p = (char *) malloc (ft_end ((char *)s1, set) + 1);
if (!p)
return (NULL);
i = 0;
while (i < ft_end((char *)s1, set))
{
p[i] = s1[i];
i++;
}
p[i] = 0;
return (p);
}