-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
64 lines (57 loc) · 1.66 KB
/
ft_strtrim.c
File metadata and controls
64 lines (57 loc) · 1.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchemin <tchemin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/07 15:00:37 by tchemin #+# #+# */
/* Updated: 2025/11/10 16:06:48 by tchemin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_is_in_set(char c, char *set)
{
int i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
int ft_last_good_char(char *str, char *set)
{
int i;
i = (int)ft_strlen(str) - 1;
while (ft_is_in_set(str[i], (char *)set) && i >= 0)
i--;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *s2;
int i;
int j;
int index_s2;
if (s1 == NULL || set == NULL)
return (NULL);
i = 0;
while (ft_is_in_set(s1[i], (char *)set))
i++;
j = ft_last_good_char((char *)s1, (char *)set);
if (j < i)
return (ft_calloc(1, sizeof(char)));
s2 = ft_calloc((j - i + 2), sizeof(char));
if (!s2)
return (NULL);
index_s2 = 0;
while (index_s2 <= j - i)
{
s2[index_s2] = s1[i + index_s2];
index_s2++;
}
return (s2);
}