-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
59 lines (54 loc) · 1.63 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_strtrim.c :+: :+: */
/* +:+ */
/* By: lbartels <[email protected]> +#+ */
/* +#+ */
/* Created: 2023/10/09 14:07:33 by lbartels #+# #+# */
/* Updated: 2023/10/09 14:07:33 by lbartels ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static int check_len(int start, int end)
{
if (end - start < 0)
return (0);
return (end - start);
}
static int check_set(char const s, char const *set)
{
while (*set != '\0')
{
if (s == *set)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(char const *s, char const *set)
{
char *ptr;
int i;
int start;
int end;
start = 0;
while (check_set(s[start], set) == 1)
start++;
end = ft_strlen((char *)s);
if (end != 0)
while (check_set(s[end - 1], set) == 1)
end--;
ptr = (char *)malloc(sizeof(char) * check_len(start, end) + 1);
if (!ptr)
return (NULL);
i = 0;
while (end > start)
{
ptr[i] = s[start];
i++;
start++;
}
ptr[i] = '\0';
return (ptr);
}