libft/ft_strtrim.c
2023-12-12 17:30:46 +01:00

53 lines
1.5 KiB
C
Executable File

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 16:30:26 by fgras-ca #+# #+# */
/* Updated: 2023/03/13 14:12:41 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int char_in_set(char c, char const *set)
{
size_t i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
size_t i;
size_t start;
size_t end;
if (!s1)
return (0);
start = 0;
while (s1[start] && char_in_set(s1[start], set))
start++;
end = ft_strlen(s1);
while (end > start && char_in_set(s1[end - 1], set))
end--;
str = (char *)malloc(sizeof(*s1) * (end - start + 1));
if (!str)
return (0);
i = 0;
while (start < end)
str[i++] = s1[start++];
str[i] = 0;
return (str);
}