-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
68 lines (61 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eel-orch <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/20 11:26:04 by eel-orch #+# #+# */
/* Updated: 2019/11/05 02:15:10 by eel-orch ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int srch(char *s, char c)
{
int i;
i = 0;
while (s[i] != '\0')
{
if (s[i++] == c)
return (1);
}
return (0);
}
static int lasts(char *s, char c)
{
int i;
i = 0;
while (s[i] != '\0')
{
if (s[i++] == c)
return (1);
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
int index;
int str;
int end;
char *ptr;
if (s1 == 0)
return (0);
index = ft_strlen(s1);
str = 0;
end = 0;
while (s1[str] != '\0' && srch((char *)set, s1[str]))
str++;
while (index > 0 && lasts((char *)set, s1[--index]))
end++;
if (str + end > (int)ft_strlen(s1))
end = 0;
index = 0;
if ((ptr = (char *)malloc(ft_strlen(s1) + 1 - str - end)))
{
while (str < (int)ft_strlen(s1) - end)
ptr[index++] = s1[str++];
ptr[index] = '\0';
return (ptr);
}
return (0);
}