-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
60 lines (52 loc) · 1.68 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahmaymou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/09 12:21:30 by ahmaymou #+# #+# */
/* Updated: 2022/10/26 16:37:46 by ahmaymou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_set(const char *set, const char c)
{
int i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
static char *trim_first(const char *s, const char *set)
{
while (*s && is_set(set, *s))
s++;
return ((char *)s);
}
static size_t trim_last(const char *s, const char *set, size_t len_s1)
{
char *temp;
temp = (char *)s;
while (is_set(set, temp[len_s1 - 1]) && len_s1)
len_s1--;
return (len_s1 + 1);
}
char *ft_strtrim(char const *s1, char const *set)
{
unsigned int len_s1;
char *to_return;
if (!s1 || !set)
return (NULL);
s1 = trim_first(s1, set);
len_s1 = trim_last(s1, set, ft_strlen(s1));
to_return = (char *)malloc((len_s1) * sizeof(char));
if (!to_return)
return (NULL);
ft_strlcpy(to_return, s1, len_s1);
return (to_return);
}