-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
46 lines (42 loc) · 1003 Bytes
/
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
#include "libft.h"
static int ft_get_trm_start_stop(const char *s1, const char *set, int counter)
{
size_t j;
j = 0;
while (set[j] != s1[counter] && set[j] != '\0')
j++;
if (set[j] == s1[counter])
return (1);
return (0);
}
/* Trims a string in regards to a reference set. Occurence of every single
character of the string (set), no matter it's order, is being removed at the
beginning and the end of s1. The return value is NULL if the allocation fails
or the trimmed copy of s1 (char *cpys). */
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int k;
int start_trm;
int stop_trm;
char *cpys;
i = 0;
k = 0;
if (!s1)
return (0);
while (ft_get_trm_start_stop(s1, set, i) == 1 && s1[i] != '\0')
i++;
start_trm = i;
k = ft_strlen(s1);
if (start_trm == k)
{
cpys = malloc(1);
cpys[0] = '\0';
return (cpys);
}
while (ft_get_trm_start_stop(s1, set, k) == 1)
k--;
stop_trm = k;
cpys = ft_substr(s1, start_trm, stop_trm - start_trm + 1);
return (cpys);
}