-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
49 lines (45 loc) · 1.44 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bhagenlo <[email protected]. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/28 19:10:41 by bhagenlo #+# #+# */
/* Updated: 2022/04/08 11:36:03 by bhagenlo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_in(int c, char const *set)
{
while (*set)
{
if (c == *set)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(char const *s, char const *set)
{
char *str;
size_t i;
size_t start;
size_t end;
if (!s || !set)
return (NULL);
start = 0;
while (s[start] && is_in(s[start], set))
start++;
end = ft_strlen(s);
while (end > start && is_in(s[end - 1], set))
end--;
str = (char *)malloc(sizeof(*s) * (end - start + 1));
if (!str)
return (NULL);
i = 0;
while (start < end)
str[i++] = s[start++];
str[i] = 0;
return (str);
}