-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
84 lines (72 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
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yabenman <yabenman@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 06:49:46 by yabenman #+# #+# */
/* Updated: 2024/10/31 06:51:18 by yabenman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_exist(const char *ptr, char c)
{
int i;
i = 0;
while (ptr[i])
{
if (ptr[i] == c)
{
return (1);
}
i++;
}
return (0);
}
static int get_end(const char *s1, const char *set)
{
int len;
len = ft_strlen(s1);
if (len == 0)
return (0);
while (--len)
{
if (is_exist(set, s1[len]) == 0)
return (len);
}
return (0);
}
static int get_start(const char *s1, const char *set)
{
int i;
i = 0;
while (s1[i])
{
if (is_exist(set, s1[i]) == 0)
return (i);
i++;
}
return (-1);
}
char *ft_strtrim(const char *s1, const char *set)
{
int end;
int start;
if (!s1 || !set)
return ((char *)s1);
start = get_start(s1, set);
end = get_end(s1, set);
if (start < 0)
return (ft_strdup(""));
return (ft_substr(s1 + start, 0, (end - start + 1)));
}
/*
#include <stdio.h>
int main(void)
{
const char *s1 = "ba ter ab";
const char *s2 = "ab";
printf("%s.\n", ft_strtrim(s1, s2));
}
*/