-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
51 lines (47 loc) · 1.45 KB
/
ft_strtrim.c
File metadata and controls
51 lines (47 loc) · 1.45 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aymohamm <aymohamm@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 09:41:46 by aymohamm #+# #+# */
/* Updated: 2023/11/21 09:08:15 by aymohamm ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include <string.h>
int trimmed(char c, char const *set)
{
while (*set)
{
if (c == *set)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(char const *str, char const *set)
{
int start;
int end;
int len;
char *empty;
start = 0;
if (!str)
return (NULL);
end = ft_strlen(str) - 1;
while (trimmed(str[start], set))
start++;
while (end > start && trimmed(str[end], set))
end--;
len = end - start + 1;
if (len <= 0)
{
empty = (char *)malloc(1);
*empty = '\0';
return (empty);
}
return (ft_substr(str, start, len));
}