[libft] Part 2

James An·2020년 12월 31일
0

42SEOUL

목록 보기
2/11
post-thumbnail

1. substr

Prototype

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

#include "libft.h"

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

	if (s == '\0')
		return (NULL);
	str = (char *)malloc(sizeof(char) * (len + 1));
	if (!str)
		return (NULL);
	idx = 0;
	while (idx < len && start < ft_strlen(s))
	{
		str[idx] = s[start];
		idx++;
		start++;
	}
	str[idx] = '\0';
	return (str);
}

Description

  • 문자열 s에서 시작 위치인start부터 최대len 만큼 문자열을 반환하는 함수
  • start가 문자열 s의 길이보다 크거나 같다면 NULL을 반환한다.

2. strjoin

Prototype

char *ft_strjoin(char const *s1, char const *s2);

#include "libft.h"

char		*ft_strjoin(char const *s1, char const *s2)
{
	int		idx;
	int		s1_len;
	int		s2_len;
	char	*res;

	if (s1 && s2)
	{
		s1_len = ft_strlen(s1);
		s2_len = ft_strlen(s2);
		res = (char *)malloc(sizeof(char) * (s1_len + s2_len + 1));
		if (!res)
			return (NULL);
		idx = -1;
		while (s1[++idx])
			res[idx] = s1[idx];
		idx = -1;
		while (s2[++idx])
		{
			res[s1_len] = s2[idx];
			s1_len++;
		}
		res[s1_len] = '\0';
		return (res);
	}
	return (NULL);
}

Description

  • 문자열 s1과 문자열 s2를 합친 문자열을 반환해주는 함수

3. strtrim

Prototype

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

#include "libft.h"

char	*ft_strtrim(char const *s1, char const *set)
{
	int				i;
	unsigned int	len;
	char			*start;
	char			*end;
	char			*new;

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

Description

  • 문자열 s1의 앞뒤에 set에 포함된 문자가 연속되어 있는만큼 문자들을 제거한 문자열을 반환하는 함수.
  • 문자열 s1의 앞뒤에set에 포함되지 않는 문자를 만나면 중단한다.
  1. s1 = "ABCABCDDDABCABC" set = "ABC"
    result = "DDD"
  2. s1 = "ABDCCCADBC" set = "ABC"
    result = "DCCCAD"
  3. s1 = " \t \t \n \n\n\n\t" set = " \n\t"
    result = ""
  4. s1 = "" set = ""
    result = ""

Problem

  • [crash]: your strtrim does not work with full blank input
    Test code:
    char s1 = " \t \t \n \n\n\n\t";
    char
    s2 = "";
    char *ret = ft_strtrim(s1, " \n\t");

    if (!strcmp(ret, s2))
    	exit(TEST_SUCCESS);
    exit(TEST_FAILED);
  • [crash]: your strtrim does not work with empty input
    Test code:
    char s1 = "";
    char
    s2 = "";
    char *ret = ft_strtrim(s1, " \n\t");

    if (!strcmp(ret, s2))
    	exit(TEST_SUCCESS);
    exit(TEST_FAILED);

solve

while (*start && ft_strchr(set, *start))
{
	start++;
	len--;
}
while (len >= 0 && *start && ft_strchr(set, *--end))
	len--;

*start가 가르키는 값이 NULL인 경우 while이 실행되지 않는 조건을 추가해서 최종적으로 NULL값만 들어있는 문자열을 반환하게 만들어 해결하였음.

4. split

Prototype

char **ft_split(char const *s, char c)

#include "libft.h"

static char			**ft_malloc_free(char **res)
{
	unsigned int	idx;

	idx = 0;
	while (res[idx])
	{
		free(res[idx]);
		idx++;
	}
	free(res);
	return (NULL);
}

static unsigned int	ft_get_len(char const *s, char c)
{
	unsigned int	idx;
	unsigned int	len;

	idx = 0;
	len = 0;
	if (s[idx++] != c)
		len++;
	while (s[idx])
	{
		if ((s[idx] != c && s[idx - 1] == c))
			len++;
		idx++;
	}
	return (len);
}

char				**ft_split(char const *s, char c)
{
	char			**res;
	char			*curr_str;
	unsigned int	curr_str_len;
	unsigned int	get_len;
	unsigned int	idx;

	if (!s)
		return (NULL);
	get_len = ft_get_len(s, c);
	res = (char **)malloc(sizeof(char *) * (get_len + 1));
	if (!res)
		return (NULL);
	idx = 0;
	while (idx < get_len)
	{
		while (*s == c)
			s++;
		curr_str = (char *)s;
		curr_str_len = 0;
		while (*s && *s++ != c)
			curr_str_len++;
		res[idx] = (char *)malloc(sizeof(char) * (curr_str_len + 1));
		if (!res[idx])
			return (ft_malloc_free(res));
		ft_strlcpy(res[idx], curr_str, curr_str_len + 1);
		idx++;
	}
	res[idx] = NULL;
	return (res);
}

Description

  • 문자열 s를 문자 c로 구분하는 함수
  • 정적 함수(static func):ft_split.c 파일 내에서만 동작하는 함수를 만들기 위함
  • ft_malloc_free: malloc으로 초기화된 res에 동적 할당이 실패하면 res를 free해주는 함수
  • ft_get_len: 문자열 s를 문자 c로 구분하였을 때 생기는 1차원 배열의 수를 반환한다.

참고사이트

5. itoa

Prototype

char *ft_itoa(int n);

#include "libft.h"

int		num_counter(int n)
{
	int	cnt;
	
	cnt = 1;
	if (-21483648 < n && n < 0)
		n *= -1;
	else if (n == -2147483648)
	{
		cnt++;
		n = 147483648;
	}
	while (n >= 10)
	{
		cnt++;
		n /= 10;
	}
	return (cnt);
}

char	*positive_num(int n, int n_cnt)
{
	char	*str;

	str = (char *)malloc(sizeof(char) * (n_cnt + 1));
	if (!str)
		return (NULL);
	str[n_cnt] = '\0';
	while (n_cnt--)
	{
		str[n_cnt] = (n % 10) + '0';
		n /= 10;
	}
	return (str);
}

char	*ft_itoa(int n)
{
	int				n_cnt;
	unsigned int	num;
	char			*str;

	n_cnt = num_counter(n);
	if (n < 0)
	{
		str = (char *)malloc(sizeof(char) * (n_cnt + 2));
		if (!str)
			return (NULL);
		num = n * -1;
		str[n_cnt + 1] = '\0';
		while (n_cnt)
		{
			str[n_cnt] = (num % 10) + '0';
			num /= 10;
			n_cnt--;
		}
		str[0] = '-';
		return (str);
	}
	return (positive_num(n, n_cnt));
}

Description

  • 주어진 정수 n을 문자열로 변환한 후 반환하는 함수
  • n이 음수일 때, - 부호를 붙여주는 것을 고려해야한다.

6. strmapi

Prototype

char *ft_strmapi(char const *s, char (*f)(unsigned int, char));

#include "libft.h"

char	*ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
	char	*str;
	size_t	idx;

	if (!s || !f)
		return (NULL);
	if (s)
	{
		str = ft_strdup((const char *)s);
		if (str == NULL)
			return (NULL);
		idx = 0;
		while (str[idx])
		{
			str[idx] = f((unsigned int)idx, str[idx]);
			idx++;
		}
		return (str);
	}
	else
		return (NULL);
}

Description

  • 문자열 s의 각 문자에 대해 함수 f를 적용한 문자열을 반환하는 함수
  • 함수 포인터는 특정한 함수에 대한 메모리 주소를 가지고 있는 포인터이다.

7. putchar

Prototype

void ft_putchar_fd(char c, int fd);

#include "libft.h"

void	ft_putchar_fd(char c, int fd)
{
	write(fd, &c, 1);
}

Description

  • 문자 c를 파일 디스크립터 fd로 출력하는 함수

참고사이트

8. putstr

Prototype

void ft_putstr_fd(char *s, int fd)

#include "libft.h"

void	ft_putstr_fd(char *s, int fd)
{
	size_t	idx;

	idx = 0;
	if (!s)
		return ;
	while (s[idx])
	{
		ft_putchar_fd(s[idx], fd);
		idx++;
	}
}

Description

  • 주어진 문자열 s를 파일 디스크립터 fd로 출력하는 함수

9. putendl

Prototype

void ft_putendl_fd(char *s, int fd);

#include "libft.h"

void	ft_putendl_fd(char *s, int fd)
{
	int idx;

	if (!s)
		return ;
	idx = 0;
	while (s[idx])
	{
		ft_putchar_fd(s[idx], fd);
		idx++;
	}
	ft_putchar_fd('\n', fd);
}

Description

  • 주어진 문자열 s과 줄바꿈을 파일 디스크립터 fd로 출력하는 함수

10. putnbr

Prototype

void ft_putnbr_fd(int n, int fd);

#include "libft.h"

void	ft_putnbr_fd(int n, int fd)
{
	char c;

	if (-2147483648 < n && n < 0)
	{
		ft_putchar_fd('-', fd);
		ft_putnbr_fd(-n, fd);
	}
	else if (0 <= n && n <= 2147483647)
	{
		if (n >= 10)
			ft_putnbr_fd(n / 10, fd);
		c = (n % 10) + '0';
		ft_putchar_fd(c, fd);
	}
	else if (n == -2147483648)
	{
		ft_putchar_fd('-', fd);
		ft_putchar_fd('2', fd);
		ft_putnbr_fd(147483648, fd);
	}
	else
		return ;
}

Description

  • 주어진 정수 n을 파일 디스크립터 fd로 출력하는 함수
  • int의 범위는 -2147483648부터 2147483647까지인 점을 고려해 함수를 구현해야한다.
profile
born 2 code :)

0개의 댓글