-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
82 lines (73 loc) · 1.81 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
77
78
79
80
81
82
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyalexan <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/22 10:28:01 by kyalexan #+# #+# */
/* Updated: 2021/12/15 14:47:15 by kyalexan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int in_set(char c, char const *set)
{
int i;
i = 0;
while (set[i])
{
if (c == set[i])
return (1);
i++;
}
return (0);
}
static int find_start(char const *s1, char const *set)
{
int start;
start = 0;
while (s1[start])
{
if (!in_set(s1[start], set))
break ;
start++;
}
return (start);
}
static int find_end(char const *s1, char const *set)
{
int end;
end = ft_strlen(s1);
while (s1[end - 1])
{
if (!in_set(s1[end - 1], set) || end == 1)
break ;
end--;
}
return (end);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *s;
int start;
int end;
start = find_start(s1, set);
end = find_end(s1, set);
if (start > end)
{
s = malloc(sizeof(char));
if (!s)
return (NULL);
*s = '\0';
return (s);
}
else
{
s = malloc(sizeof(char) * (end - start + 1));
if (!s)
return (NULL);
ft_memcpy(s, s1 + start, end - start);
s[end - start] = '\0';
}
return (s);
}