-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
74 lines (65 loc) · 1.71 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
65
66
67
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nmattera <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/09 17:57:09 by nmattera #+# #+# */
/* Updated: 2022/06/11 11:01:37 by nmattera ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int check(char s, char const *set)
{
int i;
i = -1;
while (set[++i])
if (set[i] == s)
return (1);
return (0);
}
int ft_rtrim(char const *str, char const *set)
{
int i;
size_t count;
i = 0;
count = 0;
while (str[i])
i++;
i--;
while (i >= 0 && check(str[i], set))
{
i--;
count++;
}
if (i < 0)
return (0);
return (count);
}
int ft_trim(char const *str, char const *set)
{
size_t i;
i = 0;
while (str[i] && check(str[i], set))
i++;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t i;
int count;
char *dest;
count = ft_trim(s1, set) + ft_rtrim(s1, set);
dest = malloc(sizeof(char) * (ft_strlen(s1) - count + 1));
if (!dest)
return (NULL);
i = 0;
while (i < (ft_strlen(s1) - count))
{
dest[i] = s1[ft_trim(s1, set) + i];
i++;
}
dest[i] = 0;
return (dest);
}