Add Ex0 to Ex3 (Ex4 and Ex5 is empty actually)
This commit is contained in:
72
ex0/ft_strdup.c
Normal file
72
ex0/ft_strdup.c
Normal 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/16 16:31:19 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);
|
||||
}*/
|
||||
114
ex1/ft_range.c
Normal file
114
ex1/ft_range.c
Normal file
@@ -0,0 +1,114 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_range.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/16 14:44:51 by lclerel- #+# #+# */
|
||||
/* Updated: 2026/03/16 16:07:36 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 size = max - min;
|
||||
int i = 0;
|
||||
|
||||
int max_err = 2;
|
||||
int min_err = 9;
|
||||
int i_err = 0;
|
||||
int *tab_err;
|
||||
int size_err = max_err - min_err;
|
||||
|
||||
tab_err = ft_range(min_err, max_err);
|
||||
|
||||
tab = ft_range(min, max);
|
||||
if (!tab)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
write(1, "Test avec Min : 2, Max : 10. : ", 31);
|
||||
while (i < size)
|
||||
{
|
||||
ft_putnbr(tab[i]);
|
||||
ft_putchar(',');
|
||||
i++;
|
||||
}
|
||||
ft_putchar('\n');
|
||||
|
||||
// Test avec full Error (Pour sa que les variables sont Err)
|
||||
write(1, "Test avec Min : 9, Max : 2. : ", 30);
|
||||
if (!tab_err)
|
||||
{
|
||||
write(1, "Retourne Null (Min a 9, Max a 2)", 32);
|
||||
}
|
||||
while (i_err < size_err)
|
||||
{
|
||||
ft_putnbr(tab_err[i_err]);
|
||||
ft_putchar(',');
|
||||
i++;
|
||||
}
|
||||
|
||||
ft_putchar('\n');
|
||||
free(tab);
|
||||
free(tab_err);
|
||||
}*/
|
||||
115
ex2/ft_ultimate_range.c
Normal file
115
ex2/ft_ultimate_range.c
Normal file
@@ -0,0 +1,115 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ultimate_range.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/16 16:07:45 by lclerel- #+# #+# */
|
||||
/* Updated: 2026/03/16 16:30:33 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;
|
||||
int i = 0;
|
||||
// Integers pour les errors
|
||||
int max_err = 2;
|
||||
int min_err = 9;
|
||||
int i_err = 0;
|
||||
int *tab_err;
|
||||
int size_err;
|
||||
|
||||
size_err = ft_ultimate_range(&tab_err ,min_err, max_err);
|
||||
size = ft_ultimate_range(&tab, min, max);
|
||||
if (size == -1)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
write(1, "Test avec Min : 2, Max : 10. : ", 31);
|
||||
while (i < size)
|
||||
{
|
||||
ft_putnbr(tab[i]);
|
||||
ft_putchar(',');
|
||||
i++;
|
||||
}
|
||||
ft_putchar('\n');
|
||||
write(1, "10 n'est pas present car il est exclu (MAX excluded)", 52);
|
||||
write(1, "\n", 1);
|
||||
// Test avec full Error (Pour sa que les variables sont Err)
|
||||
write(1, "Test avec Min : 9, Max : 2. : ", 30);
|
||||
if (size_err == 0)
|
||||
{
|
||||
write(1, "Retourne Null (Min a 9, Max a 2)", 32);
|
||||
}
|
||||
while (i_err < size_err)
|
||||
{
|
||||
ft_putnbr(tab_err[i_err]);
|
||||
ft_putchar(',');
|
||||
i++;
|
||||
}
|
||||
ft_putchar('\n');
|
||||
free(tab);
|
||||
}*/
|
||||
140
ex3/ft_strjoin.c
Normal file
140
ex3/ft_strjoin.c
Normal file
@@ -0,0 +1,140 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strjoin.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/16 16:37:56 by lclerel- #+# #+# */
|
||||
/* Updated: 2026/03/16 18:16:42 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);
|
||||
}
|
||||
0
ex4/ft_convert_base.c
Normal file
0
ex4/ft_convert_base.c
Normal file
0
ex5/ft_split.c
Normal file
0
ex5/ft_split.c
Normal file
Reference in New Issue
Block a user