-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
79 lines (72 loc) · 1.89 KB
/
Copy pathft_strtrim.c
File metadata and controls
79 lines (72 loc) · 1.89 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: etchipoq <etchipoq@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/16 12:16:08 by etchipoq #+# #+# */
/* Updated: 2025/11/07 15:43:41 by etchipoq ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_set(char c, const char *set)
{
int i;
i = 0;
while (set[i])
{
if (c == set[i])
return (1);
i++;
}
return (0);
}
static int le(const char *s, const char *s2)
{
int i;
int end;
i = 0;
end = ft_strlen(s) - 1;
while (s[i] && is_set(s[i], s2))
i++;
while (end >= 0 && is_set(s[end], s2))
end--;
if (end - i < 0)
return (0);
return (end - i + 1);
}
char *ft_strtrim(char const *s1, char const *set)
{
int i;
char *str;
int end;
i = 0;
if (!s1 || !set)
return (NULL);
end = ft_strlen(s1) - 1;
str = malloc(sizeof(char) * (le(s1, set) + 1));
if (!str)
return (NULL);
while (is_set(s1[i], set) && s1[i])
i++;
while (is_set(s1[end], set) && end >= 0)
end--;
if (end < i)
{
str[0] = '\0';
return (str);
}
ft_strlcpy(str, &s1[i], le(s1, set) + 1);
return (str);
}
/* #include <stdio.h>
int main (void)
{
//s = ft_strtrim("tripouille xxx", " x");
char *p=" xxx xxx";
char *sep = " x";
char *res = ft_strtrim(p,sep);
printf("%s\n", res);
free(res);
} */