-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_strtrim.c
62 lines (58 loc) · 1.88 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marsoare <marsoare@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 14:49:43 by marsoare #+# #+# */
/* Updated: 2024/04/20 10:22:20 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* ft_strtrim - Trims the leading and trailing characters specified in 'set'
* from the input string 's1'.
*
* Think of it like giving your string a haircut, removing any unwanted
* characters from the start and end. For example:
*
* ft_strtrim("<<<42 is awesome>>>", "<>")
* // Result: "42 is awesome"
*
* Now your string is neat and tidy!
*/
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int j;
i = 0;
if (!s1 || !set || s1[0] == '\0')
return (ft_strdup(""));
j = ft_strlen(s1) - 1;
while (s1[i] && ft_strchr(set, (int)s1[i]))
i++;
while (j > i && ft_strrchr(set, (int)s1[j]))
j--;
if (j < i)
return (ft_strdup(""));
return (ft_substr(s1, i, j - i + 1));
}
/*
static char *ft_strndup(const char *s, int start, int end)
{
char *dup;
int i;
dup = (char *)malloc(sizeof(char) * (end - start + 1));
if (!dup)
return (NULL);
i = 0;
while (start < end)
{
dup[i] = s[start];
i++;
start++;
}
dup[i] = 0;
return (dup);
}*/