-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
59 lines (54 loc) · 1.74 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gudaniel <gudaniel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 14:00:08 by gudaniel #+# #+# */
/* Updated: 2024/04/25 14:10:20 by gudaniel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_strndup(const char *s, size_t n)
{
size_t count;
size_t i;
char *dup_str;
i = 0;
count = 0;
if (!s)
return (NULL);
while (s[count] && count < n)
count++;
dup_str = (char *)malloc((count + 1) * sizeof(char));
if (!dup_str)
return (NULL);
while (s[i] && i < n)
{
dup_str[i] = s[i];
i++;
}
dup_str[i] = '\0';
return (dup_str);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *sub_str;
size_t end_char;
size_t start_char;
if (!s1 || !set)
return (NULL);
start_char = 0;
end_char = ft_strlen(s1);
while (s1[start_char] && ft_strchr(set, s1[start_char]))
start_char++;
while (start_char < end_char && ft_strchr(set, s1[end_char]))
end_char--;
if (start_char > end_char)
return (NULL);
sub_str = ft_strndup(s1 + start_char, (end_char - start_char) + 1);
if (!sub_str)
return (NULL);
return (sub_str);
}