1
0

first commit

This commit is contained in:
KeyZox
2023-08-06 20:12:38 +02:00
parent 1fe062c970
commit eb6b113e51
125 changed files with 2987 additions and 0 deletions

22
C08/ex00/ft.h Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/03 11:02:39 by ajoly #+# #+# */
/* Updated: 2022/08/04 09:32:35 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_H
# define FT_H
void ft_putchar(char c);
void ft_swap(int *a, int *b);
void ft_putstr(char *str);
int ft_strlen(char *str);
int ft_strcmp(char *s1, char *s2);
#endif

26
C08/ex01/ft_boolean.h Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_boolean.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/04 09:35:42 by ajoly #+# #+# */
/* Updated: 2022/08/04 21:15:45 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_BOOLEAN_H
# define FT_BOOLEAN_H
# include <unistd.h>
# define EVEN(x) x % 2 == 0
# define EVEN_MSG "I have an even number of arguments.\n"
# define ODD_MSG "I have an odd number of arguments.\n"
# define TRUE 1
# define FALSE 0
# define SUCCESS 0
typedef int t_bool;
#endif

18
C08/ex02/ft_abs.h Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_abs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/04 10:03:57 by ajoly #+# #+# */
/* Updated: 2022/08/04 10:38:23 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_ABS_H
# define FT_ABS_H
# define ABS(x) (x > 0) ? x : -x
#endif

22
C08/ex03/ft_point.h Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_point.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/04 10:40:55 by ajoly #+# #+# */
/* Updated: 2022/08/04 10:47:01 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_POINT_H
# define FT_POINT_H
typedef struct s_point
{
int x;
int y;
} t_point;
#endif

67
C08/ex04/ft_strs_to_tab.c Normal file
View File

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strs_to_tab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/04 10:52:25 by ajoly #+# #+# */
/* Updated: 2022/08/04 21:46:09 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_stock_str.h"
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strdup(char *src)
{
int i;
char *dest;
i = 0;
while (src[i])
i++;
dest = malloc(i + 1);
i = 0;
while (src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
struct s_stock_str *ft_strs_to_tab(int ac, char **av)
{
t_stock_str *tab;
int i;
tab = malloc(sizeof(t_stock_str) * (ac + 1));
if (tab == NULL)
return (NULL);
i = 0;
while (i < ac)
{
tab[i].str = av[i];
tab[i].copy = ft_strdup(tab[i].str);
tab[i].size = ft_strlen(av[i]);
i++;
}
tab[i].str = 0;
tab[i].size = 0;
tab[i].copy = 0;
return (tab);
}

View File

@ -0,0 +1,7 @@
#include <stdio.h>
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}

View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <unistd.h>
void ft_print_alphabet(void)
{
char ch;
ch = 'a';
while (ch <= 'z')
{
write(1, &ch, 1);
ch++;
}
}

View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <unistd.h>
void ft_print_reverse_alphabet(void)
{
char ch;
ch = 'z';
while (ch >= 'a')
{
write(1, &ch, 1);
ch--;
}
}

View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <unistd.h>
void ft_print_numbers(void)
{
char n;
n = '0';
while (n <= '9')
{
write(1, &n, 1);
n++;
}
}

View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <unistd.h>
void ft_is_negative(int n)
{
if (n < 0)
{
write(1, "N", 1);
}
else
{
write(1, "P", 1);
}
}

View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <unistd.h>
void ft_print_nb(char a, char b, char c)
{
write(1, &a, 1);
write(1, &b, 1);
write(1, &c, 1);
}
void ft_print_comb(void)
{
char a;
char b;
char c;
a = '0';
while (a <= '7')
{
b = a + 1;
while (b <= '8')
{
c = b + 1;
while (c <= '9')
{
ft_print_nb(a, b, c);
if (a != '7')
{
write(1, ", ", 2);
}
c++;
}
b++;
}
a++;
}
}

View File

@ -0,0 +1,57 @@
#include <stdio.h>
#include <unistd.h>
void convert_nb(char nb1, char nb1_10, char nb2, char nb2_10)
{
char r_nb1;
char r_nb1_10;
char r_nb2;
char r_nb2_10;
r_nb1 = nb1 + 48;
r_nb1_10 = nb1_10 + 48;
r_nb2 = nb2 + 48;
r_nb2_10 = nb2_10 + 48;
write(1, &r_nb2_10, 1);
write(1, &r_nb2, 1);
write(1, " ", 1);
write(1, &r_nb1_10, 1);
write(1, &r_nb1, 1);
}
void get_nb(char a, char b)
{
char nb1;
char nb2;
char nb1_10;
char nb2_10;
nb1 = a % 10;
nb2 = b % 10;
nb1_10 = a / 10;
nb2_10 = b / 10;
convert_nb(nb1, nb1_10, nb2, nb2_10);
}
void ft_print_comb2(void)
{
char nb1;
char nb2;
nb1 = 0;
nb2 = 0;
while (nb2 <= 98)
{
nb1 = nb2 + 1;
while (nb1 <= 99)
{
get_nb(nb1, nb2);
nb1++;
if (nb2 < 98 || nb1 < 99)
{
write(1, ", ", 2);
}
}
nb2++;
}
}

6
ended/C01/ex00/ft_ft.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
void ft_ft(int *nbr)
{
*nbr = 42;
}

View File

@ -0,0 +1,6 @@
#include <stdio.h>
void ft_ultimate_ft(int *********nbr)
{
*********nbr = 42;
}

10
ended/C01/ex02/ft_swap.c Normal file
View File

@ -0,0 +1,10 @@
#include <stdio.h>
void ft_swap(int *a, int *b)
{
int f;
f = *a;
*a = *b;
*b = f;
}

View File

@ -0,0 +1,7 @@
#include <stdio.h>
void ft_div_mod(int a, int b, int *div, int *mod)
{
*div = a / b;
*mod = a % b;
}

View File

@ -0,0 +1,12 @@
#include <stdio.h>
void ft_ultimate_div_mod(int *a, int *b)
{
int f;
int g;
f = *a;
g = *b;
*a = f / g;
*b = f % g;
}

View File

@ -0,0 +1,13 @@
#include <unistd.h>
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i])
{
write(1, &str[i], 1);
i++;
}
}

View File

@ -0,0 +1,11 @@
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}

View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(void)
{
char ff[254] = "";
ft_strcpy(ff, "ffff");
printf("%s", ff);
}

View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(void)
{
char ff[254] = "";
ft_strncpy(ff, "ffffffffff", 4);
printf("%s", ff);
}

View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(void)
{
int result;
result = ft_str_is_alpha("adfec");
printf("%d", result);
}

View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(void)
{
int result;
result = ft_str_is_numeric("1155");
printf("%d", result);
}

View File

@ -0,0 +1,7 @@
int main(void)
{
int result;
result = ft_str_is_lowercase("adfAec");
printf("%d", result);
}

View File

@ -0,0 +1,7 @@
int main(void)
{
int result;
result = ft_str_is_uppercase("adfAec");
printf("%d", result);
}

View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(void)
{
int result;
result = ft_str_is_printable("adfAec-- ");
printf("%d", result);
}

View File

@ -0,0 +1,5 @@
int main(void)
{
char str[] = "fFff";
printf("%s", ft_strupcase(str));
}

View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(void)
{
char str[] = "fFffA";
printf("%s", ft_strlowcase(str));
}

View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(void)
{
char str[250] = "salut, comment tu vas ? 42mots quarante-deux; cinqUante+et+un";
printf("%s", ft_strcapitalize(str));
}

View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(void)
{
char ff[220];
ft_strlcpy(ff, "ffffff", 4);
printf("%s", ff);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 13:14:34 by ajoly #+# #+# */
/* Updated: 2022/07/19 13:14:45 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strcpy(char *dest, char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 13:15:09 by ajoly #+# #+# */
/* Updated: 2022/07/19 13:15:13 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strncpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
i = 0;
while (i < n && src[i] != '\0')
{
dest[i] = src[i];
i++;
}
while (i < n)
{
dest[i] = '\0';
i++;
}
return (dest);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_alpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 13:26:15 by ajoly #+# #+# */
/* Updated: 2022/07/19 13:26:21 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_alpha(char *str)
{
int i;
i = 0;
while (str[i] != '\0' )
{
if (str[i] < 'A' || (str[i] > 'Z' && str[i] < 'a') || str[i] > 'z')
{
return (0);
}
i++;
}
return (1);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_numeric.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 16:54:22 by ajoly #+# #+# */
/* Updated: 2022/07/19 16:54:30 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_numeric(char *str)
{
int i;
i = 0;
while (str[i] != '\0' )
{
if (str[i] < '0' || str[i] > '9')
{
return (0);
}
i++;
}
return (1);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_lowercase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 15:48:34 by ajoly #+# #+# */
/* Updated: 2022/07/19 15:48:39 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_lowercase(char *str)
{
int i;
i = 0;
while (str[i] != '\0' )
{
if (str[i] < 'a' || str[i] > 'z')
{
return (0);
}
i++;
}
return (1);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_uppercase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 16:02:59 by ajoly #+# #+# */
/* Updated: 2022/07/19 16:03:03 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_uppercase(char *str)
{
int i;
i = 0;
while (str[i] != '\0' )
{
if (str[i] < 'A' || str[i] > 'Z')
{
return (0);
}
i++;
}
return (1);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_printable.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 16:12:36 by ajoly #+# #+# */
/* Updated: 2022/07/19 16:12:40 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_printable(char *str)
{
int i;
i = 0;
while (str[i] != '\0' )
{
if (str[i] < 32 || str[i] > 126)
{
return (0);
}
i++;
}
return (1);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strupcase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 16:42:15 by ajoly #+# #+# */
/* Updated: 2022/07/19 16:42:19 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strupcase(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] -= 32;
}
i++;
}
return (str);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strupcase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 16:42:15 by ajoly #+# #+# */
/* Updated: 2022/07/19 16:42:19 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strlowcase(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] += 32;
}
i++;
}
return (str);
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcapitalize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 19:09:13 by ajoly #+# #+# */
/* Updated: 2022/07/19 19:09:17 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strcapitalize(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
if (str[i] >= 'a' && str[i] <= 'z')
{
if (str[i - 1] <= 47 || str[i - 1] == 0
|| (str[i - 1] >= 58 && str[i - 1] <= 64)
|| (str[i - 1] >= 91 && str[i - 1] <= 96)
|| (str[i - 1] >= 123 && str[i - 1] <= 126))
{
str[i] -= 32;
}
}
if (str[i] >= 'A' && str[i] <= 'Z')
{
if (str[i - 1] > 47)
{
str[i] += 32;
}
}
i++;
}
return (str);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/20 08:23:38 by ajoly #+# #+# */
/* Updated: 2022/07/20 08:23:41 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
unsigned int ft_strlcpy(char *dest, char *src, unsigned int size)
{
unsigned int i;
i = 0;
if (size > 0)
{
while (--size && src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
while (src[i])
i++;
return (i);
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main()
{
printf("%d", ft_strcmp("zfz", "gfg"));
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main()
{
printf("%d", ft_strncmp("abcde", "abcda", 5));
}

View File

@ -0,0 +1,10 @@
#include <stdio.h>
int main(void)
{
char ff[20] = "addfs f";
char fl[20] = "adfda";
ft_strcat(ff, fl);
printf("%s", ff);
}

View File

@ -0,0 +1,9 @@
#include <stdio.h>
int main(void)
{
char ff[20] = "addfs ";
char fl[20] = "adfda";
ft_strncat(ff, fl, 9);
printf("%s", ff);
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main(void)
{
printf("%s", ft_strstr("fffff lll", "ll"));
}

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/20 16:55:23 by ajoly #+# #+# */
/* Updated: 2022/07/20 16:55:30 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
{
i++;
}
return (s1[i] - s2[i]);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/21 10:14:08 by ajoly #+# #+# */
/* Updated: 2022/07/21 10:14:11 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_strncmp(char *s1, char *s2, unsigned int n)
{
unsigned int i;
i = 0;
if (n == 0)
{
return (0);
}
while (s1[i] == s2[i] && i + 1 < n && s1[i] != '\0' && s2[i] != '\0')
{
i++;
}
return (s1[i] - s2[i]);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/21 15:08:59 by ajoly #+# #+# */
/* Updated: 2022/07/21 15:11:10 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strcat(char *dest, char *src)
{
int i;
int l;
i = 0;
l = 0;
while (dest[i] != '\0')
{
i++;
}
while (src[l] != '\0')
{
dest[i] = src[l];
i++;
l++;
}
dest[i] = '\0';
return (dest);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/22 09:49:37 by ajoly #+# #+# */
/* Updated: 2022/07/22 09:49:45 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strncat(char *dest, char *src, unsigned int nb)
{
unsigned int i;
unsigned int l;
i = 0;
l = 0;
while (dest[i] != '\0')
{
i++;
}
while (src[l] != '\0' && l < nb)
{
dest[i + l] = src[l];
l++;
}
dest[i + l] = '\0';
return (dest);
}

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/25 10:18:22 by ajoly #+# #+# */
/* Updated: 2022/07/25 10:18:26 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
char *ft_strstr(char *str, char *to_find)
{
int i;
int j;
char *l[255];
i = 0;
j = 0;
if (str == 0)
{
return (NULL);
}
while (str[i] != '\0')
{
if (str[i] == to_find[j])
{
str[j] = to_find[j];
j++;
}
i++;
}
l[i] = '\0';
return (*str);
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main(void)
{
printf("%d", ft_strlen("ffff"));
}

View File

@ -0,0 +1,4 @@
int main(void)
{
ft_putstr("fff");
}

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/25 13:03:53 by ajoly #+# #+# */
/* Updated: 2022/07/25 13:03:55 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putnbr(int nb)
{
long nb_long;
int nb_temp;
int div;
nb_long = nb;
div = 1;
if(nb_long < 0)
{
write(1, "-", 1);
nb_long *= -1;
}
while (nb_long / 10 >= div)
div *= 10;
while (div >= 1)
{
nb_temp = (nb_long / div) + 48;
write(1, &nb_temp, 1);
nb_long = nb_long % div;
div = div / 10;
}
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main()
{
printf("%d\n", ft_atoi(" ---+-++123jkjh54984"));
}

View File

@ -0,0 +1,4 @@
int main(void)
{
ft_putnbr_base(126, "0123456789abcdef");
}

View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/25 10:23:43 by ajoly #+# #+# */
/* Updated: 2022/07/25 10:23:45 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/25 12:11:23 by ajoly #+# #+# */
/* Updated: 2022/07/25 12:11:25 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
write(1, &str[i], 1);
i++;
}
}

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/25 13:03:53 by ajoly #+# #+# */
/* Updated: 2022/07/25 13:03:55 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putnbr(int nb)
{
long nb_long;
int nb_temp;
int div;
nb_long = nb;
div = 1;
if (nb_long < 0)
{
write(1, "-", 1);
nb_long *= -1;
}
while (nb_long / 10 >= div)
div *= 10;
while (div >= 1)
{
nb_temp = (nb_long / div) + 48;
write(1, &nb_temp, 1);
nb_long = nb_long % div;
div = div / 10;
}
}

37
ended/C04/ex03/ft_atoi.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/25 13:48:15 by ajoly #+# #+# */
/* Updated: 2022/07/25 13:48:19 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_atoi(char *str)
{
int i;
int result;
int neg;
i = 0;
neg = 1;
result = 0;
while (str[i] == ' ' || str[i] == '\t' || str[i] == '\r'
|| str[i] == '\n' || str[i] == '\v' || str[i] == '\f')
i++;
while (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
neg *= -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
result = result * 10 + (str[i] - 48);
i++;
}
return (result * neg);
}

View File

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/26 13:30:23 by ajoly #+# #+# */
/* Updated: 2022/07/26 13:30:27 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int check_base(char *base)
{
int i;
i = 0;
if (base[0] == '\0' && base[1] == '\0')
{
return (0);
}
while (base[i])
{
if (base[i] < 26 || base[i] > 126)
return (0);
if (base[i] == '+' || base[i] == '-')
return (0);
if (base[i] == base[i + 1])
return (0);
i++;
}
return (1);
}
void ft_putnbr_base(int nbr, char *base)
{
int base_count;
int i;
int final[255];
i = 0;
base_count = 0;
if (check_base(base))
{
if (nbr < 0)
{
nbr = -nbr;
write(1, "-", 1);
}
while (base[base_count])
base_count++;
while (nbr)
{
final[i] = nbr % base_count;
nbr = nbr / base_count;
i++;
}
while (--i >= 0)
write(1, &base[final[i]], 1);
}
}

View File

@ -0,0 +1,4 @@
int main(void)
{
printf("%d", ft_iterative_factorial(126));
}

View File

@ -0,0 +1,4 @@
int main(void)
{
printf("%d", ft_recursive_factorial(126));
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main(void)
{
printf("%d", ft_iterative_power(2, 2));
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main(void)
{
printf("%d", ft_recursive_power(5, 2));
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main(void)
{
printf("%d", ft_fibonacci(14));
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main()
{
printf("%d", ft_sqrt(8));
}

View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(void)
{
printf("%d", ft_is_prime(12));
}

View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main(void)
{
printf("%d", ft_find_next_prime(5));
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_iterative_factorial.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/27 11:34:45 by ajoly #+# #+# */
/* Updated: 2022/07/27 11:34:47 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_iterative_factorial(int nb)
{
int i;
int result;
i = 1;
result = 1;
if (nb < 0)
return (0);
if (nb == 0 || nb == 1)
return (1);
while (i <= nb)
{
result = i * result;
i++;
}
return (result);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_recursive_factorial.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/27 14:51:29 by ajoly #+# #+# */
/* Updated: 2022/08/01 13:39:06 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_recursive_factorial(int nb)
{
if (nb < 0)
return (0);
if (nb == 1 || nb == 0)
return (1);
return (nb * ft_recursive_factorial(nb - 1));
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_iterative_power.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/27 16:42:03 by ajoly #+# #+# */
/* Updated: 2022/07/27 16:42:04 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_iterative_power(int nb, int power)
{
int i;
i = 1;
if (power < 0)
return (0);
else if (power == 0)
return (1);
while (power > 0)
{
i = i * nb;
power--;
}
return (i);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_recursive_power.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/27 18:32:22 by ajoly #+# #+# */
/* Updated: 2022/07/27 18:51:35 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_recursive_power(int nb, int power)
{
if (power < 0)
return (0);
else if (power == 0)
return (1);
return (nb * ft_recursive_power(nb, power - 1));
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fibonacci.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/27 18:51:47 by ajoly #+# #+# */
/* Updated: 2022/07/27 18:51:48 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_fibonacci(int index)
{
if (index < 0)
return (-1);
if (index == 0)
return (0);
if (index == 1)
return (1);
return (ft_fibonacci(index - 1) + ft_fibonacci(index - 2));
}

29
ended/C05/ex05/ft_sqrt.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sqrt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/27 21:41:03 by ajoly #+# #+# */
/* Updated: 2022/08/01 15:57:14 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_sqrt(int nb)
{
int i;
i = 1;
while (i * i < nb && i <= nb / 2)
i++;
if ((nb % i) == 0)
return (i);
return (0);
}
#include <stdio.h>
int main()
{
printf("%d", ft_sqrt(__INT_MAX__));
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_is_prime.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/28 10:27:29 by ajoly #+# #+# */
/* Updated: 2022/08/01 15:58:00 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_is_prime(int nb)
{
int i;
i = 2;
if (nb <= 1)
return (0);
while (i <= nb / i)
{
if (nb % i == 0)
return (0);
i++;
}
return (1);
}

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_find_next_prime.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/29 10:45:00 by ajoly #+# #+# */
/* Updated: 2022/08/01 15:57:50 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
int ft_is_prime(int nb)
{
int i;
i = 2;
if (nb <= 1)
return (0);
while (i <= nb / i)
{
if (nb % i == 0)
return (0);
i++;
}
return (1);
}
int ft_find_next_prime(int nb)
{
int i;
i = nb;
while (ft_is_prime(i) != 1)
{
i++;
}
return (i);
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_program_name.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/28 14:40:17 by ajoly #+# #+# */
/* Updated: 2022/08/02 14:15:41 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int main(int argc, char **argv)
{
int i;
i = 0;
if (argc >= 1)
{
while (argv[0][i])
{
write(1, &argv[0][i], 1);
i++;
}
write(1, "\n", 1);
}
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_params.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/28 16:07:09 by ajoly #+# #+# */
/* Updated: 2022/08/02 14:12:31 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int main(int argc, char **argv)
{
int i;
int j;
i = 1;
if (argc >= 1)
{
while (argv[i])
{
j = 0;
while (argv[i][j])
{
write(1, &argv[i][j], 1);
j++;
}
write(1, "\n", 1);
i++;
}
}
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_rev_params.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/28 16:30:21 by ajoly #+# #+# */
/* Updated: 2022/08/01 10:43:28 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int main(int argc, char **argv)
{
int i;
int j;
i = argc;
while (i > 1)
{
i--;
j = 0;
while (argv[i][j])
{
write(1, &argv[i][j], 1);
j++;
}
write(1, "\n", 1);
}
}

View File

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sort_params.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/28 16:57:15 by ajoly #+# #+# */
/* Updated: 2022/08/02 10:29:03 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdio.h>
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
{
i++;
}
return (s1[i] - s2[i]);
}
void ft_print_argv(char **argv)
{
int i;
int j;
i = 1;
while (argv[i])
{
j = 0;
while (argv[i][j])
{
write(1, &argv[i][j], 1);
j++;
}
write(1, "\n", 1);
i++;
}
}
int main(int argc, char **argv)
{
int i;
int j;
char *temp;
i = 1;
while (i < argc)
{
j = 1;
while (j < argc - i)
{
if (ft_strcmp(argv[j], argv[j + 1]) > 0)
{
temp = argv[j];
argv[j] = argv[j + 1];
argv[j + 1] = temp;
}
j++;
}
i++;
}
ft_print_argv(argv);
}

View File

@ -0,0 +1,9 @@
#include <stdio.h>
int main(void)
{
char *cpy = ft_strdup("lll lll fff");
printf("%s", cpy);
free(cpy);
return (0);
}

View File

@ -0,0 +1,15 @@
#include <stdio.h>
int main(void)
{
int *l = ft_range(5, 10);
int i = 0;
while(l[i])
{
printf("%d", l[i]);
i++;
}
free(l);
return (0);
}

View File

@ -0,0 +1,21 @@
#include <stdio.h>
int main ()
{
int max;
int min;
int *tab;
int size;
int j;
min = 12;
max = 15;
size = max-min;
j = ft_ultimate_range(&tab, min, max);
int i = 0;
while (i < size)
{
printf("%d\n", tab[i]);
printf("%d", j);
i++;
}
}

View File

@ -0,0 +1,16 @@
#include <stdio.h>
int main (void)
{
char *sep;
char *tab[50];
char *j;
tab[0] = "Hello";
tab[1] = "World";
tab[2] = "ffdsfadf";
sep = ";";
j = ft_strjoin(3, tab, sep);
printf("%s", j);
free(j);
return (0);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/28 18:42:08 by ajoly #+# #+# */
/* Updated: 2022/08/04 09:17:22 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
char *ft_strdup(char *src)
{
int i;
char *dest;
i = 0;
while (src[i])
i++;
dest = malloc(i);
i = 0;
while (src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}

36
ended/C07/ex01/ft_range.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/01 15:14:57 by ajoly #+# #+# */
/* Updated: 2022/08/04 12:52:11 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int *ft_range(int min, int max)
{
int i;
int *result;
int diff;
diff = max - min;
result = malloc((diff + 1) * sizeof(int));
i = 0;
if (min >= max)
{
result = NULL;
return (result);
}
while (min < max)
{
result[i] = min;
i++;
min++;
}
return (result);
}

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultimate_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/02 21:00:29 by ajoly #+# #+# */
/* Updated: 2022/08/03 14:55:34 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int ft_ultimate_range(int **range, int min, int max)
{
int i;
int *result;
int diff;
diff = max - min;
result = malloc((diff + 1) * sizeof(int));
i = 0;
if (min >= max)
{
*range = NULL;
return (0);
}
while (min < max)
{
result[i] = min;
i++;
min++;
}
*range = result;
return (i);
}

View File

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/03 09:24:30 by ajoly #+# #+# */
/* Updated: 2022/08/04 11:16:46 by ajoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
char *ft_strcat(char *dest, char *src)
{
int i;
int l;
i = 0;
l = 0;
while (dest[i] != '\0')
i++;
while (src[l] != '\0')
{
dest[i] = src[l];
i++;
l++;
}
dest[i] = '\0';
return (dest);
}
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strjoin(int size, char **strs, char *sep)
{
int i;
char *result;
int len;
i = -1;
len = ft_strlen(sep) * (size - 1);
while (++i < size)
len = len + ft_strlen(strs[i]);
if (size == 0)
{
result = malloc(sizeof(char));
*result = 0;
return (result);
}
i = 0;
result = malloc(len);
while (i < size)
{
ft_strcat(result, strs[i]);
if (i < size - 1)
ft_strcat(result, sep);
i++;
}
result[len] = '\0';
return (result);
}

138
ended/rush01/ex00/checks.c Normal file
View File

@ -0,0 +1,138 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:33:44 by kjimenez #+# #+# */
/* Updated: 2022/07/24 15:14:35 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
int left_to_right(int **arr_answer, int row, int expected)
{
int i;
int j;
int count;
int if_increasing;
i = 0;
count = 1;
while (arr_answer[row][i] <= 3)
{
j = i;
if_increasing = i;
while (j > 0)
{
if (arr_answer[row][i] > arr_answer[row][j - 1])
if_increasing--;
j--;
}
if (j == if_increasing)
count++;
i++;
}
return (count == expected);
}
int up_to_down(int **arr_answer, int col, int expected)
{
int i;
int j;
int count;
int if_increasing;
i = 0;
count = 1;
while (i < 4 && arr_answer[i][col] <= 3)
{
j = i;
if_increasing = i;
while (j > 0)
{
if (arr_answer[i][col] > arr_answer[j - 1][col])
if_increasing--;
j--;
}
if (j == if_increasing)
count++;
i++;
}
return (count == expected);
}
int right_to_left(int **arr_answer, int row, int expected)
{
int i;
int j;
int count;
int if_increasing;
i = 3;
count = 1;
while (arr_answer[row][i] <= 3)
{
j = i;
if_increasing = i;
while (j < 3)
{
if (arr_answer[row][i] > arr_answer[row][j + 1])
if_increasing++;
j++;
}
if (j == if_increasing)
count++;
i--;
}
return (count == expected);
}
int down_to_up(int **arr_answer, int col, int expected)
{
int i;
int j;
int count;
int if_increasing;
i = 3;
count = 1;
while (i > 0 && arr_answer[i][col] <= 3)
{
j = i;
if_increasing = i;
while (j < 3)
{
if (arr_answer[i][col] > arr_answer[j + 1][col])
if_increasing++;
j++;
}
if (j == if_increasing)
count++;
i--;
}
return (count == expected);
}
int check_duplicates(int **board)
{
int i;
int j;
int k;
i = -1;
while (++i < 4)
{
j = -1;
while (++j < 4)
{
k = j;
while (++k < 4)
{
if (board[j][i] == board[k][i])
return (0);
}
}
}
return (1);
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checks.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:45:31 by kjimenez #+# #+# */
/* Updated: 2022/07/24 15:14:11 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CHECKS_H
# define CHECKS_H
int left_to_right(int **arr_answer, int row, int expected);
int up_to_down(int **arr_answer, int col, int expected);
int right_to_left(int **arr_answer, int row, int expected);
int down_to_up(int **arr_answer, int col, int expected);
int check_duplicates(int **board);
#endif

36
ended/rush01/ex00/lib.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:26:42 by kjimenez #+# #+# */
/* Updated: 2022/07/24 16:16:26 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
int i;
i = -1;
while (str[++i])
write(1, &str[i], 1);
}
void ft_swap(int *i, int *j)
{
int temp;
temp = *i;
*i = *j;
*j = temp;
}

20
ended/rush01/ex00/lib.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:44:57 by kjimenez #+# #+# */
/* Updated: 2022/07/24 14:44:59 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIB_H
# define LIB_H
void ft_putchar(char c);
void ft_putstr(char *str);
void ft_swap(int *i, int *j);
#endif

137
ended/rush01/ex00/main.c Normal file
View File

@ -0,0 +1,137 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 18:04:54 by kjimenez #+# #+# */
/* Updated: 2022/07/24 13:58:55 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "checks.h"
#include "parser.h"
#include "permutations.h"
#include "lib.h"
void print_board(int **board, int count)
{
int i;
int j;
if (count == 4)
{
i = -1;
while (++i < 4)
{
j = -1;
while (++j < 4)
{
ft_putchar(board[i][j] + '0');
if (j < 3)
ft_putchar(' ');
}
ft_putchar('\n');
}
exit(0);
}
free(board);
}
int **create_board(int *permut_1, int *permut_2, int *permut_3, int *permut_4)
{
int **board;
int i;
board = (int **) malloc(4 * sizeof(int *));
i = -1;
while (++i < 4)
board[i] = (int *) malloc(4 * sizeof(int));
i = -1;
while (++i < 4)
board[0][i] = permut_1[i];
i = -1;
while (++i < 4)
board[1][i] = permut_2[i];
i = -1;
while (++i < 4)
board[2][i] = permut_3[i];
i = -1;
while (++i < 4)
board[3][i] = permut_4[i];
return (board);
}
void solve_board(int **board, int **arr_expected)
{
int i;
int count;
i = 0;
count = 0;
if (check_duplicates(board))
{
while (i < 4)
{
if (up_to_down(board, i, arr_expected[0][i])
&& down_to_up(board, i, arr_expected[1][i])
&& left_to_right(board, i, arr_expected[2][i])
&& right_to_left(board, i, arr_expected[3][i]))
count++;
i++;
}
}
print_board(board, count);
}
void solve_all_boards(int **permutations, int **arr_expected)
{
int line_1;
int line_2;
int line_3;
int line_4;
line_1 = -1;
while (++line_1 < 24)
{
line_2 = -1;
while (++line_2 < 24)
{
line_3 = -1;
while (++line_3 < 24)
{
line_4 = -1;
while (++line_4 < 24)
{
solve_board(create_board(permutations[line_1],
permutations[line_2], permutations[line_3],
permutations[line_4]), arr_expected);
}
}
}
}
ft_putstr("Error\n");
}
int main(int argc, char **argv)
{
int **permutations;
int **check;
if (argc == 2)
{
if (check_input(argv[1]))
{
check = str_to_tab(argv[1]);
permutations = generate_permutations();
solve_all_boards(permutations, check);
free(permutations);
free(check);
return (0);
}
}
ft_putstr("Error\n");
return (0);
}

View File

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:28:23 by kjimenez #+# #+# */
/* Updated: 2022/07/24 14:28:26 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int check_input(char *str)
{
int i;
i = -1;
while (str[++i])
{
if (i % 2 == 1)
{
if (str[i] != ' ')
return (0);
}
else if (i % 2 == 0)
{
if (str[i] < '1' || str[i] > '4')
return (0);
}
}
if (i != 31)
return (0);
return (1);
}
int **str_to_tab(char *str)
{
int i;
int j;
int k;
int **check;
i = -1;
check = (int **) malloc(4 * sizeof(int *));
while (++i < 4)
check[i] = (int *) malloc(4 * sizeof(int));
i = -1;
k = 0;
while (++i < 4)
{
j = -1;
while (++j < 4)
{
check[i][j] = str[k] - '0';
k += 2;
}
}
return (check);
}

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:45:05 by kjimenez #+# #+# */
/* Updated: 2022/07/24 14:45:08 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSER_H
# define PARSER_H
int check_input(char *str);
int **str_to_tab(char *str);
#endif

View File

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* permutations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:35:22 by kjimenez #+# #+# */
/* Updated: 2022/07/24 14:35:25 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "lib.h"
int perm_next(int n[])
{
int tail;
int i;
int j;
i = 3;
while (i > 0 && n[i - 1] >= n[i])
i--;
tail = i;
if (tail > 0)
{
j = 3;
while (j > tail && n[j] <= n[tail - 1])
j--;
ft_swap(&n[tail - 1], &n[j]);
}
i = tail;
j = 3;
while (i < j)
{
ft_swap(&n[i], &n[j]);
i++;
j--;
}
return (tail != 0);
}
int **generate_permutations(void)
{
int **permut;
int perm[4];
int i;
int j;
permut = (int **) malloc(24 * sizeof(int *));
j = 0;
i = 0;
while (++i <= 4)
perm[i - 1] = i;
while (1)
{
permut[j] = (int *) malloc(4 * sizeof(int));
i = -1;
while (++i < 4)
permut[j][i] = perm[i];
j++;
if (!perm_next(perm))
break ;
}
return (permut);
}

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* permutations.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:45:13 by kjimenez #+# #+# */
/* Updated: 2022/07/24 15:50:47 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PERMUTATIONS_H
# define PERMUTATIONS_H
int **generate_permutations(void);
int perm_next(int n[], int count);
#endif

View File

@ -0,0 +1,30 @@
SRCS = ft_main.c ft_errors.c ft_parse_dict.c ft_parse_line.c ft_parser.c ft_utils1.c ft_cut_nbr.c rush02.h
OBJS = ${SRCS:.c=.o}
NAME = rush-02
CC = gcc
CFLAGS = -Wall -Werror -Wextra
RM = rm -f
.c.o:
${CC} ${CFLAGS} -c $< -o ${<:.c=.o}
${NAME}: ${OBJS}
${CC} -o ${NAME} ${OBJS}
all: ${NAME}
clean:
${RM} ${OBJS}
fclean: clean
${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

View File

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cut_nbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lvincent <lvincent@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/31 19:07:48 by julemart #+# #+# */
/* Updated: 2022/07/31 23:35:38 by lvincent ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
char **i_is_1(char **tab, char *nbr)
{
char *temp;
temp = malloc(2);
if (nbr[1] == '1')
{
temp[0] = nbr[1];
temp[1] = nbr[2];
tab = add_tab_str(tab, temp);
}
else if (nbr[1] != '0')
{
temp[0] = nbr[1];
temp[1] = '0';
tab = add_tab_str(tab, temp);
}
return (tab);
}
char **ft_number(char **tab, char *nbr)
{
int i;
char *temp;
i = 0;
temp = malloc(2);
temp[1] = '\0';
while (nbr[i])
{
if (i == 0 && nbr[i] != '0')
{
temp[0] = nbr[i];
tab = add_tab_str(tab, temp);
tab = add_tab_str(tab, "100");
}
else if (i == 1)
{
if (nbr[i] == '1')
i++;
tab = i_is_1(tab, nbr);
}
if (i == 2 && nbr[i] != '0')
{
temp[0] = nbr[i];
tab = add_tab_str(tab, temp);
}
i++;
}
return (tab);
}
char *set_zeros(char *str)
{
int i;
char *test;
i = ft_strlen(str);
if (i % 3 == 1)
{
str = ft_addchar(str, '0');
str = ft_addchar(str, '0');
}
if (i % 3 == 2)
{
str = ft_addchar(str, '0');
}
return (str);
}
char **ft_comp(char *str)
{
int i;
int j;
char *c;
char **tab;
tab = malloc(sizeof(char *));
c = malloc(4);
c[3] = '\0';
j = 0;
i = 0;
while (str[i] != '\0')
{
j = 0;
while (j < 3)
{
c[j] = str[i + j];
j++;
}
tab = ft_number(tab, c);
i += 3;
}
return (tab);
}

Some files were not shown because too many files have changed in this diff Show More