-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
52 lines (46 loc) · 1.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dpaluszk <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/10 18:43:10 by dpaluszk #+# #+# */
/* Updated: 2024/03/20 14:31:07 by dpaluszk ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_check(char c, const char *set);
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
size_t beginning;
size_t end;
size_t new;
beginning = 0;
end = ft_strlen(s1);
new = 0;
while (s1[beginning] != '\0' && ft_check(s1[beginning], set))
beginning++;
while (end > beginning && ft_check(s1[end - 1], set))
end--;
str = (char *)malloc(sizeof(char) * (end - beginning + 1));
if (str == NULL)
return (NULL);
while (beginning < end)
str[new++] = s1[beginning++];
str[new] = '\0';
return (str);
}
static int ft_check(char c, const char *set)
{
size_t i;
i = 0;
while (set[i] != '\0')
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}