-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
56 lines (50 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: retahri <retahri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/02 15:52:53 by retahri #+# #+# */
/* Updated: 2024/11/04 15:44:43 by retahri ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int filter_str(const char *str, char c)
{
size_t idx;
idx = 0;
while (str[idx])
{
if (str[idx] == c)
return (1);
idx++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *copy;
size_t start;
size_t end;
if (!s1)
return (NULL);
if (!set)
return (ft_strdup(s1));
start = 0;
while (s1[start] && filter_str(set, s1[start]))
start++;
end = ft_strlen(s1) - 1;
if (s1[start] == '\0')
return (ft_strdup(""));
while (filter_str(set, s1[end]) && (end > start))
end--;
copy = ft_substr (s1, start, (end - start) + 1);
return (copy);
}
// int main()
// {
// char *arr;
// printf("%s", arr = ft_strtrim(".a.a.a.a.hbcd.a..aaa...a", ".a"));
// free(arr);
// }