-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
49 lines (45 loc) · 1.41 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: tpouget <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/14 17:04:38 by tpouget #+# #+# */
/* Updated: 2021/04/19 15:33:04 by tpouget ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_in_set(char c, char const *set)
{
if (!set)
return (0);
while (*set)
{
if (*set == c)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int len;
char *trimmed;
i = 0;
len = 0;
if (!s1)
return (NULL);
while (is_in_set(s1[len], set))
len++;
i = len;
while (s1[len])
len++;
while (len - 1 >= 0 && is_in_set(s1[len - 1], set) && len != i)
len--;
trimmed = ft_strndup(s1 + i, len - i);
if (!trimmed)
return (NULL);
return (trimmed);
}