-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
46 lines (41 loc) · 1.4 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-khni <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/05 20:48:14 by ael-khni #+# #+# */
/* Updated: 2021/11/10 09:22:16 by ael-khni ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static int in_set(char const c, char const *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 start;
int end;
if (!s1)
return (NULL);
start = 0;
end = ft_strlen(s1) - 1;
while (s1[start] && in_set(s1[start], set))
start++;
while (s1[end] && in_set(s1[end], set))
end--;
if (end == -1)
return (ft_substr(s1, start, 0));
return (ft_substr(s1, start, (end - start + 1)));
}