From 3c90d29fe5bf9ba6da090cd59c8c139d269f8bd4 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sun, 10 Mar 2024 13:35:18 +0100 Subject: Add everything --- ft_strtrim.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 ft_strtrim.c (limited to 'ft_strtrim.c') diff --git a/ft_strtrim.c b/ft_strtrim.c new file mode 100644 index 0000000..933987b --- /dev/null +++ b/ft_strtrim.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser + +static int is_in(char c, const char *str) +{ + while (*str) + { + if (c == *str) + return (1); + else + str++; + } + return (0); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + int i; + char *start; + char *end; + char *result; + + start = (char *)s1; + while (is_in(*start, set)) + start++; + i = 0; + while (s1[i]) + i++; + end = (char *)s1 + i - 1; + while (end > start && is_in(*end, set)) + end--; + i = end - start + 1; + result = malloc(i + 1); + if (result) + { + result[i] = '\0'; + i = 0; + while (start <= end) + result[i++] = *(start++); + return (result); + } + return (0); +} + +/* #include */ + +/* int main(void) */ +/* { */ +/* char s1[] = " \t \t \t "; */ + +/* printf("%s\n", ft_strtrim(s1, " \t")); */ +/* } */ -- cgit v1.2.3