-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
61 lines (55 loc) · 1.61 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edegraev <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 16:05:46 by edegraev #+# #+# */
/* Updated: 2023/11/16 08:31:37 by edegraev ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_in_set(char c, char const *set)
{
while (*set)
if (c == *set++)
return (0);
return (1);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t end;
char *r;
if (!s1)
return (NULL);
if (!set)
return (ft_strdup(s1));
start = 0;
end = ft_strlen(s1);
while (is_in_set(s1[start], set) == 0)
start++;
if (start == ft_strlen(s1))
{
r = ft_strdup("");
if (!r)
return (NULL);
else
return (r);
}
while (is_in_set(s1[end - 1], set) == 0)
end--;
r = ft_substr(s1, start, end - start);
return (r);
}
// #include <stdio.h>
// int main(void)
// {
// char *s1;
// char *set;
// s1 = "Hello World";
// set = "HW";
// printf("%s", ft_strtrim(s1, set));
// return (0);
// }