libft/42

Libft Part2

dev.tkim42 2021. 2. 10. 00:10

1. substr

함수이름

- ft_substr

 

프로토타입

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

 

매개변수

- char const *s : 복사할 문자열.
- unsigned int start : 문자열 's'에 있는 하위 문자열의 시작 인덱스.
- size_t len : 복사할 문자열의 최대 길이.

 

리턴값

- char * : 복사한 문자열 , 할당 실패시 NULL.

 

설명

- 문자열 s의 start인덱스부터 len길이만큼 새로운 메모리를 할당해서 반환해주는 함수

#include "libft.h"

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

	if (!s)
		return (NULL);
	if (ft_strlen(s) < start)
		return (ft_strdup(""));
	if (!(substr = (char *)malloc(sizeof(char) * (len + 1))))
		return (NULL);
	ft_strlcpy(substr, s + start, len + 1);
	return (substr);
}

 

2. strjoin

함수이름

- ft_strjoin

 

프로토타입

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

 

매개변수

- char const *s1 : 첫 번째 문자열.
- char const *s2 : 두 번째 문자열.

 

리턴값

- char * : 합친 문자열 주소 반환

 

설명

- 문자열 s1과 문자열 s2를 합치는 함수

#include "libft.h"

char	*ft_strjoin(char const *s1, char const *s2)
{
	char	*s3;
	int		s1_len;
	int		s2_len;
	int		i;
	int		j;

	i = 0;
	j = 0;
	s1_len = ft_strlen(s1);
	s2_len = ft_strlen(s2);
	if (!s1 && !s2)
		return (NULL);
	else if (!s1 || !s2)
		return (!(s1) ? ft_strdup(s2) : ft_strdup(s1));
	if (!(s3 = (char *)malloc(sizeof(char) * (s1_len + s2_len + 1))))
		return (NULL);
	while (i < s1_len)
	{
		s3[i] = s1[i];
		i++;
	}
	while (j < s2_len)
		s3[i++] = s2[j++];
	s3[i] = '\0';
	return (s3);
}

 

3. strtrim

함수이름

- ft_strtrim

 

프로토타입

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

 

매개변수

- char const *s1 : 검사할 문자열.
- char const *set : 제거할 문자열.

 

리턴값

- char * : set이 제거된 문자열, 할당 실패시 NULL

 

설명

- 문자열 s1 앞뒤로 set에 들어간 문자는 모두 삭제하는 함수

#include "libft.h"

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

	if (!s1)
		return (0);
	i = 0;
	start = 0;
	end = ft_strlen(s1) - 1;
	while (s1[i] && ft_strchr(set, s1[i]))
		start = (i++) + 1;
	if (start >= ft_strlen(s1))
		return ((char *)ft_calloc(sizeof(char), 1));
	i = ft_strlen(s1) - 1;
	while (i && s1[i] && ft_strchr(set, s1[i]))
		end = (i--) - 1;
	return (ft_substr(s1, start, (end - start + 1)));
}

 

4. split

함수이름

- ft_split

 

프로토타입

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

 

매개변수

- char const *s : 분할할 문자열
- char c : 구분 문자

 

리턴값

- char ** : 2차원 문자열 배열  , 할당 실패시 NULL

 

설명

- 문자열 s를  구분 문자로 나누는 함수

Function name ft_split
Prototype char **ft_split(char const *s, char c);
Turn in files -
Parameters #1. 분할할 문자열.
#2. 구분 문자.
#1. The string to be split.
#2. The delimiter character.
Return value 분할로 인한 새 문자열 배열. 할당 실패시 NULL.
The array of new strings resulting from the split. NULL if the allocation fails.
External functs. malloc, free
Description malloc(3)을 할당하고 구분 문자 'c'를 사용하여 문자열 's'을 분할하여 얻은 새로운 문자열 배열을 반환. 그 배열은NULL로 끝나야 합니다.
Allocates (with malloc(3)) and returns an array of strings obtained by splitting ’s’ using the character ’c’ as a delimiter. The array must be ended by a NULL pointer.

 

5. itoa

함수이름

- ft_itoa

 

프로토타입

- char *ft_itoa(int n);

 

매개변수

- int n : 변환할 정수

 

리턴값

- char * : 변환된 문자열 주소 리턴 할당 실패시 NULL

 

설명

- 정수를 문자열로 반환해주는 함수

#include "libft.h"

size_t	num_len(int n)
{
	size_t	len;

	len = 0;
	if (n == 0)
		return (1);
	if (n < 0)
		len++;
	while (n)
	{
		n /= 10;
		len++;
	}
	return (len);
}

char	*ft_itoa(int n)
{
	size_t	len;
	size_t	sign;
	char	*tmp;

	len = num_len(n);
	if (!(tmp = (char *)malloc(len + 1)))
		return (NULL);
	tmp[len] = '\0';
	if (n < 0)
	{
		tmp[0] = '-';
		sign = 1;
	}
	else
		sign = 0;
	while (len-- > sign)
	{
		tmp[len] = '0' + n % 10 * (n < 0 ? -1 : 1);
		n /= 10;
	}
	return (tmp);
}

 

6. strmapi

함수이름

- ft_strmapi

 

프로토타입

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

 

매개변수

- char const *s : f함수를 적용시킬 문자열

- char (*f)(unsigned int, char) : 각 문자열에 적용할 함수

 

리턴값

- f함수를 적용시킨 문자열 , 할당 실패시 NULL

 

설명

- 함수포인터를 사용해 문자열 s에 f함수를 적용한다

- 함수포인터 : 특정 함수에 대한 메모리 주소를 담을 수 있는 것

#include "libft.h"

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

	i = 0;
	if (!(s && f))
		return (NULL);
	if (!(tmp = malloc(ft_strlen(s) + 1)))
		return (NULL);
	while (s[i])
	{
		tmp[i] = f(i, s[i]);
		i++;
	}
	tmp[i] = '\0';
	return (tmp);
}

 

7. ft_putchar_fd

함수이름

- ft_putchar_fd

 

프로토타입

- void ft_putchar_fd(char c, int fd);

 

매개변수

- char c : 출력할 문자

- int fd : 쓰여질 파일디스크립터

 

리턴값

 

설명

- 문자 c를 주어진 파일디스크립터로 출력

#include "libft.h"

void	ft_putchar_fd(char c, int fd)
{
	if (fd < 0)
		return ;
	write(fd, &c, 1);
}

 

8. ft_putstr_fd

함수이름

- ft_putstr_fd

 

프로토타입

- void ft_putstr_fd(char *s, int fd);

 

매개변수

- char *s : 출력할 문자열

- int fd : 쓰여질 파일디스크립터

 

리턴값

 

설명

- 문자열 s를 주어진 파일디스크립터로 출력

#include "libft.h"

void	ft_putstr_fd(char *s, int fd)
{
	if (!s || fd < 0)
		return ;
	while (*s)
	{
		write(fd, s++, 1);
	}
}

 

9. ft_putendl_fd

함수이름

- ft_putendl_fd

 

프로토타입

- void ft_putendl_fd(char *s, int fd);

 

매개변수

- char *s : 출력할 문자열

- int fd : 쓰여질 파일디스크립터

 

리턴값

 

설명

- 문자열을 주어진 파일디스크립터로 출력하고 newline으로 끝내기

#include "libft.h"

void	ft_putendl_fd(char *s, int fd)
{
	if (!s || fd < 0)
		return ;
	while (*s)
	{
		write(fd, s++, 1);
	}
	write(fd, "\n", 1);
}

 

10. ft_putnbr_fd

함수이름

- ft_putnbr_fd

 

프로토타입

- void ft_putnbr_fd(int n, int fd);

 

매개변수

- int n : 출력할 정수

- int fd : 쓰여질 파일디스크립터

 

리턴값

 

설명

- 정수 n을 주어진 파일디스크립터로 출력

#include "libft.h"

void	print_nbr(int n, int fd)
{
	if (n >= 10)
		print_nbr(n / 10, fd);
	write(fd, &"0123456789"[n % 10], 1);
}

void	ft_putnbr_fd(int n, int fd)
{
	if (fd < 0)
		return ;
	if (n == -2147483648)
	{
		write(fd, "-2147483648", 11);
		return ;
	}
	if (n < 0)
	{
		write(fd, "-", 1);
		n = n * -1;
	}
	print_nbr(n, fd);
}

 

 

 

 

 

split 수정하기.