-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
52 lines (47 loc) · 1.57 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: maceccar <maceccar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 15:43:00 by maceccar #+# #+# */
/* Updated: 2023/11/10 15:43:00 by maceccar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_contains_char(const char *str, char c)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *result;
int start_inx;
int end_idx;
int count;
int i;
start_inx = 0;
while (ft_contains_char(set, s1[start_inx]) && s1[start_inx])
start_inx++;
end_idx = ft_strlen(s1);
while (ft_contains_char(set, s1[end_idx - 1]) == 1 && end_idx > start_inx)
end_idx--;
count = end_idx - start_inx + 1;
result = malloc(sizeof(char) * (count));
if (!result)
return (NULL);
i = 0;
while (start_inx < end_idx)
result[i++] = s1[start_inx++];
result[i] = '\0';
return (result);
}