-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
73 lines (65 loc) · 1.78 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ytaya <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 13:33:34 by ytaya #+# #+# */
/* Updated: 2021/11/06 20:24:39 by ytaya ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_ischarset(char c, const char *set)
{
while (*set)
{
if (c == *set)
return (1);
set++;
}
return (0);
}
static int ft_getfirst(char const *s1, char const *set)
{
int i;
i = 0;
while (ft_ischarset(s1[i], set) && s1[i])
i++;
return (i);
}
static int ft_getlast(char const *s1, char const *set)
{
int n;
n = ft_strlen(s1) - 1;
while (ft_ischarset(s1[n], set) && n > 0)
n--;
return (n);
}
char *ft_strtrim(char const *s1, char const *set)
{
int n;
char *string;
size_t start;
size_t end;
size_t i;
if (!s1)
return (0);
i = 0;
end = ft_getlast(s1, set);
start = ft_getfirst(s1, set);
if (end == 0 || start == ft_strlen(s1))
return (ft_strdup(""));
n = (end - start) + 2;
string = (char *) malloc(sizeof(char) * n);
if (!string)
return (0);
while (start <= end)
{
string[i] = s1[start];
i++;
start++;
}
string[i] = '\0';
return (string);
}