summaryrefslogtreecommitdiff
path: root/libft/ft_strtrim.c
blob: d844fb489e679c42fcf45158c7efea52b22bf9fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_strtrim.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: kdx    <kdx   @student.42angouleme.fr      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/09/28 09:23:36 by kdx               #+#    #+#             */
/*   Updated: 2022/10/14 02:37:46 by kdx              ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"

char	*ft_strtrim(char const *s1, char const *set)
{
	size_t	begin;
	size_t	to_copy;
	size_t	len;

	if (s1 == NULL || set == NULL)
		return (NULL);
	len = ft_strlen(s1);
	if (len == 0)
		return (ft_strdup(""));
	begin = 0;
	while (ft_strchr(set, s1[begin]) != NULL)
		begin += 1;
	to_copy = len - begin;
	while (ft_strchr(set, s1[begin + to_copy - 1]) != NULL && to_copy)
		to_copy -= 1;
	return (ft_substr(s1, begin, to_copy));
}