-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
76 lines (69 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
65
66
67
68
69
70
71
72
73
74
75
76
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmaaqoul <bmaaqoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/12 18:22:20 by bmaaqoul #+# #+# */
/* Updated: 2021/11/15 16:08:52 by bmaaqoul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t first(char const *src, char const *set)
{
size_t i;
size_t j;
j = 0;
i = 0;
while (src[j] && set[i])
{
if (src[j] == set[i])
{
i = 0;
j++;
}
else
i++;
}
return (j);
}
static size_t last(char const *src, char const *set)
{
size_t i;
size_t j;
j = ft_strlen(src);
i = 0;
while (set[i])
{
if (src[j - 1] == set[i])
{
i = 0;
j--;
}
else
i++;
}
return (j);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t l;
size_t f;
size_t i;
char *ptr;
if (!s1 || !set)
return (NULL);
i = 0;
f = first(s1, set);
l = last(s1, set);
if (f >= l)
return (ft_strdup(""));
ptr = (char *) malloc(sizeof(char) * (l - f + 1));
if (!ptr)
return (NULL);
while (f < l)
ptr[i++] = s1[f++];
ptr[i] = '\0';
return (ptr);
}