-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
53 lines (48 loc) · 1.51 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cnatanae <cnatanae@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/16 13:53:17 by cnatanae #+# #+# */
/* Updated: 2023/10/25 16:51:09 by cnatanae ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_comp(char c, char const *set)
{
int idx;
idx = 0;
while (set[idx])
{
if (c == set[idx])
{
return (1);
}
idx++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
int start;
int end;
int idx;
char *nstr;
end = ft_strlen(s1);
start = 0;
idx = 0;
while (ft_comp(s1[start], set))
start++;
while (ft_comp(s1[end - 1], set) && end > 0)
end--;
if (start >= end)
return ((char *)ft_calloc(1, sizeof(char)));
nstr = (char *)ft_calloc(1, sizeof(char) * (end - start + 1));
if (!nstr)
return (NULL);
while (start < end)
nstr[idx++] = s1[start++];
return (nstr);
}