-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
49 lines (44 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_trim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hmoumani <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/19 16:04:31 by hmoumani #+# #+# */
/* Updated: 2019/10/25 13:01:01 by hmoumani ### ########.fr */
/* */
/* ************************************************************************** */
#include "lib.h"
static int ft_check(char const c, char const *set)
{
int i;
i = 0;
while (set[i] != '\0')
{
if (c == set[i])
return (1);
else
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
int s1_len;
int i;
char *trim;
if (!s1 || !set)
return (NULL);
s1_len = ft_strlen(s1);
i = 0;
trim = NULL;
while (s1[i] != '\0' && ft_check(s1[i], set))
i++;
while (s1_len > 0 && ft_check(s1[s1_len - 1], set))
s1_len--;
if (i > (s1_len - 1))
return (ft_strdup(""));
trim = ft_substr(s1, i, (s1_len - i));
return (trim);
}