-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
55 lines (49 loc) · 1.55 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rjada <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 11:24:31 by rjada #+# #+# */
/* Updated: 2021/10/10 19:14:14 by rjada ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int is_set(char str_pos, char const *set);
char *ft_strtrim(char const *s1, char const *set)
{
char *res;
int start;
int end;
int i;
if (!s1 || !set)
return (NULL);
start = 0;
while (s1[start] && is_set(s1[start], set))
++start;
end = ft_strlen(s1);
while (end > start && is_set(s1[end - 1], set))
--end;
res = (char *) malloc(sizeof(char) * (end - start + 1));
if (!res)
return (NULL);
i = 0;
while (start < end)
res[i++] = s1[start++];
res[i] = 0;
return (res);
}
static int is_set(char str_pos, char const *set)
{
int i;
i = 0;
while (set[i])
{
if (set[i] == str_pos)
return (1);
i++;
}
return (0);
}