42_cub3d/Libft/ft_strtrim.c
2024-10-09 03:35:09 +02:00

40 lines
1.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/18 18:34:05 by grobledo #+# #+# */
/* Updated: 2023/02/18 18:34:08 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(const char *s1, const char *set)
{
size_t i;
size_t j;
size_t k;
size_t dlen;
char *dest;
if (!s1)
return (NULL);
i = 0;
j = ft_strlen(s1);
k = -1;
while (ft_strchr(set, s1[i]) != 0 && s1[i])
i++;
if (!s1[i])
return (ft_strdup(""));
while (ft_strchr(set, s1[j]) != 0 && j > 0)
j--;
dlen = (j - i);
dest = ft_calloc ((dlen + 2), sizeof (char));
if (dest == 0)
return (NULL);
while (++k <= dlen)
dest[k] = s1[i + k];
return (dest);
}