-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
66 lines (59 loc) · 1.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wbertoni <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/23 17:13:40 by wbertoni #+# #+# */
/* Updated: 2020/01/30 15:43:42 by wbertoni ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_findchar(const char *set, char c)
{
int i;
i = 0;
while ((char)set[i])
{
if ((char)set[i] == c)
return (1);
i++;
}
return (0);
}
static int ft_start(char const *s1, char const *set, int i)
{
while (ft_findchar(set, (char)s1[i]))
i++;
return (i);
}
static int ft_end(char const *s1, char const *set, int i)
{
while (ft_findchar(set, (char)s1[i]))
i--;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *cpy;
int lstindex;
size_t i;
if (s1 == NULL || set == NULL)
return (NULL);
i = ft_start(s1, set, 0);
if (i != ft_strlen(s1))
{
lstindex = ft_end(s1, set, ft_strlen(s1) - 1);
cpy = ft_substr(s1, i, (lstindex - i) + 1);
if (cpy == NULL)
return (NULL);
}
else
{
cpy = (char *)ft_calloc(1, 1 * sizeof(char));
if (cpy == NULL)
return (NULL);
}
return (cpy);
}