-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
52 lines (47 loc) · 1.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbrown <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/18 15:06:05 by jbrown #+# #+# */
/* Updated: 2022/01/28 14:25:31 by jbrown ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int trimmatch(char c, const char *set)
{
int i;
i = 0;
while (set[i])
{
if (c == set[i])
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
int first;
int last;
int i;
char *s2;
if (!(s1 && set))
return (NULL);
first = 0;
while (trimmatch(s1[first], set) && s1[first])
first++;
last = ft_strlen(s1);
while (trimmatch(s1[last - 1], set) && last > first)
last--;
s2 = malloc((last - first + 1) * sizeof(*s2));
if (!s2)
return (NULL);
i = 0;
while (first < last)
s2[i++] = s1[first++];
s2[i] = '\0';
return (&s2[0]);
}