This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathft_strtrim.c
56 lines (51 loc) · 1.62 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dground <dground@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/09 18:34:21 by dground #+# #+# */
/* Updated: 2021/10/09 23:58:58 by dground ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_check_char(char c, char const *set)
{
int i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *trimmed;
int untrimmed_len;
int i;
int j;
if (!s1)
return (ft_strdup(""));
if (!set)
return (ft_strdup((char *) s1));
untrimmed_len = ft_strlen(s1);
i = 0;
while (ft_check_char(s1[i], set))
i++;
if (i == untrimmed_len)
return (ft_strdup(""));
while (ft_check_char(s1[untrimmed_len - 1], set))
untrimmed_len--;
trimmed = (char *)malloc(sizeof(char) * (untrimmed_len - i + 1));
if (!trimmed)
return (NULL);
j = 0;
while (i < untrimmed_len)
trimmed[j++] = s1[i++];
trimmed[j] = '\0';
return (trimmed);
}