-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_strtrim.c
42 lines (38 loc) · 1.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cado-car <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/31 11:45:17 by cado-car #+# #+# */
/* Updated: 2021/07/31 11:47:42 by cado-car ### ########lyon.fr */
/* */
/* ************************************************************************** */
/*
* 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
* #1. The string to be trimmed.
* #2. The reference set of characters to trim.
* RETURN VALUES
* The trimmed string. NULL if the allocation fails.
*/
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
size_t i;
size_t j;
if (!s1 || !set)
return (NULL);
i = 0;
while (ft_strchr(set, s1[i]) && s1[i] != '\0')
i++;
j = ft_strlen((char *)s1);
while (ft_strchr(set, s1[j]) && j != 0)
j--;
if ((int)(j - i + 1) <= 0)
return (ft_calloc(1, 1));
return (ft_substr(s1, i, (j - i + 1)));
}