libft/str/ft_strtrim.c
Raphael 2cf0714dc7
docs(str): moving the actual documentation on the header
- The documentation on the header allow u to see on the files where the
header is inclued
2025-09-05 16:30:00 +02:00

41 lines
1.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:55:44 by rparodi #+# #+# */
/* Updated: 2025/09/05 16:16:46 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "str.h"
#include <stdlib.h>
char *ft_strtrim(char const *s1, char const *set)
{
size_t i;
size_t j;
size_t k;
char *str;
if (!s1)
return (NULL);
i = 0;
while (ft_strchr(set, s1[i]) != NULL && s1[i])
i++;
if (i == ft_strlen(s1))
return (ft_strdup(""));
j = ft_strlen(s1);
while (ft_strrchr(set, s1[j - 1]) != NULL && j > i)
j--;
str = (char *)malloc(j - i + 1);
if (!str)
return (NULL);
k = 0;
while (i < j)
str[k++] = s1[i++];
str[k] = '\0';
return (str);
}