-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
47 lines (44 loc) · 1.7 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmaurer <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/30 19:40:49 by mmaurer #+# #+# */
/* Updated: 2021/09/07 22:26:08 by mmaurer ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* #1. The string to be trimmed.
* #2. The reference set of characters to trim.
* The trimmed string. NULL if the allocation fails.
* Allocates (with malloc(3)) and returns a copy of
* ’s1’ with the characters specified in ’set’ removed
* from the beginning and the end of the string.
*/
char *ft_strtrim(char const *s1, char const *set)
{
char *result;
char *s2;
size_t start;
size_t end;
size_t buf_size;
s2 = (char *)s1;
result = 0;
if (s1 != 0 && set != 0)
{
start = 0;
end = ft_strlen(s1);
while (s1[start] && ft_strchr(set, s1[start]))
++start;
while (s1[end - 1] && ft_strchr(set, s1[end - 1]) && end > start)
--end;
buf_size = end - start + 1;
result = (char *)malloc(sizeof(char) * (buf_size));
if (result)
ft_strlcpy(result, &s2[start], (buf_size));
}
return (result);
}