-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
executable file
·53 lines (46 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jchoy-me <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/11 14:29:59 by jchoy-me #+# #+# */
/* Updated: 2023/07/17 16:32:41 by jchoy-me ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
DESCRIPTION:
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.
PARAMETERS:
s1: The string to be trimmed.
set: The reference set of characters to trim.
RETURN VALUE:
The trimmed string.
NULL if the allocation fails.
EXTERNAL FUNCTIONS:
malloc
*/
char *ft_strtrim(char const *s1, char const *set)
{
char *dest;
int start;
int end;
int nbytes;
if (s1 == NULL || set == NULL)
return (NULL);
start = 0;
end = ft_strlen(s1);
while (s1[start] != '\0' && ft_strchr(set, s1[start]))
start++;
while (end > start && ft_strchr(set, s1[end - 1]))
end--;
nbytes = end - start + 1;
dest = (char *) malloc(sizeof(char) * nbytes);
if (dest == NULL)
return (NULL);
ft_strlcpy(dest, &(s1[start]), nbytes);
return (dest);
}