-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
executable file
·77 lines (70 loc) · 1.65 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: zdnaya <zdnaya@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/23 16:11:37 by zdnaya #+# #+# */
/* Updated: 2019/11/09 03:07:45 by zdnaya ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *firstocc(char const *s, const char *set)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (s[i] && set[j])
{
if (s[i] == set[j])
{
i++;
j = 0;
}
else
j++;
}
return ((char*)&s[i]);
}
static int lastocc(char const *s, char const *set)
{
int i;
int j;
i = 0;
j = ft_strlen(s);
j--;
while (j > 0 && set[i])
{
if (set[i] == s[j])
{
j--;
i = 0;
}
else
{
i++;
}
}
return (j);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *tmp;
int i;
i = 0;
if (!s1)
return (NULL);
s1 = firstocc(s1, set);
tmp = (char *)malloc(sizeof(char) * lastocc(s1, set) + 2);
if (tmp == NULL)
return (NULL);
while (i < lastocc(s1, set) + 1)
{
tmp[i] = s1[i];
i++;
}
tmp[i] = '\0';
return (tmp);
}