-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
64 lines (57 loc) · 1.67 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: aymoulou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/19 18:07:40 by aymoulou #+# #+# */
/* Updated: 2021/11/11 21:43:38 by aymoulou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t trim_start(char const *s1, char const *set)
{
size_t i;
i = 0;
while (ft_memchr(set, s1[i], ft_strlen(set)) && s1[i])
{
i++;
}
return (i);
}
static size_t trim_end(char const *s1, char const *set)
{
size_t i;
i = ft_strlen(s1) - 1;
while (ft_memchr(set, s1[i], ft_strlen(set)) && s1[i] >= 0)
{
i--;
}
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
size_t end;
size_t start;
size_t i;
if (!s1 || !set)
return (0);
end = trim_end(s1, set);
start = trim_start(s1, set);
if (start == ft_strlen(s1) || !*s1)
return (ft_strdup(""));
str = (char *) malloc(sizeof(char) * ((end - start) + 2));
if (!str)
return (0);
i = 0;
while (start <= end)
{
str[i] = s1[start];
i++;
start++;
}
str[i] = 0;
return (str);
}