1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_strtrim.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: phella <phella@student.21-school.ru> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2021/10/16 13:56:06 by phella #+# #+# */
- /* Updated: 2021/10/16 14:35:22 by phella ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "libft.h"
- static int ft_check_set(char c, char const *set)
- {
- size_t i;
- i = 0;
- while (set[i])
- {
- if (set[i] == c)
- return (1);
- i++;
- }
- return (0);
- }
- char *ft_strtrim(char const *s1, char const *set)
- {
- char *str;
- size_t i;
- size_t start;
- size_t end;
- if (!s1 || !set)
- return (NULL);
- start = 0;
- while (s1[start] && ft_check_set(s1[start], set))
- start++;
- end = ft_strlen(s1);
- while (end > start && ft_check_set(s1[end - 1], set))
- end--;
- str = (char *)malloc(sizeof(*s1) * (end - start + 1));
- if (!str)
- return (NULL);
- i = 0;
- while (start < end)
- str[i++] = s1[start++];
- str[i] = 0;
- return (str);
- }
|