-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
62 lines (55 loc) · 1.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amahla <amahla@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:36:57 by amahla #+# #+# */
/* Updated: 2022/05/02 12:36:59 by amahla ### ########.fr */
/* */
/* ************************************************************************** */
#include"libft.h"
static size_t is_set(char c, char const *set)
{
size_t i;
i = 0;
while (set && *(set + i))
{
if (c == *(set + i++))
return (1);
}
return (0);
}
static size_t slen(char const *str)
{
size_t i;
i = 0;
while (str && *(str + i))
i++;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *dst;
size_t i;
size_t size;
i = 0;
if (!s1 || !set)
return (NULL);
while (is_set(*s1, set))
s1++;
size = slen(s1);
while (size != 0 && is_set(*(s1 + size - 1), set))
size--;
dst = malloc((size + 1) * sizeof(char));
if (!dst)
return (NULL);
while (*(s1 + i) && i < size)
{
*(dst + i) = *(s1 + i);
i++;
}
*(dst + i) = '\0';
return (dst);
}