ft_strtrim

nawkim·2021년 5월 26일
0

libft

목록 보기
27/44

1. 프로토타입

char	*ft_strtrim(char const *s1, char const *set)

2. 용도

3. 리턴값

4. 코드 구현

#include "libft.h"

static int	ft_check_start(char const *s1, char const *set)
{
	int		s;
	int		t;
	int		cnt;

	s = 0;
	while (s1[s] != '\0')
	{
		t = 0;
		cnt = 0;
		while (set[t] != '\0')
		{
			if (s1[s] != set[t])
				cnt++;
			t++;
		}
		if (cnt == t)
			return (s);
		s++;
	}
	return (-1);
}

static int	ft_check_end(char const *s1, char const *set)
{
	int		t;
	int		e;
	int		cnt;

	e = (int)ft_strlen(s1) - 1;
	while (e >= 0)
	{
		t = 0;
		cnt = 0;
		while (set[t] != '\0')
		{
			if (s1[e] != set[t])
				cnt++;
			t++;
		}
		if (cnt == t)
			return (e);
		e--;
	}
	return (-1);
}

char		*ft_strtrim(char const *s1, char const *set)
{
	int		s;
	int		e;
	int		t;
	char	*ret;

	t = 0;
	s = ft_check_start(s1, set);
	e = ft_check_end(s1, set);
	if (s == -1)
	{
		ret = (char*)malloc(sizeof(char));
		ret[0] = 0;
		return (ret);
	}
	ret = (char *)malloc(sizeof(char) * (e - s + 2));
	if (ret == 0)
		return (NULL);
	while (t <= e - s)
	{
		ret[t] = s1[s + t];
		t++;
	}
	ret[t] = '\0';
	return (ret);
}

5. 코드 설명

profile
공부 기록.

0개의 댓글