nouveau fichier : ft_strtrim.c
This commit is contained in:
parent
78f458cfef
commit
633ada7d5c
55
ft_strtrim.c
Normal file
55
ft_strtrim.c
Normal file
@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strtrim.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/11/29 23:52:05 by apommier #+# #+# */
|
||||
/* Updated: 2020/12/02 19:29:33 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <libft.h>
|
||||
|
||||
static int is_set(char const *set, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (set[i])
|
||||
{
|
||||
if (set[i] == c)
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *ft_strtrim(char const *s1, char const *set)
|
||||
{
|
||||
int j;
|
||||
int i;
|
||||
int len;
|
||||
char *dest;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
len = ft_strlen(s1);
|
||||
while (is_set(set, s1[i]))
|
||||
i++;
|
||||
while (is_set(set, s1[len - j - 1]))
|
||||
j++;
|
||||
len = len - i - j;
|
||||
dest = malloc(sizeof(char) * (len + 1));
|
||||
if (dest == 0)
|
||||
return (0);
|
||||
j = 0;
|
||||
while (s[i] && len - j)
|
||||
{
|
||||
dest[j] = s[i];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
dest[j] = 0;
|
||||
return (dest);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user