-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
53 lines (45 loc) · 1.58 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anamartinez <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 15:17:51 by anamartinez #+# #+# */
/* Updated: 2023/05/05 14:04:32 by anamartinez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_index_start(char const *s1, char const *set)
{
int i;
i = 0;
while (s1[i] && ft_strchr(set, s1[i]))
i++;
return (i);
}
static int get_index_end(char const *s1, char const *set)
{
int i;
i = ft_strlen(s1) - 1;
while (i >= 0 && ft_strchr(set, s1[i]))
i--;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
int index_begin;
int index_end;
if (!s1 || !set)
return (NULL);
index_begin = get_index_start(s1, set);
index_end = get_index_end(s1, set);
return (ft_substr(s1, index_begin, index_end + 1 - index_begin));
}
// int main(void)
// {
// char s1[] = "fsabcdef";
// char s2[] = "fas";
// printf("%s", ft_strtrim(s1, s2));
// return (0);
// }