42_libft/ft_strtrim.c

45 lines
1.4 KiB
C
Raw Permalink Normal View History

2022-09-27 11:25:41 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2022-10-04 08:21:55 -04:00
/* ft_strtrim.c :+: :+: :+: */
2022-09-27 11:25:41 -04:00
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
2022-10-04 08:21:55 -04:00
/* Created: 2022/09/29 11:33:53 by cchauvet #+# #+# */
/* Updated: 2022/09/29 18:03:20 by cchauvet ### ########.fr */
2022-09-27 11:25:41 -04:00
/* */
/* ************************************************************************** */
#include "libft.h"
2022-10-04 08:21:55 -04:00
static int ft_is_in(const char c, const char *charset)
2022-09-27 11:25:41 -04:00
{
2022-10-04 08:21:55 -04:00
while (*charset)
{
if (c == *charset)
return (1);
charset++;
}
return (0);
}
char *ft_strtrim(const char *s1, const char *set)
{
size_t start;
size_t stop;
char *str;
2022-09-27 11:25:41 -04:00
2022-10-04 08:21:55 -04:00
if (s1 == NULL || set == NULL)
return (NULL);
if (!*s1 || !*s1)
return (ft_strdup(""));
start = 0;
while (ft_is_in(s1[start], set))
start++;
stop = ft_strlen(s1) - 1;
while (ft_is_in(s1[stop], set))
stop--;
str = ft_substr(s1, start, stop - start + 1);
return (str);
2022-09-27 11:25:41 -04:00
}