열렬히.뛰기

c07

école 42 > C > c07

ex00

c
#include <stdlib.h>

int	ft_strlen(char *str)
{
	int	x;

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

char	*ft_strdup(char *src)
{
	char	*arr;
	int		size;
	int		i;

	size = ft_strlen(src);
	arr = (char *)malloc(sizeof(char) * (size + 1));
	i = 0;
	while (src[i] != '\0')
	{
		arr[i] = src[i];
		i++;
	}
	arr[i] = '\0';
	return (arr);
}

ex01

c
#include <stdlib.h>

int	*ft_range(int min, int max)
{
	int			*arr;
	long long	size;
	int			i;

	if (max <= min)
	{
		arr = NULL;
		return (arr);
	}
	size = (long long)(max - min);
	arr = (int *)malloc(size * sizeof(int));
	if (!arr)
		return (0);
	i = 0;
	while (min < max)
	{
		arr[i] = min;
		min++;
		i++;
	}
	return (arr);
}

ex02

c
#include <stdlib.h>

int	ft_ultimate_range(int **range, int min, int max)
{
	int	i;
	int	temp;

	if (min >= max)
		return (0);
	*range = (int *)malloc((max - min) * sizeof(int));
	if (*range == NULL)
		return (-1);
	i = 0;
	temp = min;
	while (min < max)
	{
		*(*range + i) = min;
		min++;
		i++;
	}
	return (max - temp);
}

ex03

c
#include <stdlib.h>

int	ft_strlen(char *str)
{
	int	x;

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

int	find_n(int size, char **strs, char *sep)
{
	int	n;
	int	i;

	i = 0;
	n = 0;
	while (i < size)
	{
		n += ft_strlen(strs[i]);
		i++;
	}
	n += (size - 1) * ft_strlen(sep);
	return (n);
}

char	*glue(char *integrated, int size, char **strs, char *sep)
{
	int	i;
	int	j;
	int	k;
	int	x;

	i = 0;
	j = 0;
	x = 0;
	while (j < size)
	{
		k = 0;
		while (strs[j][k] != '\0')
		{
			integrated[i++] = strs[j][k++];
		}
		x = 0;
		while (sep[x] != '\0' && j != size - 1)
			integrated[i++] = sep[x++];
		j++;
	}
	integrated[i] = '\0';
	return (integrated);
}

char	*ft_strjoin(int size, char **strs, char *sep)
{
	char	*integrated;
	char	*arr;
	int		n;

	if (size == 0)
	{
		arr = (char *)malloc(sizeof(char));
		arr[0] = 0;
		return (arr);
	}
	n = find_n(size, &(*strs), sep);
	integrated = (char *)malloc((n + 1) * sizeof(char));
	integrated = glue(integrated, size, strs, sep);
	return (integrated);
}

ex04

c
#include <stdlib.h>

int		ft_strlen(char *base);
int		intlen(long long x, char *base);
char	*put_num(long long x, char *arr, int size, char *base);
char	*get_the_new(int x, char *base);

int	insider(char ch, char *base)
{
	int	i;

	i = 0;
	while (base[i] != '\0')
	{
		if (ch == base[i])
			return (i);
		i++;
	}
	return (-1);
}

int	checkbase(char *base)
{
	int	i;
	int	temp;

	i = 0;
	while (base[i] != '\0')
	{
		temp = 1;
		if (base[i] == '+' || base[i] == '-')
			return (0);
		else if ((base[i] >= 9 && base[i] <= 13) || base[i] == 32)
			return (0);
		while (temp <= i)
		{
			if (base[i] == base[i - temp])
				return (0);
			temp++;
		}
		i++;
	}
	if (i == 1 || i == 0)
		return (0);
	return (1);
}

int	checkminus(char *str)
{
	int	flag;

	flag = 0;
	while ((*str >= 9 && *str <= 13) || *str == 32)
		str++;
	while (*str == '-' || *str == '+')
	{
		if (*str == '-')
			flag++;
		str++;
	}
	return (flag % 2);
}

int	change_to_decimal(char *nbr, char *base_from, int sign)
{
	int	i;
	int	number;

	i = 0;
	while ((nbr[i] >= 9 && nbr[i] <= 13) || nbr[i] == 32)
		i++;
	while (nbr[i] == '-' || nbr[i] == '+')
		i++;
	number = 0;
	while (insider(nbr[i], base_from) != -1 && nbr[i] != '\0')
	{
		number *= ft_strlen(base_from);
		number += insider(nbr[i], base_from);
		i++;
	}
	return (sign * number);
}

char	*ft_convert_base(char *nbr, char *base_from, char *base_to)
{
	int		sign;
	int		x;
	char	*new;

	if (checkbase(base_from) == 0)
		return (NULL);
	if (checkbase(base_to) == 0)
		return (NULL);
	sign = 1;
	if (checkminus(nbr) == 1)
		sign = -1;
	x = change_to_decimal(nbr, base_from, sign);
	new = get_the_new(x, base_to);
	return (new);
}
c
#include <stdlib.h>

int	ft_strlen(char *base)
{
	int	i;

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

int	intlen(long long x, char *base)
{
	int	i;

	i = 0;
	if (x < 0)
	{
		i++;
		x *= -1;
	}
	if (x == 0)
		return (1);
	while (x > 0)
	{
		i++;
		x /= ft_strlen(base);
	}
	return (i);
}

char	*put_num(long long x, char *arr, int size, char *base)
{
	if (x < 0)
	{
		arr[0] = '-';
		x *= -1;
	}
	arr[size--] = '\0';
	while (x > 0)
	{
		arr[size--] = base[x % ft_strlen(base)];
		x /= ft_strlen(base);
	}
	return (arr);
}

char	*get_the_new(int x, char *base)
{
	int		size;
	char	*arr;

	size = intlen((long long) x, base);
	arr = (char *)malloc((size + 1) * sizeof(char));
	if (x == 0)
	{
		arr[0] = base[0];
		arr[1] = '\0';
		return (arr);
	}
	arr = put_num((long long) x, arr, size, base);
	return (arr);
}

ex05

c
#include <stdlib.h>

int	in(char str, char *charset)
{
	while (*charset != '\0')
	{
		if (str == *charset || str == 0)
			return (1);
		charset++;
	}
	return (0);
}

int	arr_size(char *str, char *charset)
{
	int	i;
	int	ans;

	i = 0;
	ans = 0;
	while (str[i] != '\0')
	{
		while (str[i] != '\0' && in(str[i], charset))
			i++;
		if (!in(str[i++], charset))
			ans++;
		while (str[i] != '\0' && !in(str[i], charset))
			i++;
	}
	return (ans);
}

int	ft_strlen(char *str)
{
	int	x;

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

char	*put_word(char *charset, char *str)
{
	int		x;
	int		size;
	char	*word;

	size = 0;
	while (!in(str[size], charset) && str[size])
		size++;
	word = (char *)malloc(size + 1);
	x = 0;
	while (x < size)
	{
		word[x] = str[x];
		x++;
	}
	word[x] = '\0';
	return (word);
}

char	**ft_split(char *str, char *charset)
{
	char	**newstr;
	int		i;
	int		x;

	newstr = (char **)malloc((arr_size(str, charset) + 1) * sizeof(char *));
	x = 0;
	i = 0;
	while (str[i] != '\0')
	{
		if (in(str[i], charset))
			i++;
		else
		{
			newstr[x] = put_word(charset, &str[i]);
			x++;
			while (!in(str[i], charset) && str[i++])
				;
		}
	}
	newstr[x] = NULL;
	return (newstr);
}