-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
64 lines (57 loc) · 1.61 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
57
58
59
60
61
62
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: motoure <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/10 21:53:40 by motoure #+# #+# */
/* Updated: 2020/01/08 16:14:31 by motoure ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
int check_set(char c, char const *set)
{
int i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
int len_trim(char *s1, const char *set)
{
int i;
int y;
y = 0;
i = 0;
while (check_set(s1[i], set) && s1[i])
i++;
if (s1[i] == '\0')
return (1);
y = i;
i = ft_strlen((char *)s1) - 1;
while (check_set(s1[i], set))
i--;
return (i - y + 1);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
int len;
char *s2;
if (!s1 || !set)
return (ft_strdup(""));
s2 = (char*)s1;
len = len_trim(s2, set);
if (!(str = malloc(sizeof(char) * len + 1)))
return (0);
while (check_set(*s2, set))
s2++;
ft_strlcpy(str, s2, len + 1);
return (str);
}