728x90
반응형

ft_strrchr: 문자열 s의 뒤에서부터 문자 c를 찾아 그 위치를 반환해주는 함수

char	*ft_strrchr(const char *s, int c)
{
	int		i;

	i = (int)ft_strlen(s) + 1;
	while (i--)
	{
		if (s[i] == (char)c)
			return ((char *)s + i);
	}
	return (NULL);
}

 

ft_strtrim: 문자열 s1에서 앞, 뒤에 문자열 set을 자른 문자열을 반환해주는 함수

start = 0 -> s1의 맨 앞에서부터 set 값이 없을 때까지 start 증가

end = ft_strlen(s1) -> s1의 맨 뒤에서부터 set 값이 없을 때까지 end 감소

새 문자열 str에 end - start + 1 만큼의 메모리를 할당해주고 s1의 start부터 end까지의 값들을 복사해주면 된다.

마지막에 null을 넣어주는 것을 잊지 말자!

int		ft_setcheck(char c, char const *set)
{
	int	i;

	i = 0;
	while (set[i])
	{
		if (set[i++] == c)
			return (1);
	}
	return (0);
}

char	*ft_strtrim(char const *s1, char const *set)
{
	char	*str;
	int		start;
	int		end;
	int		i;

	i = 0;
	start = 0;
	if (s1 == 0 || set == 0)
		return (NULL);
	end = (int)ft_strlen(s1);
	while (s1[start] && ft_setcheck(s1[start], set))
		start++;
	while (end > start && ft_setcheck(s1[end - 1], set))
		end--;
	if (!(str = (char *)malloc(sizeof(char) * (end - start + 1))))
		return (NULL);
	while (start < end)
		str[i++] = s1[start++];
	str[i] = '\0';
	return (str);
}

 

ft_substr: 문자열 s의 start번째 인덱스부터 시작하는 크기가 len인 문자열을 반환해주는 함수

start가 문자열 s의 길이보다 크거나 같을 때만 잘 처리해주면 된다. ft_strdup을 사용해서 빈 문자열을 반환해줬다.

char	*ft_substr(char const *s, unsigned int start, size_t len)
{
	char	*str;
	size_t	i;

	i = 0;
	if (s == 0)
		return (NULL);
	if (start >= ft_strlen(s))
		return (ft_strdup(""));
	if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
		return (NULL);
	while (s[i] && i < len)
		str[i++] = s[start++];
	str[i] = 0;
	return (str);
}

 

ft_tolower: 매개변수로 들어오는 값이 대문자면 소문자로 바꿔주는 함수

int	ft_tolower(int c)
{
	if (c >= 65 && c <= 90)
		return (c + 32);
	return (c);
}

 

ft_toupper: 매개변수로 들어오는 값이 소문자면 대문자로 바꿔주는 함수

int	ft_toupper(int c)
{
	if (c >= 97 && c <= 122)
		return (c - 32);
	return (c);
}
728x90
반응형

+ Recent posts