libft/ft_strtrim.c
Etienne Rey-bethbeder 9a3da40a5c der des der des der
2022-10-19 17:46:58 +02:00

87 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 02:41:32 by erey-bet #+# #+# */
/* Updated: 2022/10/05 14:41:29 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_start(const char *str, char const *set)
{
int i;
int y;
int check;
i = 0;
while (str[i])
{
y = 0;
check = 0;
while (set[y])
{
if (str[i] == set[y])
check = 1;
y++;
}
if (check == 0)
return (i);
i++;
}
return (0);
}
static int get_end(const char *str, char const *set)
{
int i;
int y;
int check;
i = ft_strlen(str) - 1;
while (i > 0)
{
y = 0;
check = 0;
while (set[y])
{
if (str[i] == set[y])
check = 1;
y++;
}
if (check == 0)
return (i + 1);
i--;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
int i;
int start;
int end;
if (s1 == NULL || set == NULL)
return (NULL);
start = get_start(s1, set);
end = get_end(s1, set);
str = malloc(end - start + 1);
if (str == NULL)
return (NULL);
i = 0;
while (start < end)
{
str[i] = s1[start];
i++;
start++;
}
str[i] = '\0';
return (str);
}