-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
74 lines (67 loc) · 1.68 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lrosch <lrosch@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/23 19:02:02 by lrosch #+# #+# */
/* Updated: 2021/07/01 19:15:14 by lrosch ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_check(const char c, const char *set)
{
int i;
i = 0;
while (set[i] != '\0')
{
if (c == set[i])
{
return (1);
}
i++;
}
return (0);
}
char *ft_allocate(const char *s1, const char *set)
{
int i;
int j;
char *str;
j = (ft_strlen(s1) - 1);
i = 0;
while (ft_check(s1[i], set) == 1 && s1[i] != '\0')
i++;
while (ft_check(s1[j], set) == 1 && j > i)
j--;
str = malloc(j - i + 2);
return (str);
}
char *ft_strtrim(const char *s1, const char *set)
{
int i;
int j;
int k;
char *str;
j = (ft_strlen(s1) - 1);
i = 0;
k = 0;
str = (ft_allocate(s1, set));
while (ft_check(s1[i], set) == 1 && s1[i] != '\0')
{
i++;
}
while (ft_check(s1[j], set) == 1 && j > i)
{
j--;
}
while (i <= j)
{
str[k] = s1[i];
k++;
i++;
}
str[k] = '\0';
return (str);
}