summaryrefslogtreecommitdiff
path: root/ft_strtrim.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-03-10 13:35:18 +0100
committerDominik Kaiser2024-03-10 13:35:18 +0100
commit3c90d29fe5bf9ba6da090cd59c8c139d269f8bd4 (patch)
tree4e2ac5e37895152cf1497ea7e5dc084bd510a1e6 /ft_strtrim.c
parentc63ee0defff25db4612e9114d847b5045948e366 (diff)
downloadlibft-3c90d29fe5bf9ba6da090cd59c8c139d269f8bd4.tar.gz
libft-3c90d29fe5bf9ba6da090cd59c8c139d269f8bd4.zip
Add everything
Diffstat (limited to 'ft_strtrim.c')
-rw-r--r--ft_strtrim.c63
1 files changed, 63 insertions, 0 deletions
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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/03/07 10:24:17 by dkaiser #+# #+# */
+/* Updated: 2024/03/07 14:47:40 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+
+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 <stdio.h> */
+
+/* int main(void) */
+/* { */
+/* char s1[] = " \t \t \t "; */
+
+/* printf("%s\n", ft_strtrim(s1, " \t")); */
+/* } */