-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
69 lines (64 loc) · 1.81 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yeparra- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/26 22:20:32 by yeparra- #+# #+# */
/* Updated: 2023/10/26 23:34:01 by yeparra- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int set_pers(char c, const char *set)
{
while (*set)
{
if (c == *set)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t end;
size_t i;
char *str;
start = 0;
end = ft_strlen(s1);
if (!s1 || !set)
return (NULL);
while (s1[start] && set_pers(s1[start], set))
start++;
if (s1[start] == '\0')
return (ft_strdup(""));
while (end > start && set_pers(s1[end - 1], set))
end--;
str = (char *)malloc(sizeof(char) * (end - start +1));
if (!str)
return (NULL);
i = 0;
while (start < end)
str[i++] = s1[start++];
str[i] = '\0';
return (str);
}
/*
int main()
{
char *s1 = "\tHello, World! \t";
char *set = " \t";
char *result;
result = ft_strtrim(s1, set);
if (result == NULL)
printf("La cadena es nula\n");
else
{
printf("Cadena original: \"%s\"\n", s1);
printf("Cadenas eliminando tab: \"%s\"\n", result);
free(result);
}
return (0);
}*/