-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
56 lines (51 loc) · 1.46 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
50
51
52
53
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smurad <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/18 07:54:33 by smurad #+# #+# */
/* Updated: 2019/12/18 13:28:59 by smurad ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int isset(char c, char const *set)
{
int i;
i = 0;
while (set[i])
{
if (c == set[i])
return (1);
else
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
int iend;
char *res;
unsigned int i;
res = NULL;
i = 0;
if (!s1 || !set)
return (NULL);
iend = ft_strlen(s1) - 1;
while (s1[i] && isset(s1[i], set))
{
i++;
iend--;
}
if (iend >= 0)
{
while (isset(s1[iend + i], set))
iend--;
}
res = malloc(sizeof(char) * iend + 1);
if (res == NULL)
return (res);
res = ft_substr(s1, i, iend + 1);
return (res);
}