diff options
Diffstat (limited to 'libft/ft_strtrim.c')
-rw-r--r-- | libft/ft_strtrim.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c new file mode 100644 index 0000000..62f41aa --- /dev/null +++ b/libft/ft_strtrim.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/28 09:23:36 by kdx #+# #+# */ +/* Updated: 2022/09/28 22:07:02 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t begin; + size_t to_copy; + size_t len; + + if (s1 == NULL || set == NULL) + return (NULL); + len = ft_strlen(s1); + if (len == 0) + return (ft_strdup("")); + begin = 0; + while (ft_strchr(set, s1[begin]) != NULL) + begin++; + to_copy = len - begin; + while (ft_strchr(set, s1[begin + to_copy - 1]) != NULL && to_copy) + to_copy--; + return (ft_substr(s1, begin, to_copy)); +} |