-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
executable file
·80 lines (71 loc) · 1.99 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chonorat <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/16 14:29:08 by chonorat #+# #+# */
/* Updated: 2022/12/05 16:36:34 by chonorat ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int check_set(char c, char const *set)
{
int index;
index = 0;
while (set[index])
{
if (c == set[index])
return (1);
index++;
}
return (0);
}
static int check_onlyset(char const *s1, char const *set)
{
int index_j;
index_j = 0;
while (s1[index_j])
{
if (check_set(s1[index_j], set) == 0)
return (0);
index_j++;
}
return (1);
}
static char *str_cpy(char *dst, char const *src, int index, int index_j)
{
int index_k;
index_k = 0;
while (index <= index_j)
dst[index_k++] = src[index++];
dst[index_k] = '\0';
return (dst);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *new_str;
int index;
int index_j;
index = 0;
index_j = 0;
new_str = NULL;
if (!s1 || !set)
return (NULL);
if (check_onlyset(s1, set) == 1)
{
new_str = (char *)malloc(sizeof(""));
return (new_str);
}
while (check_set(s1[index], set))
index++;
while (s1[index_j + 1])
index_j++;
while (check_set(s1[index_j], set))
index_j--;
new_str = (char *)malloc(sizeof(char) * ((index_j - index) + 2));
if (!new_str)
return (NULL);
return (str_cpy(new_str, s1, index, index_j));
}