2023.07.31(월) 14:00 서초클러스터 C9

ft_printf

# include <stdlib.h>
# include <unistd.h>
# include <stdarg.h>

int	ft_putchar(char c)
{
	write(1, &c, 1);
	return (1);
}

int ft_putstr(char *str)
{
	int i = 0;

	if (!str)
		str = "(null)";
	while (str[i])
		ft_putchar(str[i++]);
	return (i);
}

int ft_len(long long n, int base)
{
	int len = 1;

	if (n < 0)
	{
		len++;
		n = -n;
	}
	while (n >= base)
	{
		n = n  / base;
		len++;
	}
	return (len);
}

int ft_putnbr(int n, int base, char *arr)
{
	long long nb;

	nb = (long long)n;
	if (nb < 0)
	{
		ft_putchar('-');
		nb = -nb;
	}
	if (nb >= base)
		ft_putnbr(nb / base, base, arr);
	ft_putchar(arr[nb % base]);
	return (ft_len(n, base));
}

int ft_printf_type(char str, va_list *ap)
{
	int	len;

	len = 0;
	if (str == 'c')
		len += ft_putchar(va_arg(*ap, int));
	else if (str == 's')
		len += ft_putstr(va_arg(*ap, char *));
	else if (str == 'd')
		len += ft_putnbr(va_arg(*ap, int), 10, "0123456789");
	else if (str == 'x')
		len += ft_putnbr(va_arg(*ap, unsigned int), 16, "0123456789abcdef");
	return (len);
}

int ft_printf(const char *str, ...)
{
	va_list	ap;
	int		i;
	int		len;

	i = 0;
	len = 0;
	va_start(ap, str);
	while (str[i])
	{
		if (str[i] != '%')
			len += ft_putchar(str[i++]);
		else
		{
			len += ft_printf_type(str[i + 1], &ap);
			i += 2;
		}
	}
	va_end(ap);
	return (len);
}

int main()
{
	char *str = NULL;
	int num1 = 3847;
	int num2 = 74995663;
	ft_printf("hello testeeee %s aaaa %d Aaaaaa %x", str, num1, num2);
}

get_next_line

#include <stdlib.h>
#include <unistd.h>

size_t	ft_strlen(char *str)
{
	size_t i = 0;

	while (str[i])
		i++;
	return (i);
}

char *ft_strmove(char *s1, char *s2, size_t len)
{
	if (!s1 && !s2)
		return (NULL);
	size_t i = len - 1;
	if ((size_t)s1 != (size_t)s2 < len)
	{
		while (i < len)
		{
			s1[i] = s2[i];
			i--;
		}
	}
	else
	{
		size_t j = 0;
		while (s2[j])
		{
			s1[j] = s2[j];
			j++;
		}
		s1[j] = '\\0';
	}
	return (s1);
}

char *str_dup(char *str)
{
	char *arr;
	int len =  ft_strlen(str);
	arr = malloc(sizeof(char) * len + 1);
	int i = 0;
	while (str[i])
	{
		arr[i] = str[i];
		i++;
	}
	arr[i] = '\\0';
	return (arr);
}

char *ft_strjoin(char *s1, char *s2)
{
	char *arr;
	int len;

	if (!s1)
	{
		s1 = malloc(1);
		s1[0] = '\\0';
	}
	len = ft_strlen(s1) + ft_strlen(s2);
	arr = malloc(sizeof(char) * (len + 1));
	ft_strmove(arr, s1, ft_strlen(s1));
	ft_strmove(&arr[ft_strlen(s1)], s2, ft_strlen(s2) + 1);
	arr[len] = '\\0';
	free(s1);
	return (arr);
}

int	check_n(char *buf)
{
	int	i;

	if (!buf)
		return (0);
	i = 0;
	while (buf[i])
	{
		if (buf[i] == '\\n')
			return (i + 1);
		i++;
	}
	return (0);
}

char	*read_result(char **buf, char **srg, int read_size)
{
	if (read_size == 0)
	{
		free(*buf);
		return (*srg);
	}
	if (*srg != NULL)
		free(*srg);
	free(*buf);
	return (NULL);
}

char	*read_line(int fd, char **srg)
{
	char	*buf;
	int		read_size;

	if (fd < 0 || BUFFER_SIZE <= 0)
		return (NULL);
	buf = malloc(sizeof(char) * (BUFFER_SIZE + 1));
	if (!buf)
	{
		free(*srg);
		return (NULL);
	}
	buf[0] = '\\0';
	while (!check_n(buf))
	{
		read_size = read(fd, buf, BUFFER_SIZE);
		if (read_size == 0 || read_size == -1)
			return (read_result(&buf, srg, read_size));
		buf[read_size] = '\\0';
		*srg = ft_strjoin(*srg, buf);
		if (!(*srg))
			break ;
	}
	free(buf);
	return (*srg);
}

char	*get_new_line(char **srg)
{
	char	*res;
	int		len;
	int		idx;
	int		srg_len;

	srg_len = ft_strlen(*srg);
	idx = 0;
	len = check_n(*srg);
	res = malloc(sizeof(char) * (len + 1));
	if (!res)
		return (NULL);
	while (idx < len)
	{
		res[idx] = *(*srg + idx);
		idx++;
	}
	res[idx] = '\\0';
	ft_memmove(*srg, *srg + len, srg_len - len);
	(*srg)[srg_len - len] = '\\0';
	return (res);
}

char	*get_next_line(int fd)
{
	static char	*srg;
	char		*res;

	srg = read_line(fd, &srg);
	if (!srg)
		return (NULL);
	if (!check_n(srg))
	{
		if ((*srg))
			res = ft_strdup(srg);
		else
			res = NULL;
		free(srg);
		srg = NULL;
	}
	else
	{
		res = get_new_line(&srg);
		if (!res)
		{
			free(srg);
			srg = NULL;
		}
	}
	return (res);
}

inter

argv[1] 문자열 중 argv[2]와 중복되는 문자만 출력

#include <unistd.h>

int check_av(char c, char *str)
{
	int i = 0;

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

int check_av1(char c, char *str, int repeat)
{
	int i = 0;

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

int main(int ac, char **av)
{
	int i = 0;

	if (ac == 3)
	{
		while (av[1][i])
		{
			if (check_av(av[1][i], av[2]))
			{
				if (check_av1(av[1][i], av[1], i))
					write(1, &av[1][i], 1);
			}
			i++;
		}
	}
	write(1, "\\n", 1);
	return (0);
}

union

argv[1], argv[2] 문자 중 중복되는 값을 제외하고 출력

#include <unistd.h>

int check_dup(char c, char *str, int position)
{
	int i = 0;

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

int main(int ac, char **av)
{
	int i = 0;
	int j = 0;

	if (ac == 3)
	{
		while (av[1][i])
		{
			if (check_dup(av[1][i], av[1], i))
				write(1, &av[1][i], 1);
			i++;
		}
		while (av[2][j])
		{
			if (check_dup(av[2][j], av[1], i))
			{
				if (check_dup(av[2][j], av[2], j))
					write(1, &av[2][j], 1);
			}
			j++;
		}
	}
	write(1, "\\n",1);
	return (0);
}