c09 - KimTaebin-ai/study_posts GitHub Wiki

ex00

#include <unistd.h>

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

#include <unistd.h>

void	ft_putstr(char *str)
{
	int	i;

	i = 0;
	while (str[i] != '\0')
		write(1, &str[i++], 1);
}

#include <unistd.h>

int	ft_strcmp(char *s1, char *s2)
{
	int	i;
	int	n;

	i = 0;
	n = 0;
	while (1)
	{
		n = s1[i] - s2[i];
		if (n != 0)
			break ;
		if (s1[i] == '\0' || s2[i] == '\0')
			break ;
		i++;
	}
	return (n);
}

#include <unistd.h>

int	ft_strlen(char *str)
{
	int	i;

	i = 0;
	while (str[i] != '\0')
		i++;
	return (i);
}


void	ft_swap(int *a, int *b)
{
	int	swap;

	swap = *a;
	*a = *b;
	*b = swap;
}
gcc -c ft_putchar.c ft_putstr.c ft_strcmp.c ft_strlen.c ft_swap.c
ar rc libft.a ft_putchar.o ft_putstr.o ft_strcmp.o ft_strlen.o ft_swap.o
ranlib libft.a

ex01

NAME =	libft.a

HEADER =	./includes/

SRCS =	srcs/ft_putchar.c\
	srcs/ft_swap.c\
	srcs/ft_putstr.c\
	srcs/ft_strlen.c\
	srcs/ft_strcmp.c

OBJS =	$(SRCS:.c=.o)

CC = cc 

RM = rm -f

CFLAGS = -Wall -Wextra -Werror -I${HEADER}

$(NAME) :	$(OBJS)
	ar rc ${NAME} ${OBJS}
	ranlib ${NAME}

.c.o:
	$(CC) ${CFLAGS} -c $< -o ${<:.c=.o}

all :
	$(NAME)

clean : 
	$(RM) $(OBJS)

fclean :	clean
	$(RM) $(NAME)

re :	fclean all

.PHONY:	all	clean	fclean	re

ex02

#include <stdlib.h>

int	sep(char c, char *sep)
{
	while (*sep != '\0')
	{
		if (*sep == c)
			return (1);
		sep++;
	}
	return (0);
}

char	*ft_str_dup(char *str, char *charset)
{
	char	*new_str;
	int		len;
	int		i;

	len = 0;
	while (str[len] != '\0' && !sep(str[len], charset))
		len++;
	new_str = (char *)malloc(sizeof(char) * (len + 1));
	if (new_str == 0)
		return (0);
	i = 0;
	while (i < len)
	{
		new_str[i] = str[i];
		i++;
	}
	new_str[i] = '\0';
	return (new_str);
}

int	count(char *str, char *charset)
{
	int	cnt;

	cnt = 0;
	while (*str != '\0')
	{
		if (!sep(*str, charset))
		{
			cnt++;
			while (!sep(*str, charset) && *str != '\0')
				str++;
		}
		else
			str++;
	}
	return (cnt);
}

char	**ft_split(char *str, char *charset)
{
	int		num;
	int		i;
	char	**result;

	num = count(str, charset);
	result = (char **)malloc(sizeof(char *) * (num + 1));
	if (result == 0)
		return (0);
	i = 0;
	while (*str != '\0')
	{
		if (!sep(*str, charset))
		{
			result[i++] = ft_str_dup(str, charset);
			while (!sep(*str, charset) && *str != '\0')
				str++;
		}
		else
			str++;
	}
	result[i] = 0;
	return (result);
}
⚠️ **GitHub.com Fallback** ⚠️