-
Notifications
You must be signed in to change notification settings - Fork 9
/
ft_strtrim.c
48 lines (44 loc) · 1.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rchallie <rchallie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/10 14:54:46 by rchallie #+# #+# */
/* Updated: 2019/10/23 10:40:53 by rchallie ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_in_set(char c, const char *set)
{
while (*set)
if (c == *set++)
return (0);
return (1);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t end;
char *rtn;
if (!s1)
return (NULL);
if (!set)
return (ft_strdup(s1));
start = 0;
end = ft_strlen(s1);
while (is_in_set(s1[start], set) == 0)
start++;
if (start == ft_strlen(s1))
{
if (!(rtn = ft_strdup("")))
return (NULL);
else
return (rtn);
}
while (is_in_set(s1[end - 1], set) == 0)
end--;
rtn = ft_substr(s1, start, end - start);
return (rtn);
}