Push Exos

This commit is contained in:
lclerel- lclerel-
2026-03-19 15:15:53 +01:00
commit 28fb6502a2
7 changed files with 845 additions and 0 deletions

72
ex0/ft_strdup.c Normal file
View File

@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/16 14:01:43 by lclerel- #+# #+# */
/* Updated: 2026/03/19 11:37:45 by lclerel- ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
char *ft_strdup(char *src)
{
int i;
int len;
char *dst;
i = 0;
len = 0;
while (src[len])
{
len++;
}
dst = malloc(sizeof(char) * (len + 1));
if (dst == NULL)
{
return (NULL);
}
while (i < len)
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
return (dst);
}
/*
void ft_putchar(char chara)
{
write(1, &chara, 1);
}
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i])
{
ft_putchar(str[i]);
i++;
}
}
int main(void)
{
char *dest;
dest = ft_strdup("TestTest");
if (dest == NULL)
{
return (1);
}
ft_putstr(dest);
free(dest);
}*/

116
ex1/ft_range.c Normal file
View File

@@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/16 14:44:51 by lclerel- #+# #+# */
/* Updated: 2026/03/19 14:05:04 by lclerel- ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
int *ft_range(int min, int max)
{
int *range;
int i;
int size;
i = 0;
if (min >= max)
{
return (NULL);
}
size = max - min;
range = malloc(sizeof(int) * size);
if (!range)
{
return (NULL);
}
while (i < size)
{
range[i] = min + i;
i++;
}
return (range);
}
/*void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putnbr(int nb)
{
char value;
if (nb == -2147483648)
{
write(1, "-2147483648", 11);
return ;
}
if (nb < 0)
{
write(1, "-", 1);
nb = -nb;
}
if (nb > 9)
{
ft_putnbr(nb / 10);
}
value = nb % 10 + '0';
write(1, &value, 1);
}
int main(void)
{
int *tab;
int min = 2;
int max = 10;
int i = 0;
// TEST 1 : Cas Normal
tab = ft_range(min, max);
write(1, "/~~~Test 1~~~\\\n", 15);
write(1, "> Min : ", 8);
ft_putnbr(min);
write(1, " <\n> Max : ", 11);
ft_putnbr(max);
write(1, " <\n> Resultat : ", 16);
if (!tab)
write(1, "NULL", 4);
else
{
while (i < (max - min))
{
ft_putnbr(tab[i]);
if (i < (max - min - 1))
write(1, ", ", 2);
i++;
}
}
write(1, " <\n\\~~~Test 1~~~/\n\n", 18);
free(tab);
// TEST 2 : Cas Erreur (Min > Max)
int min_err = 9;
int max_err = 2;
int *tab_err = ft_range(min_err, max_err);
write(1, "/~~~Test 2~~~\\\n", 15);
write(1, "> Min : ", 8);
ft_putnbr(min_err);
write(1, " <\n> Max : ", 11);
ft_putnbr(max_err);
write(1, " <\n> Resultat : ", 16);
if (!tab_err)
write(1, "NULL (OK)", 9);
else
write(1, "ERREUR : Devrait etre NULL", 26);
write(1, " <\n\\~~~Test 2~~~/\n", 18);
if (tab_err)
free(tab_err);
return (0);
}*/

125
ex2/ft_ultimate_range.c Normal file
View File

@@ -0,0 +1,125 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultimate_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/16 16:07:45 by lclerel- #+# #+# */
/* Updated: 2026/03/19 14:02:24 by lclerel- ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
int ft_ultimate_range(int **range, int min, int max)
{
int i;
int *tab;
int size;
i = 0;
if (min >= max)
{
*range = NULL;
return (0);
}
size = max - min;
tab = malloc(sizeof(int) * size);
if (!tab)
{
*range = NULL;
return (-1);
}
while (i < size)
{
tab[i] = min + i;
i++;
}
*range = tab;
return (size);
}
/*void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putnbr(int nb)
{
char value;
if (nb == -2147483648)
{
write(1, "-2147483648", 11);
return ;
}
if (nb < 0)
{
write(1, "-", 1);
nb = -nb;
}
if (nb > 9)
{
ft_putnbr(nb / 10);
}
value = nb % 10 + '0';
write(1, &value, 1);
}
int main(void)
{
int *tab;
int min = 2;
int max = 10;
int size;
// Integers pour les errors
int max_err = 2;
int min_err = 9;
int *tab_err;
int size_err;
// Integers pour le 3eme Test
int max2 = -5;
int min2 = -1;
int *tab2;
int size2;
size_err = ft_ultimate_range(&tab_err ,min_err, max_err);
size = ft_ultimate_range(&tab, min, max);
size2 = ft_ultimate_range(&tab2, min2, max2);
if (size == -1)
{
return (1);
}
// Test 1, Test Min 2 Max 10.
write(1, "/~~~Test 1~~~\\ \n", 16);
write(1, "> Min : ", 8);
ft_putnbr(min);
write(1, " <\n> Max : ", 11);
ft_putnbr(max);
write(1, " <\n> Resultat : ", 17);
ft_putnbr(size);
write(1, " <\n\\~~~Test 1~~~/", 18);
ft_putchar('\n');
// Test 2, Test Min 9 Max 2 (Return Error)
write(1, "/~~~Test 2~~~\\ \n", 16);
write(1, "> Min : ", 8);
ft_putnbr(min_err);
write(1, " <\n> Max : ", 11);
ft_putnbr(max_err);
write(1, " <\n> Resultat : ", 17);
ft_putnbr(size_err);
write(1, " <\n\\~~~Test 2~~~/", 18);
ft_putchar('\n');
// Test 3, Test Min -1 Max -5.
write(1, "/~~~Test 3~~~\\ \n", 16);
write(1, "> Min : ", 8);
ft_putnbr(min2);
write(1, " <\n> Max : ", 11);
ft_putnbr(max2);
write(1, " <\n> Resultat : ", 17);
ft_putnbr(size2);
write(1, " <\n\\~~~Test 3~~~/", 18);
ft_putchar('\n');
free(tab);
}*/

133
ex3/ft_strjoin.c Normal file
View File

@@ -0,0 +1,133 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/16 16:37:56 by lclerel- #+# #+# */
/* Updated: 2026/03/19 11:38:19 by lclerel- ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}
int get_total_len(int size, char **strs, char *sep)
{
int i;
int total;
i = 0;
total = 0;
while (i < size)
{
total = total + ft_strlen(strs[i]);
if (i < size - 1)
{
total = total + ft_strlen(sep);
}
i++;
}
return (total);
}
char *ft_strcat(char *dest, char *src)
{
int i;
int j;
i = 0;
j = 0;
while (dest[i])
{
i++;
}
while (src[j])
{
dest[i + j] = src[j];
j++;
}
dest[i + j] = '\0';
return (dest);
}
char *ft_strjoin(int size, char **strs, char *sep)
{
char *res;
int i;
int len;
i = 0;
if (size == 0)
{
res = malloc(1);
if (res)
res[0] = '\0';
return (res);
}
len = get_total_len(size, strs, sep);
res = malloc(sizeof(char) * (len + 1));
if (!res)
return (NULL);
res[0] = '\0';
while (i < size)
{
ft_strcat(res, strs[i]);
if (i < size - 1)
ft_strcat(res, sep);
i++;
}
return (res);
}
/*
void ft_putchar(char chara)
{
write(1, &chara, 1);
}
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i])
{
ft_putchar(str[i]);
i++;
}
}
int main(void)
{
char *tab[5];
char *result;
char *space;
tab[0] = "Salut";
tab[1] = "Je";
tab[2] = "Suis";
tab[3] = "Lumi";
tab[4] = "!";
space = " ";
result = ft_strjoin(5, tab, space);
if (result)
{
ft_putstr(result);
ft_putstr("\n");
free(result);
}
return (0);
}*/

200
ex4/ft_convert_base.c Normal file
View File

@@ -0,0 +1,200 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/17 14:20:07 by lclerel- #+# #+# */
/* Updated: 2026/03/19 11:36:19 by lclerel- ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
void ft_putstr(char *str);
int ft_strlen(char *str);
int ft_nbrlen(long nbr, int base_size);
int check_base(char *base);
int ft_index(char c, char *base)
{
int i;
i = 0;
while (base[i])
{
if (base[i] == c)
return (i);
i++;
}
return (-1);
}
int ft_atoi_base(char *str, char *base)
{
int i;
int sign;
long res;
res = 0;
i = 0;
sign = 1;
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
i++;
while (str[i] == '-' || str[i] == '+')
{
if (str[i++] == '-')
sign *= -1;
}
while (str[i])
{
if (str[i] && ft_index(str[i], base) != -1)
{
res = (res * ft_strlen(base)) + ft_index(str[i], base);
i++;
}
}
return (res * sign);
}
void ft_populate_res(char *res, long n, char *base_to, int size)
{
int base_len;
base_len = ft_strlen(base_to);
res[size] = '\0';
if (n == 0)
res[0] = base_to[0];
if (n < 0)
{
res[0] = '-';
n = -n;
}
while (n > 0)
{
res[size - 1] = base_to[n % base_len];
n /= base_len;
size--;
}
}
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
{
long n;
char *res;
int size;
if (!nbr || !base_from || !base_to)
return (NULL);
if (!check_base(base_from) || !check_base(base_to))
return (NULL);
n = ft_atoi_base(nbr, base_from);
size = ft_nbrlen(n, ft_strlen(base_to));
res = malloc(sizeof(char) * (size + 1));
if (!res)
return (NULL);
ft_populate_res(res, n, base_to, size);
return (res);
}
/*
int main(void)
{
char *result;
char *hex;
char *bin;
char *dec;
char *vif;
hex = "0123456789abcdef";
dec = "0123456789";
bin = "01";
vif = "poneyvif";
// Test 1 : 42 > Dec to Bin
ft_putstr("Test avec 42 | ");
ft_putstr("Base From : Decimal (Base 10), Base To : Binary (Base 2)");
write(1, "\n", 1);
result = ft_convert_base("42", dec, bin);
if (result == NULL)
{
write(1, "Erreur.", 7);
}
ft_putstr("Resultat : ");
ft_putstr(result);
write(1, "\n", 1);
free(result);
// Test 2 : 2a (42) > Hex to Vif
ft_putstr("Test avec 42 (2a) | ");
ft_putstr("Base From : Hexadecimal (Base 16), Base To : poneyvif (Base 8)");
write(1, "\n", 1);
result = ft_convert_base("2a", hex, vif);
if (result == NULL)
{
write(1, "Erreur.", 7);
}
ft_putstr("Resultat : ");
ft_putstr(result);
write(1, "\n", 1);
free(result);
// Test 3 : 0 > Dec to Bin
ft_putstr("Test avec 0 | ");
ft_putstr("Base From : Decimal (Base 10), Base To : Binary (Base 2)");
write(1, "\n", 1);
result = ft_convert_base("0", dec, bin);
if (result == NULL)
{
write(1, "Erreur.", 7);
}
ft_putstr("Resultat : ");
ft_putstr(result);
write(1, "\n", 1);
free(result);
// Test 4 : -2147483648 > Dec to Hex
ft_putstr("Test avec -2147483648 | ");
ft_putstr("Base From : Decimal (Base 10), Base To : Hexadecimal (Base 16)");
write(1, "\n", 1);
result = ft_convert_base("-2147483648", dec, hex);
if (result == NULL)
{
write(1, "Erreur.", 7);
}
ft_putstr("Resultat : ");
ft_putstr(result);
write(1, "\n", 1);
free(result);
// Test 5 : Bordel Ultime > Binary to Dec
ft_putstr("Test avec ---+--101010 | ");
ft_putstr("Base From : Binary (Base 2), Base To : Decimal (Base 10)");
write(1, "\n", 1);
result = ft_convert_base(" ---+--101010", bin, dec);
if (result == NULL)
{
write(1, "Erreur.", 7);
}
ft_putstr("Resultat : ");
ft_putstr(result);
write(1, "\n", 1);
free(result);
// Test 6 : NULL > Binary to Dec
// ft_putstr("Test avec NULL | ");
ft_putstr("Base From : Binary (Base 2), Base To : Decimal (Base 10)");
write(1, "\n", 1);
result = ft_convert_base(NULL, bin, dec);
if (result == NULL)
{
write(1, "Erreur. NULL est mis comme variable", 35);
write(1, "\n", 1);
}
else
{
ft_putstr("Resultat : ");
ft_putstr(result);
write(1, "\n", 1);
free(result);
}
return (0);
}*/

82
ex4/ft_convert_base2.c Normal file
View File

@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_base2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/17 14:22:20 by lclerel- #+# #+# */
/* Updated: 2026/03/17 18:16:30 by lclerel- ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i])
{
write(1, &str[i], 1);
i++;
}
}
int ft_nbrlen(long nbr, int base_size)
{
int len;
len = 0;
if (nbr <= 0)
{
len++;
if (nbr < 0)
nbr = -nbr;
}
while (nbr > 0)
{
nbr /= base_size;
len++;
}
return (len);
}
int check_base(char *base)
{
int i;
int j;
i = 0;
if (!base[0] || !base[1])
return (0);
while (base[i])
{
if (base[i] == '+' || base[i] == '-'
|| (base[i] >= 9 && base[i] <= 13)
|| base[i] == 32)
return (0);
j = i + 1;
while (base[j])
{
if (base[i] == base[j])
return (0);
j++;
}
i++;
}
return (1);
}

117
ex5/ft_split.c Normal file
View File

@@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/18 10:29:59 by lclerel- #+# #+# */
/* Updated: 2026/03/19 11:47:13 by lclerel- ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
int ft_sep(char c, char *charset)
{
int i;
i = 0;
while (charset[i])
{
if (c == charset[i])
return (1);
i++;
}
return (0);
}
int ft_words(char *str, char *charset)
{
int i;
int count;
count = 0;
i = 0;
while (str[i])
{
if (ft_sep(str[i], charset) == 0
&& (i == 0 || ft_sep(str[i - 1], charset) == 1))
{
count++;
}
i++;
}
return (count);
}
char *ft_get_word(char *str, char *charset)
{
char *word;
int i;
int len;
len = 0;
i = 0;
while (str[len] && ft_sep(str[len], charset) == 0)
len++;
word = malloc(sizeof(char) * (len + 1));
if (!word)
return (NULL);
while (i < len)
{
word[i] = str[i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char *str, char *charset)
{
char **res;
int i;
int j;
i = 0;
j = 0;
res = malloc(sizeof(char *) * (ft_words(str, charset) + 1));
if (!res)
return (NULL);
while (str[i])
{
while (str[i] && ft_sep(str[i], charset) == 1)
i++;
if (str[i])
{
res[j++] = ft_get_word(&str[i], charset);
while (str[i] && ft_sep(str[i], charset) == 0)
i++;
}
}
res[j] = 0;
return (res);
}
/*int main(void)
{
char **res;
int i;
int j;
res = ft_split("Ceci Est+Un-Test^Fait/Par&Lumi.", " ^+/-*&");
i = 0;
write(1, "Test avec : Ceci Est+Un-Test^Fait/Par&Lumi. : \n", 47);
while (res[i])
{
j = 0;
while (res[i][j])
{
write(1, &res[i][j], 1);
j++;
}
write(1, "\n", 1);
i++;
}
return (0);
}*/