├── .gitmodules ├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md ├── main.h └── number.c ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── libmy.a ├── main.c ├── main.h └── quote ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md └── main.h ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_school.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 103-find_loop.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── 0-isupper.c ├── 0-main.c ├── 0-memset.c ├── 0-strcat.c ├── 1-create_dynamic_lib.sh ├── 1-isdigit.c ├── 1-main.c ├── 1-memcpy.c ├── 1-strncat.c ├── 100-atoi.c ├── 100-operations.so ├── 101-make_me_win.sh ├── 2-strchr.c ├── 2-strlen.c ├── 2-strncpy.c ├── 3-islower.c ├── 3-puts.c ├── 3-strcmp.c ├── 3-strspn.c ├── 4-isalpha.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 6-abs.c ├── 9-strcpy.c ├── README.md ├── _putchar.c ├── libdynamic.so ├── main.h └── nrandom.so ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── m.h ├── main.c ├── school.c └── tests │ └── 5-main.py ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 1-binary_recursive.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md ├── listint │ ├── create_list.c │ ├── free_list.c │ └── print_list.c ├── search_algos.h ├── skiplist │ ├── create_skiplist.c │ ├── free_skiplist.c │ └── print_skiplist.c └── tests │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 102-main.c │ ├── 103-main.c │ ├── 104-main.c │ ├── 105-main.c │ └── 106-main.c └── README.md /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "0x16-simple_shell"] 2 | path = 0x16-simple_shell 3 | url = https://github.com/aysuarex/simple_shell 4 | [submodule "0x11-printf"] 5 | path = 0x11-printf 6 | url = https://github.com/aysuarex/printf 7 | [submodule "0x1D-binary_trees"] 8 | path = 0x1D-binary_trees 9 | url = https://github.com/aysuarex/binary_trees 10 | [submodule "0x1B-sorting_algorithms"] 11 | path = 0x1B-sorting_algorithms 12 | url = https://github.com/aysuarex/sorting_algorithms 13 | [submodule "0x19-stacks_queues-lifo_fifo"] 14 | path = 0x19-stacks_queues-lifo_fifo 15 | url = https://github.com/aysuarex/monty 16 | -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - print quote 6 | * 7 | * Description: prints Dors's quote 8 | * 9 | * Return: Always 1 (Success) 10 | */ 11 | 12 | int main(void) 13 | { 14 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: prints "Programming is like building a multilingual puzzle" 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | puts("\"Programming is like building a multilingual puzzle"); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main -Entry point 5 | * 6 | * Description: prints "with proper grammar, but the outcome is a piece 7 | * of art," 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | printf("with proper grammar, but the outcome is a piece of art,\n"); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int a; 10 | long int b; 11 | long long int c; 12 | char d; 13 | float f; 14 | 15 | printf("Size of a char: %lu byte(s)\n", (unsigned long)sizeof(d)); 16 | printf("Size of an int: %lu byte(s)\n", (unsigned long)sizeof(a)); 17 | printf("Size of a long int: %lu byte(s)\n", (unsigned long)sizeof(b)); 18 | printf("Size of a long long int: %lu byte(s)\n", (unsigned long)sizeof(c)); 19 | printf("Size of a float: %lu byte(s)\n", (unsigned long)sizeof(f)); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - assigns a random number to int n everytime 6 | * it executes, and prints it 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | if (n > 0) 16 | printf("%d is positive\n", n); 17 | else if (n == 0) 18 | printf("%d is zero\n", n); 19 | else if (n < 0) 20 | printf("%d is negative\n", n); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - main block 6 | * Description: Print statements based on the last digit of the random number. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | int m; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | m = n % 10; 17 | 18 | if (m > 5) 19 | printf("Last digit of %i is %i and is greater than 5\n", 20 | n, m); 21 | else if (m == 0) 22 | printf("Last digit of %i is %i and is 0\n", n, m); 23 | else 24 | printf("Last digit of %i is %i and is less than 6 and not 0\n", 25 | n, m); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible combinations of two digits. 5 | * Numbers must be separated by commas and a space. 6 | * 01 and 10 are considered as the same combination of the two digits. 7 | * Print only the smallest combination of two digits. 8 | * Numbers should be printed in ascending order, with two digits. 9 | * You can only use `putchar`. 10 | * You can only use `putchar` up to 5 times. 11 | * You are not allowed to use any variable of type `char`. 12 | * Return: 0 13 | */ 14 | int main(void) 15 | { 16 | int i, j, k; 17 | 18 | i = 0; 19 | 20 | while (i < 100) 21 | { 22 | j = i % 10; /* singles digit */ 23 | k = i / 10; /* doubles digit */ 24 | 25 | if (k < j) 26 | { 27 | putchar(k + '0'); 28 | putchar(j + '0'); 29 | 30 | if (i < 89) 31 | { 32 | putchar(44); 33 | putchar(32); 34 | } 35 | } 36 | 37 | i++; 38 | } 39 | putchar('\n'); 40 | 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible different combinations of 3 digits. 5 | * Numbers must be separated by commas and a space. 6 | * The 3 digits must be different. 7 | * 012, 120, 102, 021, 201, 210 are considered the same combination. 8 | * print only the smallest combination of 3 digits. 9 | * Numbers should be printed in ascending order. 10 | * You can only use `putchar` to print to console. 11 | * You can only use `putchar` up to 6 times. 12 | * You are not allowed to use any variables of type `char`. 13 | * Return: 0 14 | */ 15 | int main(void) 16 | { 17 | int i, j, k, l; 18 | 19 | for (i = 0; i < 1000; i++) 20 | { 21 | j = i / 100; /* hundreds */ 22 | k = (i / 10) % 10; /* tens */ 23 | l = i % 10; /* singles */ 24 | 25 | if (j < k && k < l) 26 | { 27 | putchar(j + '0'); 28 | putchar(k + '0'); 29 | putchar(l + '0'); 30 | 31 | if (i < 700) 32 | { 33 | putchar(44); 34 | putchar(32); 35 | } 36 | } 37 | } 38 | putchar('\n'); 39 | 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible combinations of two two-digit numbers. 5 | * Numbers should range from 0 to 99. 6 | * The two numbers should be separated by a space. 7 | * All numbers should be printed with two digits. 1 should be printed as 01. 8 | * Combination of numbers must be separated by a comma followed by a space. 9 | * Combinations of numbers should be printed in ascending order. 10 | * `00 01` and `01 00` are considered as the same combination. 11 | * You can only use `putchar` to print to console. 12 | * You can only use `putchar` up to 8 times. 13 | * You are not allowed to use any variable of type `char`. 14 | * Return: 0 15 | */ 16 | int main(void) 17 | { 18 | int i, j; 19 | int a, b, c, d; 20 | 21 | for (i = 0; i < 100; i++) 22 | { 23 | a = i / 10; /* doubles fnum */ 24 | b = i % 10; /* singles fnum */ 25 | 26 | for (j = 0; j < 100; j++) 27 | { 28 | c = j / 10; /* doubles snum */ 29 | d = j % 10; /* singles snum */ 30 | 31 | if (a < c || (a == c && b < d)) 32 | { 33 | putchar(a + '0'); 34 | putchar(b + '0'); 35 | putchar(32); 36 | putchar(c + '0'); 37 | putchar(d + '0'); 38 | 39 | if (!(a == 9 && b == 8)) 40 | { 41 | putchar(44); 42 | putchar(32); 43 | } 44 | } 45 | } 46 | } 47 | putchar(10); 48 | 49 | return (0); 50 | } 51 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Use `putchar` function to print the alphabet in lowercase. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c = 'a'; 10 | 11 | while (c <= 'z') 12 | { 13 | putchar(c); 14 | c++; 15 | } 16 | putchar('\n'); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Use `putchar` to print lowercase and then uppercase alphabet. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c = 'a'; 10 | 11 | while (c <= 'z') 12 | { 13 | putchar(c); 14 | c++; 15 | } 16 | 17 | c = 'A'; 18 | 19 | while (c <= 'Z') 20 | { 21 | putchar(c); 22 | c++; 23 | } 24 | 25 | putchar('\n'); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Use `putchar` to print all letters but the letter 'q' and 'e'. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c = 'a'; 10 | 11 | while (c <= 'z') 12 | { 13 | if (c != 'q' && c != 'e') 14 | putchar(c); 15 | c++; 16 | } 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all numbers of base 10, starting from 0. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | printf("%i", i); 14 | } 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all numbers of base 10, starting from 0. 5 | * You are not allowed to use any variable of type char. 6 | * You can only use `putchar` to print to console. 7 | * You can only use `putchar` twice. 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < 10; i++) 15 | { 16 | putchar(i + '0'); 17 | } 18 | putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print the alphabet in reverse order in lowercase. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c; 10 | 11 | for (c = 'z'; c >= 'a'; c--) 12 | { 13 | putchar(c); 14 | } 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all numbers of base64 in lowercase. 5 | * You can only use `putchar`, and only 3 times. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | int i; 12 | 13 | for (i = 0; i < 10; i++) 14 | { 15 | putchar(i + '0'); 16 | } 17 | for (c = 'a'; c < 'g'; c++) 18 | { 19 | putchar(c); 20 | } 21 | putchar('\n'); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible combinations fo single-digit numbers. 5 | * Numbers must be separated by commas and a space. 6 | * You can only use `putchar` to print to console 7 | * You can only use `putchar` up to four times. 8 | * You are not allowed to use any variable of type `char`. 9 | * Return: 0 10 | */ 11 | int main(void) 12 | { 13 | int i = 0; 14 | 15 | while (i < 10) 16 | { 17 | putchar(i + '0'); 18 | if (i < 9) 19 | { 20 | putchar(44); 21 | putchar(32); 22 | } 23 | i++; 24 | } 25 | putchar('\n'); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - prints _putchar, followed by a new line 5 | * Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | _putchar('_'); 11 | _putchar('p'); 12 | _putchar('u'); 13 | _putchar('t'); 14 | _putchar('c'); 15 | _putchar('h'); 16 | _putchar('a'); 17 | _putchar('r'); 18 | _putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - prints the alphabe, in lowercase, followed by a new line 5 | */ 6 | void print_alphabet(void) 7 | { 8 | char ch; 9 | 10 | ch = 'a'; 11 | 12 | while (ch <= 'z') 13 | { 14 | _putchar(ch); 15 | ch++; 16 | } 17 | 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - Check Main 5 | * @a: An integer a 6 | * @b: An integer b 7 | * Description: Function that adds two integers and returns the result 8 | * Return: Result to add a and b 9 | */ 10 | int add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the n times table, starting with 0 5 | * @n: number of the times table 6 | */ 7 | void print_times_table(int n) 8 | { 9 | int i, j, k; 10 | 11 | if (n >= 0 && n <= 15) 12 | { 13 | for (i = 0; i <= n; i++) 14 | { 15 | for (j = 0; j <= n; j++) 16 | { 17 | k = j * i; 18 | if (j == 0) 19 | { 20 | _putchar(k + '0'); 21 | } else if (k < 10 && j != 0) 22 | { 23 | _putchar(','); 24 | _putchar(' '); 25 | _putchar(' '); 26 | _putchar(' '); 27 | _putchar(k + '0'); 28 | } else if (k >= 10 && k < 100) 29 | { 30 | _putchar(','); 31 | _putchar(' '); 32 | _putchar(' '); 33 | _putchar((k / 10) + '0'); 34 | _putchar((k % 10) + '0'); 35 | } else if (k >= 100) 36 | { 37 | _putchar(','); 38 | _putchar(' '); 39 | _putchar((k / 100) + '0'); 40 | _putchar(((k / 10) % 10) + '0'); 41 | _putchar((k % 10) + '0'); 42 | } 43 | } 44 | _putchar('\n'); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i, sum = 0; 10 | 11 | for (i = 0; i < 1024; i++) 12 | { 13 | if ((i % 5) == 0 || (i % 3) == 0) 14 | sum += i; 15 | } 16 | printf("%d\n", sum); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | long int fibonacci[50]; 11 | 12 | fibonacci[0] = 1; 13 | fibonacci[1] = 2; 14 | printf("%ld, %ld, ", fibonacci[0], fibonacci[1]); 15 | 16 | for (i = 2; i < 50; i++) 17 | { 18 | fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]; 19 | if (i == 49) 20 | printf("%ld\n", fibonacci[i]); 21 | else 22 | printf("%ld, ", fibonacci[i]); 23 | } 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - print first 50 fibonacci 4 | * 5 | *Return: 0 always. 6 | */ 7 | int main(void) 8 | { 9 | long int i, x = 1, y = 2, sum = 0, tSum = 0; 10 | 11 | for (i = 0; i < 49; i++) 12 | { 13 | if ((y % 2 == 0) && (y <= 4000000)) 14 | { 15 | tSum = tSum + y; 16 | } 17 | sum = x + y; 18 | x = y; 19 | y = sum; 20 | 21 | } 22 | printf("%ld\n", tSum); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - finds and prints the first 98 Fibonacci numbers, 4 | * starting with 1 and 2 5 | * followed by a new line 6 | * Return: ALways 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int i, j, k, j1, j2, k1, k2; 11 | 12 | j = 1; 13 | k = 2; 14 | 15 | printf("%lu", j); 16 | 17 | for (i = 1; i < 91; i++) 18 | { 19 | printf(", %lu", k); 20 | k = k + j; 21 | j = k - j; 22 | } 23 | 24 | j1 = j / 1000000000; 25 | j2 = j % 1000000000; 26 | k1 = k / 1000000000; 27 | k2 = k % 1000000000; 28 | 29 | for (i = 92; i < 99; ++i) 30 | { 31 | printf(", %lu", k1 + (k2 / 1000000000)); 32 | printf("%lu", k2 % 1000000000); 33 | k1 = k1 + j1; 34 | j1 = k1 - j1; 35 | k2 = k2 + j2; 36 | j2 = k2 - j2; 37 | } 38 | 39 | printf("\n"); 40 | 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - Check Main 6 | * @n: A input integer 7 | * Description: function that prints all natural numbers 8 | * from n to 98, followed by a new line. 9 | * Return: Nothing 10 | */ 11 | void print_to_98(int n) 12 | { 13 | if (n <= 98) 14 | { 15 | for (; n <= 98; n++) 16 | { 17 | printf("%d", n); 18 | 19 | if (n == 98) 20 | continue; 21 | printf(", "); 22 | } 23 | printf("\n"); 24 | } 25 | else 26 | { 27 | for (; n >= 98; n--) 28 | { 29 | printf("%d", n); 30 | 31 | if (n == 98) 32 | continue; 33 | printf(", "); 34 | } 35 | printf("\n"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Check Main 5 | * Description: function uses _putchar function to print 6 | * alphabet in lowercase 10 times 7 | * Return: Nothing. 8 | */ 9 | 10 | void print_alphabet_x10(void) 11 | { 12 | char i; 13 | int j; 14 | 15 | for (j = 0; j < 10; j++) 16 | { 17 | for (i = 'a'; i <= 'z'; i++) 18 | _putchar(i); 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Check Main 5 | * @c: An input character 6 | * Description: function uses _putchar function to print 7 | * alphabet in lowercase 10 times 8 | * Return: 1 if is lowercase or 0 if is uppercase 9 | */ 10 | int _islower(int c) 11 | { 12 | char i; 13 | int lower = 0; 14 | 15 | for (i = 'a'; i <= 'z'; i++) 16 | { 17 | if (i == c) 18 | lower = 1; 19 | } 20 | 21 | return (lower); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _isalpha - Check Main 4 | * @c: An input character 5 | * Description: function returns 1 if the character is a 6 | * letter, lowercase or uppercase. 7 | * Return: 1 or 0 in otherwise. 8 | */ 9 | int _isalpha(int c) 10 | { 11 | char lower, upper; 12 | int isletter = 0; 13 | 14 | for (lower = 'a'; lower <= 'z'; lower++) 15 | { 16 | for (upper = 'A'; upper <= 'Z'; upper++) 17 | { 18 | if (c == lower || c == upper) 19 | isletter = 1; 20 | } 21 | } 22 | return (isletter); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_sign - Determines if the input number 4 | * greater, iqual or less than zero. 5 | * 6 | * @n: The input number as an integer. 7 | * 8 | * Return: 1 is greater than zero. 0 is zero. 9 | * -1 is less than zero. 10 | */ 11 | int print_sign(int n) 12 | { 13 | if (n > 0) 14 | { 15 | _putchar(43); 16 | return (1); 17 | } 18 | else if (n < 0) 19 | { 20 | _putchar(45); 21 | return (-1); 22 | } 23 | else 24 | { 25 | _putchar(48); 26 | return (0); 27 | } 28 | _putchar('\n'); 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_abs - Check Main 4 | * @r: An integre input 5 | * Description: This function returns absolute value of a number 6 | * Return: Absolut value of number r 7 | */ 8 | int _abs(int r) 9 | { 10 | if (r >= 0) 11 | return (r); 12 | else 13 | return (r * -1); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_last_digit - Check Main 4 | * @r: An integer input 5 | * Description: This function prints the last digit of a number 6 | * Return: last digit of number r 7 | */ 8 | int print_last_digit(int r) 9 | { 10 | int n; 11 | 12 | if (r < 0) 13 | n = -1 * (r % 10); 14 | else 15 | n = r % 10; 16 | _putchar((n % 10) + '0'); 17 | return (n % 10); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - Check Main 5 | * Description: function prints every minute in one day 6 | * Return: Nothing 7 | */ 8 | void jack_bauer(void) 9 | { 10 | int H, M; 11 | 12 | for (H = 0; H < 24; H++) 13 | { 14 | for (M = 0; M < 60; M++) 15 | { 16 | _putchar((H / 10) + '0'); 17 | _putchar((H % 10) + '0'); 18 | _putchar(':'); 19 | _putchar((M / 10) + '0'); 20 | _putchar((M % 10) + '0'); 21 | _putchar('\n'); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * times_table - a function that prints the 9 times table, starting with 0 4 | * rone = row, cone = column, d = digits of current result 5 | * Return: times table 6 | * add extra space past single digit 7 | */ 8 | void times_table(void) 9 | { 10 | int r, c, d; 11 | 12 | for (r = 0; r <= 9; r++) 13 | { 14 | _putchar('0'); 15 | _putchar(','); 16 | _putchar(' '); 17 | for (c = 1; c <= 9; c++) 18 | { 19 | d = (r * c); 20 | if ((d / 10) > 0) 21 | { 22 | _putchar((d / 10) + '0'); 23 | } 24 | else 25 | { 26 | _putchar(' '); 27 | } 28 | _putchar((d % 10) + '0'); 29 | 30 | if (c < 9) 31 | { 32 | _putchar(','); 33 | _putchar(' '); 34 | } 35 | } 36 | _putchar('\n'); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int r); 8 | int print_last_digit(int r); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int a, int b); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | void putformat(int); 15 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /*while (i < 10)*/ 17 | /*{*/ 18 | /*putchar(i);*/ 19 | /*}*/ 20 | 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a >= b && a >= c) 16 | { 17 | largest = a; 18 | } 19 | else if (b >= a && b >= c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | 28 | return (largest); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if (year % 4 == 0 || ((year % 100 == 0) && (year % 400 == 0))) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | Debugging is the process of finding and resolving bugs (Defects or probelms that prevent correct operation) within computer program,software or system. Debugging tactics can involve interactive debugging, control flow analysis, unit testing, integration testing, log file analysis, monitoring at the application or system level, memory dumps, and profiling. 2 | 3 | # Debugging 4 | 5 | Project done during **Full Stack Software Engineering studies** at **ALX**. It aims to learn how to debug in **C language**. 6 | 7 | ## Technologies 8 | * C files are compiled using `gcc 4.8.4` 9 | * C files are written according to the C90 standard 10 | * Tested on Ubuntu 14.04 LTS 11 | 12 | ## Files 13 | All of the following files are programs written in C: 14 | 15 | | Filename | Description | 16 | | -------- | ----------- | 17 | | `0-main.c` | Test that function `positive_or_negative()` gives the correct output when given a case of `0` | 18 | | `1-main.c` | Fixed program to the infinite loop | 19 | | `2-largest_number.c` | Prints the largest of three integers | 20 | | `3-print_remaining_days.c` | Converts a date to the day of year and determines how many days are left in the year | 21 | | `holberton.h` | Header file | 22 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x03-debugging/number.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x03-debugging/number.c -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 0-isupper.c 3 | * Auth: Suara Ayomide 4 | */ 5 | 6 | #include "main.h" 7 | 8 | /** 9 | * _isupper - Checks for uppercase characters. 10 | * @c: The character to be checked. 11 | * 12 | * Return: 1 if character is uppercase, 0 otherwise. 13 | */ 14 | int _isupper(int c) 15 | { 16 | if (c >= 'A' && c <= 'Z') 17 | return (1); 18 | 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks if parameter is a number between 0 to 9. 5 | * @c: input number. 6 | * Return: 1 if is a number (0 to 9), 0 in other case. 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= 48 && c <= 57) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle. 5 | * @size: numbers of lines. 6 | * Return: no return. 7 | */ 8 | void print_triangle(int size) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < size; i++) 13 | { 14 | for (j = 1; j < (size - i); j++) 15 | _putchar(' '); 16 | for (j--; j < size; j++) 17 | _putchar(35); 18 | if (i < (size - 1)) 19 | _putchar('\n'); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints largest prime factor. 6 | * Return: Always 0. 7 | */ 8 | 9 | int main(void) 10 | { 11 | long int n, fp; 12 | 13 | n = 612852475143; 14 | for (fp = 2; fp <= n; fp++) 15 | { 16 | if (n % fp == 0) 17 | { 18 | n /= fp; 19 | fp--; 20 | } 21 | } 22 | printf("%ld\n", fp); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int m, d, count; 11 | 12 | if (n < 0) 13 | { 14 | _putchar(45); 15 | m = n * -1; 16 | } 17 | else 18 | { 19 | m = n; 20 | } 21 | 22 | d = m; 23 | count = 1; 24 | 25 | while (d > 9) 26 | { 27 | d /= 10; 28 | count *= 10; 29 | } 30 | 31 | for (; count >= 1; count /= 10) 32 | { 33 | _putchar(((m / count) % 10) + 48); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two integers. 5 | * @a: first number. 6 | * @b: second number. 7 | * Return: multiplication. 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints numbers between 0 to 9. 5 | * Return: no return. 6 | */ 7 | void print_numbers(void) 8 | { 9 | int ch; 10 | 11 | for (ch = 48; ch < 58; ch++) 12 | { 13 | _putchar(ch); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 4-print_most_numbers.c 3 | * Auth: Suara Ayomide 4 | */ 5 | 6 | #include "main.h" 7 | 8 | /** 9 | * print_most_numbers - Prints the numbers from 0-9 except for 2 and 4. 10 | */ 11 | void print_most_numbers(void) 12 | { 13 | int num; 14 | 15 | for (num = 0; num <= 9; num++) 16 | { 17 | if (num != 2 && num != 4) 18 | _putchar((num % 10) + '0'); 19 | } 20 | 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints numbers between 0 to 14 5 | * 10 times. 6 | * Return: no return. 7 | */ 8 | void more_numbers(void) 9 | { 10 | int i, ch; 11 | 12 | for (i = 0; i < 10; i++) 13 | { 14 | for (ch = 0; ch < 15; ch++) 15 | { 16 | if (ch >= 10) 17 | _putchar((ch / 10) + 48); 18 | _putchar((ch % 10) + 48); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - prints straight line n times. 5 | * @n: times straight line is printed. 6 | * Return: no return. 7 | */ 8 | void print_line(int n) 9 | { 10 | int i; 11 | 12 | for (i = 0; i < n; i++) 13 | { 14 | _putchar(95); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - prints diagonal line n times. 5 | * @n: times diagonal line is printed. 6 | * Return: no return. 7 | */ 8 | void print_diagonal(int n) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < n; i++) 13 | { 14 | for (j = 0; j < i; j++) 15 | { 16 | _putchar(' '); 17 | } 18 | _putchar(92); 19 | if (i < (n - 1)) 20 | _putchar('\n'); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints hashes squares. 5 | * @size: size of the square. 6 | * Return: no return. 7 | */ 8 | void print_square(int size) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < size; i++) 13 | { 14 | for (j = 0; j < size; j++) 15 | { 16 | _putchar(35); 17 | } 18 | if (i != size - 1) 19 | _putchar('\n'); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints Buzz each numbers of 3 and 5. 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int n; 10 | 11 | n = 1; 12 | printf("%d", n); 13 | for (n = 2; n <= 100; n++) 14 | { 15 | if ((n % 3 == 0) && (n % 5 == 0)) 16 | { 17 | printf(" FizzBuzz"); 18 | } 19 | else if (n % 3 == 0) 20 | { 21 | printf(" Fizz"); 22 | } 23 | else if (n % 5 == 0) 24 | { 25 | printf(" Buzz"); 26 | } 27 | else 28 | { 29 | printf(" %d", n); 30 | } 31 | } 32 | printf("\n"); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _isupper(int c); 5 | int _isdigit(int c); 6 | int mul(int a, int b); 7 | void print_numbers(void); 8 | int _putchar(char c); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - main 5 | * @n: - pinter to int 6 | */ 7 | 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swaps the values of two integers 5 | * @a: integer to swap 6 | * @b: integer to swap 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | int c = *a; 12 | *a = *b; 13 | *b = c; 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer. 5 | * @s: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | 17 | if (*(s + count) == '-') 18 | pn *= -1; 19 | 20 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 21 | { 22 | if (size > 0) 23 | m *= 10; 24 | size++; 25 | } 26 | count++; 27 | } 28 | 29 | for (i = count - size; i < count; i++) 30 | { 31 | oi = oi + ((*(s + i) - 48) * m); 32 | m /= 10; 33 | } 34 | return (oi * pn); 35 | } 36 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - generates keygen. 6 | * Return: 0 Always. 7 | */ 8 | int main(void) 9 | { 10 | int r = 0, c = 0; 11 | time_t t; 12 | 13 | srand((unsigned int) time(&t)); 14 | while (c < 2772) 15 | { 16 | r = rand() % 128; 17 | if ((c + r) > 2772) 18 | break; 19 | c = c + r; 20 | printf("%c", r); 21 | } 22 | printf("%c\n", (2772 - c)); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input string. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, 5 | * @str: pointer to the string to print 6 | * Return: void 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i]) 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string, in reverse, followed by a new line. 5 | * @s: input string. 6 | * Return: no return. 7 | */ 8 | void print_rev(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (count >= 0) 13 | { 14 | if (s[count] == '\0') 15 | break; 16 | count++; 17 | } 18 | 19 | for (count--; count >= 0; count--) 20 | _putchar(s[count]); 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverses a string. 5 | * @s: input string. 6 | * Return: no return. 7 | */ 8 | void rev_string(char *s) 9 | { 10 | int count = 0, i, j; 11 | char *str, temp; 12 | 13 | while (count >= 0) 14 | { 15 | if (s[count] == '\0') 16 | break; 17 | count++; 18 | } 19 | str = s; 20 | 21 | for (i = 0; i < (count - 1); i++) 22 | { 23 | for (j = i + 1; j > 0; j--) 24 | { 25 | temp = *(str + j); 26 | *(str + j) = *(str + (j - 1)); 27 | *(str + (j - 1)) = temp; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints one char out of 2 of a string. 5 | * @str: input string. 6 | * Return: no return. 7 | */ 8 | void puts2(char *str) 9 | { 10 | int count = 0; 11 | 12 | while (count >= 0) 13 | { 14 | if (str[count] == '\0') 15 | { 16 | _putchar('\n'); 17 | break; 18 | } 19 | if (count % 2 == 0) 20 | _putchar(str[count]); 21 | count++; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - print second half of a string 5 | * @str: char array string type 6 | * Description: If odd number of chars, print (length - 1) / 2 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | ; 15 | 16 | i++; 17 | for (i /= 2; str[i] != '\0'; i++) 18 | { 19 | _putchar(str[i]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - prints n elements of an array of integers. 6 | * @a: input array. 7 | * @n: input n elements 8 | * Return: no return. 9 | */ 10 | void print_array(int *a, int n) 11 | { 12 | int i = 0; 13 | 14 | for (; i < n; i++) 15 | { 16 | printf("%d", *(a + i)); 17 | if (i != (n - 1)) 18 | printf(", "); 19 | } 20 | printf("\n"); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcpy - copies the string pointed to by src, 5 | * including the terminating null byte, to the 6 | * buffer pointed to by dest. 7 | * @dest: destination. 8 | * @src: source. 9 | * Return: the pointer to dest. 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int count = 0; 14 | 15 | while (count >= 0) 16 | { 17 | *(dest + count) = *(src + count); 18 | if (*(src + count) == '\0') 19 | break; 20 | count++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | void reset_to_98(int *n); 2 | void swap_int(int *a, int *b); 3 | int _strlen(char *s); 4 | void _puts(char *str); 5 | void print_rev(char *s); 6 | void rev_string(char *s); 7 | void puts2(char *str); 8 | void puts_half(char *str); 9 | int _putchar(char); 10 | void print_array(int *a, int n); 11 | char *_strcpy(char *dest, char *src); 12 | int _atoi(char *s); 13 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - function commute srtings 5 | * @dest: param pointer to a char 6 | * @src: param pointer to a char 7 | * Return: return value of dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | 23 | while (src[j] != '\0') 24 | { 25 | dest[i] = src[j]; 26 | j++; 27 | i++; 28 | } 29 | 30 | dest[i] = '\0'; 31 | return (dest); 32 | } 33 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes used from src. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rot13 - encodes a string using rot13 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *rot13(char *s) 9 | { 10 | int count = 0, i; 11 | char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 12 | char rot13[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; 13 | 14 | while (*(s + count) != '\0') 15 | { 16 | for (i = 0; i < 52; i++) 17 | { 18 | if (*(s + count) == alphabet[i]) 19 | { 20 | *(s + count) = rot13[i]; 21 | break; 22 | } 23 | } 24 | count++; 25 | } 26 | 27 | return (s); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int m, d, count; 11 | 12 | if (n < 0) 13 | { 14 | _putchar(45); 15 | m = n * -1; 16 | } 17 | else 18 | { 19 | m = n; 20 | } 21 | 22 | d = m; 23 | count = 1; 24 | 25 | while (d > 9) 26 | { 27 | d /= 10; 28 | count *= 10; 29 | } 30 | 31 | for (; count >= 1; count /= 10) 32 | { 33 | _putchar(((m / count) % 10) + 48); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | p[5] = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * infinite_add - adds two numbers 5 | * @n1: number one. 6 | * @n2: number two. 7 | * @r: buffer that the function will use to store the result. 8 | * @size_r: buffer size: 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 13 | { 14 | int c1 = 0, c2 = 0, op, bg, dr1, dr2, add = 0; 15 | 16 | while (*(n1 + c1) != '\0') 17 | c1++; 18 | while (*(n2 + c2) != '\0') 19 | c2++; 20 | if (c1 >= c2) 21 | bg = c1; 22 | else 23 | bg = c2; 24 | if (size_r <= bg + 1) 25 | return (0); 26 | r[bg + 1] = '\0'; 27 | c1--, c2--, size_r--; 28 | dr1 = *(n1 + c1) - 48, dr2 = *(n2 + c2) - 48; 29 | while (bg >= 0) 30 | { 31 | op = dr1 + dr2 + add; 32 | if (op >= 10) 33 | add = op / 10; 34 | else 35 | add = 0; 36 | if (op > 0) 37 | *(r + bg) = (op % 10) + 48; 38 | else 39 | *(r + bg) = '0'; 40 | if (c1 > 0) 41 | c1--, dr1 = *(n1 + c1) - 48; 42 | else 43 | dr1 = 0; 44 | if (c2 > 0) 45 | c2--, dr2 = *(n2 + c2) - 48; 46 | else 47 | dr2 = 0; 48 | bg--, size_r--; 49 | } 50 | if (*(r) == '0') 51 | return (r + 1); 52 | else 53 | return (r); 54 | } 55 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - prints a buffer 6 | * @b: buffer. 7 | * @size: size of buffer. 8 | * Return: no return. 9 | */ 10 | void print_buffer(char *b, int size) 11 | { 12 | int j, k, l; 13 | 14 | if (size <= 0) 15 | printf("\n"); 16 | else 17 | { 18 | for (j = 0; j < size; j += 10) 19 | { 20 | printf("%.8x:", j); 21 | for (k = j; k < j + 10; k++) 22 | { 23 | if (k % 2 == 0) 24 | printf(" "); 25 | if (k < size) 26 | printf("%.2x", *(b + k)); 27 | else 28 | printf(" "); 29 | } 30 | printf(" "); 31 | for (l = j; l < j + 10; l++) 32 | { 33 | if (l >= size) 34 | break; 35 | if (*(b + l) < 32 || *(b + l) > 126) 36 | printf("%c", '.'); 37 | else 38 | printf("%c", *(b + l)); 39 | } 40 | printf("\n"); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equals, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reverse_array - reverses the content of an array 4 | * of integers. 5 | * @a: array. 6 | * @n: number of elements of the array. 7 | * Return: no return. 8 | */ 9 | 10 | void reverse_array(int *a, int n) 11 | { 12 | int i, j, temp; 13 | 14 | for (i = 0; i < n - 1; i++) 15 | { 16 | for (j = i + 1; j > 0; j--) 17 | { 18 | temp = *(a + j); 19 | *(a + j) = *(a + (j - 1)); 20 | *(a + (j - 1)) = temp; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * string_toupper - changes all lowercase letters of a string to uppercase 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *string_toupper(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if ((*(s + count) >= 97) && (*(s + count) <= 122)) 15 | *(s + count) = *(s + count) - 32; 16 | count++; 17 | } 18 | 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * cap_string - capitalizes all words of a string 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *cap_string(char *s) 9 | { 10 | int count = 0, i; 11 | int sep_words[] = {32, 9, 10, 44, 59, 46, 33, 63, 34, 40, 41, 123, 125}; 12 | 13 | if (*(s + count) >= 97 && *(s + count) <= 122) 14 | *(s + count) = *(s + count) - 32; 15 | count++; 16 | while (*(s + count) != '\0') 17 | { 18 | for (i = 0; i < 13; i++) 19 | { 20 | if (*(s + count) == sep_words[i]) 21 | { 22 | if ((*(s + (count + 1)) >= 97) && (*(s + (count + 1)) <= 122)) 23 | *(s + (count + 1)) = *(s + (count + 1)) - 32; 24 | break; 25 | } 26 | } 27 | count++; 28 | } 29 | return (s); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encodes a string into 1337 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *leet(char *s) 9 | { 10 | int count = 0, i; 11 | int low_letters[] = {97, 101, 111, 116, 108}; 12 | int upp_letters[] = {65, 69, 79, 84, 76}; 13 | int numbers[] = {52, 51, 48, 55, 49}; 14 | 15 | while (*(s + count) != '\0') 16 | { 17 | for (i = 0; i < 5; i++) 18 | { 19 | if (*(s + count) == low_letters[i] || *(s + count) == upp_letters[i]) 20 | { 21 | *(s + count) = numbers[i]; 22 | break; 23 | } 24 | } 25 | count++; 26 | } 27 | 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | char *_strcat(char *dest, char *src); 2 | char *_strncat(char *dest, char *src, int n); 3 | char *_strncpy(char *dest, char *src, int n); 4 | int _strcmp(char *s1, char *s2); 5 | void reverse_array(int *a, int n); 6 | char *string_toupper(char *); 7 | char *cap_string(char *); 8 | char *leet(char *); 9 | char *rot13(char *); 10 | void print_number(int n); 11 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 12 | void print_buffer(char *b, int size); 13 | int _putchar(char); 14 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: source adress. 6 | * @to: target adress. 7 | * Return: no return. 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chessboard 5 | * @a: input pointer. 6 | * Return: no return. 7 | */ 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | unsigned int i, m = 0; 11 | 12 | for (i = 0; i < 64; i++) 13 | { 14 | if (i % 8 == 0 && i != 0) 15 | { 16 | m = i; 17 | _putchar('\n'); 18 | } 19 | _putchar(a[i / 8][i - m]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_diagsums - prints the sum of the two 5 | * diagonals of a square matrix of integers 6 | * @a: input pointer. 7 | * @size: size of the matrix 8 | * Return: no return. 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i, sum1 = 0, sum2 = 0; 13 | 14 | for (i = 0; i < (size * size); i++) 15 | { 16 | if (i % (size + 1) == 0) 17 | sum1 += *(a + i); 18 | if (i % (size - 1) == 0 && i != 0 && i < size * size - 1) 19 | sum2 += *(a + i); 20 | } 21 | printf("%d, %d\n", sum1, sum2); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *_memset(char *s, char b, unsigned int n); 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn(char *s, char *accept); 8 | char *_strpbrk(char *s, char *accept); 9 | char *_strstr(char *haystack, char *needle); 10 | int _putchar(char); 11 | void print_chessboard(char (*a)[8]); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *to); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - prints a string, followed by a new line. 5 | * @s: string 6 | * Return: no return. 7 | */ 8 | void _puts_recursion(char *s) 9 | { 10 | if (*s != '\0') 11 | { 12 | _putchar(*s); 13 | _puts_recursion(s + 1); 14 | } 15 | else 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints a string in reverse. 5 | * @s: string 6 | * Return: no return. 7 | */ 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s != '\0') 11 | { 12 | _print_rev_recursion(s + 1); 13 | _putchar(*s); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | 16 | /** 17 | * comparator - compares each character of the string. 18 | * @s: string 19 | * @n1: smallest iterator. 20 | * @n2: biggest iterator. 21 | * Return: . 22 | */ 23 | int comparator(char *s, int n1, int n2) 24 | { 25 | if (*(s + n1) == *(s + n2)) 26 | { 27 | if (n1 == n2 || n1 == n2 + 1) 28 | return (1); 29 | return (0 + comparator(s, n1 + 1, n2 - 1)); 30 | } 31 | return (0); 32 | } 33 | 34 | /** 35 | * is_palindrome - detects if a string is a palindrome. 36 | * @s: string. 37 | * Return: 1 if s is a palindrome, 0 if not. 38 | */ 39 | int is_palindrome(char *s) 40 | { 41 | if (*s == '\0') 42 | return (1); 43 | return (comparator(s, 0, _strlen_recursion(s) - 1)); 44 | } 45 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two strings. 5 | * @s1: string 1. 6 | * @s2: string 2. It can contains a * as a special character. 7 | * Return: 1 if are identical, 0 if not. 8 | */ 9 | int wildcmp(char *s1, char *s2) 10 | { 11 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 12 | return (0); 13 | if (*s1 == '\0' && *s2 == '\0') 14 | return (1); 15 | if (*s1 == *s2) 16 | return (wildcmp(s1 + 1, s2 + 1)); 17 | if (*s2 == '*') 18 | return (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a given number. 5 | * @n: input number. 6 | * Return: factorial of the number. 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n < 0) 11 | return (-1); 12 | else if (n == 0) 13 | return (1); 14 | else 15 | return (n * factorial(n - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - returns the value of x raised to the power of y. 5 | * @x: base. 6 | * @y: exponent. 7 | * Return: value of the exponentiation. 8 | */ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | return (-1); 13 | else if (y == 0) 14 | return (1); 15 | else 16 | return (x * _pow_recursion(x, y - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * power_operation - returns the natural square root of a number. 5 | * @n: input number. 6 | * @c: iterator. 7 | * Return: square root or -1. 8 | */ 9 | int power_operation(int n, int c) 10 | { 11 | if (c % (n / c) == 0) 12 | { 13 | if (c * (n / c) == n) 14 | return (c); 15 | else 16 | return (-1); 17 | } 18 | return (0 + power_operation(n, c + 1)); 19 | } 20 | /** 21 | * _sqrt_recursion - returns the natural square root of a number. 22 | * @n: input number. 23 | * Return: natural square root. 24 | */ 25 | int _sqrt_recursion(int n) 26 | { 27 | if (n < 0) 28 | return (-1); 29 | if (n == 0) 30 | return (0); 31 | if (n == 1) 32 | return (1); 33 | return (power_operation(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * is_prime - detects if an input number is a prime number. 5 | * @n: input number. 6 | * @c: iterator. 7 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 8 | */ 9 | int is_prime(unsigned int n, unsigned int c) 10 | { 11 | if (n % c == 0) 12 | { 13 | if (n == c) 14 | return (1); 15 | else 16 | return (0); 17 | } 18 | return (0 + is_prime(n, c + 1)); 19 | } 20 | /** 21 | * is_prime_number - detects if an input number is a prime number. 22 | * @n: input number. 23 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 24 | */ 25 | int is_prime_number(int n) 26 | { 27 | if (n == 0) 28 | return (0); 29 | if (n < 0) 30 | return (0); 31 | if (n == 1) 32 | return (0); 33 | return (is_prime(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char); 5 | void _puts_recursion(char *s); 6 | void _print_rev_recursion(char *s); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int x, int y); 10 | int _sqrt_recursion(int n); 11 | int is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - function commute srtings 5 | * @dest: param pointer to a char 6 | * @src: param pointer to a char 7 | * Return: return value of dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | while (src[j] != '\0') 23 | { 24 | dest[i] = src[j]; 25 | j++; 26 | i++; 27 | } 28 | 29 | dest[i] = '\0'; 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes used from src. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer. 5 | * @s: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | if (*(s + count) == '-') 17 | pn *= -1; 18 | 19 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 20 | { 21 | if (size > 0) 22 | m *= 10; 23 | size++; 24 | } 25 | count++; 26 | } 27 | 28 | for (i = count - size; i < count; i++) 29 | { 30 | oi = oi + ((*(s + i) - 48) * m); 31 | m /= 10; 32 | } 33 | return (oi * pn); 34 | } 35 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input stringeturn. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - prints 1 or 0 depending on input 5 | * @c: first parameter 6 | * 7 | * Description: prints all lowercase letters 8 | * Return: Always(0). 9 | */ 10 | 11 | int _islower(int c) 12 | { 13 | return (c >= 'a' && c <= 'z'); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, 5 | * @str: pointer to the string to print 6 | * Return: void 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i]) 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equals, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - Check if character is a alphabet character. 4 | * @c: type int character 5 | * Return: 1 if letter, lowercase or uppercase, and 0 otherwise 6 | */ 7 | 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 11 | return (1); 12 | 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - compute the absolute value of an integer 4 | * @n: int type number 5 | * Return: absolute value of @n 6 | */ 7 | int _abs(int n) 8 | { 9 | if (n < 0) 10 | { 11 | return (n * -1); 12 | } 13 | else 14 | { 15 | return (n); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcpy - copies the string pointed to by src, 5 | * including the terminating null byte, to the 6 | * buffer pointed to by dest. 7 | * @dest: destination. 8 | * @src: source. 9 | * Return: the pointer to dest. 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int count = 0; 14 | 15 | while (count >= 0) 16 | { 17 | *(dest + count) = *(src + count); 18 | if (*(src + count) == '\0') 19 | break; 20 | count++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C - Static libraries 2 | 3 | In this project, I learned what static libraries are and practiced creating and 4 | using them with `ar`, `ranlib`, and `nm`. 5 | 6 | ## Tasks :page_with_curl: 7 | 8 | * **0. A library is not a luxury but one of the necessities of life** 9 | * [libmy.a](./libmy.a): C Static library containing all the functions 10 | listed below : 11 | * `int _putchar(char c);` 12 | * `int _islower(int c);` 13 | * `int _isalpha(int c);` 14 | * `int _abs(int n);` 15 | * `int _isupper(int c);` 16 | * `int _isdigit(int c);` 17 | * `int _strlen(char *s);` 18 | * `void _puts(char *s);` 19 | * `char *_strcpy(char *dest, char *src);` 20 | * `int _atoi(char *s);` 21 | * `char *_strcat(char *dest, char *src);` 22 | * `char *_strncat(char *dest, char *src, int n);` 23 | * `char *_strncpy(char *dest, char *src, int n);` 24 | * `int _strcmp(char *s1, char *s2);` 25 | * `char *_memset(char *s, char b, unsigned int n);` 26 | * `char *_memcpy(char *dest, char *src, unsigned int n);` 27 | * `char *_strchr(char *s, char c);` 28 | * `unsigned int _strspn(char *s, char *accept);` 29 | * `char *_strpbrk(char *s, char *accept);` 30 | * `char *_strstr(char *haystack, char *needle);` 31 | 32 | * [main.h](./main.h): Header file containing the prototypes of all functions 33 | included in `libmy.a`. 34 | 35 | * **1. Without libraries what have we? We have no past and no future** 36 | * [create_static_lib.sh](./create_static_lib.sh): Bash script that creates a static 37 | library called `liball.a` from all the `.c` files in the current directory. 38 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar -rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int main(void) 4 | { 5 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 6 | return (0); 7 | } 8 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | /*a list that was given from the task 0 */ 5 | 6 | int _putchar(char c); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int _abs(int n); 10 | int _isupper(int c); 11 | int _isdigit(int c); 12 | int _strlen(char *s); 13 | void _puts(char *s); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | char *_strcat(char *dest, char *src); 17 | char *_strncat(char *dest, char *src, int n); 18 | char *_strncpy(char *dest, char *src, int n); 19 | int _strcmp(char *s1, char *s2); 20 | char *_memset(char *s, char b, unsigned int n); 21 | char *_memcpy(char *dest, char *src, unsigned int n); 22 | char *_strchr(char *s, char c); 23 | unsigned int _strspn(char *s, char *accept); 24 | char *_strpbrk(char *s, char *accept); 25 | char *_strstr(char *haystack, char *needle); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x09-static_libraries/quote -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints its name, followed by a new line. 4 | * @argc: number of command line arguments. 5 | * @argv: array that contains the program command line arguments. 6 | * Return: 0 - success. 7 | */ 8 | int main(int argc __attribute__((unused)), char *argv[]) 9 | { 10 | printf("%s\n", argv[0]); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the number of arguments passed into it. 4 | * @argc: number of command line arguments. 5 | * @argv: array that contains the program command line arguments. 6 | * Return: 0 - success. 7 | */ 8 | int main(int argc, char *argv[] __attribute__((unused))) 9 | { 10 | printf("%d\n", argc - 1); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - prints the minimum number of coins to make change for an amount. 5 | * of money. 6 | * @argc: number of command line arguments. 7 | * @argv: array that contains the program command line arguments. 8 | * Return: 0 - success. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int cents, ncoins = 0; 13 | 14 | if (argc == 1 || argc > 2) 15 | { 16 | printf("Error\n"); 17 | return (1); 18 | } 19 | 20 | cents = atoi(argv[1]); 21 | 22 | while (cents > 0) 23 | { 24 | if (cents >= 25) 25 | cents -= 25; 26 | else if (cents >= 10) 27 | cents -= 10; 28 | else if (cents >= 5) 29 | cents -= 5; 30 | else if (cents >= 2) 31 | cents -= 2; 32 | else if (cents >= 1) 33 | cents -= 1; 34 | ncoins += 1; 35 | } 36 | printf("%d\n", ncoins); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all arguments it receives. 4 | * @argc: number of command line arguments. 5 | * @argv: array that contains the program command line arguments. 6 | * Return: 0 - success. 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | int i; 11 | 12 | for (i = 0; i < argc; i++) 13 | printf("%s\n", argv[i]); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - multiplies two numbers. 5 | * @argc: number of command line arguments. 6 | * @argv: array that contains the program command line arguments. 7 | * Return: 0 - success. 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | if (argc != 3) 12 | { 13 | printf("Error\n"); 14 | return (1); 15 | } 16 | printf("%d\n", atoi(argv[1]) * atoi(argv[2])); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - adds positive numbers. 6 | * @argc: number of command line arguments. 7 | * @argv: array that contains the program command line arguments. 8 | * Return: 0 - success. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int i, j, add = 0; 13 | 14 | for (i = 1; i < argc; i++) 15 | { 16 | for (j = 0; argv[i][j] != '\0'; j++) 17 | { 18 | if (!isdigit(argv[i][j])) 19 | { 20 | printf("Error\n"); 21 | return (1); 22 | } 23 | } 24 | add += atoi(argv[i]); 25 | } 26 | printf("%d\n", add); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * create_array - creates an array of chars. 5 | * @size: size of the array. 6 | * @c: storaged char 7 | * 8 | * Return: pointer of an array of chars 9 | */ 10 | char *create_array(unsigned int size, char c) 11 | { 12 | char *cr; 13 | unsigned int i; 14 | 15 | if (size == 0) 16 | return (NULL); 17 | 18 | cr = malloc(sizeof(c) * size); 19 | 20 | if (cr == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | cr[i] = c; 25 | 26 | return (cr); 27 | } 28 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strdup - returns a pointer to a newly allocated space in memory. 5 | * @str: string. 6 | * 7 | * Return: pointer of an array of chars 8 | */ 9 | char *_strdup(char *str) 10 | { 11 | char *strout; 12 | unsigned int i, j; 13 | 14 | if (str == NULL) 15 | return (NULL); 16 | 17 | for (i = 0; str[i] != '\0'; i++) 18 | ; 19 | 20 | strout = (char *)malloc(sizeof(char) * (i + 1)); 21 | 22 | if (strout == NULL) 23 | return (NULL); 24 | 25 | for (j = 0; j <= i; j++) 26 | strout[j] = str[j]; 27 | 28 | return (strout); 29 | } 30 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments of a program. 6 | * @ac: argument count. 7 | * @av: argument vector. 8 | * 9 | * Return: pointer of an array of char 10 | */ 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *aout; 14 | int c, i, j, ia; 15 | 16 | if (ac == 0) 17 | return (NULL); 18 | 19 | for (c = i = 0; i < ac; i++) 20 | { 21 | if (av[i] == NULL) 22 | return (NULL); 23 | 24 | for (j = 0; av[i][j] != '\0'; j++) 25 | c++; 26 | c++; 27 | } 28 | 29 | aout = malloc((c + 1) * sizeof(char)); 30 | 31 | if (aout == NULL) 32 | { 33 | free(aout); 34 | return (NULL); 35 | } 36 | 37 | for (i = j = ia = 0; ia < c; j++, ia++) 38 | { 39 | if (av[i][j] == '\0') 40 | { 41 | aout[ia] = '\n'; 42 | i++; 43 | ia++; 44 | j = 0; 45 | } 46 | if (ia < c - 1) 47 | aout[ia] = av[i][j]; 48 | } 49 | aout[ia] = '\0'; 50 | 51 | return (aout); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * count_word - helper function to count the number of words in a string 6 | * @s: string to evaluate 7 | * 8 | * Return: number of words 9 | */ 10 | int count_word(char *s) 11 | { 12 | int flag, c, w; 13 | 14 | flag = 0; 15 | w = 0; 16 | 17 | for (c = 0; s[c] != '\0'; c++) 18 | { 19 | if (s[c] == ' ') 20 | flag = 0; 21 | else if (flag == 0) 22 | { 23 | flag = 1; 24 | w++; 25 | } 26 | } 27 | 28 | return (w); 29 | } 30 | /** 31 | * **strtow - splits a string into words 32 | * @str: string to split 33 | * 34 | * Return: pointer to an array of strings (Success) 35 | * or NULL (Error) 36 | */ 37 | char **strtow(char *str) 38 | { 39 | char **matrix, *tmp; 40 | int i, k = 0, len = 0, words, c = 0, start, end; 41 | 42 | while (*(str + len)) 43 | len++; 44 | words = count_word(str); 45 | if (words == 0) 46 | return (NULL); 47 | 48 | matrix = (char **) malloc(sizeof(char *) * (words + 1)); 49 | if (matrix == NULL) 50 | return (NULL); 51 | 52 | for (i = 0; i <= len; i++) 53 | { 54 | if (str[i] == ' ' || str[i] == '\0') 55 | { 56 | if (c) 57 | { 58 | end = i; 59 | tmp = (char *) malloc(sizeof(char) * (c + 1)); 60 | if (tmp == NULL) 61 | return (NULL); 62 | while (start < end) 63 | *tmp++ = str[start++]; 64 | *tmp = '\0'; 65 | matrix[k] = tmp - c; 66 | k++; 67 | c = 0; 68 | } 69 | } 70 | else if (c++ == 0) 71 | start = i; 72 | } 73 | 74 | matrix[k] = NULL; 75 | 76 | return (matrix); 77 | } 78 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - concatenates two strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * 9 | * Return: pointer of an array of chars 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *strout; 14 | unsigned int i, j, k, limit; 15 | 16 | if (s1 == NULL) 17 | s1 = ""; 18 | if (s2 == NULL) 19 | s2 = ""; 20 | 21 | for (i = 0; s1[i] != '\0'; i++) 22 | ; 23 | 24 | for (j = 0; s2[j] != '\0'; j++) 25 | ; 26 | 27 | strout = malloc(sizeof(char) * (i + j + 1)); 28 | 29 | if (strout == NULL) 30 | { 31 | free(strout); 32 | return (NULL); 33 | } 34 | 35 | for (k = 0; k < i; k++) 36 | strout[k] = s1[k]; 37 | 38 | limit = j; 39 | for (j = 0; j <= limit; k++, j++) 40 | strout[k] = s2[j]; 41 | 42 | return (strout); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - returns a pointer to a 2 dimensional array of integers. 6 | * @width: width of the array. 7 | * @height: height of the array. 8 | * 9 | * Return: pointer of an array of integers 10 | */ 11 | int **alloc_grid(int width, int height) 12 | { 13 | int **gridout; 14 | int i, j; 15 | 16 | if (width < 1 || height < 1) 17 | return (NULL); 18 | 19 | gridout = malloc(height * sizeof(int *)); 20 | if (gridout == NULL) 21 | { 22 | free(gridout); 23 | return (NULL); 24 | } 25 | 26 | for (i = 0; i < height; i++) 27 | { 28 | gridout[i] = malloc(width * sizeof(int)); 29 | if (gridout[i] == NULL) 30 | { 31 | for (i--; i >= 0; i--) 32 | free(gridout[i]); 33 | free(gridout); 34 | return (NULL); 35 | } 36 | } 37 | 38 | for (i = 0; i < height; i++) 39 | for (j = 0; j < width; j++) 40 | gridout[i][j] = 0; 41 | 42 | return (gridout); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - frees a 2 dimensional grid. 6 | * @grid: multidimensional array of integers. 7 | * @height: height of the grid. 8 | * 9 | * Return: no return 10 | */ 11 | void free_grid(int **grid, int height) 12 | { 13 | if (grid != NULL && height != 0) 14 | { 15 | for (; height >= 0; height--) 16 | free(grid[height]); 17 | free(grid); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * malloc_checked - allocates memory. 5 | * @b: amount of bytes. 6 | * 7 | * Return: pointer to the allocated memory. 8 | * if malloc fails, status value is equal to 98. 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | char *p; 13 | 14 | p = malloc(b); 15 | if (p == NULL) 16 | exit(98); 17 | return (p); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - concatenates two strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * @n: amount of bytes. 9 | * 10 | * Return: pointer to the allocated memory. 11 | * if malloc fails, status value is equal to 98. 12 | */ 13 | char *string_nconcat(char *s1, char *s2, unsigned int n) 14 | { 15 | char *sout; 16 | unsigned int ls1, ls2, lsout, i; 17 | 18 | if (s1 == NULL) 19 | s1 = ""; 20 | 21 | if (s2 == NULL) 22 | s2 = ""; 23 | 24 | for (ls1 = 0; s1[ls1] != '\0'; ls1++) 25 | ; 26 | 27 | for (ls2 = 0; s2[ls2] != '\0'; ls2++) 28 | ; 29 | 30 | if (n > ls2) 31 | n = ls2; 32 | 33 | lsout = ls1 + n; 34 | 35 | sout = malloc(lsout + 1); 36 | 37 | if (sout == NULL) 38 | return (NULL); 39 | 40 | for (i = 0; i < lsout; i++) 41 | if (i < ls1) 42 | sout[i] = s1[i]; 43 | else 44 | sout[i] = s2[i - ls1]; 45 | 46 | sout[i] = '\0'; 47 | 48 | return (sout); 49 | } 50 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _realloc - reallocates a memory block. 5 | * @ptr: pointer to the memory previously allocated. 6 | * @old_size: size, in bytes, of the allocated space of ptr. 7 | * @new_size: new size, in bytes, of the new memory block. 8 | * 9 | * Return: ptr. 10 | * if new_size == old_size, returns ptr without changes. 11 | * if malloc fails, returns NULL. 12 | */ 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 14 | { 15 | if (new_size == 0 && ptr != NULL) 16 | { 17 | free(ptr); 18 | return (NULL); 19 | } 20 | 21 | if (ptr == NULL) 22 | ptr = malloc(new_size); 23 | 24 | if (new_size == old_size) 25 | return (ptr); 26 | 27 | free(ptr); 28 | ptr = malloc(new_size); 29 | 30 | return (ptr); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _calloc - allocates memory for an array. 5 | * @nmemb: number of elements. 6 | * @size: size of bytes. 7 | * 8 | * Return: pointer to the allocated memory. 9 | * if nmemb or size is 0, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | void *_calloc(unsigned int nmemb, unsigned int size) 13 | { 14 | char *p; 15 | unsigned int i; 16 | 17 | if (nmemb == 0 || size == 0) 18 | return (NULL); 19 | 20 | p = malloc(nmemb * size); 21 | 22 | if (p == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; i < (nmemb * size); i++) 26 | p[i] = 0; 27 | 28 | return (p); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * array_range - creates an array of integers. 5 | * @min: minimum value. 6 | * @max: maximum value. 7 | * 8 | * Return: pointer to the newly created array. 9 | * if man > mix, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | int *array_range(int min, int max) 13 | { 14 | int *ar; 15 | int i; 16 | 17 | if (min > max) 18 | return (NULL); 19 | 20 | ar = malloc(sizeof(*ar) * ((max - min) + 1)); 21 | 22 | if (ar == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; min <= max; i++, min++) 26 | ar[i] = min; 27 | 28 | return (ar); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | void *malloc_checked(unsigned int b); 5 | char *string_nconcat(char *s1, char *s2, unsigned int n); 6 | void *_calloc(unsigned int nmemb, unsigned int size); 7 | int *array_range(int min, int max); 8 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _OBJECT_LIKE_MACRO_ 2 | #define _OBJECT_LIKE_MACRO_ 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef _PI_ 2 | #define _PI_ 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_LIKE_MACRO_ 2 | #define _FUNCTION_LIKE_MACRO_ 3 | 4 | #define ABS(x) ((x) < (0) ? ((x) * (-1)) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUM_LIKE_MACRO_ 2 | #define _SUM_LIKE_MACRO_ 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # C - Preprocessor 2 | 3 | In this project, I learned about common predefined macros in C while 4 | practicing implementing my own and include-guarding header files. 5 | 6 | ## Tasks :page_with_curl: 7 | 8 | * **0. Object-like Macro** 9 | * [0-object_like_macro.h](./0-object_like_macro.h): Header file that defines a 10 | macro named `SIZE` as an abbreviation for the token `1024`. 11 | 12 | * **1. Pi** 13 | * [1-pi.h](./1-pi.h): Header file that defines a macro named `PI` as an abbreviation 14 | for the token `3.14159265359`. 15 | 16 | * **2. File name** 17 | * [2-main.c](./2-main.c): C program that prints the name of the file it was 18 | compiled from followed by a new line. 19 | 20 | * **3. Function-like macro** 21 | * [3-function_like_macro.h](./3-function_like_macro.h): Header file that defines a 22 | function-like macro `ABS(x)` that computes the absolute value of a number `x`. 23 | 24 | * **4. SUM** 25 | * [4-sum.h](./4-sum.h): Header file that defines a function-like macro `SUM(x, y)` 26 | that computes the sum of the numbers `x` and `y`. 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - initializes a variable of type struct dog. 5 | * @d: struct dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: no return. 11 | */ 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d) 15 | { 16 | d->name = name; 17 | d->age = age; 18 | d->owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * print_dog - prints a struct dog. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (d) 13 | { 14 | if (!(d->name)) 15 | printf("Name: (nil)\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | printf("Age: %f\n", d->age); 20 | 21 | if (!(d->owner)) 22 | printf("Owner: (nil)\n"); 23 | else 24 | printf("Owner: %s\n", d->owner); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * new_dog - creates a new dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: struct dog. 11 | * if fails, returns NULL. 12 | */ 13 | dog_t *new_dog(char *name, float age, char *owner) 14 | { 15 | dog_t *p_dog; 16 | int i, lname, lowner; 17 | 18 | p_dog = malloc(sizeof(*p_dog)); 19 | if (p_dog == NULL || !(name) || !(owner)) 20 | { 21 | free(p_dog); 22 | return (NULL); 23 | } 24 | 25 | for (lname = 0; name[lname]; lname++) 26 | ; 27 | 28 | for (lowner = 0; owner[lowner]; lowner++) 29 | ; 30 | 31 | p_dog->name = malloc(lname + 1); 32 | p_dog->owner = malloc(lowner + 1); 33 | 34 | if (!(p_dog->name) || !(p_dog->owner)) 35 | { 36 | free(p_dog->owner); 37 | free(p_dog->name); 38 | free(p_dog); 39 | return (NULL); 40 | } 41 | 42 | for (i = 0; i < lname; i++) 43 | p_dog->name[i] = name[i]; 44 | p_dog->name[i] = '\0'; 45 | 46 | p_dog->age = age; 47 | 48 | for (i = 0; i < lowner; i++) 49 | p_dog->owner[i] = owner[i]; 50 | p_dog->owner[i] = '\0'; 51 | 52 | return (p_dog); 53 | } 54 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - frees dogs. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d) 13 | { 14 | free(d->name); 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # C - Structures, typedef 2 | 3 | In this project, I learned about using structures and `typedef` in C. 4 | 5 | ## Header File :file_folder: 6 | 7 | * [dog.h](./dog.h): Header file containing definitions and prototypes for all types 8 | and functions written in the project. 9 | 10 | | Type/File | Defintion/Prototype | 11 | | --------------- | ------------------------------------------------------------------------ | 12 | | `struct dog` |
  • `char *name`
  • `float age`
  • `char *owner`
| 13 | | `typedef dog_t` | `struct dog` | 14 | | `1-init_dog.c` | `void init_dog(struct dog *d, char *name, float age, char *owner);` | 15 | | `2-print_dog.c` | `void print_dog(struct dog *d);` | 16 | | `4-new_dog.c` | `dog_t *new_dog(char *name, float age, char *owner);` | 17 | | `5-free_dog.c` | `void free_dog(dog_t *d);` | 18 | 19 | ## Tasks :page_with_curl: 20 | 21 | * **0. Django** 22 | * [dog.h](./dog.h): Header file that defines a new type `struct dog` with the 23 | following elements: 24 | * `char *name` 25 | * `float age` 26 | * `char *owner` 27 | 28 | * **1. A dog is the only thing on earth that loves you more than you love yourself** 29 | * [1-init_dog.c](./1-init_dog.c): C function that initializes a variable of type `struct dog`. 30 | 31 | * **2. A dog will teach you unconditional love. If you can have that in your life, things won't be too bad** 32 | * [2-print_dog.c](./2-print_dog.c): C function that prints a `struct dog`. 33 | * If an element of `d` is `NULL`, the function prints `(nil)` instead of the element. 34 | * If `d` is `NULL`, the function prints nothing. 35 | 36 | * **3. Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to read** 37 | * [dog.h](./dog.h): Header file that defines a new type `dog_t` as a new name for the 38 | type `struct dog`. 39 | 40 | * **4. A door is what a dog is perpetually on the wrong side of** 41 | * [4-new_dog.c](./4-new_dog.c): C function that creates a dog. 42 | * Returns `NULL` if the function fails. 43 | 44 | * **5. How many legs does a dog have if you call his tail a leg? Four. Saying that a tail is a leg doesn't make it a leg** 45 | * [5-free_dog.c](./5-free_dog.c): C function that frees dogs. 46 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOG_H_ 2 | #define _DOG_H_ 3 | 4 | /** 5 | * dog_t - Typedef for struct dog 6 | */ 7 | typedef struct dog dog_t; 8 | 9 | /** 10 | * struct dog - struct that stores some information of a dog 11 | * @name: name of the dog 12 | * @age: age of the dog 13 | * @owner: owner of the dog 14 | * 15 | * Description: struct called "dog" that stores its name, its age 16 | * and the name of its owner. 17 | */ 18 | struct dog 19 | { 20 | char *name; 21 | float age; 22 | char *owner; 23 | }; 24 | 25 | void init_dog(struct dog *d, char *name, float age, char *owner); 26 | void print_dog(struct dog *d); 27 | dog_t *new_dog(char *name, float age, char *owner); 28 | void free_dog(dog_t *d); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - prints a name. 5 | * @name: input name. 6 | * @f: function pointer. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (name && f) 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - executes a function given as a 5 | * parameter on each element of an array. 6 | * @array: input integer array. 7 | * @size: size of the array. 8 | * @action: pointer to the function. 9 | * 10 | * Return: no return. 11 | */ 12 | void array_iterator(int *array, size_t size, void (*action)(int)) 13 | { 14 | unsigned int i; 15 | 16 | if (array && action) 17 | for (i = 0; i < size; i++) 18 | action(array[i]); 19 | } 20 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX students. 6 | * @argc: argument count. 7 | * @argv: argument vector. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | char *opc = (char *) main; 14 | int i, nbytes; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | exit(1); 20 | } 21 | 22 | nbytes = atoi(argv[1]); 23 | 24 | if (nbytes < 0) 25 | { 26 | printf("Error\n"); 27 | exit(2); 28 | } 29 | 30 | for (i = 0; i < nbytes; i++) 31 | { 32 | printf("%02x", opc[i] & 0xFF); 33 | if (i != nbytes - 1) 34 | printf(" "); 35 | } 36 | 37 | printf("\n"); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer 5 | * @array: input integer array. 6 | * @size: size of the array. 7 | * @cmp: pointer to the function to be used 8 | * to compare values. 9 | * 10 | * Return: index of the first eement for which the cmp 11 | * function does not return 0. If no elements matches, 12 | * return -1. If size <= 0, return -1. 13 | */ 14 | int int_index(int *array, int size, int (*cmp)(int)) 15 | { 16 | int i; 17 | 18 | if (array && cmp) 19 | { 20 | if (size <= 0) 21 | return (-1); 22 | 23 | for (i = 0; i < size; i++) 24 | if (cmp(array[i])) 25 | return (i); 26 | } 27 | 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H_ 2 | #define _CALC_H_ 3 | 4 | #include 5 | #include 6 | /** 7 | * struct op - Struct op 8 | * 9 | * @op: The operator 10 | * @f: The function associated 11 | */ 12 | typedef struct op 13 | { 14 | char *op; 15 | int (*f)(int a, int b); 16 | } op_t; 17 | 18 | int op_add(int a, int b); 19 | int op_sub(int a, int b); 20 | int op_mul(int a, int b); 21 | int op_div(int a, int b); 22 | int op_mod(int a, int b); 23 | int (*get_op_func(char *s))(int, int); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * get_op_func - selects the correct function to perform 5 | * the operation asked by the user. 6 | * @s: char operator. 7 | * 8 | * Return: pointer to the function that corresponds to the operator. 9 | */ 10 | int (*get_op_func(char *s))(int, int) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | int i = 0; 21 | 22 | while (i < 10) 23 | { 24 | if (s[0] == ops->op[i]) 25 | break; 26 | i++; 27 | } 28 | 29 | return (ops[i / 2].f); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * main - check the code for ALX students. 5 | * @argc: argument count. 6 | * @argv: argument vector. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int a, b; 13 | int (*operation)(int, int); 14 | 15 | if (argc != 4) 16 | { 17 | printf("Error\n"); 18 | exit(98); 19 | } 20 | 21 | if (argv[2][1]) 22 | { 23 | printf("Error\n"); 24 | exit(99); 25 | } 26 | 27 | operation = get_op_func(argv[2]); 28 | 29 | if (operation == NULL) 30 | { 31 | printf("Error\n"); 32 | exit(99); 33 | } 34 | 35 | a = atoi(argv[1]); 36 | b = atoi(argv[3]); 37 | 38 | printf("%d\n", operation(a, b)); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * op_add - adds two numbers. 5 | * @a: first number. 6 | * @b: second number. 7 | * 8 | * Return: add. 9 | */ 10 | int op_add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | 15 | /** 16 | * op_sub - subctracts two numbers. 17 | * @a: first number. 18 | * @b: second number. 19 | * 20 | * Return: difference. 21 | */ 22 | int op_sub(int a, int b) 23 | { 24 | return (a - b); 25 | } 26 | 27 | /** 28 | * op_mul - multiplies two numbers. 29 | * @a: first number. 30 | * @b: second number. 31 | * 32 | * Return: multiplication. 33 | */ 34 | int op_mul(int a, int b) 35 | { 36 | return (a * b); 37 | } 38 | 39 | /** 40 | * op_div - divides two numbers. 41 | * @a: first number. 42 | * @b: second number. 43 | * 44 | * Return: division. 45 | */ 46 | int op_div(int a, int b) 47 | { 48 | if (b == 0) 49 | { 50 | printf("Error\n"); 51 | exit(100); 52 | } 53 | return (a / b); 54 | } 55 | 56 | /** 57 | * op_mod - calculates the module of two numbers. 58 | * @a: first number. 59 | * @b: second number. 60 | * 61 | * Return: remainder of the division. 62 | */ 63 | int op_mod(int a, int b) 64 | { 65 | if (b == 0) 66 | { 67 | printf("Error\n"); 68 | exit(100); 69 | } 70 | return (a % b); 71 | } 72 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_ 2 | #define _FUNCTION_POINTERS_ 3 | 4 | #include 5 | 6 | void print_name(char *name, void (*f)(char *)); 7 | void array_iterator(int *array, size_t size, void (*action)(int)); 8 | int int_index(int *array, int size, int (*cmp)(int)); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - returns the sum of all its parameters. 5 | * @n: amount of the arguments. 6 | * 7 | * Return: sum of its parameters. 8 | */ 9 | int sum_them_all(const unsigned int n, ...) 10 | { 11 | va_list valist; 12 | unsigned int i; 13 | int sum = 0; 14 | 15 | if (n == 0) 16 | return (0); 17 | 18 | va_start(valist, n); 19 | 20 | for (i = 0; i < n; i++) 21 | sum += va_arg(valist, int); 22 | 23 | va_end(valist); 24 | 25 | return (sum); 26 | } 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_numbers - prints numbers. 5 | * @separator: string to be printed between numbers. 6 | * @n: number of integers passed to the function. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_numbers(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list valist; 13 | unsigned int i; 14 | 15 | va_start(valist, n); 16 | 17 | for (i = 0; i < n; i++) 18 | { 19 | printf("%d", va_arg(valist, int)); 20 | if (separator && i < n - 1) 21 | printf("%s", separator); 22 | } 23 | 24 | printf("\n"); 25 | va_end(valist); 26 | } 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - prints strings. 5 | * @separator: string to be printed between the strings. 6 | * @n: number of strings passed to the function. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_strings(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list valist; 13 | unsigned int i; 14 | char *str; 15 | 16 | va_start(valist, n); 17 | 18 | for (i = 0; i < n; i++) 19 | { 20 | str = va_arg(valist, char *); 21 | 22 | if (str) 23 | printf("%s", str); 24 | else 25 | printf("(nil)"); 26 | 27 | if (i < n - 1) 28 | if (separator) 29 | printf("%s", separator); 30 | } 31 | 32 | printf("\n"); 33 | va_end(valist); 34 | } 35 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_all - prints anything. 5 | * @format: a list of types of arguments passed to the function. 6 | * 7 | * Return: no return. 8 | */ 9 | void print_all(const char * const format, ...) 10 | { 11 | va_list valist; 12 | unsigned int i = 0, j, c = 0; 13 | char *str; 14 | const char t_arg[] = "cifs"; 15 | 16 | va_start(valist, format); 17 | while (format && format[i]) 18 | { 19 | j = 0; 20 | while (t_arg[j]) 21 | { 22 | if (format[i] == t_arg[j] && c) 23 | { 24 | printf(", "); 25 | break; 26 | } j++; 27 | } 28 | switch (format[i]) 29 | { 30 | case 'c': 31 | printf("%c", va_arg(valist, int)), c = 1; 32 | break; 33 | case 'i': 34 | printf("%d", va_arg(valist, int)), c = 1; 35 | break; 36 | case 'f': 37 | printf("%f", va_arg(valist, double)), c = 1; 38 | break; 39 | case 's': 40 | str = va_arg(valist, char *), c = 1; 41 | if (!str) 42 | { 43 | printf("(nil)"); 44 | break; 45 | } 46 | printf("%s", str); 47 | break; 48 | } i++; 49 | } 50 | printf("\n"), va_end(valist); 51 | } 52 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _VARIADIC_FUNCTIONS_ 2 | #define _VARIADIC_FUNCTIONS_ 3 | 4 | #include 5 | #include 6 | 7 | int sum_them_all(const unsigned int n, ...); 8 | void print_numbers(const char *separator, const unsigned int n, ...); 9 | void print_strings(const char *separator, const unsigned int n, ...); 10 | void print_all(const char * const format, ...); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_list - prints all the elements of a list_t list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t print_list(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | if (h->str == NULL) 16 | printf("[%d] %s\n", 0, "(nil)"); 17 | else 18 | printf("[%d] %s\n", h->len, h->str); 19 | h = h->next; 20 | nelem++; 21 | } 22 | return (nelem); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * list_len - returns then number of elements in a list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t list_len(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | h = h->next; 16 | nelem++; 17 | } 18 | return (nelem); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * bmain - function executed before main 5 | * Return: no return. 6 | */ 7 | 8 | void __attribute__ ((constructor)) bmain() 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_school.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node - adds a new node at the beginning 4 | * of a list_t list. 5 | * @head: head of the linked list. 6 | * @str: string to store in the list. 7 | * Return: address of the head. 8 | */ 9 | 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *new; 13 | size_t nchar; 14 | 15 | new = malloc(sizeof(list_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->str = strdup(str); 20 | 21 | for (nchar = 0; str[nchar]; nchar++) 22 | ; 23 | 24 | new->len = nchar; 25 | new->next = *head; 26 | *head = new; 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - adds a new node at the end 5 | * of a list_t list. 6 | * @head: head of the linked list. 7 | * @str: string to store in the list. 8 | * Return: address of the head. 9 | */ 10 | 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *new, *temp; 14 | size_t nchar; 15 | 16 | new = malloc(sizeof(list_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->str = strdup(str); 21 | 22 | for (nchar = 0; str[nchar]; nchar++) 23 | ; 24 | 25 | new->len = nchar; 26 | new->next = NULL; 27 | temp = *head; 28 | 29 | if (temp == NULL) 30 | { 31 | *head = new; 32 | } 33 | else 34 | { 35 | while (temp->next != NULL) 36 | temp = temp->next; 37 | temp->next = new; 38 | } 39 | 40 | return (*head); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list 5 | * @head: head of the linked list. 6 | * Return: no return. 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *current; 12 | 13 | while ((current = head) != NULL) 14 | { 15 | head = head->next; 16 | free(current->str); 17 | free(current); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for ALX project 16 | */ 17 | typedef struct list_s 18 | { 19 | char *str; 20 | unsigned int len; 21 | struct list_s *next; 22 | } list_t; 23 | 24 | size_t print_list(const list_t *h); 25 | size_t list_len(const list_t *h); 26 | list_t *add_node(list_t **head, const char *str); 27 | list_t *add_node_end(list_t **head, const char *str); 28 | void free_list(list_t *head); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the elements of a list. 5 | * @h: head of a list. 6 | * 7 | * Return: numbers of nodes. 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t nnodes = 0; 12 | 13 | while (h != NULL) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns the number of elements in 5 | * a linked list. 6 | * @h: head of a list. 7 | * 8 | * Return: numbers of nodes. 9 | */ 10 | size_t listint_len(const listint_t *h) 11 | { 12 | size_t nnodes = 0; 13 | 14 | while (h != NULL) 15 | { 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes the node at index 5 | * of a linked list. 6 | * @head: head of a list. 7 | * @index: index of the list where the node is 8 | * deleted. 9 | * 10 | * Return: 1 if it succeeded, -1 if it failed. 11 | */ 12 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 13 | { 14 | unsigned int i; 15 | listint_t *prev; 16 | listint_t *next; 17 | 18 | prev = *head; 19 | 20 | if (index != 0) 21 | { 22 | for (i = 0; i < index - 1 && prev != NULL; i++) 23 | { 24 | prev = prev->next; 25 | } 26 | } 27 | 28 | if (prev == NULL || (prev->next == NULL && index != 0)) 29 | { 30 | return (-1); 31 | } 32 | 33 | next = prev->next; 34 | 35 | if (index != 0) 36 | { 37 | prev->next = next->next; 38 | free(next); 39 | } 40 | else 41 | { 42 | free(prev); 43 | *head = next; 44 | } 45 | 46 | return (1); 47 | } 48 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: pointer to the first node. 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *p; 12 | listint_t *n; 13 | 14 | p = NULL; 15 | n = NULL; 16 | 17 | while (*head != NULL) 18 | { 19 | n = (*head)->next; 20 | (*head)->next = p; 21 | p = *head; 22 | *head = n; 23 | } 24 | 25 | *head = p; 26 | return (*head); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * print_listint_safe - prints a linked list. 28 | * @head: head of a list. 29 | * 30 | * Return: number of nodes in the list. 31 | */ 32 | size_t print_listint_safe(const listint_t *head) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | 37 | hptr = NULL; 38 | while (head != NULL) 39 | { 40 | new = malloc(sizeof(listp_t)); 41 | 42 | if (new == NULL) 43 | exit(98); 44 | 45 | new->p = (void *)head; 46 | new->next = hptr; 47 | hptr = new; 48 | 49 | add = hptr; 50 | 51 | while (add->next != NULL) 52 | { 53 | add = add->next; 54 | if (head == add->p) 55 | { 56 | printf("-> [%p] %d\n", (void *)head, head->n); 57 | free_listp(&hptr); 58 | return (nnodes); 59 | } 60 | } 61 | 62 | printf("[%p] %d\n", (void *)head, head->n); 63 | head = head->next; 64 | nnodes++; 65 | } 66 | 67 | free_listp(&hptr); 68 | return (nnodes); 69 | } 70 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp2(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * free_listint_safe - frees a linked list. 28 | * @h: head of a list. 29 | * 30 | * Return: size of the list that was freed. 31 | */ 32 | size_t free_listint_safe(listint_t **h) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | listint_t *curr; 37 | 38 | hptr = NULL; 39 | while (*h != NULL) 40 | { 41 | new = malloc(sizeof(listp_t)); 42 | 43 | if (new == NULL) 44 | exit(98); 45 | 46 | new->p = (void *)*h; 47 | new->next = hptr; 48 | hptr = new; 49 | 50 | add = hptr; 51 | 52 | while (add->next != NULL) 53 | { 54 | add = add->next; 55 | if (*h == add->p) 56 | { 57 | *h = NULL; 58 | free_listp2(&hptr); 59 | return (nnodes); 60 | } 61 | } 62 | 63 | curr = *h; 64 | *h = (*h)->next; 65 | free(curr); 66 | nnodes++; 67 | } 68 | 69 | *h = NULL; 70 | free_listp2(&hptr); 71 | return (nnodes); 72 | } 73 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - finds the loop in a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: the address of the node where the loop starts. 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *p2; 12 | listint_t *prev; 13 | 14 | p2 = head; 15 | prev = head; 16 | while (head && p2 && p2->next) 17 | { 18 | head = head->next; 19 | p2 = p2->next->next; 20 | 21 | if (head == p2) 22 | { 23 | head = prev; 24 | prev = p2; 25 | while (1) 26 | { 27 | p2 = prev; 28 | while (p2->next != head && p2->next != prev) 29 | { 30 | p2 = p2->next; 31 | } 32 | if (p2->next == head) 33 | break; 34 | 35 | head = head->next; 36 | } 37 | return (p2->next); 38 | } 39 | } 40 | 41 | return (NULL); 42 | } 43 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - add a new node at the beginning 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | 15 | new = malloc(sizeof(listint_t)); 16 | 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->next = *head; 22 | *head = new; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - add a new node at the end 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint_end(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | listint_t *temp; 15 | 16 | (void)temp; 17 | 18 | new = malloc(sizeof(listint_t)); 19 | 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | new->n = n; 24 | new->next = NULL; 25 | temp = *head; 26 | if (*head == NULL) 27 | { 28 | *head = new; 29 | } 30 | else 31 | { 32 | while (temp->next != NULL) 33 | { 34 | temp = temp->next; 35 | } 36 | temp->next = new; 37 | } 38 | 39 | return (*head); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *temp; 12 | 13 | while ((temp = head) != NULL) 14 | { 15 | head = head->next; 16 | free(temp); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint2(listint_t **head) 10 | { 11 | listint_t *temp; 12 | listint_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes the head node of 5 | * a linked list 6 | * @head: head of a list. 7 | * 8 | * Return: head node's data. 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | int hnode; 13 | listint_t *h; 14 | listint_t *curr; 15 | 16 | if (*head == NULL) 17 | return (0); 18 | 19 | curr = *head; 20 | 21 | hnode = curr->n; 22 | 23 | h = curr->next; 24 | 25 | free(curr); 26 | 27 | *head = h; 28 | 29 | return (hnode); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the nth node of a linked list 5 | * @head: head of a list. 6 | * @index: index of the node. 7 | * 8 | * Return: nth node. If node does not exist, returns NULL. 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < index && head != NULL; i++) 15 | { 16 | head = head->next; 17 | } 18 | 19 | return (head); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - returns the sum of all the data (n) of 5 | * a linked list. 6 | * @head: head of a list. 7 | * 8 | * Return: sum of all the data (n). 9 | */ 10 | int sum_listint(listint_t *head) 11 | { 12 | int sum; 13 | 14 | sum = 0; 15 | while (head != NULL) 16 | { 17 | sum += head->n; 18 | head = head->next; 19 | } 20 | 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a new node 5 | * at a given position. 6 | * @head: head of a list. 7 | * @idx: index of the list where the new node is 8 | * added. 9 | * @n: integer element. 10 | * 11 | * Return: the address of the new node, or NULL if it 12 | * failed. 13 | */ 14 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 15 | { 16 | unsigned int i; 17 | listint_t *new; 18 | listint_t *h; 19 | 20 | h = *head; 21 | 22 | if (idx != 0) 23 | { 24 | for (i = 0; i < idx - 1 && h != NULL; i++) 25 | { 26 | h = h->next; 27 | } 28 | } 29 | 30 | if (h == NULL && idx != 0) 31 | return (NULL); 32 | 33 | new = malloc(sizeof(listint_t)); 34 | if (new == NULL) 35 | return (NULL); 36 | 37 | new->n = n; 38 | 39 | if (idx == 0) 40 | { 41 | new->next = *head; 42 | *head = new; 43 | } 44 | else 45 | { 46 | new->next = h->next; 47 | h->next = new; 48 | } 49 | 50 | return (new); 51 | } 52 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * @n: integer 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | * for ALX project 14 | */ 15 | typedef struct listint_s 16 | { 17 | int n; 18 | struct listint_s *next; 19 | } listint_t; 20 | 21 | /** 22 | * struct listp_s - singly linked list 23 | * @p: pointers of nodes 24 | * @next: points to the next node 25 | * 26 | * Description: singly linked list of pointers 27 | */ 28 | typedef struct listp_s 29 | { 30 | void *p; 31 | struct listp_s *next; 32 | } listp_t; 33 | 34 | size_t print_listint(const listint_t *h); 35 | size_t listint_len(const listint_t *h); 36 | listint_t *add_nodeint(listint_t **head, const int n); 37 | listint_t *add_nodeint_end(listint_t **head, const int n); 38 | void free_listint(listint_t *head); 39 | void free_listint2(listint_t **head); 40 | int pop_listint(listint_t **head); 41 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 42 | int sum_listint(listint_t *head); 43 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 44 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 45 | listint_t *reverse_listint(listint_t **head); 46 | size_t print_listint_safe(const listint_t *head); 47 | size_t free_listint_safe(listint_t **h); 48 | listint_t *find_listint_loop(listint_t *head); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to an 5 | * unsigned int. 6 | * @b: binary. 7 | * 8 | * Return: unsigned int. 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int ui; 13 | int len, base_two; 14 | 15 | if (!b) 16 | return (0); 17 | 18 | ui = 0; 19 | 20 | for (len = 0; b[len] != '\0'; len++) 21 | ; 22 | 23 | for (len--, base_two = 1; len >= 0; len--, base_two *= 2) 24 | { 25 | if (b[len] != '0' && b[len] != '1') 26 | { 27 | return (0); 28 | } 29 | 30 | if (b[len] & 1) 31 | { 32 | ui += base_two; 33 | } 34 | } 35 | 36 | return (ui); 37 | } 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary representation 5 | * of a number. 6 | * @n: unsigned long int. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_binary(unsigned long int n) 11 | { 12 | if (n >> 0) 13 | { 14 | if (n >> 1) 15 | print_binary(n >> 1); 16 | _putchar((n & 1) + '0'); 17 | } 18 | else 19 | { 20 | _putchar('0'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * 6 | * Return: 0 if big endian, 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | unsigned int x; 11 | char *c; 12 | 13 | x = 1; 14 | c = (char *) &x; 15 | 16 | return ((int)*c); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given 5 | * index. 6 | * @n: unsigned long int input. 7 | * @index: index of the bit. 8 | * 9 | * Return: value of the bit. 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (n == 0 && index < 64) 16 | return (0); 17 | 18 | for (i = 0; i <= 63; n >>= 1, i++) 19 | { 20 | if (index == i) 21 | { 22 | return (n & 1); 23 | } 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets the value of a bit to 1. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | *n = (*n | m); 20 | 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a bit to 0. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | 20 | if (*n & m) 21 | *n ^= m; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - returns the number of bits you would 5 | * need to flip to get from one number to another 6 | * @n: number one. 7 | * @m: number two. 8 | * 9 | * Return: number of bits. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned int nbits; 14 | 15 | for (nbits = 0; n || m; n >>= 1, m >>= 1) 16 | { 17 | if ((n & 1) != (m & 1)) 18 | nbits++; 19 | } 20 | 21 | return (nbits); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | unsigned int binary_to_uint(const char *b); 5 | void print_binary(unsigned long int n); 6 | int _putchar(char c); 7 | int get_bit(unsigned long int n, unsigned int index); 8 | int set_bit(unsigned long int *n, unsigned int index); 9 | int clear_bit(unsigned long int *n, unsigned int index); 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 11 | int get_endianness(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - reads a text file and prints the letters 5 | * @filename: filename. 6 | * @letters: numbers of letters printed. 7 | * 8 | * Return: numbers of letters printed. It fails, returns 0. 9 | */ 10 | ssize_t read_textfile(const char *filename, size_t letters) 11 | { 12 | int fd; 13 | ssize_t nrd, nwr; 14 | char *buf; 15 | 16 | if (!filename) 17 | return (0); 18 | 19 | fd = open(filename, O_RDONLY); 20 | 21 | if (fd == -1) 22 | return (0); 23 | 24 | buf = malloc(sizeof(char) * (letters)); 25 | if (!buf) 26 | return (0); 27 | 28 | nrd = read(fd, buf, letters); 29 | nwr = write(STDOUT_FILENO, buf, nrd); 30 | 31 | close(fd); 32 | 33 | free(buf); 34 | 35 | return (nwr); 36 | } 37 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file 5 | * @filename: filename. 6 | * @text_content: content writed in the file. 7 | * 8 | * Return: 1 if it success. -1 if it fails. 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | int nletters; 14 | int rwr; 15 | 16 | if (!filename) 17 | return (-1); 18 | 19 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 20 | 21 | if (fd == -1) 22 | return (-1); 23 | 24 | if (!text_content) 25 | text_content = ""; 26 | 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | 35 | close(fd); 36 | 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text at the end of a file 5 | * @filename: filename. 6 | * @text_content: added content. 7 | * 8 | * Return: 1 if the file exists. -1 if the fails does not exist 9 | * or if it fails. 10 | */ 11 | int append_text_to_file(const char *filename, char *text_content) 12 | { 13 | int fd; 14 | int nletters; 15 | int rwr; 16 | 17 | if (!filename) 18 | return (-1); 19 | 20 | fd = open(filename, O_WRONLY | O_APPEND); 21 | 22 | if (fd == -1) 23 | return (-1); 24 | 25 | if (text_content) 26 | { 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | } 35 | 36 | close(fd); 37 | 38 | return (1); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * error_file - checks if files can be opened. 6 | * @file_from: file_from. 7 | * @file_to: file_to. 8 | * @argv: arguments vector. 9 | * Return: no return. 10 | */ 11 | void error_file(int file_from, int file_to, char *argv[]) 12 | { 13 | if (file_from == -1) 14 | { 15 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 16 | exit(98); 17 | } 18 | if (file_to == -1) 19 | { 20 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]); 21 | exit(99); 22 | } 23 | } 24 | 25 | /** 26 | * main - check the code for ALX students. 27 | * @argc: number of arguments. 28 | * @argv: arguments vector. 29 | * Return: Always 0. 30 | */ 31 | int main(int argc, char *argv[]) 32 | { 33 | int file_from, file_to, err_close; 34 | ssize_t nchars, nwr; 35 | char buf[1024]; 36 | 37 | if (argc != 3) 38 | { 39 | dprintf(STDERR_FILENO, "%s\n", "Usage: cp file_from file_to"); 40 | exit(97); 41 | } 42 | 43 | file_from = open(argv[1], O_RDONLY); 44 | file_to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0664); 45 | error_file(file_from, file_to, argv); 46 | 47 | nchars = 1024; 48 | while (nchars == 1024) 49 | { 50 | nchars = read(file_from, buf, 1024); 51 | if (nchars == -1) 52 | error_file(-1, 0, argv); 53 | nwr = write(file_to, buf, nchars); 54 | if (nwr == -1) 55 | error_file(0, -1, argv); 56 | } 57 | 58 | err_close = close(file_from); 59 | if (err_close == -1) 60 | { 61 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 62 | exit(100); 63 | } 64 | 65 | err_close = close(file_to); 66 | if (err_close == -1) 67 | { 68 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 69 | exit(100); 70 | } 71 | return (0); 72 | } 73 | 74 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all the elements of a 5 | * dlistint_t list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | printf("%d\n", h->n); 25 | count++; 26 | h = h->next; 27 | } 28 | 29 | return (count); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements in 5 | * a double linked list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | count++; 25 | h = h->next; 26 | } 27 | 28 | return (count); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds a new node at the beginning 5 | * of a dlistint_t list 6 | * 7 | * @head: head of the list 8 | * @n: value of the element 9 | * Return: the address of the new element 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | dlistint_t *h; 15 | 16 | new = malloc(sizeof(dlistint_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->prev = NULL; 22 | h = *head; 23 | 24 | if (h != NULL) 25 | { 26 | while (h->prev != NULL) 27 | h = h->prev; 28 | } 29 | 30 | new->next = h; 31 | 32 | if (h != NULL) 33 | h->prev = new; 34 | 35 | *head = new; 36 | 37 | return (new); 38 | } 39 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds a new node at the end 5 | * of a dlistint_t list 6 | * 7 | * @head: head of the list 8 | * @n: value of the element 9 | * Return: the address of the new element 10 | */ 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *h; 14 | dlistint_t *new; 15 | 16 | new = malloc(sizeof(dlistint_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->next = NULL; 22 | 23 | h = *head; 24 | 25 | if (h != NULL) 26 | { 27 | while (h->next != NULL) 28 | h = h->next; 29 | h->next = new; 30 | } 31 | else 32 | { 33 | *head = new; 34 | } 35 | 36 | new->prev = h; 37 | 38 | return (new); 39 | } 40 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a dlistint_t list 5 | * 6 | * @head: head of the list 7 | * Return: no return 8 | */ 9 | void free_dlistint(dlistint_t *head) 10 | { 11 | dlistint_t *tmp; 12 | 13 | if (head != NULL) 14 | while (head->prev != NULL) 15 | head = head->prev; 16 | 17 | while ((tmp = head) != NULL) 18 | { 19 | head = head->next; 20 | free(tmp); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - returns the 5 | * nth node of a dlistint_t linked list 6 | * 7 | * @head: head of the list 8 | * @index: index of the nth node 9 | * Return: nth node 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | 18 | while (head->prev != NULL) 19 | head = head->prev; 20 | 21 | i = 0; 22 | 23 | while (head != NULL) 24 | { 25 | if (i == index) 26 | break; 27 | head = head->next; 28 | i++; 29 | } 30 | 31 | return (head); 32 | } 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all the data (n) 5 | * of a doubly linked list 6 | * 7 | * @head: head of the list 8 | * Return: sum of the data 9 | */ 10 | int sum_dlistint(dlistint_t *head) 11 | { 12 | int sum; 13 | 14 | sum = 0; 15 | 16 | if (head != NULL) 17 | { 18 | while (head->prev != NULL) 19 | head = head->prev; 20 | 21 | while (head != NULL) 22 | { 23 | sum += head->n; 24 | head = head->next; 25 | } 26 | } 27 | 28 | return (sum); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - inserts a new node at 5 | * a given position 6 | * 7 | * @h: head of the list 8 | * @idx: index of the new node 9 | * @n: value of the new node 10 | * Return: the address of the new node, or NULL if it failed 11 | */ 12 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 13 | { 14 | dlistint_t *new; 15 | dlistint_t *head; 16 | unsigned int i; 17 | 18 | new = NULL; 19 | if (idx == 0) 20 | new = add_dnodeint(h, n); 21 | else 22 | { 23 | head = *h; 24 | i = 1; 25 | if (head != NULL) 26 | while (head->prev != NULL) 27 | head = head->prev; 28 | while (head != NULL) 29 | { 30 | if (i == idx) 31 | { 32 | if (head->next == NULL) 33 | new = add_dnodeint_end(h, n); 34 | else 35 | { 36 | new = malloc(sizeof(dlistint_t)); 37 | if (new != NULL) 38 | { 39 | new->n = n; 40 | new->next = head->next; 41 | new->prev = head; 42 | head->next->prev = new; 43 | head->next = new; 44 | } 45 | } 46 | break; 47 | } 48 | head = head->next; 49 | i++; 50 | } 51 | } 52 | 53 | return (new); 54 | } 55 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - deletes the node at index of a 5 | * dlistint_t linked list 6 | * 7 | * @head: head of the list 8 | * @index: index of the new node 9 | * Return: 1 if it succeeded, -1 if it failed 10 | */ 11 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 12 | { 13 | dlistint_t *h1; 14 | dlistint_t *h2; 15 | unsigned int i; 16 | 17 | h1 = *head; 18 | 19 | if (h1 != NULL) 20 | while (h1->prev != NULL) 21 | h1 = h1->prev; 22 | 23 | i = 0; 24 | 25 | while (h1 != NULL) 26 | { 27 | if (i == index) 28 | { 29 | if (i == 0) 30 | { 31 | *head = h1->next; 32 | if (*head != NULL) 33 | (*head)->prev = NULL; 34 | } 35 | else 36 | { 37 | h2->next = h1->next; 38 | 39 | if (h1->next != NULL) 40 | h1->next->prev = h2; 41 | } 42 | 43 | free(h1); 44 | return (1); 45 | } 46 | h2 = h1; 47 | h1 = h1->next; 48 | i++; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * for ALX project 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always EXIT_SUCCESS; 9 | */ 10 | int main(void) 11 | { 12 | printf("%d\n", _strlen("My Dyn lib")); 13 | return (EXIT_SUCCESS); 14 | } -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - function commute srtings 5 | * @dest: param pointer to a char 6 | * @src: param pointer to a char 7 | * Return: return value of dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | while (src[j] != '\0') 23 | { 24 | dest[i] = src[j]; 25 | j++; 26 | i++; 27 | } 28 | 29 | dest[i] = '\0'; 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc *.c -c -fPIC 3 | gcc *.o -shared -o liball.so 4 | export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH 5 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int add(int a, int b); 4 | int sub(int a, int b); 5 | int mul(int a, int b); 6 | int div(int a, int b); 7 | int mod(int a, int b); 8 | 9 | int add(int a, int b) 10 | { 11 | return a + b; 12 | } 13 | 14 | int sub(int a, int b) 15 | { 16 | return a - b; 17 | } 18 | int mul(int a, int b) 19 | { 20 | return a * b; 21 | } 22 | int div(int a, int b) 23 | { 24 | return a / b; 25 | } 26 | int mod(int a, int b) 27 | { 28 | return a % b; 29 | } -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes used from src. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer. 5 | * @s: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | if (*(s + count) == '-') 17 | pn *= -1; 18 | 19 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 20 | { 21 | if (size > 0) 22 | m *= 10; 23 | size++; 24 | } 25 | count++; 26 | } 27 | 28 | for (i = count - size; i < count; i++) 29 | { 30 | oi = oi + ((*(s + i) - 48) * m); 31 | m /= 10; 32 | } 33 | return (oi * pn); 34 | } 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp/ https://raw.github.com/Aysuarex/alx-low_level_programming/master/0x18-dynamic_libraries/nrandom.so 3 | export LD_PRELOAD=/tmp/nrandom.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input stringeturn. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - prints 1 or 0 depending on input 5 | * @c: first parameter 6 | * 7 | * Description: prints all lowercase letters 8 | * Return: Always(0). 9 | */ 10 | 11 | int _islower(int c) 12 | { 13 | return (c >= 'a' && c <= 'z'); 14 | } 15 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, 5 | * @str: pointer to the string to print 6 | * Return: void 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i]) 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equals, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - Check if character is a alphabet character. 4 | * @c: type int character 5 | * Return: 1 if letter, lowercase or uppercase, and 0 otherwise 6 | */ 7 | 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 11 | return (1); 12 | 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - compute the absolute value of an integer 4 | * @n: int type number 5 | * Return: absolute value of @n 6 | */ 7 | int _abs(int n) 8 | { 9 | if (n < 0) 10 | { 11 | return (n * -1); 12 | } 13 | else 14 | { 15 | return (n); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcpy - copies the string pointed to by src, 5 | * including the terminating null byte, to the 6 | * buffer pointed to by dest. 7 | * @dest: destination. 8 | * @src: source. 9 | * Return: the pointer to dest. 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int count = 0; 14 | 15 | while (count >= 0) 16 | { 17 | *(dest + count) = *(src + count); 18 | if (*(src + count) == '\0') 19 | break; 20 | count++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C - Dynamic libraries 2 | 3 | In this project, learned about using dynamic libraries in C, including how to create 4 | them and how to use them with `$LD_LIBRARY_PATH`, `nm`, `ldd`, and `ldconfig`. 5 | 6 | ## Tasks :page_with_curl: 7 | 8 | * **0. A library is not a luxury but one of the necessities of life** 9 | * [libdynamic.so](./libdynamic.so): C dynamic library containing all the functions 10 | listed below: 11 | * `int _putchar(char c);` 12 | * `int _islower(int c);` 13 | * `int _isalpha(int c);` 14 | * `int _abs(int n);` 15 | * `int _isupper(int c);` 16 | * `int _isdigit(int c);` 17 | * `int _strlen(char *s);` 18 | * `void _puts(char *s);` 19 | * `char *_strcpy(char *dest, char *src);` 20 | * `int _atoi(char *s);` 21 | * `char *_strcat(char *dest, char *src);` 22 | * `char *_strncat(char *dest, char *src, int n);` 23 | * `char *_strncpy(char *dest, char *src, int n);` 24 | * `int _strcmp(char *s1, char *s2);` 25 | * `char *_memset(char *s, char b, unsigned int n);` 26 | * `char *_memcpy(char *dest, char *src, unsigned int n);` 27 | * `char *_strchr(char *s, char c);` 28 | * `unsigned int _strspn(char *s, char *accept);` 29 | * `char *_strpbrk(char *s, char *accept);` 30 | * `char *_strstr(char *haystack, char *needle);` 31 | 32 | * [main.h](./main.h): Header file containing the prototypes of all functions 33 | included in `libdynamic.so`. 34 | 35 | * **1. Without libraries what have we? We have no past and no future** 36 | * [1-create_dynamic_lib.sh](./1-create_dynamic_lib.sh): Bash script that creates a 37 | dynamic library called `liball.so` from all the `.c` files in the current directory. 38 | 39 | * **2. Let's call C functions from Python** 40 | * [100-operations.so](./100-operations.so): C dynamic library containing basic C 41 | mathematical operation functions that can be called from Python. 42 | * Includes: 43 | * `int add(int a, int b);` 44 | * `int sub(int a, int b);` 45 | * `int mul(int a, int b);` 46 | * `int div(int a, int b);` 47 | * `int mod(int a, int b);` 48 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/nrandom.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Aysuarex/alx-low_level_programming/01b36d7f8bc1fcfd9282c8162c3c679197a9008f/0x18-dynamic_libraries/nrandom.so -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creates a hash table with a given size 5 | * 6 | * @size: size of the hash table 7 | * Return: the created hash table, or NULL if function fails 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *table; 12 | hash_node_t **array; 13 | unsigned long int i; 14 | 15 | table = malloc(sizeof(hash_table_t)); 16 | if (table == NULL) 17 | return (NULL); 18 | 19 | array = malloc(sizeof(hash_node_t *) * size); 20 | if (array == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | array[i] = NULL; 25 | 26 | table->array = array; 27 | table->size = size; 28 | 29 | return (table); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - function that generates a random number through 5 | * a string given (hash number) 6 | * 7 | * @str: string 8 | * Return: hash number 9 | */ 10 | unsigned long int hash_djb2(const unsigned char *str) 11 | { 12 | unsigned long int hash; 13 | int c; 14 | 15 | hash = 5381; 16 | while ((c = *str++)) 17 | { 18 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 19 | } 20 | return (hash); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - function that generates a distributed index 5 | * according to a given hash 6 | * 7 | * @key: key passed 8 | * @size: size of the hash tables 9 | * Return: key index 10 | */ 11 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 12 | { 13 | if (key == NULL || size == 0) 14 | return (0); 15 | return (hash_djb2(key) % size); 16 | } 17 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * hash_table_set - adds an element to the hash table 7 | * @ht: The hash table 8 | * @key: The key of the new element 9 | * @value: The value of the new element 10 | * 11 | * Return: 1 on success, 0 on failure 12 | */ 13 | int 14 | hash_table_set(hash_table_t *ht, const char *key, const char *value) 15 | { 16 | unsigned long int index = 0; 17 | hash_node_t *new_hash_node = NULL; 18 | hash_node_t *tmp = NULL; 19 | 20 | if (!ht || !key || !(*key) || !value) 21 | return (0); 22 | 23 | index = key_index((unsigned char *)key, ht->size); 24 | tmp = ht->array[index]; 25 | 26 | /* check if key exists */ 27 | while (tmp && strcmp(tmp->key, key) != 0) 28 | tmp = tmp->next; 29 | 30 | /* update value if key already exists */ 31 | if (tmp) 32 | { 33 | free(tmp->value); 34 | tmp->value = strdup(value); 35 | return (1); 36 | } 37 | 38 | /* add new node if key not found */ 39 | 40 | new_hash_node = malloc(sizeof(*new_hash_node)); 41 | if (!new_hash_node) 42 | return (0); 43 | 44 | new_hash_node->key = strdup(key); 45 | new_hash_node->value = strdup(value); 46 | 47 | new_hash_node->next = ht->array[index]; 48 | ht->array[index] = new_hash_node; 49 | 50 | return (1); 51 | } 52 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - retrieves a value associated with a key 5 | * 6 | * @ht: pointer to the hash table 7 | * @key: key of the hash 8 | * Return: value of the hash. 9 | */ 10 | char *hash_table_get(const hash_table_t *ht, const char *key) 11 | { 12 | unsigned long int k_index; 13 | hash_node_t *tmp; 14 | 15 | if (ht == NULL) 16 | return (NULL); 17 | 18 | if (key == NULL || *key == '\0') 19 | return (NULL); 20 | 21 | k_index = key_index((unsigned char *)key, ht->size); 22 | 23 | tmp = ht->array[k_index]; 24 | 25 | while (tmp != NULL) 26 | { 27 | if (strcmp(tmp->key, key) == 0) 28 | return (tmp->value); 29 | tmp = tmp->next; 30 | } 31 | 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - prints the keys and values of the hash table 5 | * 6 | * @ht: pointer to the hash table 7 | * Return: no return 8 | */ 9 | void hash_table_print(const hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *tmp; 13 | char *sep; 14 | 15 | if (ht == NULL) 16 | return; 17 | 18 | printf("{"); 19 | sep = ""; 20 | 21 | for (i = 0; i < ht->size; i++) 22 | { 23 | tmp = ht->array[i]; 24 | while (tmp != NULL) 25 | { 26 | printf("%s'%s': '%s'", sep, tmp->key, tmp->value); 27 | sep = ", "; 28 | tmp = tmp->next; 29 | } 30 | } 31 | printf("}\n"); 32 | } 33 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - deletes a hash table 5 | * 6 | * @ht: pointer to the hash table 7 | * Return: no return 8 | */ 9 | void hash_table_delete(hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *tmp1; 13 | hash_node_t *tmp2; 14 | 15 | if (ht == NULL) 16 | return; 17 | 18 | for (i = 0; i < ht->size; i++) 19 | { 20 | tmp1 = ht->array[i]; 21 | while ((tmp2 = tmp1) != NULL) 22 | { 23 | tmp1 = tmp1->next; 24 | free(tmp2->key); 25 | free(tmp2->value); 26 | free(tmp2); 27 | } 28 | } 29 | free(ht->array); 30 | free(ht); 31 | } 32 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | all: $(SRC) 4 | $(CC) $(SRC) -o school 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean: 11 | $(RM) *~ $(NAME) 12 | 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: fclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Return the perimiter of an island. 7 | 8 | The grid represents water by 0 and land by 1. 9 | 10 | Args: 11 | grid (list): A list of list of integers representing an island. 12 | Returns: 13 | The perimeter of the island defined in grid. 14 | """ 15 | width = len(grid[0]) 16 | height = len(grid) 17 | edges = 0 18 | size = 0 19 | 20 | for i in range(height): 21 | for j in range(width): 22 | if grid[i][j] == 1: 23 | size += 1 24 | if (j > 0 and grid[i][j - 1] == 1): 25 | edges += 1 26 | if (i > 0 and grid[i - 1][j] == 1): 27 | edges += 1 28 | return size * 4 - edges * 2 29 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_school 5 | * 6 | * Return: Nothing 7 | */ 8 | void print_school(void) 9 | { 10 | printf("j#0000000000000000000000000000000000000\n"); 11 | printf("j#000000000000000000@Q**g00000000000000\n"); 12 | printf("j#0000000000000000*]++]4000000000000000\n"); 13 | printf("j#000000000000000k]++]++*N#000000000000\n"); 14 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 15 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 16 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 17 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 18 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 19 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 20 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 21 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 22 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 23 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 24 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 25 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 26 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 27 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 28 | printf("j#00000000000w]++++]*0##000000000000000\n"); 29 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 30 | printf("j#00000000000000000we]+]Q00000000000000\n"); 31 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 32 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 33 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 34 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-makefiles/tests/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 5 | * integers using the Linear search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: Always EXIT_SUCCESS 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < (int)size; i++) 20 | { 21 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 22 | if (value == array[i]) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - Searches for a value in a sorted array 5 | * of integers using binary search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @size: The number of elements in the array. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the array is NULL, -1. 11 | * Otherwise, the index where the value is located. 12 | * 13 | * Description: Prints the [sub]array being searched after each change. 14 | */ 15 | int binary_search(int *array, size_t size, int value) 16 | { 17 | size_t i, left, right; 18 | 19 | if (array == NULL) 20 | return (-1); 21 | 22 | for (left = 0, right = size - 1; right >= left;) 23 | { 24 | printf("Searching in array: "); 25 | for (i = left; i < right; i++) 26 | printf("%d, ", array[i]); 27 | printf("%d\n", array[i]); 28 | 29 | i = left + (right - left) / 2; 30 | if (array[i] == value) 31 | return (i); 32 | if (array[i] > value) 33 | right = i - 1; 34 | else 35 | left = i + 1; 36 | } 37 | 38 | return (-1); 39 | } 40 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary_recursive.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search_recursive - Searches recursively for a value in a sorted 5 | * array of integers using binary search. 6 | * @array: A pointer to the first element of the [sub]array to search. 7 | * @left: The starting index of the [sub]array to search. 8 | * @right: The ending index of the [sub]array to search. 9 | * @value: The value to search for. 10 | * 11 | * Return: If the value is not present, -1. 12 | * Otherwise, the index where the value is located. 13 | * 14 | * Description: Prints the [sub]array being searched after each change. 15 | */ 16 | int binary_search_recursive(int *array, size_t left, size_t right, int value) 17 | { 18 | size_t i; 19 | 20 | if (right < left) 21 | return (-1); 22 | 23 | printf("Searching in array: "); 24 | for (i = left; i < right; i++) 25 | printf("%d, ", array[i]); 26 | printf("%d\n", array[i]); 27 | 28 | i = left + (right - left) / 2; 29 | if (array[i] == value) 30 | return (i); 31 | if (array[i] > value) 32 | return (binary_search_recursive(array, left, i - 1, value)); 33 | return (binary_search_recursive(array, i + 1, right, value)); 34 | } 35 | 36 | /** 37 | * binary_search - Searches for a value in a sorted array 38 | * of integers using binary search. 39 | * @array: A pointer to the first element of the array to search. 40 | * @size: The number of elements in the array. 41 | * @value: The value to search for. 42 | * 43 | * Return: If the value is not present or the array is NULL, -1. 44 | * Otherwise, the index where the value is located. 45 | * 46 | * Description: Prints the [sub]array being searched after each change. 47 | */ 48 | int binary_search(int *array, size_t size, int value) 49 | { 50 | if (array == NULL) 51 | return (-1); 52 | 53 | return (binary_search_recursive(array, 0, size - 1, value)); 54 | } 55 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | int index, m, k, prev; 16 | 17 | if (array == NULL || size == 0) 18 | return (-1); 19 | 20 | m = (int)sqrt((double)size); 21 | k = 0; 22 | prev = index = 0; 23 | 24 | do { 25 | printf("Value checked array[%d] = [%d]\n", index, array[index]); 26 | 27 | if (array[index] == value) 28 | return (index); 29 | k++; 30 | prev = index; 31 | index = k * m; 32 | } while (index < (int)size && array[index] < value); 33 | 34 | printf("Value found between indexes [%d] and [%d]\n", prev, index); 35 | 36 | for (; prev <= index && prev < (int)size; prev++) 37 | { 38 | printf("Value checked array[%d] = [%d]\n", prev, array[prev]); 39 | if (array[prev] == value) 40 | return (prev); 41 | } 42 | 43 | return (-1); 44 | } 45 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in an array of 5 | * integers using the Interpolation search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: index of the number 11 | */ 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t pos, low, high; 15 | double f; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | low = 0; 21 | high = size - 1; 22 | 23 | while (size) 24 | { 25 | f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]); 26 | pos = (size_t)(low + f); 27 | printf("Value checked array[%d]", (int)pos); 28 | 29 | if (pos >= size) 30 | { 31 | printf(" is out of range\n"); 32 | break; 33 | } 34 | else 35 | { 36 | printf(" = [%d]\n", array[pos]); 37 | } 38 | 39 | if (array[pos] == value) 40 | return ((int)pos); 41 | 42 | if (array[pos] < value) 43 | low = pos + 1; 44 | else 45 | high = pos - 1; 46 | 47 | if (low == high) 48 | break; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * _binary_search - Searches for a value in a sorted array 5 | * of integers using binary search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @left: The starting index of the [sub]array to search. 8 | * @right: The ending index of the [sub]array to search. 9 | * @value: The value to search for. 10 | * 11 | * Return: If the value is not present or the array is NULL, -1. 12 | * Otherwise, the index where the value is located. 13 | * 14 | * Description: Prints the [sub]array being searched after each change. 15 | */ 16 | int _binary_search(int *array, size_t left, size_t right, int value) 17 | { 18 | size_t i; 19 | 20 | if (array == NULL) 21 | return (-1); 22 | 23 | while (right >= left) 24 | { 25 | printf("Searching in array: "); 26 | for (i = left; i < right; i++) 27 | printf("%d, ", array[i]); 28 | printf("%d\n", array[i]); 29 | 30 | i = left + (right - left) / 2; 31 | if (array[i] == value) 32 | return (i); 33 | if (array[i] > value) 34 | right = i - 1; 35 | else 36 | left = i + 1; 37 | } 38 | 39 | return (-1); 40 | } 41 | 42 | /** 43 | * exponential_search - Searches for a value in a sorted array 44 | * of integers using exponential search. 45 | * @array: A pointer to the first element of the array to search. 46 | * @size: The number of elements in the array. 47 | * @value: The value to search for. 48 | * 49 | * Return: If the value is not present or the array is NULL, -1. 50 | * Otherwise, the index where the value is located. 51 | * 52 | * Description: Prints a value every time it is compared in the array. 53 | */ 54 | int exponential_search(int *array, size_t size, int value) 55 | { 56 | size_t i = 0, right; 57 | 58 | if (array == NULL) 59 | return (-1); 60 | 61 | if (array[0] != value) 62 | { 63 | for (i = 1; i < size && array[i] <= value; i = i * 2) 64 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 65 | } 66 | 67 | right = i < size ? i : size - 1; 68 | printf("Value found between indexes [%ld] and [%ld]\n", i / 2, right); 69 | return (_binary_search(array, i / 2, right, value)); 70 | } 71 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * rec_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int rec_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | { 33 | if (half > 0) 34 | return (rec_search(array, half + 1, value)); 35 | return ((int)half); 36 | } 37 | 38 | if (value < array[half]) 39 | return (rec_search(array, half + 1, value)); 40 | 41 | half++; 42 | return (rec_search(array + half, size - half, value) + half); 43 | } 44 | 45 | /** 46 | * advanced_binary - calls to rec_search to return 47 | * the index of the number 48 | * 49 | * @array: input array 50 | * @size: size of the array 51 | * @value: value to search in 52 | * Return: index of the number 53 | */ 54 | int advanced_binary(int *array, size_t size, int value) 55 | { 56 | int index; 57 | 58 | index = rec_search(array, size, value); 59 | 60 | if (index >= 0 && array[index] != value) 61 | return (-1); 62 | 63 | return (index); 64 | } 65 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @list: input list 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | listint_t *jump_list(listint_t *list, size_t size, int value) 14 | { 15 | size_t index, k, m; 16 | listint_t *prev; 17 | 18 | if (list == NULL || size == 0) 19 | return (NULL); 20 | 21 | m = (size_t)sqrt((double)size); 22 | index = 0; 23 | k = 0; 24 | 25 | do { 26 | prev = list; 27 | k++; 28 | index = k * m; 29 | 30 | while (list->next && list->index < index) 31 | list = list->next; 32 | 33 | if (list->next == NULL && index != list->index) 34 | index = list->index; 35 | 36 | printf("Value checked at index [%d] = [%d]\n", (int)index, list->n); 37 | 38 | } while (index < size && list->next && list->n < value); 39 | 40 | printf("Value found between indexes "); 41 | printf("[%d] and [%d]\n", (int)prev->index, (int)list->index); 42 | 43 | for (; prev && prev->index <= list->index; prev = prev->next) 44 | { 45 | printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n); 46 | if (prev->n == value) 47 | return (prev); 48 | } 49 | 50 | return (NULL); 51 | } 52 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - searches for a value in a skip list 5 | * 6 | * @list: input list 7 | * @value: value to search in 8 | * Return: index of the number 9 | */ 10 | skiplist_t *linear_skip(skiplist_t *list, int value) 11 | { 12 | skiplist_t *go; 13 | 14 | if (list == NULL) 15 | return (NULL); 16 | 17 | go = list; 18 | 19 | do { 20 | list = go; 21 | go = go->express; 22 | printf("Value checked at index "); 23 | printf("[%d] = [%d]\n", (int)go->index, go->n); 24 | } while (go->express && go->n < value); 25 | 26 | if (go->express == NULL) 27 | { 28 | list = go; 29 | while (go->next) 30 | go = go->next; 31 | } 32 | 33 | printf("Value found between indexes "); 34 | printf("[%d] and [%d]\n", (int)list->index, (int)go->index); 35 | 36 | while (list != go->next) 37 | { 38 | printf("Value checked at index [%d] = [%d]\n", (int)list->index, list->n); 39 | if (list->n == value) 40 | return (list); 41 | list = list->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/create_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | void free_list(listint_t *list); 5 | 6 | /** 7 | * create_list - Creates a single linked list 8 | * 9 | * @array: Pointer to the array to use to fill the list 10 | * @size: Size of the array 11 | * 12 | * Return: A pointer to the head of the created list (NULL on failure) 13 | */ 14 | listint_t *create_list(int *array, size_t size) 15 | { 16 | listint_t *list; 17 | listint_t *node; 18 | 19 | list = NULL; 20 | while (array && size--) 21 | { 22 | node = malloc(sizeof(*node)); 23 | if (!node) 24 | { 25 | free_list(list); 26 | return (NULL); 27 | } 28 | node->n = array[size]; 29 | node->index = size; 30 | node->next = list; 31 | list = node; 32 | } 33 | return (list); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | /** 5 | * free_list - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_list(listint_t *list) 10 | { 11 | listint_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_list(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | /** 5 | * print_list - Prints the content of a listint_t 6 | * 7 | * @list: Pointer to the head of the list 8 | */ 9 | void print_list(const listint_t *list) 10 | { 11 | printf("List :\n"); 12 | while (list) 13 | { 14 | printf("Index[%lu] = [%d]\n", list->index, list->n); 15 | list = list->next; 16 | } 17 | printf("\n"); 18 | } 19 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SORTING_ALGOS_H_ 2 | #define _SORTING_ALGOS_H_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * 10 | * @n: Integer 11 | * @index: Index of the node in the list 12 | * @next: Pointer to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for ALX project 16 | */ 17 | typedef struct listint_s 18 | { 19 | int n; 20 | size_t index; 21 | struct listint_s *next; 22 | } listint_t; 23 | 24 | /** 25 | * struct skiplist_s - Singly linked list with an express lane 26 | * 27 | * @n: Integer 28 | * @index: Index of the node in the list 29 | * @next: Pointer to the next node 30 | * @express: Pointer to the next node in the express lane 31 | * 32 | * Description: singly linked list node structure with an express lane 33 | * for Holberton project 34 | */ 35 | typedef struct skiplist_s 36 | { 37 | int n; 38 | size_t index; 39 | struct skiplist_s *next; 40 | struct skiplist_s *express; 41 | } skiplist_t; 42 | 43 | int linear_search(int *array, size_t size, int value); 44 | int binary_search(int *array, size_t size, int value); 45 | int jump_search(int *array, size_t size, int value); 46 | int interpolation_search(int *array, size_t size, int value); 47 | int exponential_search(int *array, size_t size, int value); 48 | int advanced_binary(int *array, size_t size, int value); 49 | listint_t *jump_list(listint_t *list, size_t size, int value); 50 | skiplist_t *linear_skip(skiplist_t *list, int value); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/create_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | void free_skiplist(skiplist_t *list); 6 | 7 | /** 8 | * init_express - Initializes the express lane of the linked list 9 | * 10 | * @list: Pointer to the head node of the list 11 | * @size: Number of nodes in the list 12 | */ 13 | void init_express(skiplist_t *list, size_t size) 14 | { 15 | const size_t step = sqrt(size); 16 | size_t i; 17 | skiplist_t *save; 18 | 19 | for (save = list, i = 0; i < size; ++i, list = list->next) 20 | { 21 | if (i % step == 0) 22 | { 23 | save->express = list; 24 | save = list; 25 | } 26 | } 27 | } 28 | 29 | /** 30 | * create_skiplist - Create a single linked list 31 | * 32 | * @array: Pointer to the array used to fill the list 33 | * @size: Size of the array 34 | * 35 | * Return: A pointer to the head of the created list (NULL on failure) 36 | */ 37 | skiplist_t *create_skiplist(int *array, size_t size) 38 | { 39 | skiplist_t *list; 40 | skiplist_t *node; 41 | size_t save_size; 42 | 43 | list = NULL; 44 | save_size = size; 45 | while (array && size--) 46 | { 47 | node = malloc(sizeof(*node)); 48 | if (!node) 49 | { 50 | free_skiplist(list); 51 | return (NULL); 52 | } 53 | node->n = array[size]; 54 | node->index = size; 55 | node->express = NULL; 56 | node->next = list; 57 | list = node; 58 | } 59 | init_express(list, save_size); 60 | return (list); 61 | } 62 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/free_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | /** 5 | * free_skiplist - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_skiplist(skiplist_t *list) 10 | { 11 | skiplist_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_skiplist(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/print_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * print_skiplist - dump the content of a skiplist_t 7 | * 8 | * @list: Pointer to the head of the list 9 | * 10 | * Return: void 11 | */ 12 | void print_skiplist(const skiplist_t *list) 13 | { 14 | const skiplist_t *node; 15 | 16 | printf("List :\n"); 17 | for (node = list; node; node = node->next) 18 | { 19 | printf("Index[%lu] = [%d]\n", node->index, node->n); 20 | } 21 | printf("\nExpress lane :\n"); 22 | for (node = list; node; node = node->express) 23 | { 24 | printf("Index[%lu] = [%d]\n", node->index, node->n); 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); 18 | printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 6, jump_search(array, size, 6)); 18 | printf("Found %d at index: %d\n\n", 1, jump_search(array, size, 1)); 19 | printf("Found %d at index: %d\n", 999, jump_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, interpolation_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 7, interpolation_search(array, size, 7)); 19 | printf("Found %d at index: %d\n", 999, interpolation_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 54, 61, 62, 76, 99 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 62, exponential_search(array, size, 62)); 18 | printf("Found %d at index: %d\n\n", 3, exponential_search(array, size, 3)); 19 | printf("Found %d at index: %d\n", 999, exponential_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/104-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 8, advanced_binary(array, size, 8)); 18 | printf("Found %d at index: %d\n\n", 5, advanced_binary(array, size, 5)); 19 | printf("Found %d at index: %d\n", 999, advanced_binary(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/105-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | listint_t *create_list(int *array, size_t size); 6 | void print_list(const listint_t *list); 7 | void free_list(listint_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | listint_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_list(array, size); 23 | print_list(list); 24 | 25 | res = jump_list(list, size, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = jump_list(list, size, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = jump_list(list, size, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_list(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/106-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | skiplist_t *create_skiplist(int *array, size_t size); 6 | void print_skiplist(const skiplist_t *list); 7 | void free_skiplist(skiplist_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | skiplist_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_skiplist(array, size); 23 | print_skiplist(list); 24 | 25 | res = linear_skip(list, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = linear_skip(list, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = linear_skip(list, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_skiplist(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | --------------------------------------------------------------------------------