From 861a36d86e7587c043131228cf3a08afd7bc43ad Mon Sep 17 00:00:00 2001 From: yctct Date: Thu, 5 Feb 2026 11:56:49 +0100 Subject: Add files, first commit --- ft_strtrim.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 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..fe217f6 --- /dev/null +++ b/ft_strtrim.c @@ -0,0 +1,92 @@ +/* +Libft Copyright (C) 2026 yctct + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + int i; + char *ptr; + int start; + int end; + int slen; + + i = 0; + while (s1[i] && ft_strchr(set, s1[i])) + i++; + start = i; + slen = ft_strlen(s1); + i = 0; + while (s1[slen - i - 1] && ft_strchr(set, s1[slen - i - 1])) + i++; + end = i; + ptr = ft_substr(s1, start, (slen - start - end)); + return (ptr); +} +/* +#include + +int main(void) +{ + printf("%s\n", ft_strtrim(" xxxtripouille", " x")); + printf("%s\n", ft_strtrim("tripouille xxx", " x")); + printf("%s\n", ft_strtrim(" xxxtripouille xxx", " x")); + printf("%s\n", ft_strtrim(" xxx xxx", " x")); + printf("%s\n", ft_strtrim("", "123")); + printf("%s\n", ft_strtrim("123", "")); + printf("%s\n", ft_strtrim("", "")); + printf("%s\n", ft_strtrim("abcdba", "acb")); + return (0); +} + +#include + +int main(void) +{ + char s1[] = "lorem \n ipsum \t dolor \n sit \t amet"; + char s2[] = "lorem ipsum dolor sit amet \n \t "; + char s3[] = " \n \t lorem ipsum dolor sit amet"; + char s4[] = " \n \t lorem \n ipsum \t dolor \n sit \t amet \t \n "; + char s5[] = " "; + + char set [] = "\t \n"; + printf("%s\n", ft_strtrim(s1, set)); + printf("\n"); + printf("%s\n", ft_strtrim(s2, set)); + printf("\n"); + printf("%s\n", ft_strtrim(s3, set)); + printf("\n"); + printf("%s\n", ft_strtrim(s4, set)); + printf("\n"); + printf("%s\n", ft_strtrim(s5, set)); + printf("\n"); + return (0); +} + +int main(void) +{ + char const s1[]= "asasHello, World.asas"; + char const s2[]= "asasasHello, World.asasasasas"; + char const s3[]= "abcabcabcabcHelloabc, World.abcabcabc"; + char const set1[]= "as"; + char const set1bis[]= "as"; + char const set3[]= "abc"; + printf("%s\n", ft_strtrim(s1, set1)); + printf("%s\n", ft_strtrim(s2, set1bis)); + printf("%s\n", ft_strtrim(s3, set3)); +} +*/ -- cgit v1.2.3