├── ex01 └── z ├── ex05 └── "\?$*’KwaMe’*$?\" ├── ex04 └── MAC.sh ├── ex02 └── clean ├── ex20 ├── fullcode ├── ft_strdup.c └── fullcode.c ├── ex21 ├── ft_range ├── fullcode.c └── ft_range.c ├── piscine_reloaded.pdf ├── ress ├── Sans titre.jpg └── Screenshot_20210228-005159.jpg ├── ex03 └── find_sh.sh ├── ex09 ├── ft_ft.c └── fullcode.c ├── ex22 └── abs.h ├── ex10 ├── ft_swap.c └── fullcode.c ├── ex11 ├── ft_div_mod.c └── fullcode.c ├── ex16 ├── ft_strlen.c └── fullcode.c ├── ex08 └── ft_is_negative.c ├── ex07 └── ft_print_numbers.c ├── ex14 ├── ft_sqrt.c └── fullcode.c ├── ex06 └── ft_print_alphabet.c ├── ex13 ├── ft_recursive_factorial.c └── fullcode.c ├── ex12 ├── ft_iterative_factorial.c └── fullcode.c ├── ex18 └── ft_print_params.c ├── ex19 └── ft_sort_params.c ├── ex15 ├── ft_putstr.c └── fullcode.c ├── ex17 ├── ft_strcmp.c └── fullcode.c ├── ex00 ├── Readme.md └── exo.tar └── README.md /ex01/z: -------------------------------------------------------------------------------- 1 | Z 2 | -------------------------------------------------------------------------------- /ex05/"\?$*’KwaMe’*$?\": -------------------------------------------------------------------------------- 1 | 42 -------------------------------------------------------------------------------- /ex04/MAC.sh: -------------------------------------------------------------------------------- 1 | ifconfig -a | grep ether | awk '{print $2}' 2 | -------------------------------------------------------------------------------- /ex02/clean: -------------------------------------------------------------------------------- 1 | find . -type f \( -name "*~" -o -name "#*#" \) -print -delete 2 | -------------------------------------------------------------------------------- /ex20/fullcode: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ablaamim/Piscine-Reloaded/HEAD/ex20/fullcode -------------------------------------------------------------------------------- /ex21/ft_range: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ablaamim/Piscine-Reloaded/HEAD/ex21/ft_range -------------------------------------------------------------------------------- /piscine_reloaded.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ablaamim/Piscine-Reloaded/HEAD/piscine_reloaded.pdf -------------------------------------------------------------------------------- /ress/Sans titre.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ablaamim/Piscine-Reloaded/HEAD/ress/Sans titre.jpg -------------------------------------------------------------------------------- /ex03/find_sh.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | find . -type f -name "*.sh" | rev | cut -d / -f1 | cut -c 4- | rev 4 | -------------------------------------------------------------------------------- /ress/Screenshot_20210228-005159.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ablaamim/Piscine-Reloaded/HEAD/ress/Screenshot_20210228-005159.jpg -------------------------------------------------------------------------------- /ex21/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/18 15:57:07 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/22 18:51:32 by root ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | -------------------------------------------------------------------------------- /ex09/ft_ft.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_ft.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: Alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/09 13:54:18 by Alaamimi #+# #+# */ 9 | /* Updated: 2021/02/09 13:54:45 by Alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | void ft_ft(int *nbr) 14 | { 15 | *nbr = 42; 16 | } 17 | -------------------------------------------------------------------------------- /ex22/abs.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* abs.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ablaamim +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/10/29 14:26:21 by ablaamim #+# #+# */ 9 | /* Updated: 2021/10/29 14:27:06 by ablaamim ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_ABS_H 14 | #define FT_ABS_H 15 | 16 | #define ABS(value) ? -value : value 17 | -------------------------------------------------------------------------------- /ex10/ft_swap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_swap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: Alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/09 14:17:12 by Alaamimi #+# #+# */ 9 | /* Updated: 2021/02/09 14:17:56 by Alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | void ft_swap(int *a, int *b) 14 | { 15 | int tmp; 16 | tmp = *a; 17 | *a = *b; 18 | *b = tmp; 19 | } 20 | -------------------------------------------------------------------------------- /ex11/ft_div_mod.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_div_mod.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: Alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/09 16:18:29 by Alaamimi #+# #+# */ 9 | /* Updated: 2021/02/09 16:19:19 by Alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | void ft_div_mod(int a, int b, int *div, int *mod) 14 | { 15 | *div = a / b; 16 | *mod = a % b; 17 | } 18 | -------------------------------------------------------------------------------- /ex16/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 17:51:25 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/10 18:09:46 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_strlen(char *str) 14 | { 15 | int len; 16 | 17 | len = 0; 18 | while (str[len] != '\0') 19 | { 20 | len++; 21 | } 22 | return (len); 23 | } 24 | -------------------------------------------------------------------------------- /ex08/ft_is_negative.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_is_negative.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/27 01:34:50 by alaamimi #+# #+# */ 9 | /* Updated: 2021/01/27 01:38:46 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | void ft_putchar(char c); 14 | 15 | void ft_is_negative(int n) 16 | { 17 | if (n < 0) 18 | ft_putchar('N'); 19 | else 20 | ft_putchar('P'); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /ex07/ft_print_numbers.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_numbers.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/27 01:29:24 by alaamimi #+# #+# */ 9 | /* Updated: 2021/01/27 01:39:31 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | void ft_putchar(char c); 14 | 15 | void ft_print_numbers(void) 16 | { 17 | char nbr; 18 | 19 | nbr = '0'; 20 | while(nbr <= '9') 21 | { 22 | ft_putchar(nbr); 23 | nbr++; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ex14/ft_sqrt.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sqrt.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 16:19:06 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/10 16:21:44 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_sqrt(int nb) 14 | { 15 | int sqrt; 16 | 17 | sqrt = 0; 18 | while (sqrt <= nb / 2) 19 | { 20 | if ((sqrt * sqrt) == nb) 21 | return(nb); 22 | else 23 | sqrt++; 24 | } 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /ex06/ft_print_alphabet.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_alphabet.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/27 01:19:48 by alaamimi #+# #+# */ 9 | /* Updated: 2021/01/27 01:24:58 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | 14 | void ft_putchar(char c); 15 | 16 | void ft_print_alphabet(void) 17 | { 18 | char ltr; 19 | 20 | letter = 'a'; 21 | while(letter <= 'z') 22 | { 23 | ft_putchar(ltr); 24 | letter++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ex13/ft_recursive_factorial.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_recursive_factorial.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 13:55:16 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/10 13:57:42 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_recursive_factorial(int nb) 14 | { 15 | if (nb == 0) 16 | return (1); 17 | else if (nb < 0 || nb > 12) 18 | return (0); 19 | else 20 | return (nb * ft_recursive_factorial(nb - 1)); 21 | } 22 | -------------------------------------------------------------------------------- /ex12/ft_iterative_factorial.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_iterative_factorial.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 13:45:51 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/10 13:46:23 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | 14 | int ft_iterative_factorial(int nb) 15 | { 16 | int product; 17 | 18 | if (nb > 12) 19 | return (0); 20 | if (nb < 0) 21 | return (0); 22 | product = 1; 23 | while (nb >= 1) 24 | { 25 | product = product * nb; 26 | nb--; 27 | } 28 | return (product); 29 | } 30 | -------------------------------------------------------------------------------- /ex20/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/18 00:32:13 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/18 00:39:06 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | #include 13 | 14 | int ft_strlen(char *str) 15 | { 16 | int len; 17 | 18 | len = 0; 19 | while(str[len] != '\0') 20 | { 21 | len++; 22 | } 23 | return (len); 24 | } 25 | 26 | int *strdup(char *src) 27 | { 28 | int i; 29 | char *dest; 30 | 31 | i = 0; 32 | dest = malloc(sizeof(*src) * (ft_strlen((char *)src) + 1)); 33 | if (dest = NULL) 34 | return (NULL); 35 | while(src[i] != '\0') 36 | { 37 | dest[i] = src[i]; 38 | i++; 39 | } 40 | dest[i] = '\0'; 41 | return(dest); 42 | } 43 | -------------------------------------------------------------------------------- /ex09/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/27 01:40:03 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/09 16:49:05 by Alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | void ft_putchar(char c) 17 | { 18 | write(1, &c, 1); 19 | } 20 | 21 | void ft_putnbr(int nb) 22 | { 23 | if (nb < 0) 24 | { 25 | nb = -nb; 26 | ft_putchar('-'); 27 | } 28 | if (nb < 10 && nb >= 0) 29 | ft_putchar(nb + '0'); 30 | if (nb >= 10) 31 | { 32 | ft_putnbr(nb / 10); 33 | ft_putnbr(nb % 10); 34 | } 35 | } 36 | 37 | void ft_ft(int *nbr) 38 | { 39 | *nbr = 42; 40 | } 41 | 42 | int main(void) 43 | { 44 | int i; 45 | int *nbr; 46 | 47 | i = 0; 48 | nbr = &i; 49 | ft_putnbr(i); 50 | ft_putchar('\n'); 51 | ft_ft(nbr); 52 | ft_putnbr(i); 53 | ft_putchar('\n'); 54 | return EXIT_SUCCESS; 55 | } 56 | -------------------------------------------------------------------------------- /ex13/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 13:46:49 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/10 13:57:19 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | void ft_putchar(char c) 17 | { 18 | write(1, &c, 1); 19 | } 20 | 21 | void ft_putnbr(int nb) 22 | { 23 | if (nb < 0) 24 | { 25 | nb = -nb; 26 | ft_putchar('-'); 27 | } 28 | if (nb > 10) 29 | { 30 | ft_putnbr(nb / 10); 31 | ft_putnbr(nb % 10); 32 | } 33 | if (nb >= 0 && nb < 10) 34 | ft_putchar(nb + '0'); 35 | } 36 | 37 | int ft_recursive_factorial(int nb) 38 | { 39 | if (nb == 0) 40 | return (1); 41 | else if (nb < 0 || nb > 12) 42 | return (0); 43 | else 44 | return (nb * ft_recursive_factorial(nb - 1)); 45 | } 46 | 47 | int main(void) 48 | { 49 | int i; 50 | 51 | i = 0; 52 | ft_putnbr(i); 53 | ft_putchar('\n'); 54 | ft_putnbr(ft_recursive_factorial(i)); 55 | ft_putchar('\n'); 56 | return (EXIT_SUCCESS); 57 | } 58 | -------------------------------------------------------------------------------- /ex18/ft_print_params.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_params.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/11 19:37:08 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/11 20:08:37 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | void ft_putchar(char c) 16 | { 17 | write(1, &c, 1); 18 | } 19 | 20 | void ft_putstr(char *str) 21 | { 22 | int i; 23 | 24 | i = 0; 25 | while(str[i] != '\0') 26 | { 27 | ft_putchar(str[i]); 28 | i++; 29 | } 30 | } 31 | 32 | void ft_putnbr(int nb) 33 | { 34 | if (nb < 0) 35 | { 36 | nb = -nb; 37 | ft_putchar('-'); 38 | } 39 | if (nb >= 10) 40 | { 41 | ft_putnbr(nb / 10); 42 | ft_putnbr(nb % 10); 43 | } 44 | if (nb >= 0 && nb < 10) 45 | ft_putchar(nb + '0'); 46 | } 47 | 48 | int main(int argc, char **argv) 49 | { 50 | int i; 51 | 52 | i = 1; 53 | (void)argc; 54 | (void)argv; 55 | while (i < argc) 56 | { 57 | ft_putstr(argv[i]); 58 | ft_putchar('\n'); 59 | i++; 60 | } 61 | write(1, "Arguments count = ", 20); 62 | ft_putnbr(argc); 63 | ft_putchar('\n'); 64 | return (0); 65 | } 66 | -------------------------------------------------------------------------------- /ex12/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 13:45:20 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/10 13:58:59 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | 14 | #include 15 | #include 16 | 17 | void ft_putchar(char c) 18 | { 19 | write(1, &c, 1); 20 | } 21 | 22 | void ft_putnbr(int nb) 23 | { 24 | if (nb < 0) 25 | { 26 | nb = -nb; 27 | ft_putchar('-'); 28 | } 29 | if (nb >= 0 && nb < 10) 30 | ft_putchar(nb + '0'); 31 | if (nb >= 10) 32 | { 33 | ft_putnbr(nb / 10); 34 | ft_putnbr(nb % 10); 35 | } 36 | } 37 | 38 | int ft_iterative_factorial(int nb) 39 | { 40 | int product; 41 | 42 | if (nb > 12) 43 | return (0); 44 | if (nb < 0) 45 | return (0); 46 | product = 1; 47 | while (nb >= 1) 48 | { 49 | product = product * nb; 50 | nb--; 51 | } 52 | return (product); 53 | } 54 | 55 | int main(void) 56 | { 57 | int i; 58 | i = 0; 59 | 60 | ft_putnbr(i); 61 | ft_putchar('\n'); 62 | ft_putnbr(ft_iterative_factorial(i)); 63 | ft_putchar('\n'); 64 | 65 | return (EXIT_SUCCESS); 66 | } 67 | -------------------------------------------------------------------------------- /ex21/ft_range.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_range.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/18 15:56:57 by alaamimi #+# #+# */ 9 | /* Updated: 2021/10/29 14:06:36 by root ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | int *ft_range(int min, int max) 17 | { 18 | int *str; 19 | int i; 20 | 21 | 22 | if (min >= max) 23 | return (0); 24 | i = 0; 25 | max -= min; 26 | str = (int *)malloc(sizeof(int) * max); 27 | while (i < max) 28 | { 29 | str[i] = min + i; 30 | i++; 31 | } 32 | return (str); 33 | } 34 | 35 | void ft_intarray_debug(int *tab, int size) 36 | { 37 | int i; 38 | 39 | i = 0; 40 | printf("["); 41 | while (i < size - 1) 42 | { 43 | printf("%d, ", tab[i]); 44 | i++; 45 | } 46 | printf("%d", tab[size - 1]); 47 | printf("]"); 48 | } 49 | 50 | int main(void) 51 | { 52 | int len; 53 | int arr[] = {0, 1, 2, 3, 4}; 54 | 55 | len = sizeof(arr) / sizeof(arr[0]); 56 | ft_intarray_debug(arr, len); 57 | printf("\n"); 58 | ft_intarray_debug(ft_range(10, 20), 10); 59 | printf("\n"); 60 | return (EXIT_SUCCESS); 61 | } 62 | -------------------------------------------------------------------------------- /ex10/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: Alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/09 14:08:05 by Alaamimi #+# #+# */ 9 | /* Updated: 2021/02/09 16:40:47 by Alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | void ft_putchar(char c) 17 | { 18 | write(1, &c, 1); 19 | } 20 | 21 | void ft_putnbr(int nb) 22 | { 23 | if (nb < 0) 24 | { 25 | nb = - nb; 26 | ft_putchar('-'); 27 | } 28 | if (nb >= 0 && nb < 10) 29 | { 30 | ft_putchar(nb + '0'); 31 | } 32 | if (nb > 10) 33 | { 34 | ft_putnbr(nb / 10); 35 | ft_putnbr(nb % 10); 36 | } 37 | } 38 | 39 | void ft_swap(int *a, int *b) 40 | { 41 | int tmp; 42 | 43 | tmp = *a; 44 | *a = *b; 45 | *b = tmp; 46 | } 47 | 48 | int main(void) 49 | { 50 | int nb1; 51 | int nb2; 52 | int *a; 53 | int *b; 54 | 55 | nb1 = 0; 56 | nb2 = 1; 57 | a = &nb1; 58 | b = &nb2; 59 | ft_putnbr(nb1); 60 | ft_putchar('\n'); 61 | ft_putnbr(nb2); 62 | ft_putchar('\n'); 63 | ft_swap(a, b); 64 | ft_putnbr(nb1); 65 | ft_putchar('\n'); 66 | ft_putnbr(nb2); 67 | ft_putchar('\n'); 68 | return EXIT_SUCCESS; 69 | } 70 | -------------------------------------------------------------------------------- /ex19/ft_sort_params.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sort_params.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/11 20:17:26 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/11 20:29:23 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | void ft_putchar(char c) 16 | { 17 | write(1, &c, 1); 18 | } 19 | 20 | void ft_putstr(char *str) 21 | { 22 | int i; 23 | 24 | i = 0; 25 | while (str[i] != '\0') 26 | { 27 | ft_putchar(str[i]); 28 | i++; 29 | } 30 | } 31 | 32 | int ft_strcmp(char *s1, char *s2) 33 | { 34 | int i; 35 | 36 | i = 0; 37 | while ((s1[i] == s2[i]) && (s1[i] != '\0' && s2[i] != '\0')) 38 | i++; 39 | return (s1[i] - s2[i]); 40 | } 41 | 42 | int main(int argc, char **argv) 43 | { 44 | int i; 45 | int j; 46 | char *tmp; 47 | 48 | i = 0; 49 | while (++i < argc) 50 | { 51 | j = i; 52 | while (++j < argc) 53 | { 54 | if (ft_strcmp(argv[i], argv[j]) > 0) 55 | { 56 | tmp = argv[i]; 57 | argv[i] = argv[j]; 58 | argv[j] = tmp; 59 | } 60 | } 61 | } 62 | i = 0; 63 | while (++i < argc) 64 | { 65 | ft_putstr(argv[i]); 66 | ft_putchar('\n'); 67 | } 68 | return (0); 69 | } 70 | -------------------------------------------------------------------------------- /ex11/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: Alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/09 14:56:44 by Alaamimi #+# #+# */ 9 | /* Updated: 2021/02/09 16:24:11 by Alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | void ft_putchar(char c) 17 | { 18 | write(1, &c, 1); 19 | } 20 | 21 | void ft_putnbr(int nb) 22 | { 23 | if (nb < 0) 24 | { 25 | nb = -nb; 26 | ft_putchar('-'); 27 | } 28 | if (nb > 10) 29 | { 30 | ft_putnbr(nb / 10); 31 | ft_putnbr(nb % 10); 32 | } 33 | if (nb >= 0 && nb < 10) 34 | { 35 | ft_putchar(nb + '0'); 36 | } 37 | } 38 | 39 | void ft_div_mod(int a, int b, int *div, int *mod) 40 | { 41 | if (b != 0) 42 | { 43 | *div = a / b; 44 | *mod = a % b; 45 | } 46 | } 47 | 48 | int main(void) 49 | { 50 | int a; 51 | int b; 52 | int div; 53 | int mod; 54 | int *pdiv; 55 | int *pmod; 56 | 57 | a = 8; 58 | b = 3; 59 | div = 0; 60 | mod = 0; 61 | pdiv = ÷ 62 | pmod = &mod; 63 | pmod = &mod; 64 | ft_putnbr(a); 65 | ft_putchar('\n'); 66 | ft_putnbr(b); 67 | ft_putchar('\n'); 68 | ft_div_mod(a, b, pdiv, pmod); 69 | ft_putnbr(div); 70 | ft_putchar('\n'); 71 | ft_putnbr(mod); 72 | ft_putchar('\n'); 73 | return (EXIT_SUCCESS); 74 | } 75 | -------------------------------------------------------------------------------- /ex14/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_sqrt.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 14:31:38 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/10 16:17:43 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | void ft_putchar(char c) 17 | { 18 | write(1, &c, 1); 19 | } 20 | 21 | void ft_putnbr(int nb) 22 | { 23 | if (nb < 0) 24 | { 25 | nb = -nb; 26 | ft_putchar('-'); 27 | } 28 | if (nb >= 10) 29 | { 30 | ft_putnbr(nb / 10); 31 | ft_putnbr(nb % 10); 32 | } 33 | if (nb >= 0 && nb < 10) 34 | ft_putchar(nb + '0'); 35 | } 36 | 37 | int ft_sqrt(int nb) 38 | { 39 | int sqrt; 40 | 41 | sqrt = 0; 42 | while (sqrt <= nb / 2) 43 | { 44 | if ((sqrt * sqrt) == nb) 45 | return (sqrt); 46 | else 47 | sqrt++; 48 | } 49 | return (0); 50 | 51 | } 52 | 53 | int main(void) 54 | { 55 | int i; 56 | 57 | i = 0; 58 | ft_putnbr(i); 59 | ft_putchar('\n'); 60 | ft_putnbr(ft_sqrt(i)); 61 | ft_putchar('\n'); 62 | write (1, "-----\n", 6); 63 | i = 100; 64 | ft_putnbr(i); 65 | ft_putchar('\n'); 66 | ft_putnbr(ft_sqrt(i)); 67 | ft_putchar('\n'); 68 | write(1, "-----\n", 6); 69 | i = -1; 70 | ft_putnbr(i); 71 | ft_putchar('\n'); 72 | ft_putnbr(ft_sqrt(i)); 73 | ft_putchar('\n'); 74 | return (EXIT_SUCCESS); 75 | } 76 | -------------------------------------------------------------------------------- /ex16/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 17:53:35 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/10 18:09:12 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | void ft_putchar(char c) 17 | { 18 | write(1, &c, 1); 19 | } 20 | 21 | void ft_putstr(char *str) 22 | { 23 | while (*str != '\0') 24 | { 25 | ft_putchar(*str); 26 | *str++; 27 | } 28 | } 29 | 30 | void ft_putnbr(int nb) 31 | { 32 | if (nb < 0) 33 | { 34 | nb = -nb; 35 | ft_putchar('-'); 36 | } 37 | if (nb >= 10) 38 | { 39 | ft_putnbr(nb / 10); 40 | ft_putnbr(nb % 10); 41 | } 42 | if (nb >= 0 && nb < 10) 43 | ft_putchar(nb + '0'); 44 | } 45 | 46 | int ft_strlen(char *str) 47 | { 48 | int len; 49 | 50 | len = 0; 51 | while (str[len] != '\0') 52 | { 53 | len++; 54 | } 55 | return (len); 56 | } 57 | 58 | int main(void) 59 | { 60 | int i; 61 | i = 0; 62 | char str[] = "vlad"; 63 | 64 | ft_putstr(str); 65 | ft_putchar('\n'); 66 | i = ft_strlen(str); 67 | ft_putnbr(i); 68 | ft_putchar('\n'); 69 | write(1, "-----\n", 6); 70 | char str1[] = "0123456789"; 71 | ft_putstr(str1); 72 | ft_putchar('\n'); 73 | i = ft_strlen(str1); 74 | ft_putnbr(i); 75 | ft_putchar('\n'); 76 | write(1, "-----\n", 6); 77 | return (EXIT_SUCCESS); 78 | } 79 | -------------------------------------------------------------------------------- /ex15/ft_putstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 16:54:59 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/11 18:56:06 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /*This function displays the string s to the standard output. We do this using 14 | our ft_putchar function. We start by declaring a i variable that we 15 | will use to move through our string. We use a i because it is 16 | guaranteed to be big enough to contain the size of the biggest object your 17 | system can handle. This way we can display the absolute biggest string that 18 | our computer can handle. We set i equal to 0 to start at the beginning of 19 | the parameter string s. We then do a simple loop stating that so long as we 20 | have not reached the end of our string we want the loop to continue. We move 21 | to each index position of our string and place the character in that 22 | position as a parameter for our ft_putchar function. The ft_putchar function 23 | will put that character in the standard output and then we increment the i 24 | variable to continue moving through the string. We do this until we reach 25 | the terminating '\0' of the string.*/ 26 | 27 | void ft_putstr(char *str) 28 | { 29 | int i; 30 | 31 | i = 0; 32 | while (str[i] != '\0') 33 | { 34 | ft_putchar(str[i]); 35 | i++; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /ex20/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/17 23:16:03 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/18 00:15:41 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | void ft_putchar(char c) 18 | { 19 | write(1, &c, 1); 20 | } 21 | 22 | void ft_putstr(char *str) 23 | { 24 | int i; 25 | 26 | i = 0; 27 | while(str[i] != '\0') 28 | { 29 | ft_putchar(str[i]); 30 | i++; 31 | } 32 | } 33 | 34 | int ft_strlen(char *str) 35 | { 36 | int len; 37 | 38 | len = 0; 39 | while(str[len] != '\0') 40 | { 41 | len++; 42 | } 43 | return(len); 44 | } 45 | 46 | char *ft_strdup(char *src) 47 | { 48 | int i; 49 | char *dest; 50 | 51 | i = 0; 52 | dest = malloc(sizeof(*src) * ((ft_strlen((char *)src)) + 1)); 53 | if (dest == NULL) 54 | return (NULL); 55 | while (src[i] != '\0') 56 | { 57 | dest[i] = src[i]; 58 | i++; 59 | } 60 | dest[i] = '\0'; 61 | return (dest); 62 | } 63 | 64 | int main(void) 65 | { 66 | char str[] = "Duplicate me motherfucker"; 67 | 68 | ft_putstr(str); 69 | ft_putchar('\n'); 70 | printf("%p\n", str); 71 | write(1, "-----\n", 6); 72 | ft_strdup(str); 73 | ft_putstr(str); 74 | ft_putchar('\n'); 75 | printf("%p\n", str); 76 | write(1, "-----\n", 6); 77 | return (EXIT_SUCCESS); 78 | } 79 | -------------------------------------------------------------------------------- /ex17/ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/11 18:38:16 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/11 18:39:29 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /*According to the man this function lexicographically compares the NULL 14 | terminated strings s1 and s2 that are passed into it's parameters. The 15 | function returns an integer greater than, equal to, or less than 0 depending 16 | on whether the string s1 is greater than, equal to, or less than string s2. 17 | The comparison is done using unsigned characters, so that '\200' is greater 18 | than '\0'.*/ 19 | 20 | int ft_strcmp(char *s1, char *s2) 21 | { 22 | /*i will start by creating an index variable i (counter), and making it an unsigned 23 | int in case the input is a long string. hen i set it equal to 0 to 24 | place the counter at the start of the string inside the while loop.*/ 25 | 26 | unsigned int i; 27 | 28 | i = 1; 29 | 30 | /*The while loop requires two conditions to be true for it to begin looping 31 | and to continue looping ofc. i the loop to happen as long as i have 32 | not reached the end of the string s1 && i want the loop to happen only 33 | so long as the character at position i in s1 is the same as the character 34 | in postion i in s2. The second we find a difference in the string i want 35 | to compare the difference. */ 36 | while (s1[i] != '\0' && s1[i] == s2[i]) 37 | { 38 | i++; 39 | 40 | 41 | /*When it has either found a non matching character or it has reached the 42 | end of s1 the function should return the difference between the character in position i in 43 | s1 and the character in position i in s2. 44 | */ 45 | } 46 | return (s1[i] - s2[i]); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /ex15/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/10 16:41:02 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/10 16:54:42 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | void ft_putchar(char c) 17 | { 18 | write(1, &c, 1); 19 | } 20 | 21 | /*This function displays the string s to the standard output. We do this using 22 | our ft_putchar function. We start by declaring a i variable i that we 23 | will use to move through our string. We use a i because it is 24 | guaranteed to be big enough to contain the size of the biggest object your 25 | system can handle. This way we can display the absolute biggest string that 26 | our computer can handle. We set i equal to 0 to start at the beginning of 27 | the parameter string s. We then do a simple loop stating that so long as we 28 | have not reached the end of our string we want the loop to continue. We move 29 | to each index position of our string and place the character in that 30 | position as a parameter for our ft_putchar function. The ft_putchar function 31 | will put that character in the standard output and then we increment the i 32 | variable to continue moving through the string. We do this until we reach 33 | the terminating '\0' of the string.*/ 34 | 35 | void ft_putstr(char *str) 36 | { 37 | int i; 38 | 39 | i = 0; 40 | while (str[i] != '\0') 41 | { 42 | ft_putchar(str[i]); 43 | i++; 44 | } 45 | } 46 | 47 | int main(void) 48 | { 49 | char str[] = "vlad is cool"; 50 | ft_putstr(str); 51 | ft_putchar('\n'); 52 | write(1, "-----\n", 6); 53 | char str1[] = ""; 54 | ft_putstr(str1); 55 | ft_putchar('\n'); 56 | write(1, "-----\n", 6); 57 | char str2[] = "Pipe this to another | and export the $"; 58 | ft_putstr(str2); 59 | ft_putchar('\n'); 60 | write(1, "-----\n", 6); 61 | return (EXIT_SUCCESS); 62 | } 63 | -------------------------------------------------------------------------------- /ex17/fullcode.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fullcode.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: alaamimi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/11 17:50:46 by alaamimi #+# #+# */ 9 | /* Updated: 2021/02/11 18:31:06 by alaamimi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | void ft_putchar(char c) 17 | { 18 | write(1, &c, 1); 19 | } 20 | 21 | void ft_putstr(char *str) 22 | { 23 | int i; 24 | 25 | i = 0; 26 | while (str[i] != '\0') 27 | { 28 | ft_putchar(str[i]); 29 | i++; 30 | } 31 | } 32 | 33 | void ft_putnbr(int nb) 34 | { 35 | if (nb < 0) 36 | { 37 | nb = -nb; 38 | ft_putchar('-'); 39 | } 40 | if (nb > 10) 41 | { 42 | ft_putnbr(nb / 10); 43 | ft_putnbr(nb % 10); 44 | } 45 | if (nb >= 0 && nb < 10) 46 | ft_putchar(nb + '0'); 47 | } 48 | 49 | /*According to the man this function lexicographically compares the NULL 50 | terminated strings s1 and s2 that are passed into it's parameters. The 51 | function returns an integer greater than, equal to, or less than 0 depending 52 | on whether the string s1 is greater than, equal to, or less than string s2. 53 | The comparison is done using unsigned characters, so that '\200' is greater 54 | than '\0'.*/ 55 | 56 | int ft_strcmp(char *s1, char *s2) 57 | { 58 | /*i will start by creating an index variable i (counter), and making it an unsigned 59 | int in case the input is a long string. hen i set it equal to 0 to 60 | place the counter at the start of the string inside the while loop.*/ 61 | 62 | unsigned int i; 63 | 64 | i = 1; 65 | 66 | /*The while loop requires two conditions to be true for it to begin looping 67 | and to continue looping ofc. i the loop to happen as long as i have 68 | not reached the end of the string s1 && i want the loop to happen only 69 | so long as the character at position i in s1 is the same as the character 70 | in postion i in s2. The second we find a difference in the string i want 71 | to compare the difference. */ 72 | while (s1[i] != '\0' && s1[i] == s2[i]) 73 | { 74 | i++; 75 | 76 | 77 | /*When it has either found a non matching character or it has reached the 78 | end of s1 the function should return the difference between the character in position i in 79 | s1 and the character in position i in s2. 80 | */ 81 | } 82 | return (s1[i] - s2[i]); 83 | } 84 | 85 | int main(void) 86 | { 87 | int i; 88 | 89 | i = '1'; 90 | char str1[] = "Vlad"; 91 | char str2[] = "Vlad"; 92 | write(1, "-----\n", 6); 93 | ft_putstr(str1); 94 | ft_putchar('\n'); 95 | ft_putstr(str2); 96 | ft_putchar('\n'); 97 | i = ft_strcmp(str1, str2); 98 | ft_putnbr(ft_strcmp(str1, str2)); 99 | ft_putchar('\n'); 100 | write(1, "-----\n", 6); 101 | 102 | char str3[] = "a"; 103 | char str4[] = "ab"; 104 | ft_putstr(str3); 105 | ft_putchar('\n'); 106 | ft_putstr(str4); 107 | ft_putchar('\n'); 108 | i = ft_strcmp(str3, str4); 109 | ft_putnbr(ft_strcmp(str3, str4)); 110 | ft_putchar('\n'); 111 | write(1, "-----\n", 6); 112 | 113 | char str5[] = "ab"; 114 | char str6[] = "a"; 115 | ft_putstr(str5); 116 | ft_putchar('\n'); 117 | ft_putstr(str6); 118 | ft_putchar('\n'); 119 | i = ft_strcmp(str5, str6); 120 | ft_putnbr(ft_strcmp(str5, str6)); 121 | ft_putchar('\n'); 122 | write(1, "-----\n", 6); 123 | 124 | return (EXIT_SUCCESS); 125 | } 126 | -------------------------------------------------------------------------------- /ex00/Readme.md: -------------------------------------------------------------------------------- 1 | ------------------------------------------ 2 | #### Objective: 3 | ``` 4 | $ ls -l 5 | total 42 6 | drwx--xr-x 2 login wheel XX Jun 1 20:47 test0 7 | -rwx--xr-- 1 login wheel 4 Jun 1 21:46 test1 8 | dr-x---r-- 2 login wheel XX Jun 1 22:45 test2 9 | -r-----r-- 2 login wheel 1 Jun 1 23:44 test3 10 | -rw-r----x 1 login wheel 2 Jun 1 23:43 test4 11 | -r-----r-- 2 login wheel 1 Jun 1 23:44 test5 12 | lrwxr-xr-x 1 login wheel 5 Jun 1 22:20 test6 -> test0 13 | $ 14 | ``` 15 | 16 | ------------------------------------------ 17 | #### Command `ls -l` output explanation 18 | ``` 19 | -rwxrw-r-- 10 root root 2048 Jan 13 07:11 afile.exe 20 | ?UUUGGGOOOS 00 UUUUUU GGGGGG #### └─ date stamp and file name are obvious ;-) 21 | ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 22 | │ │ │ │ │ │ │ │ └─── File Size 23 | │ │ │ │ │ │ │ └──────── Group Name (for example, Users, Administrators, etc) 24 | │ │ │ │ │ │ └─────────────── Owner Acct 25 | │ │ │ │ │ └────────────────────── Link count (what constitutes a "link" here varies) 26 | │ │ │ │ └─────────────────────────── Alternative Access (blank means none defined, anything else varies) 27 | │ └──┴──┴───────────────────────────── Read, Write and Special access modes for [U]ser, [G]roup, and [O]thers (everyone else) 28 | └───────────────────────────────────── File type flag - 29 | ``` 30 | >The permissions (mode) flags (UUUGGGOOO) are three sets of three chars, where 31 | the first set is "User" (i.e., Owner), the second set is "Group" and the third 32 | set is "Others" (i.e., everyone else; anyone who is neither Owner nor Group). 33 | 34 | ------------------------------------------ 35 | #### How to create all that we need 36 | 37 | > * Remember that we use `touch` and `mkdir` to create files and folders 38 | respectively. 39 | 40 | But now to the new part: we need to create a _soft link_ for the folder `test0` 41 | > That is what the symbol `->` means. 42 | 43 | And if we pay attention to detail we notice that `test5` is a __hard link__** 44 | of the file `test3` 45 | > We determine that due to the __link count__** size on both files plus the 46 | fact that they share the same `date-stamp` 47 | 48 | And as a review of the difference between __hard__** and __soft__** links we can 49 | review this image: 50 | 51 | Here is how we get to that picture: 52 | 1. Create a name `myfile.txt` in the file system that points to a new inode 53 | (which contains the metadata for the file and points to the blocks of data that 54 | contain its contents, i.e. the text "Hello, World!": 55 | `$ echo 'Hello, World!' > myfile.txt` 56 | 2. Create a hard link `my-hard-link` to the file `myfile.txt`, which means 57 | "create a file that should point to the same inode that `myfile.txt` points to": 58 | `ln myfile.txt my-hard-link` 59 | 3. Create a soft link `my-soft-link` to the file `myfile.txt`, which means 60 | "create a file that should point to the file `myfile.txt`": 61 | `ln -s myfile.txt my-soft-link` 62 | Look what will now happen if `myfile.txt` is deleted (or moved): `my-hard-link` 63 | still points to the same contents, and is thus unaffected, whereas 64 | `my-soft-link` now points to nothing. Is up to future tasks to evaluate the 65 | pros/cons of each. 66 | 67 | ------------------------------------------ 68 | #### Changing the timestamp of a symlink 69 | We just need to add the [OPTION] -h to `touch` 70 | ``` 71 | touch -h -a -m -t 201806012220.00 test6 72 | ``` 73 | > Use `man touch` for more info. 74 | 75 | ------------------------------------------ 76 | #### Change the FILE SIZE + timestamp + permissions 77 | > But is better to leave it to the end, and for now focus on how to create all 78 | the folders and files accordingly with the requirements and come back to it 79 | latter. 80 | 81 | ------------------------------------------ 82 | #### How do file permissions apply to symlinks? 83 | It depends on how you call `chmod` and the platform you are running on. 84 | For example, on a Linux system, `man chmod` says this: 85 | >**chmod** never changes the permissions of symbolic links; the **chmod** 86 | system call cannot change their permissions. This is not a problem since the 87 | permissions of symbolic links are never used. However, for each symbolic link 88 | listed on the command line, **chmod** changes the permissions of the pointed-to 89 | file. In contrast, **chmod** ignores symbolic links encountered during recursive 90 | directory traversals. 91 | 92 | However, on a Mac, _chmod_ can be used to modify the permissions of a symbolic 93 | link using options such as this (from `man chmod`): 94 | >**-h** If the file is a symbolic link, change the mode of the link itself 95 | rather than the file that the link points to. 96 | 97 | ------------------------------------------ 98 | -------------------------------------------------------------------------------- /ex00/exo.tar: -------------------------------------------------------------------------------- 1 | test0/0000715000000000000000000000000014055500244010606 5ustar rootroottest10000714000000000000000000000000014055500244010522 0ustar rootroottest2/0000504000000000000000000000000014002032110010564 5ustar rootroottest30000404000000000000000000000000014002033747010521 0ustar rootroottest40000541000000000000000000000000014002032124010510 0ustar rootroottest50000404000000000000000000000000014002033747011507 1test3ustar rootroottest60000777000000000000000000000000014055513160011523 2test0ustar rootroot -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # :books: Piscine Reloaded : 2 | 3 | > This is the Piscine Reloaded project @ 1337 (42 Network), it no longer exists in the new graph. 4 | 5 | > Review the basics with a set of well-picked assignments from the selection Piscine. 6 | 7 | 8 | 9 | 10 | ### :dart: Objectives : 11 | > - Reviewing. 12 | > - Basics of programming. 13 | ### :floppy_disk: Skills : 14 | > - Imperative programming 15 | > - Unix 16 | > - Rigor 17 | 18 | ### :eyes: Old holy graph : 19 | 20 | 21 | 22 | ### :information_source: The new holy graph does not include this project !! 23 | 24 | 25 | 26 | # :books: Subject : 27 | 28 | > [Piscine Reloaded pdf](https://github.com/Alaamimi/Piscine-Reloaded/blob/master/piscine_reloaded.pdf) 29 | 30 | ### This project consists of multiple generic functions that can be used in a lot of situations : 31 | | Exercises | Task | 32 | |--- |--- | 33 | | Ex00 | ls -l in folder should display specific things. | 34 | | Ex01 |Create a file which show Z when displayed with cat command. | 35 | | Ex02 | Find a command line that will search from current directory and any sub-directories files ending with ~ or beginning by #. It will show and delete these. Only one command allowed, no ";" or "&&" | 36 | | Ex03 | Find a command line that will search from current directory and any sub-directories files ending by ".sh", displaying only their name without ".sh" | 37 | | Ex04 | Write a command line that displays MAC adresses from your machine. | 38 | | Ex05 | Create a file containing ONLY "42", named "\?$\*'KwaMe'\*$?\". | 39 | | Ex06 | void ft_print_alphabet(void); Show the alphabet in lowercase a > z. | 40 | | Ex07 | void ft_print_numbers(void); Write all numbers on the same line 1 > 9. | 41 | | Ex08 | void ft_is_negative(int n); Return N when n is negative or P when it is positive (0 is positive). | 42 | | Ex09 | void ft_ft(int \*nbr); Give the \*nbr pointer the value 42. | 43 | | Ex10 | void ft_swap(int \*a, int \*b); Swap integers values using their adresses. | 44 | | Ex11 | void ft_div_mod(int a, int b, int \*div, int \*mod); Divide a by b and store at \*div, store the rest at \*mod. | 45 | | Ex12 | int ft_iterative_factorial(int nb); Iterative function that returns a number, result of factorial from nb. | 46 | | Ex13 | int ft_recursive_factorial(int nb); Returns the factorial of nb, recursively. | 47 | | Ex14 | int ft_sqrt(int nb); Returns square root of nb if it exist, else returns 0. | 48 | | Ex15 | void ft_putstr(char \*str); Display a string. | 49 | | Ex16 | int ft_strlen(char \*str); Reproduces strlen behavior. | 50 | | Ex17 | int ft_strcmp(char \*s1, char \*s2); Reproduces strcmp behavior. | 51 | | Ex18 | Write a program that displays arguments passed in command line. | 52 | | Ex19 | Write a program that display arguments passed in command line, ordered by ASCII. | 53 | | Ex20 | char \*ft_strdup(char \*src); Reproduces strdup behavior. | 54 | | Ex21 | int \*ft_range(int min, int max); Returns an int array containing all integers between min and max. | 55 | | Ex22 | Write a macro ABS that replaces it's parameter by an absolute value. | 56 | | Ex23 | Write an ft_point.h that will compile the ft_point.c | 57 | | Ex24 | Makefile. Fetches src in /src, headers in /includes, require clean/fclean/re/all rules. | 58 | | Ex25 | void ft_foreach(int \*tab, int length, void(\*f)(int)); Apply a function on all elements of an array. | 59 | | Ex26 | int ft_count_if(char \*\*tab, int(\*f)(char\*)); Returns number of elements in the array that returns 1. | 60 | | Ex27 | Write a program called ft_display_file that displays content of file passed in argument on STDOUT. Makefile with all/clean/fclean. Malloc FORBIDDEN. Errors display on STDERR. | 61 | 62 | ### Guide through problems, its all explained ;)! So don't panic . 63 | 64 | 65 | 66 | ## Exercise 00 : Oh yeah, mooore... 67 | 68 | 1. :books: Task : 69 | 70 | ``` 71 | $> ls -l 72 | total 42 73 | drwx--xr-x 2 login wheel XX Jun 1 20:47 test0 74 | -rwx--xr-- 1 login wheel 4 Jun 1 21:46 test1 75 | dr-x---r-- 2 login wheel XX Jun 1 22:45 test2 76 | -r-----r-- 2 login wheel 1 Jun 1 23:44 test3 77 | -rw-r----x 1 login wheel 2 Jun 1 23:43 test4 78 | -r-----r-- 2 login wheel 1 Jun 1 23:44 test5 79 | lrwxr-xr-x 1 login wheel 5 Jun 1 22:20 test6 -> test0 80 | $> 81 | ``` 82 | 83 | 2. :books: Check `man touch` : 84 | 85 | ``` 86 | -t Change the access and modification times to the specified time instead of the current time of day. The argument is of the form 87 | ``[[CC]YY]MMDDhhmm[.SS]'' where each pair of letters represents the following: 88 | CC The first two digits of the year (the century). 89 | YY The second two digits of the year. If ``YY'' is specified, but ``CC'' is not, a value for ``YY'' between 69 and 99 results in a ``CC'' 90 | value of 19. Otherwise, a ``CC'' value of 20 is used. 91 | MM The month of the year, from 01 to 12. 92 | DD the day of the month, from 01 to 31. 93 | hh The hour of the day, from 00 to 23. 94 | mm The minute of the hour, from 00 to 59. 95 | SS The second of the minute, from 00 to 61. 96 | 97 | If the ``CC'' and ``YY'' letter pairs are not specified, the values default to the current year. If the ``SS'' letter pair is not specified, the 98 | value defaults to 0. 99 | ``` 100 | > [More details in this folder](https://github.com/Alaamimi/Piscine-Reloaded/blob/master/ex00/Readme.md) 101 | 102 | --- 103 | 3. :floppy_disk: Script : 104 | 105 | ```touch -h -t "06012220" test6``` 106 | 107 | ## Exercise 01 : Z 108 | 109 | --- 110 | 1. :beetle: Test && Debug : 111 | 112 | `z 113 | ` 114 | 115 | 2. :floppy_disk: Script : 116 | 117 | > echo "Z" > z 118 | 119 | ## Exercise 02 : clean 120 | 121 | > In a file called clean place the command line that will search for all files - in the current directory as well as in its sub-directories - with a name ending by ~, or with a name that start and end by # 122 | > * The command line will show and erase all files found. 123 | > * Only one command is allowed: no ’;’ or ’&&’ or other shenanigans. 124 | 125 | 1. :books: Useful command - `touch none{0..10}~` to create 10 files. 126 | 127 | 2. :books: `-name` 128 | 129 | > -name pattern 130 | > True if the last component of the pathname being examined matches pattern. 131 | > Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') 132 | > may be used as part of pattern. These characters may be matched explicitly 133 | > by escaping them with a backslash (``\''). 134 | 135 | 3. We will use next flags: 136 | > `-print` - verbose output 137 | > `-delete` - all 138 | > `-name` - input your find's name 139 | > `-exec ls -l {} \;` - more 140 | > `-o` - connection 141 | 142 | --- 143 | 4. :floppy_disk: Script : 144 | 145 | ```find . -name "#*" -print -delete -o -name "*#" -delete -print -o -name "*~" -delete -print``` 146 | 147 | ```find . -name -type f \(-name "*#" -o -name "#*#") -print -delete``` 148 | 149 | ## Exercise 03 : find_sh 150 | 151 | --- 152 | 1. :floppy_disk: Script : 153 | 154 | ``` find . "*.sh" -print | awk -F "." '/.sh/{ print $2 }' | awk -F "/" '{ print $2 }' ``` 155 | 156 | > :books: `man awk` : 157 | 158 | ## Exercise 04 : MAC 159 | 1. `man ifconfig` 160 | 2. `man awk` 161 | 162 | --- 163 | 4. :floppy_disk: Script : 164 | 165 | ```ifconfig en0 | grep ether -w | awk -F " " '{ print $2 }'``` 166 | 167 | ## Exercise 05 : Can you create it ? 168 | 169 | --- 170 | 1. :floppy_disk: Script : 171 | 172 | ```touch '"\?$*’KwaMe’*$?\"'``` 173 | 174 | > p.s. Simple - `''` or `echo 42 > '"\?$*’KwaMe’*$?\"'` 175 | 176 | ## Exercise 06 : ft_print_alphabet 177 | 1. :dart: Task : 178 | 179 | > Create a function that displays the alphabet in lowercase, on a single line, by ascending order, starting from the letter ’a’. 180 | 181 | 2. :dart: Task : 182 | 183 | > This function is easier to enter, that would constantly use. 184 | > void - is NONE type, func - return NONE, but put `char` character 185 | 186 | ```c 187 | void ft_putchar(char c) 188 | { 189 | write(1, &c, 1); 190 | } 191 | ``` 192 | 193 | 2.1 `man ASCII` : 194 | 195 | ``` 196 | 97 a 98 b 99 c 100 d 101 e 102 f 103 g 197 | 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 198 | 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 199 | 120 x 121 y 122 z 200 | ``` 201 | 202 | > `(i >= 97 && i <= 122)` while i fits the condition 203 | 204 | --- 205 | 3. :dart: Function : 206 | 207 | ```c 208 | void ft_print_alphabet(void) 209 | { 210 | char ltr; 211 | 212 | ltr = 'a'; // ltr = 97; 213 | while (ltr <= 'z') // While (ltr <= 122) 214 | { 215 | ft_putchar(ltr); 216 | ltr++; 217 | } 218 | } 219 | ``` 220 | 221 | 4. :beetle: :wrench: Test && Debug : 222 | 223 | ```c 224 | int main(void) 225 | { 226 | ft_print_alphabet(); 227 | return (0); 228 | } 229 | ``` 230 | 231 | 5. :8ball: Expected output : 232 | 233 | ```c 234 | > $> abcd...z 235 | ``` 236 | 237 | ## Exercise 07 : ft_print_numbers 238 | 239 | 1. :dart: Task : 240 | 241 | > Create a function that displays all digits, on a single line, by ascending order. 242 | 243 | 2. `man ascii`: 244 | 245 | ``` 246 | 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 247 | 56 8 57 9 248 | ``` 249 | 250 | --- 251 | 3. :dart: Function: 252 | 253 | ``` c 254 | void ft_print_numbers(void) 255 | { 256 | char nbr; 257 | 258 | nbr = '0'; 259 | while (nbr >= '0' && nbr <= '9') 260 | { 261 | ft_putchar(nbr); 262 | nbr++; 263 | } 264 | ``` 265 | 266 | ## Exercise 08: ft_is_negative 267 | 268 | 1. :dart: Task : 269 | 270 | > Create a function that displays ’N’ or ’P’ depending on the integer’s sign entered as a parameter. If n is negative, display ’N’. If n is positive or null, display ’P’. 271 | 272 | --- 273 | 2. :dart: Function: 274 | 275 | ```c 276 | void ft_is_negative(int n) 277 | { 278 | if (n < 0) 279 | ft_putchar('N'); 280 | else 281 | ft_putchar('P'); 282 | } 283 | ``` 284 | 285 | ## Exercise 09 : ft_ft 286 | 287 | 1. :dart: Task : 288 | 289 | > Create a function that takes a pointer to int as a parameter, and sets the value "42" to that int. 290 | 291 | 2. About pointers: 292 | 293 | > [Check this video about Pointers](https://www.youtube.com/watch?v=XISnO2YhnsY&ab_channel=CS50) 294 | 295 | 2. :beetle: Test && debug: 296 | 297 | > Use ft_putnbr(); function to display numbers passed as parameter. 298 | 299 | > First we print the value of i, then print the value passed to the pointer. 300 | 301 | ```c 302 | int main(void) 303 | { 304 | int i; 305 | int *nbr; 306 | 307 | i = 0; 308 | nbr = &i; 309 | ft_putnbr(i); 310 | ft_putchar('\n'); 311 | ft_ft(nbr); 312 | ft_putnbr(i); 313 | ft_putchar('\n'); 314 | return(0); 315 | } 316 | ``` 317 | 318 | > :beetle: Common mistake : 319 | 320 | ```bash 321 | ft_ft.c:14:9: warning: incompatible integer to pointer conversion passing 'int' to 322 | parameter of type 'int *'; take the address with & [-Wint-conversion] 323 | ft_ft(i); 324 | ^ 325 | & 326 | ``` 327 | > This message was displayed, because we did not insert a pointer to the function 328 | 329 | --- 330 | 3. :dart: Function : 331 | 332 | ```c 333 | void ft_ft(int *nbr) 334 | { 335 | *nbr = 42; 336 | } 337 | ``` 338 | 339 | ## Exercise 10 : ft_swap 340 | 341 | 1. :dart: Task : 342 | 343 | > Create a function that swaps the value of two integers whose addresses are entered as parameters. 344 | 345 | > *Here’s how it should be prototyped:* 346 | > ``` void ft_swap(int *a, int *b);``` 347 | 348 | 349 | 2. :beetle: Test && debug : 350 | 351 | ```c 352 | int main() 353 | { 354 | int nb1; 355 | int nb2; 356 | int *a; 357 | int *b; 358 | 359 | nb1 = 0; 360 | nb2 = 1; 361 | a = &nb1; 362 | b = &nb2; 363 | ft_putnbr(nb1); 364 | ft_putchar('\n'); 365 | ft_putnbr(nb2); 366 | ft_putchar('\n'); 367 | ft_swap(a, b); 368 | ft_putnbr(nb1); 369 | ft_putchar('\n'); 370 | ft_putnbr(nb2); 371 | ft_putchar('\n'); 372 | return(0); 373 | } 374 | ``` 375 | 376 | --- 377 | 3. :dart: Function : 378 | 379 | ```c 380 | void ft_swap(int *a, int *b) 381 | { 382 | int tmp; 383 | tmp = *a; 384 | *a = *b; 385 | *b = tmp; 386 | } 387 | ``` 388 | 389 | ## Exercise 11 : ft_div_mod 390 | 391 | 1. :dart: Task : 392 | 393 | > Create a function ft_div_mod prototyped like this : 394 | > `void ft_div_mod(int a, int b, int *div, int *mod);` 395 | > 396 | 397 | > This function divides parameters a by b and stores the result in the int pointed by div. It also stores the remainder of the division of a by b in the int pointed by mod. 398 | 399 | 2. :books: About div & mod [standard operation in C](https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B) 400 | 401 | | Operator name | Syntax | C++ prototype examples | 402 | | ------------- |:-------------:| ----------------------:| 403 | | Multiplication| a * b | `K::operator *(S b);` | 404 | | Division | a / b | `K::operator /(S b);` | 405 | | Modulo | a % b | `R K::operator %(S b);`| 406 | 407 | 2. :beetle: Test && debug : 408 | ```c 409 | int main(void) 410 | { 411 | int a; 412 | int b; 413 | int div; 414 | int mod; 415 | int *pdiv; 416 | int *pmod; 417 | 418 | a = 6; 419 | b = 2; 420 | div = 0; 421 | mod = 0; 422 | pdiv = ÷ 423 | pmod = &mod; 424 | ft_putnbr(a); 425 | ft_putchar('\n'); 426 | ft_putnbr(b); 427 | ft_putchar('\n'); 428 | ft_div_mod(a, b, pdiv, pmod); 429 | ft_putnbr(div); 430 | ft_putchar('\n'); 431 | ft_putnbr(mod); 432 | ft_putchar('\n'); 433 | return(0); 434 | } 435 | ``` 436 | 437 | --- 438 | 3. :dart: Function : 439 | 440 | ```c 441 | void ft_div_mod(int a, int b, int *div, int *mod) 442 | { 443 | *div = a / b; 444 | *mod = a % b; 445 | } 446 | ``` 447 | 448 | ## Exercise 12 : ft_iterative_factorial 449 | 450 | 1. :dart: Task : 451 | 452 | > Create an iterated function that returns a number. This number is the result of a factorial operation based on the number given as a parameter. 453 | 454 | > If there’s an error, the function should return 0. 455 | 456 | > Here’s how it should be prototyped : 457 | > ```c int ft_iterative_factorial(int nb);``` 458 | 459 | > Your function must return its result in less than two seconds. 460 | 461 | 2. :books: About factorial : 462 | 463 | ```math 464 | 7! = 1 * 2 * 3 * 4 * 5 * 6 * 7 465 | 0! = 1 466 | ``` 467 | 468 | --- 469 | 3. :dart: Function : 470 | 471 | ```c 472 | int ft_iterative_factorial(int nb) 473 | { 474 | int product; 475 | 476 | if (nb > 12) 477 | return (0); 478 | if (nb < 0) 479 | return (0); 480 | product = 1; 481 | while(nb >= 1) 482 | { 483 | product = product * nb; 484 | nb--; 485 | } 486 | return (product); 487 | } 488 | ``` 489 | --- 490 | 4. :beetle: Test && debug : 491 | 492 | ```c 493 | int main(void) 494 | { 495 | int i; 496 | i = 5; 497 | 498 | ft_putnbr(i); 499 | ft_putchar('\n'); 500 | ft_putnbr(ft_iterative_factorial(i)); 501 | ft_putchar('\n'); 502 | return (0); 503 | 504 | } 505 | 506 | ``` 507 | ## Exercise 13 : ft_recursive_factorial 508 | 509 | 1. :dart: Task : 510 | 511 | > Create a function that returns the factorial of the number passed as parameter. 512 | > Here's how it should be prototyped : 513 | 514 | ```c 515 | int ft_recursive_factorial(int nb); 516 | ``` 517 | > [CS50 course explaining this topic in details](https://www.youtube.com/watch?v=mz6tAJMVmfM&ab_channel=CS50) 518 | --- 519 | 3. :dart: Function : 520 | 521 | ```c 522 | int ft_recursive_factorial(int nb) 523 | { 524 | if (nb == 0) 525 | return(1); 526 | else if (nb < 0 || nb > 12) 527 | return(0); 528 | else 529 | return (nb * ft_recursive_factorial(nb - 1)); 530 | } 531 | ``` 532 | 533 | > p.s. Check this: `else if (nb < 0 || nb > 12)`, fact function only take positive numbers 534 | >, also it overflows beyond the size of int once it reaches 12. 535 | 536 | ## Exercise 14 : ft_sqrt 537 | 538 | 1. :dart: Task: 539 | 540 | > Create a function that returns the square root of a number (if it exists), or 0 if the square root is an irrational number. 541 | > Here’s how it should be prototyped : 542 | 543 | ```c 544 | int ft_sqrt(int nb); 545 | ``` 546 | 547 | > Your function must return its result in less than two seconds. 548 | 549 | --- 550 | 551 | 3. :dart: Function: 552 | 553 | ```c 554 | 555 | int ft_sqrt(int nb) 556 | { 557 | int sqrt; 558 | 559 | sqrt = 0; 560 | while(sqrt <= nb / 2) 561 | { 562 | if ((sqrt * sqrt) == nb) 563 | return (nb); 564 | else 565 | sqrt++; 566 | } 567 | return (0); 568 | } 569 | ``` 570 | 571 | ## Exercise 15 : ft_putstr 572 | 573 | 1. :books: Classic problem : 574 | 575 | ```c 576 | /*This function displays the string s to the standard output. We do this using 577 | our ft_putchar function. We start by declaring a i variable that we 578 | will use to move through our string. We use a i because it is 579 | guaranteed to be big enough to contain the size of the biggest object your 580 | system can handle. This way we can display the absolute biggest string that 581 | our computer can handle. We set i equal to 0 to start at the beginning of 582 | the parameter string s. We then do a simple loop stating that so long as we 583 | have not reached the end of our string we want the loop to continue. We move 584 | to each index position of our string and place the character in that 585 | position as a parameter for our ft_putchar function. The ft_putchar function 586 | will put that character in the standard output and then we increment the i 587 | variable to continue moving through the string. We do this until we reach 588 | the terminating '\0' of the string.*/ 589 | 590 | void ft_putstr(char *str) 591 | { 592 | int i; 593 | 594 | i = 0; 595 | while (str[i] != '\0') 596 | { 597 | ft_putchar(str[i]); 598 | i++; 599 | } 600 | } 601 | ``` 602 | 603 | 2. :dart: Function : 604 | ```c 605 | int main(void) 606 | { 607 | char str[] = "vlad is cool"; 608 | ft_putstr(str); 609 | ft_putchar('\n'); 610 | write(1, "-----\n", 6); 611 | char str1[] = ""; 612 | ft_putstr(str1); 613 | ft_putchar('\n'); 614 | write(1, "-----\n", 6); 615 | char str2[] = "Pipe this to another | and export the $"; 616 | ft_putstr(str2); 617 | ft_putchar('\n'); 618 | write(1, "-----\n", 6); 619 | return (EXIT_SUCCESS); 620 | } 621 | 622 | ``` 623 | ## Exercise 16 : ft_strlen 624 | 625 | 1. Task : 626 | 627 | Reproduce the behavior of the function strlen. 628 | 629 | 2. Function : 630 | 631 | ``` c 632 | int ft_strlen(char *str) 633 | { 634 | int len; 635 | 636 | len = 0; 637 | while (str[len] != '\0') 638 | { 639 | len++; 640 | } 641 | return(len); 642 | } 643 | ``` 644 | 645 | 3. Test: 646 | 647 | ```c 648 | int main() 649 | { 650 | int i; 651 | 652 | i = 0; 653 | char str[] = "Hello world"; 654 | ft_putstr(str); 655 | ft_putchar('\n'); 656 | i = ft_strlen(i); 657 | ft_putnbr(i); 658 | ft_putchar('\n'); 659 | return(0); 660 | } 661 | ``` 662 | 663 | ## Exercise 17 : ft_strcmp 664 | 665 | 1. strcmp string.h function : 666 | 667 | > man strcmp 668 | 669 | ``` bash 670 | DESCRIPTION 671 | The strcmp() and strncmp() functions lexicographically compare the null- 672 | terminated strings s1 and s2. 673 | 674 | The strncmp() function compares not more than n characters. Because 675 | strncmp() is designed for comparing strings rather than binary data, 676 | characters that appear after a `\0' character are not compared. 677 | 678 | RETURN VALUES 679 | The strcmp() and strncmp() functions return an integer greater than, 680 | equal to, or less than 0, according as the string s1 is greater than, 681 | equal to, or less than the string s2. The comparison is done using 682 | unsigned characters, so that `\200' is greater than `\0'. 683 | ``` 684 | 685 | 2. Source of strcmp : 686 | 687 | ```c 688 | /*Compare S1 and S2, returning less than, equal to or 689 | greater than zero if S1 is lexicographically less than, 690 | equal to or greater than S2.*/ 691 | int strcmp (const char *p1, const char *p2) 692 | { 693 | const unsigned char *s1 = (const unsigned char *) p1; 694 | const unsigned char *s2 = (const unsigned char *) p2; 695 | unsigned char c1, c2; 696 | do 697 | { 698 | c1 = (unsigned char) *s1++; 699 | c2 = (unsigned char) *s2++; 700 | if (c1 == '\0') 701 | return c1 - c2; 702 | } 703 | while (c1 == c2); 704 | return c1 - c2; 705 | } 706 | ``` 707 | 708 | > Note this: return only value c1 - c2; 709 | 710 | 3. Function : 711 | 712 | ```c 713 | /*According to the man this function lexicographically compares the NULL 714 | terminated strings s1 and s2 that are passed into it's parameters. The 715 | function returns an integer greater than, equal to, or less than 0 depending 716 | on whether the string s1 is greater than, equal to, or less than string s2. 717 | The comparison is done using unsigned characters, so that '\200' is greater 718 | than '\0'.*/ 719 | 720 | int ft_strcmp(char *s1, char *s2) 721 | { 722 | /*i will start by creating an index variable i (counter), and making it an unsigned 723 | int in case the input is a long string. hen i set it equal to 0 to 724 | place the counter at the start of the string inside the while loop.*/ 725 | 726 | unsigned int i; 727 | 728 | i = 1; 729 | 730 | /*The while loop requires two conditions to be true for it to begin looping 731 | and to continue looping ofc. i the loop to happen as long as i have 732 | not reached the end of the string s1 && i want the loop to happen only 733 | so long as the character at position i in s1 is the same as the character 734 | in postion i in s2. The second we find a difference in the string i want 735 | to compare the difference. */ 736 | while (s1[i] != '\0' && s1[i] == s2[i]) 737 | { 738 | i++; 739 | 740 | 741 | /*When it has either found a non matching character or it has reached the 742 | end of s1 the function should return the difference between the character in position i in 743 | s1 and the character in position i in s2. 744 | */ 745 | } 746 | return (s1[i] - s2[i]); 747 | } 748 | 749 | ``` 750 | 751 | > Test : 752 | 753 | > I debug with all possible cases : (s1 == s2) || (s1 > s2) || (s1 < s2). 754 | 755 | > PS : i separated the output with couple of "-" characters and a newline (For output beauty.). 756 | 757 | ```c 758 | void ft_putchar(char c); 759 | void ft_pustr(char *str); 760 | void ft_putnbr(int nb); 761 | int ft_ft_strcmp(char *s1, char *s2); 762 | 763 | int main(void) 764 | { 765 | int i; 766 | 767 | i = '1'; 768 | char str1[] = "Vlad"; 769 | char str2[] = "Vlad"; 770 | write(1, "-----\n", 6); 771 | ft_putstr(str1); 772 | ft_putchar('\n'); 773 | ft_putstr(str2); 774 | ft_putchar('\n'); 775 | i = ft_strcmp(str1, str2); 776 | ft_putnbr(ft_strcmp(str1, str2)); 777 | ft_putchar('\n'); 778 | write(1, "-----\n", 6); 779 | 780 | char str3[] = "a"; 781 | char str4[] = "ab"; 782 | ft_putstr(str3); 783 | ft_putchar('\n'); 784 | ft_putstr(str4); 785 | ft_putchar('\n'); 786 | i = ft_strcmp(str3, str4); 787 | ft_putnbr(ft_strcmp(str3, str4)); 788 | ft_putchar('\n'); 789 | write(1, "-----\n", 6); 790 | 791 | char str5[] = "ab"; 792 | char str6[] = "a"; 793 | ft_putstr(str5); 794 | ft_putchar('\n'); 795 | ft_putstr(str6); 796 | ft_putchar('\n'); 797 | i = ft_strcmp(str5, str6); 798 | ft_putnbr(ft_strcmp(str5, str6)); 799 | ft_putchar('\n'); 800 | write(1, "-----\n", 6); 801 | 802 | return (EXIT_SUCCESS); 803 | } 804 | ``` 805 | 806 | > 807 | 808 | ## Exercise 18 : ft_print_params 809 | 810 | 1. Task : 811 | 812 | ```bash 813 | $>gcc ft_print_params.c -o ft_print_params && ./ft_print_params test1 test2 test3 814 | test1 test2 815 | test3 $> 816 | ``` 817 | 818 | 2. Function : 819 | 820 | ```c 821 | #include 822 | 823 | void ft_putchar(char c) 824 | { 825 | write(1, &c, 1); 826 | } 827 | 828 | void ft_putstr(char *str) 829 | { 830 | int i; 831 | 832 | i = 0; 833 | while(str[i] != '\0') 834 | { 835 | ft_putchar(str[i]); 836 | i++; 837 | } 838 | } 839 | 840 | void ft_putnbr(int nb) 841 | { 842 | if (nb < 0) 843 | { 844 | nb = -nb; 845 | ft_putchar('-'); 846 | } 847 | if (nb >= 10) 848 | { 849 | ft_putnbr(nb / 10); 850 | ft_putnbr(nb % 10); 851 | } 852 | if (nb >= 0 && nb < 10) 853 | ft_putchar(nb + '0'); 854 | } 855 | 856 | int main(int argc, char **argv) 857 | { 858 | int i; 859 | 860 | i = 1; 861 | (void)argc; 862 | (void)argv; 863 | while (i < argc) 864 | { 865 | ft_putstr(argv[i]); 866 | ft_putchar('\n'); 867 | i++; 868 | } 869 | write(1, "Arguments count = ", 20); 870 | ft_putnbr(argc); 871 | ft_putchar('\n'); 872 | return (0); 873 | } 874 | 875 | ``` 876 | 877 | ## Exercise 19 : ft_sort_params 878 | 879 | 1. Task : 880 | 881 | ``` 882 | > • We’re dealing with a program here, you should therefore have a function main in your .c file. 883 | > • Create a program that displays its given arguments sorted by ascii order. 884 | > • It should display all arguments, except for argv[0]. 885 | > • All arguments should have their own line. 886 | ``` 887 | 2. Function : 888 | ```c 889 | #include 890 | 891 | void ft_putchar(char c) 892 | { 893 | write(1, &c, 1); 894 | } 895 | 896 | void ft_putstr(char *str) 897 | { 898 | int i; 899 | 900 | i = 0; 901 | while (str[i] != '\0') 902 | { 903 | ft_putchar(str[i]); 904 | i++; 905 | } 906 | } 907 | 908 | int ft_strcmp(char *s1, char *s2) 909 | { 910 | int i; 911 | 912 | i = 0; 913 | while ((s1[i] == s2[i]) && (s1[i] != '\0' && s2[i] != '\0')) 914 | i++; 915 | return (s1[i] - s2[i]); 916 | } 917 | 918 | int main(int argc, char **argv) 919 | { 920 | int i; 921 | int j; 922 | char *tmp; 923 | 924 | i = 0; 925 | while (++i < argc) 926 | { 927 | j = i; 928 | while (++j < argc) 929 | { 930 | if (ft_strcmp(argv[i], argv[j]) > 0) 931 | { 932 | tmp = argv[i]; 933 | argv[i] = argv[j]; 934 | argv[j] = tmp; 935 | } 936 | } 937 | } 938 | i = 0; 939 | while (++i < argc) 940 | { 941 | ft_putstr(argv[i]); 942 | ft_putchar('\n'); 943 | } 944 | return (0); 945 | } 946 | 947 | ``` 948 | 949 | ## Exercise 20 : ft_strdup 950 | 951 | 1. task : 952 | ``` 953 | Reproduce the behavior of the function strdup (man strdup). 954 | 955 | ``` 956 | ```bash 957 | 958 | > MAN STRDUP: 959 | 960 | DESCRIPTION 961 | The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3). 962 | 963 | The strndup() function is similar, but only copies at most n bytes. If s is longer than n, only n bytes are copied, and a terminating null byte ('\0') is added. 964 | 965 | strdupa() and strndupa() are similar, but use alloca(3) to allocate the buffer. They are only available when using the GNU GCC suite, and suffer from the same limitations described in alloca(3). 966 | 967 | RETURN VALUE 968 | The strdup() function returns a pointer to the duplicated string, or NULL if insufficient memory was available. 969 | ``` 970 | 2. function : 971 | ```c 972 | char *ft_strdup(char *src) 973 | { 974 | int i; 975 | char *dest; 976 | 977 | i = 0; 978 | dest = malloc(sizeof(*src) * ((ft_strlen((char *)src)) + 1)); 979 | if (dest == NULL) 980 | return (NULL); 981 | while (src[i] != '\0') 982 | { 983 | dest[i] = src[i]; 984 | i++; 985 | } 986 | dest[i] = '\0'; 987 | return (dest); 988 | } 989 | ``` 990 | 3. Test : 991 | ```c 992 | void ft_putchar(char c); 993 | void ft_pustr(char *str); 994 | int ft_strlen(char *str); 995 | int *ft_strdup(char *src); 996 | 997 | int main(void) 998 | { 999 | char str[] = "Duplicate me motherfucker"; 1000 | 1001 | ft_putstr(str); 1002 | ft_putchar('\n'); 1003 | printf("%p\n", str); 1004 | write(1, "-----\n", 6); 1005 | ft_strdup(str); 1006 | ft_putstr(str); 1007 | ft_putchar('\n'); 1008 | printf("%p\n", str); 1009 | write(1, "-----\n", 6); 1010 | return (EXIT_SUCCESS); 1011 | } 1012 | ``` 1013 | ## Exercise 21 : ft_range 1014 | 1015 | 1. Task : 1016 | 1017 | • Create a function ft_range which returns an array ofints. This int array should 1018 | contain all values between min and max. 1019 | 1020 | • Min included - max excluded. 1021 | 1022 | • Here’s how it should be prototyped : 1023 | int *ft_range(int min, int max); 1024 | 1025 | • If min´value is greater or equal to max’s value, a null pointer should be returned. 1026 | 1027 | 2. Function : 1028 | 1029 | ```c 1030 | int *ft_range(int min, int max) 1031 | { 1032 | int *str; 1033 | int i; 1034 | 1035 | 1036 | if (min >= max) 1037 | return (0); 1038 | i = 0; 1039 | max -= min; 1040 | str = (int *)malloc(sizeof(int) * max); 1041 | while (i < max) 1042 | { 1043 | str[i] = min + i; 1044 | i++; 1045 | } 1046 | return (str); 1047 | } 1048 | ``` 1049 | 3. Test : 1050 | 1051 | ## Exercise 22 : ft_abs.h 1052 | 1053 | 1. Task : 1054 | 1055 | • Create a macro ABS which replaces its argument by it absolute value : 1056 | #define ABS(Value) 1057 | 1058 | 2. Function : 1059 | ```c 1060 | #ifndef FT_ABS_H 1061 | # define FT_ABS_H 1062 | 1063 | # define ABS(Value) ((Value < 0) ? (-Value) : (Value)) 1064 | 1065 | #endif 1066 | ``` 1067 | 1068 | 3. Test : 1069 | 1070 | ## Exercise 23 : ft_point.h 1071 | 1072 | 1. Task : 1073 | 1074 | • Create a file ft_point.h that’ll compile the following main : 1075 | 1076 | #include "ft_point.h" 1077 | void set_point(t_point *point) 1078 | { 1079 | point->x = 42; 1080 | point->y = 21; 1081 | } 1082 | int main(void) 1083 | { 1084 | t_point point; 1085 | set_point(&point); 1086 | return (0); 1087 | } 1088 | 1089 | 2. Function : 1090 | 1091 | ```c 1092 | #ifndef FT_POINT_H 1093 | # define FT_POINT_H 1094 | 1095 | typedef struct s_point 1096 | { 1097 | int x; 1098 | int y; 1099 | } t_point; 1100 | 1101 | void set_point(t_point *point); 1102 | 1103 | #endif 1104 | ``` 1105 | 1106 | ## Exercise 24 : Makefile 1107 | 1108 | 1. Task : 1109 | 1110 | • Create the Makefile that’ll compile your libft.a. 1111 | 1112 | • The Makefile will get its source files from the "srcs" directory. 1113 | 1114 | • The Makefile will get its header files from the "includes" directory. 1115 | 1116 | • The lib will be at the root of the exercise. 1117 | 1118 | • The Makefile should also implement the following rules: clean, fclean and re as 1119 | well as all. 1120 | 1121 | • fclean does the equivalent of a make clean and also erases the binary created 1122 | during the make. re does the equivalent of a make fclean followed by a make. 1123 | 1124 | • We’ll only fetch your Makefile and test it with our files. For this exercise, only 1125 | the following 5 mandatory functions of your lib have to be handled : (ft_putchar, 1126 | ft_putstr, ft_strcmp, ft_strlen and ft_swap). 1127 | 1128 | 2. Function : 1129 | ``` 1130 | NAME = libft.a 1131 | FLAG = -Wall -Wextra -Werror 1132 | FILES = srcs/*.c 1133 | OBJ = *.o 1134 | 1135 | ALL: $(NAME) 1136 | 1137 | $(NAME): 1138 | gcc $(FLAG) -c $(FILES) 1139 | ar rc $(NAME) $(OBJ) 1140 | 1141 | clean: 1142 | /bin/rm -f $(OBJ) 1143 | 1144 | fclean : clean 1145 | /bin/rm -f $(NAME) 1146 | 1147 | re : fclean ALL 1148 | ``` 1149 | 1150 | ## Exercise 25 : ft_foreach 1151 | 1152 | 1. Task : 1153 | 1154 | • Create the function ft_foreach which, for a given ints array, applies a function on 1155 | all elements of the array. This function will be applied following the array’s order. 1156 | 1157 | • Here’s how the function should be prototyped : 1158 | void ft_foreach(int *tab, int length, void(*f)(int)); 1159 | 1160 | • For example, the function ft_foreach could be called as follows in order to display 1161 | all ints of the array : 1162 | ft_foreach(tab, 1337, &ft_putnbr); 1163 | 1164 | 2. Function : 1165 | ```c 1166 | void ft_foreach(int *tab, int length, void (*f)(int)) 1167 | { 1168 | int i; 1169 | 1170 | i = 0; 1171 | while (i < length) 1172 | { 1173 | f(tab[i]); 1174 | i++; 1175 | } 1176 | } 1177 | ``` 1178 | 1179 | ## Exercise 26 : ft_count_if 1180 | 1181 | 1. Task : 1182 | 1183 | • Create a function ft_count_if which will return the number of elements of the 1184 | array that return 1, passed to the function f. 1185 | 1186 | • Here’s how the function should be prototyped : 1187 | int ft_count_if(char **tab, int(*f)(char*)); 1188 | 1189 | • The array will be delimited by 0. 1190 | 1191 | 1192 | 2. Function : 1193 | ```c 1194 | int ft_count_if(char **tab, int (*f)(char*)) 1195 | { 1196 | int i; 1197 | int counter; 1198 | 1199 | counter = 0; 1200 | i = 0; 1201 | while (tab[i] != 0) 1202 | { 1203 | if (f(tab[i]) == 1) 1204 | counter++; 1205 | i++; 1206 | } 1207 | return (counter); 1208 | } 1209 | ``` 1210 | 1211 | ## Exercise 27 : display_file 1212 | 1213 | 1. Task : 1214 | 1215 | • Create a program called ft_display_file that displays, on the standard output, 1216 | only the content of the file given as argument. 1217 | 1218 | • The submission directory should have a Makefile with the following rules : all, 1219 | clean, fclean. The binary will be called ft_display_file. 1220 | 1221 | • The malloc function is forbidden. You can only do this exercise by declaring a 1222 | fixed-sized array. 1223 | 1224 | • All files given as arguments will be valid. 1225 | 1226 | • Error messages have to be displayed on their reserved output. 1227 | 1228 | 2. Function : 1229 | ```c 1230 | #include 1231 | #include 1232 | #include 1233 | #include 1234 | #define BUFF_SIZE 4096 1235 | 1236 | void ft_putchar(char c) 1237 | { 1238 | write(1, &c, 1); 1239 | } 1240 | 1241 | void ft_putstr(char *str) 1242 | { 1243 | while (*str) 1244 | { 1245 | ft_putchar(*str); 1246 | str++; 1247 | } 1248 | } 1249 | 1250 | int main(int argc, char **argv) 1251 | { 1252 | int x; 1253 | int y; 1254 | char buff[BUFF_SIZE + 1]; 1255 | 1256 | if (argc < 2) 1257 | write(2, "File name missing.\n", 19); 1258 | else if (argc == 2) 1259 | { 1260 | x = open(argv[1], O_RDONLY); 1261 | y = read(x, buff, BUFF_SIZE); 1262 | buff[y] = '\0'; 1263 | ft_putstr(buff); 1264 | close(x); 1265 | } 1266 | else 1267 | write(2, "Too many arguments.\n", 20); 1268 | } 1269 | ``` 1270 | --------------------------------------------------------------------------------