-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
92 lines (85 loc) · 1.96 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
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eagoumi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/18 11:44:57 by eagoumi #+# #+# */
/* Updated: 2022/11/08 20:35:52 by eagoumi ### ########.fr */
/* */
/* ************************************************************************** */
#include "./libft.h"
int ft_checkstart(char const *realstr, char const *sh, int count)
{
int i;
size_t j;
size_t len;
i = 0;
len = ft_strlen(sh);
while (realstr[i])
{
j = 0;
while (sh[j])
{
if (realstr[i] == sh[j])
{
count++;
break ;
}
else
j++;
if (j == len)
return (count);
}
i++;
}
return (count);
}
int ft_checkend(char const *lasstr, char const *seh, int cont)
{
int i;
size_t j;
int lendx;
i = ft_strlen(lasstr) - 1;
lendx = ft_strlen(lasstr);
while (i >= 0)
{
j = 0;
while (seh[j])
{
if (lasstr[i] == seh[j])
{
cont++;
break ;
}
else
j++;
if (j == ft_strlen(seh))
return (lendx - cont);
}
i--;
}
return (lendx - cont);
}
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int j;
char *str;
char *temp;
int n;
n = 0;
if (s1 == 0 || set == 0)
return (NULL);
i = ft_checkstart(s1, set, 0);
j = ft_checkend(s1, set, 0);
temp = ft_substr(s1, i, (j - i));
if (temp == NULL)
return (NULL);
str = ft_strdup(temp);
if (str == NULL)
return (NULL);
free(temp);
return (str);
}