ft_strtrim.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_strtrim.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: phella <phella@student.21-school.ru> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2021/10/16 13:56:06 by phella #+# #+# */
  9. /* Updated: 2021/10/16 14:35:22 by phella ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. static int ft_check_set(char c, char const *set)
  14. {
  15. size_t i;
  16. i = 0;
  17. while (set[i])
  18. {
  19. if (set[i] == c)
  20. return (1);
  21. i++;
  22. }
  23. return (0);
  24. }
  25. char *ft_strtrim(char const *s1, char const *set)
  26. {
  27. char *str;
  28. size_t i;
  29. size_t start;
  30. size_t end;
  31. if (!s1 || !set)
  32. return (NULL);
  33. start = 0;
  34. while (s1[start] && ft_check_set(s1[start], set))
  35. start++;
  36. end = ft_strlen(s1);
  37. while (end > start && ft_check_set(s1[end - 1], set))
  38. end--;
  39. str = (char *)malloc(sizeof(*s1) * (end - start + 1));
  40. if (!str)
  41. return (NULL);
  42. i = 0;
  43. while (start < end)
  44. str[i++] = s1[start++];
  45. str[i] = 0;
  46. return (str);
  47. }