-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
77 lines (68 loc) · 1.8 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aerrajiy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 13:55:28 by aerrajiy #+# #+# */
/* Updated: 2022/10/18 22:38:39 by aerrajiy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_set(char const *set, char c)
{
int i;
i = 0;
while (set[i] != '\0')
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
static int first_index(char const *s1, char const *set)
{
int s_len;
s_len = 0;
while (s1[s_len] != '\0')
{
if (!is_set(set, s1[s_len]))
break ;
s_len++;
}
return (s_len);
}
static int last_index(char const *s1, char const *set, int where_first)
{
int l_len;
l_len = ft_strlen(s1) - 1;
while (l_len >= where_first)
{
if (!is_set(set, s1[l_len]))
break ;
l_len--;
}
return (l_len + 1);
}
char *ft_strtrim(char const *s1, char const *set)
{
int last;
int i;
int first;
char *new;
i = 0;
if (!s1 || !set)
return (NULL);
first = first_index(s1, set);
last = last_index(s1, set, first);
new = malloc(((last - first) + 1) * sizeof(char));
if (new != NULL)
{
while (first < last)
new[i++] = s1[first++];
new[i] = '\0';
}
return (new);
}