├── .gitignore ├── .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 ├── 10-print_comb2.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt ├── 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-holberton.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 ├── _putchar.c └── holberton.h ├── 0x03-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 ├── _putchar.c └── holberton.h ├── 0x04-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 ├── _putchar.c └── holberton.h ├── 0x05-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-print_number.c ├── 101-magic.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── 8-rot13.c ├── README.md ├── _putchar.c ├── holberton.h └── magic.c ├── 0x06-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── 9-set_string.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x07-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 7-is_palindrome.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x08-static_libraries ├── 0-isupper.o ├── 0-memset.o ├── 0-strcat.o ├── 1-isdigit.o ├── 1-memcpy.o ├── 1-strncat.o ├── 100-atoi.o ├── 2-strchr.o ├── 2-strlen.o ├── 2-strncpy.o ├── 3-islower.o ├── 3-puts.o ├── 3-strcmp.o ├── 3-strspn.o ├── 4-isalpha.o ├── 4-strpbrk.o ├── 5-strstr.o ├── 6-abs.o ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── holberton.h ├── libholberton.a └── quote ├── 0x09-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x0A-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── 5-argstostr.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x0B-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x0C-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0D-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0E-function_pointers ├── 0-print_name.c ├── 1-array_iterator.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 ├── 0x0F-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x11-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── lists.h ├── 0x12-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 ├── 0x13-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 ├── _putchar.c └── holberton.h ├── 0x14-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── holberton.h ├── 0x16-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 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 ├── 0x17-dynamic_libraries ├── 0-isupper.o ├── 0-memset.o ├── 0-strcat.o ├── 1-create_dynamic_lib.sh ├── 1-isdigit.o ├── 1-memcpy.o ├── 1-strncat.o ├── 100-atoi.o ├── 2-strchr.o ├── 2-strlen.o ├── 2-strncpy.o ├── 3-islower.o ├── 3-puts.o ├── 3-strcmp.o ├── 3-strspn.o ├── 4-isalpha.o ├── 4-strpbrk.o ├── 5-strstr.o ├── 6-abs.o ├── 9-strcpy.o ├── README.md ├── _putchar.o ├── holberton.h └── libholberton.so ├── 0x19-hash_tables ├── .gitignore ├── 0-hash_table_create.c ├── 1-djb2.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 ├── 0x1A-sorting_algorithms ├── 0-O ├── 0-bubble_sort.c ├── 1-O ├── 1-insertion_sort_list.c ├── 10-O ├── 100-shell_sort.c ├── 103-O ├── 104-O ├── 104-heap_sort.c ├── 11-O ├── 12-O ├── 13-O ├── 14-O ├── 15-O ├── 16-O ├── 17-O ├── 18-O ├── 19-O ├── 2-O ├── 2-selection_sort.c ├── 20-O ├── 21-O ├── 3-O ├── 3-quick_sort.c ├── 4-O ├── 5-O ├── 6-O ├── 7-O ├── 8-O ├── 9-O ├── README.md ├── print_array.c ├── print_list.c └── sort.h ├── 0x1B-makefiles ├── 0-Makefile ├── 1-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── README.md ├── holberton.c ├── m.h └── main.c ├── 0x1C-binary_trees ├── 0-binary_tree_node.c ├── 1-binary_tree_insert_left.c ├── 10-binary_tree_depth.c ├── 100-binary_trees_ancestor.c ├── 101-binary_tree_levelorder.c ├── 103-binary_tree_rotate_left.c ├── 104-binary_tree_rotate_right.c ├── 11-binary_tree_size.c ├── 110-binary_tree_is_bst.c ├── 111-bst_insert.c ├── 112-array_to_bst.c ├── 113-bst_search.c ├── 114-bst_remove.c ├── 115-O ├── 12-binary_tree_leaves.c ├── 120-binary_tree_is_avl.c ├── 13-binary_tree_nodes.c ├── 14-binary_tree_balance.c ├── 15-binary_tree_is_full.c ├── 16-binary_tree_is_perfect.c ├── 17-binary_tree_sibling.c ├── 18-binary_tree_uncle.c ├── 2-binary_tree_insert_right.c ├── 3-binary_tree_delete.c ├── 4-binary_tree_is_leaf.c ├── 5-binary_tree_is_root.c ├── 6-binary_tree_preorder.c ├── 7-binary_tree_inorder.c ├── 8-binary_tree_postorder.c ├── 9-binary_tree_height.c ├── README.md ├── binary_tree_print.c ├── binary_trees.h └── mains │ ├── 0-main.c │ ├── 1-main.c │ ├── 10-main.c │ ├── 100-main.c │ ├── 101-main.c │ ├── 11-main.c │ ├── 110-main.c │ ├── 111-main.c │ ├── 112-main.c │ ├── 113-main.c │ ├── 114-main.c │ ├── 12-main.c │ ├── 120-main.c │ ├── 13-main.c │ ├── 14-main.c │ ├── 16-main.c │ ├── 17-main.c │ ├── 18-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ ├── 6-main.c │ ├── 7-main.c │ ├── 8-main.c │ └── 9-main.c ├── 0x1D-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md ├── mains │ ├── 0-main.c │ └── 1-main.c └── search_algos.h └── 0x1E-huffman_rb_trees ├── README.md ├── binary_tree_print.c ├── heap ├── binary_tree_node.c ├── heap.h ├── heap_create.c ├── heap_delete.c ├── heap_extract.c ├── heap_insert.c └── helpers.c ├── huffman.h ├── huffman_priority_queue.c ├── mains ├── 0-main.c ├── 1-main.c ├── 2-main.c ├── 3-main.c ├── 4-main.c ├── 5-main.c └── 6-main.c ├── rb_tree_print.c ├── rb_trees.h └── symbol.c /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | a.out 3 | *main.c -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "0x18-stacks_queues_lifo_fifo/monty"] 2 | path = 0x18-stacks_queues_lifo_fifo/monty 3 | url = https://github.com/JennieChu/monty.git 4 | -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE >> c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $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 | #include 4 | /** 5 | * main - main block 6 | * Return: 1 7 | */ 8 | int main(void) 9 | { 10 | int s = strlen("and that piece of art is useful - Dora Korpar, 2015-10-19\n"); 11 | 12 | write(2, 13 | "and that piece of art is useful - Dora Korpar, 2015-10-19\n", 14 | s); 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $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 | * main - main block 4 | * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | puts("\"Programming is like building a multilingual puzzle"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | printf("with proper grammar, but the outcome is a piece of art,\n"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | printf("Size of a char: %i byte(s)\n", sizeof(char)); 9 | printf("Size of an int: %i byte(s)\n", sizeof(int)); 10 | printf("Size of a long int: %i byte(s)\n", sizeof(long int)); 11 | printf("Size of a long long int: %i byte(s)\n", sizeof(long long int)); 12 | printf("Size of a float: %i byte(s)\n", sizeof(float)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | # Day One of C! Hello, World -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - main block 6 | * Description: Get a random number and print the number 7 | * and if it is positive, negative, or zero 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | 17 | if (n > 0) 18 | printf("%i is positive\n", n); 19 | else if (n < 0) 20 | printf("%i is negative\n", n); 21 | else 22 | printf("%i is zero\n", n); 23 | 24 | return (0); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /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/10-print_comb2.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print numbers from 0 to 99. 5 | * Numbers must be separated by commas followed by a space. 6 | * You can only use `putchar` to print to console. 7 | * You can only use `putchar` up to 5 times. 8 | * You are not allowed to use variables of type `char`. 9 | * Return: 0 10 | */ 11 | int main(void) 12 | { 13 | int i, j, k; 14 | 15 | for (i = 0; i < 100; i++) 16 | { 17 | j = i / 10; 18 | k = i % 10; 19 | 20 | putchar(j + '0'); 21 | putchar(k + '0'); 22 | 23 | if (i < 99) 24 | { 25 | putchar(44); 26 | putchar(32); 27 | } 28 | } 29 | putchar('\n'); 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /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/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: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x01-variables_if_else_while/4-print_alphabt -------------------------------------------------------------------------------- /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-holberton.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * main - Print "Holberton" followed by a new line. 4 | * Description: You are not allowed to include standard libraries. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char holberton[] = "Holberton"; 10 | int i = 0; 11 | 12 | while (holberton[i] != '\0') 13 | { 14 | _putchar(holberton[i]); 15 | i++; 16 | } 17 | _putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_alphabet - Print alphabet in lowercase using only `_putchar` 4 | * Return: 0 5 | */ 6 | void print_alphabet(void) 7 | { 8 | char c = 'a'; 9 | 10 | while (c <= 'z') 11 | { 12 | _putchar(c); 13 | c++; 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * add - Add two integers 5 | * @x: int type number 6 | * @y: int type number 7 | * Return: result of addition 8 | */ 9 | int add(int x, int y) 10 | { 11 | return (x + y); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_times_table - Print the `n` times table, starting with 0. 5 | * Description: If `n` is greater than 15 or less than 0, print nothing. 6 | * @n: int type number 7 | */ 8 | void print_times_table(int n) 9 | { 10 | int x = 0, y, z; 11 | 12 | if (n > 15 || n < 0) 13 | return; 14 | while (x <= n) 15 | { 16 | for (y = 0; y <= n; y++) 17 | { 18 | z = x * y; 19 | if (z > 99) 20 | { 21 | _putchar(z / 100 + '0'); 22 | _putchar((z / 10 % 10) + '0'); 23 | _putchar(z % 10 + '0'); 24 | } 25 | else if (z > 9) 26 | { 27 | _putchar(' '); 28 | _putchar(z / 10 + '0'); 29 | _putchar(z % 10 + '0'); 30 | } 31 | else if (y != 0) 32 | { 33 | _putchar(' '); 34 | _putchar(' '); 35 | _putchar(z + '0'); 36 | } 37 | else 38 | _putchar(z + '0'); 39 | 40 | if (y != n) 41 | { 42 | _putchar(','); 43 | _putchar(' '); 44 | } 45 | } 46 | _putchar('\n'); 47 | x++; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - main block 7 | * Description: Print the sum of all multiples of 3 or 5 below 1024. 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int i = 1; 13 | int total = 0; 14 | 15 | while (i < 1024) 16 | { 17 | if (i % 3 == 0) 18 | total += i; 19 | else if (i % 5 == 0) 20 | total += i; 21 | 22 | i++; 23 | } 24 | printf("%d\n", total); 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - main block 5 | * Description: Print the first 50 fibonacci numbers, starting with 1 and 2. 6 | * Numbers must be coma and space separated. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int count = 2; 12 | long int i = 1, j = 2; 13 | long int k; 14 | 15 | printf("%lu, ", i); 16 | while (count <= 50) 17 | { 18 | if (count == 50) 19 | { 20 | printf("%lu\n", j); 21 | } 22 | else 23 | { 24 | printf("%lu, ", j); 25 | } 26 | 27 | k = j; 28 | j += i; 29 | i = k; 30 | count++; 31 | } 32 | 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - main block 5 | * Description: Print the sum of even Fibonacci numbers up to a fib value 6 | * not exceeding 4,000,000. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int i = 1, j = 2, total = 0; 12 | int k; 13 | 14 | while (j < 4000000) 15 | { 16 | if (j % 2 == 0) 17 | total += j; 18 | 19 | k = j; 20 | j += i; 21 | i = k; 22 | } 23 | printf("%d\n", total); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define LARGEST 10000000000 3 | /** 4 | * main - main block 5 | * Description: Find and print the first 98 fib numbers starting with 1 and 2. 6 | * Numbers should be coma and space separated. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int fr1 = 0, bk1 = 1, fr2 = 0, bk2 = 2; 12 | unsigned long int hold1, hold2, hold3; 13 | int count; 14 | 15 | printf("%lu, %lu, ", bk1, bk2); 16 | for (count = 2; count < 98; count++) 17 | { 18 | if (bk1 + bk2 > LARGEST || fr2 > 0 || fr1 > 0) 19 | { 20 | hold1 = (bk1 + bk2) / LARGEST; 21 | hold2 = (bk1 + bk2) % LARGEST; 22 | hold3 = fr1 + fr2 + hold1; 23 | fr1 = fr2, fr2 = hold3; 24 | bk1 = bk2, bk2 = hold2; 25 | printf("%lu%010lu", fr2, bk2); 26 | } 27 | else 28 | { 29 | hold2 = bk1 + bk2; 30 | bk1 = bk2, bk2 = hold2; 31 | printf("%lu", bk2); 32 | } 33 | if (count != 97) 34 | printf(", "); 35 | } 36 | printf("\n"); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * print_to_98 - Print all natural numbers from n to 98 5 | * @n: int type number 6 | */ 7 | void print_to_98(int n) 8 | { 9 | if (n > 98) 10 | { 11 | while (n >= 98) 12 | { 13 | printf("%d", n); 14 | if (n != 98) 15 | { 16 | printf(", "); 17 | } 18 | n--; 19 | } 20 | } 21 | else 22 | { 23 | while (n <= 98) 24 | { 25 | printf("%d", n); 26 | if (n != 98) 27 | { 28 | printf(", "); 29 | } 30 | n++; 31 | } 32 | } 33 | printf("\n"); 34 | } 35 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_alphabet_x10 - Print alphabet in lowercase 10 times 4 | * Return: 0 5 | */ 6 | void print_alphabet_x10(void) 7 | { 8 | int i = 0; 9 | char c; 10 | 11 | while (i < 10) 12 | { 13 | c = 'a'; 14 | while (c <= 'z') 15 | { 16 | _putchar(c); 17 | c++; 18 | } 19 | _putchar('\n'); 20 | i++; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _islower - Return 1 if letter is lowercase, 0 if not. 4 | * @c: char type letter 5 | * Return: 1 if lowercase, 0 if not lowercase 6 | */ 7 | int _islower(int c) 8 | { 9 | if (c > 96 && c < 123) 10 | return (1); 11 | else 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _isalpha - Check if character is an alphabetic character. 4 | * @c: type int character 5 | * Return: 1 if letter, lowercase or uppercase, and 0 otherwise 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c > 64 && c < 91) || (c > 96 && c < 123)) 10 | return (1); 11 | else 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_sign - Print the sign of a number 4 | * @n: type int integer, can be negative or positive 5 | * Description: print +, 0, or - depending on number, along with return 6 | * Return: 1 if +, 0 if 0, and -1 if - 7 | */ 8 | int print_sign(int n) 9 | { 10 | if (n > 0) 11 | { 12 | _putchar('+'); 13 | return (1); 14 | } 15 | else if (n < 0) 16 | { 17 | _putchar('-'); 18 | return (-1); 19 | } 20 | else 21 | { 22 | _putchar('0'); 23 | return (0); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_last_digit - print the last digit of a number 4 | * @n: int type number 5 | * Return: return value of last digit 6 | */ 7 | int print_last_digit(int n) 8 | { 9 | int nv; 10 | 11 | if (n < 0) 12 | { 13 | nv = -1 * (n % 10); 14 | _putchar(nv + '0'); 15 | return (nv); 16 | } 17 | else 18 | { 19 | nv = n % 10; 20 | _putchar(nv + '0'); 21 | return (nv); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * jack_bauer - Print every minute of the day of Jack Bauer 4 | * Description: Start from 00:00 to 23:59 5 | */ 6 | void jack_bauer(void) 7 | { 8 | int x = 0; 9 | int a = 0, b = 0, c = 0, d = 0; 10 | 11 | while (x < 1440) 12 | { 13 | _putchar(a + '0'); 14 | _putchar(b + '0'); 15 | _putchar(':'); 16 | _putchar(c + '0'); 17 | _putchar(d + '0'); 18 | _putchar('\n'); 19 | 20 | d++; 21 | if (d > 9) 22 | { 23 | d = 0; 24 | c++; 25 | } 26 | if (c > 5) 27 | { 28 | c = 0; 29 | b++; 30 | } 31 | if (b > 9) 32 | { 33 | b = 0; 34 | a++; 35 | } 36 | x++; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * times_table - Print the 9 times table, starting with 0 4 | * Return: 0 5 | */ 6 | void times_table(void) 7 | { 8 | int x = 0; /* factor */ 9 | int y; /* count */ 10 | int z; /* computed value */ 11 | 12 | while (x < 10) 13 | { 14 | y = 0; 15 | while (y < 10) 16 | { 17 | z = x * y; 18 | 19 | if (z > 9) 20 | { 21 | _putchar(z / 10 + '0'); 22 | _putchar(z % 10 + '0'); 23 | } 24 | else if (y != 0) 25 | { 26 | _putchar(' '); 27 | _putchar(z + '0'); 28 | } 29 | else 30 | { 31 | _putchar(z + '0'); 32 | } 33 | 34 | if (y != 9) 35 | { 36 | _putchar(','); 37 | _putchar(' '); 38 | } 39 | y++; 40 | } 41 | _putchar('\n'); 42 | x++; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # C Functions and Nested Loops -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/holberton.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); 8 | int print_last_digit(int); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int, int); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isupper - function that checks for uppercase characters 5 | * @c: int type number 6 | * Return: 1 if uppercase, else 0 7 | */ 8 | int _isupper(int c) 9 | { 10 | if (c > 64 && c < 91) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isdigit - Check for digits (0 through 9) 5 | * @c: int type param 6 | * Return: 1 if digit, else 0 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c > 47 && c < 58) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_triangle - print a triangle aligned right, using '#' 5 | * @size: Size of triangle 6 | */ 7 | void print_triangle(int size) 8 | { 9 | int c, i, j; 10 | 11 | c = 0; 12 | i = size - 1; 13 | while (c < size) 14 | { 15 | i = size - 1 - c; 16 | j = c + 1; 17 | while (i > 0) 18 | { 19 | _putchar(' '); 20 | i--; 21 | } 22 | while (j > 0) 23 | { 24 | _putchar('#'); 25 | j--; 26 | } 27 | _putchar('\n'); 28 | c++; 29 | } 30 | 31 | if (size <= 0) 32 | _putchar('\n'); 33 | } 34 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - main block 6 | * Description: Find and print the largest prime factor of the number 7 | * 612852475143 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int c; 13 | long num = 612852475143; 14 | 15 | for (c = (int) sqrt(num); c > 2; c++) 16 | { 17 | if (num % c == 0) 18 | { 19 | printf("%d\n", c); 20 | break; 21 | } 22 | } 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_number - Function that prints an integer. 5 | * @n: int type number 6 | * Description: Can only use _putchar to print. 7 | */ 8 | void print_number(int n) 9 | { 10 | long m; /* power of 10 */ 11 | int c; /* boolean check */ 12 | long num; /* convert int to long */ 13 | 14 | num = n; 15 | /* negatives */ 16 | if (num < 0) 17 | { 18 | num *= -1; 19 | _putchar('-'); 20 | } 21 | 22 | /* count up */ 23 | m = 1; 24 | c = 1; 25 | while (c) 26 | { 27 | if (num / (m * 10) > 0) 28 | m *= 10; 29 | else 30 | c = 0; 31 | } 32 | 33 | /* count down */ 34 | while (num >= 0) 35 | { 36 | if (m == 1) 37 | { 38 | _putchar(num % 10 + '0'); 39 | num = -1; 40 | } 41 | else 42 | { 43 | _putchar((num / m % 10) + '0'); 44 | m /= 10; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * mul - multiply 2 integers 5 | * @a: int type number 6 | * @b: int type number 7 | * Return: result of multiplication 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_numbers - print the numbers from 0 to 9 5 | * Description: You can only use _putchar twice 6 | */ 7 | void print_numbers(void) 8 | { 9 | int i; 10 | 11 | i = 0; 12 | while (i < 10) 13 | { 14 | _putchar(i + '0'); 15 | i++; 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_most_numbers - Print numbers 0 to 9, excluding 2 and 4. 5 | * Description: Can only use _putchar twice. 6 | */ 7 | void print_most_numbers(void) 8 | { 9 | int i; 10 | 11 | i = 0; 12 | while (i < 10) 13 | { 14 | if (i != 2 && i != 4) 15 | _putchar(i + '0'); 16 | 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * more_numbers - print 10 times the numbers from 0 to 14 5 | * Description: Can only use _putchar 3 times 6 | */ 7 | void more_numbers(void) 8 | { 9 | int c, i; 10 | 11 | c = 0; 12 | while (c < 10) 13 | { 14 | i = 0; 15 | while (i < 15) 16 | { 17 | if (i > 9) 18 | _putchar(i / 10 + '0'); 19 | 20 | _putchar(i % 10 + '0'); 21 | i++; 22 | } 23 | c++; 24 | _putchar('\n'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_line - draw a straight line 5 | * @n: number of times underscore is printed 6 | * Description: Can only use _putchar to print 7 | */ 8 | void print_line(int n) 9 | { 10 | int c; 11 | 12 | c = 0; 13 | while (c < n) 14 | { 15 | _putchar('_'); 16 | 17 | c++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_diagonal - draw a diagonal line 5 | * @n: number of times the '\' char is printed 6 | * Description: Can only use _putchar to print 7 | */ 8 | void print_diagonal(int n) 9 | { 10 | int c, i; 11 | 12 | c = 0; 13 | 14 | while (n > 0) 15 | { 16 | i = c; 17 | while (i > 0) 18 | { 19 | _putchar(' '); 20 | i--; 21 | } 22 | _putchar('\\'); 23 | _putchar('\n'); 24 | c++; 25 | n--; 26 | } 27 | if (c < 1) 28 | _putchar('\n'); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_square - print a square 5 | * @size: size of square 6 | * Description: Can only use _putchar to print. Use '#' to print square. 7 | */ 8 | void print_square(int size) 9 | { 10 | int x, y; 11 | 12 | y = 0; 13 | 14 | if (size < 1) 15 | _putchar('\n'); 16 | 17 | while (y < size) 18 | { 19 | x = 0; 20 | while (x < size) 21 | { 22 | _putchar('#'); 23 | x++; 24 | } 25 | _putchar('\n'); 26 | y++; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - main block, solve fizz buzz from numbers 1 to 100 5 | * Description: Multiples of 3, print Fizz. Multiples of 5, print Buzz. 6 | * Multiples of both 3 and 5 should print FizzBuzz. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | i = 1; 14 | while (i <= 100) 15 | { 16 | if (i % 3 == 0 && i % 5 == 0) 17 | printf("FizzBuzz"); 18 | else if (i % 3 == 0) 19 | printf("Fizz"); 20 | else if (i % 5 == 0) 21 | printf("Buzz"); 22 | else 23 | printf("%d", i); 24 | 25 | if (i != 100) 26 | printf(" "); 27 | i++; 28 | } 29 | printf("\n"); 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # C - more functions and nested loops -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/_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 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _isupper(int c); 3 | int _isdigit(int c); 4 | int mul(int a, int b); 5 | void print_numbers(void); 6 | void print_most_numbers(void); 7 | void more_numbers(void); 8 | void print_line(int n); 9 | void print_diagonal(int n); 10 | void print_square(int size); 11 | void print_triangle(int size); 12 | void print_number(int n); 13 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reset_to_98 - Set pointer param to 98. 5 | * @n: int type pointer 6 | */ 7 | 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * swap_int - swap the values of two integers 5 | * @a: int pointer type 6 | * @b: int pointer type 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | int c; 12 | 13 | c = *a; 14 | *a = *b; 15 | *b = c; 16 | } 17 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _atoi - Convert a string to integer. 5 | * @s: char array string 6 | * Description: Number in the string can be preceded by an infinite 7 | * number of characters. 8 | * You need to take into account all -/+ signs before the number. 9 | * If there are no numbers in the string, return 0. 10 | * No need to check for overflow. 11 | * Not allowed to hard-code special values. 12 | * Return: first integer found in string 13 | */ 14 | 15 | int _atoi(char *s) 16 | { 17 | int i; 18 | int h, p; 19 | 20 | h = 0; 21 | p = -1; 22 | for (i = 0; s[i] != '\0'; i++) 23 | { 24 | if (s[i] == '-') 25 | p *= -1; 26 | 27 | if (s[i] > 47 && s[i] < 58) 28 | { 29 | if (h < 0) 30 | h = (h * 10) - (s[i] - '0'); 31 | else 32 | h = (s[i] - '0') * -1; 33 | 34 | if (s[i + 1] < 48 || s[i + 1] > 57) 35 | break; 36 | } 37 | } 38 | if (p < 0) 39 | h *= -1; 40 | 41 | return (h); 42 | } 43 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int myrand; 8 | int count; 9 | int total; 10 | 11 | srand(time(NULL)); 12 | for (count = 0, total = 2772; total > 122; count++) 13 | { 14 | myrand = (rand() % 125) + 1; 15 | printf("%c", myrand); 16 | total -= myrand; 17 | } 18 | printf("%c", total); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - return the length of the string 5 | * @s: char type pointer 6 | * Return: length of string 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int c; 12 | 13 | for (c = 0; s[c] != '\0'; c++) 14 | ; 15 | 16 | return (c); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts - print a string to stdout 5 | * @str: char array string type 6 | * Description: Can only use _putchar 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | { 15 | _putchar(str[i]); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_rev - print a string in reverse 5 | * @s: char array string type 6 | */ 7 | 8 | void print_rev(char *s) 9 | { 10 | int i; 11 | 12 | for (i = 0; s[i] != '\0'; i++) 13 | ; 14 | 15 | for (i--; i >= 0; i--) 16 | _putchar(s[i]); 17 | 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * rev_string - reverse a string 5 | * @s: char array string type 6 | */ 7 | 8 | void rev_string(char *s) 9 | { 10 | int i, c, l; 11 | char h; 12 | 13 | for (i = 0; s[i] != '\0'; i++) 14 | ; 15 | 16 | l = i; 17 | for (i--, c = 0; c < l / 2; i--, c++) 18 | { 19 | h = s[c]; 20 | s[c] = s[i]; 21 | s[i] = h; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * puts2 - print one char out of 2 of a string 5 | * @str: char array string type 6 | * Description: Print 1st char, then 3rd, then 5th, etc.. 7 | */ 8 | 9 | void puts2(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | { 15 | if (i % 2 == 0) 16 | _putchar(str[i]); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_array - print `n` elements of an array of integers 6 | * @a: int type array pointer 7 | * @n: int type integer 8 | * Description: Numbers must be separated by comma and space. 9 | * Numbers should be displayed in the same order they are stored in array. 10 | * You can only use _putchar to print. 11 | */ 12 | 13 | void print_array(int *a, int n) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | for (n--; n >= 0; n--, i++) 19 | { 20 | printf("%d", a[i]); 21 | if (n > 0) 22 | { 23 | printf(", "); 24 | } 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * *_strcpy - See description 5 | * @dest: char type string 6 | * @src: char type string 7 | * Description: Copy the string pointed to by pointer `src` to 8 | * the buffer pointed to by `dest` 9 | * Return: Pointer to `dest` 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int i; 15 | 16 | for (i = 0; src[i] != '\0'; i++) 17 | { 18 | dest[i] = src[i]; 19 | } 20 | dest[i] = '\0'; 21 | 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | #C pointers, arrays, and strings -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/_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 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void reset_to_98(int *n); 3 | void swap_int(int *a, int *b); 4 | int _strlen(char *s); 5 | void _puts(char *str); 6 | void print_rev(char *s); 7 | void rev_string(char *s); 8 | void puts2(char *str); 9 | void puts_half(char *str); 10 | void print_array(int *a, int n); 11 | char *_strcpy(char *dest, char *src); 12 | int _atoi(char *s); 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcat - concatenate two strings 5 | * @dest: char string to concatenate to 6 | * @src: char string 7 | * Return: pointer to resulting string `dest` 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i, c; 13 | 14 | for (i = 0; dest[i] != '\0'; i++) 15 | ; 16 | 17 | for (c = 0; src[c] != '\0'; c++) 18 | { 19 | dest[i] = src[c]; 20 | i++; 21 | } 22 | 23 | dest[i] = '\0'; 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strncat - concatenate two strings 5 | * @dest: string 6 | * @src: string 7 | * @n: number of elements to concatenate in 8 | * Return: pointer to resulting `dest` 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, c; 14 | 15 | for (i = 0; dest[i] != '\0'; i++) 16 | ; 17 | 18 | for (c = 0; src[c] != '\0' && n > 0; c++, n--, i++) 19 | { 20 | dest[i] = src[c]; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * print_number - Print an integer using only _putchar 5 | * @n: integer to print 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | int power; 11 | int neg; 12 | int hold; 13 | 14 | neg = 0; 15 | power = 1; 16 | hold = n; 17 | if (n < 0) 18 | { 19 | _putchar('-'); 20 | neg = 1; 21 | } 22 | 23 | while (hold > 9 || hold < -9) 24 | { 25 | power *= 10; 26 | hold /= 10; 27 | } 28 | 29 | while (power > 0) 30 | { 31 | if (power > 9) 32 | { 33 | if (!neg) 34 | _putchar((n / power % 10) + '0'); 35 | else 36 | _putchar((n / power % 10) * -1 + '0'); 37 | 38 | power /= 10; 39 | } 40 | if (power == 1) 41 | { 42 | if (neg) 43 | _putchar((n % 10) * -1 + '0'); 44 | else 45 | _putchar(n % 10 + '0'); 46 | power = 0; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strncpy - Copy a string starting from index 0 of `dest`. 5 | * @dest: string 6 | * @src: string 7 | * @n: number of chars to copy over 8 | * Return: `dest` edited string 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n && src[i] != '\0'; i++) 16 | dest[i] = src[i]; 17 | 18 | for (; n > i; i++) 19 | dest[i] = '\0'; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcmp - Compare two strings 5 | * @s1: string 6 | * @s2: string 7 | * Return: negative int if s1 < s2, 0 if matching, and positive int if s1 > s2 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i; 13 | 14 | for (i = 0; s1[i] != '\0' || s2[i] != '\0'; i++) 15 | { 16 | if (s1[i] != s2[i]) 17 | return (s1[i] - s2[i]); 18 | } 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reverse_array - reverse the contents of an array of integers 5 | * @a: array of integers 6 | * @n: number of elements in array 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int i; 12 | int h; 13 | 14 | for (i = 0; i < (n / 2); i++) 15 | { 16 | h = a[i]; 17 | a[i] = a[n - i - 1]; 18 | a[n - i - 1] = h; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * string_toupper - Change all lowercase letters to uppercase 5 | * @c: string 6 | * Return: c 7 | */ 8 | 9 | char *string_toupper(char *c) 10 | { 11 | int i; 12 | 13 | for (i = 0; c[i] != '\0'; i++) 14 | { 15 | if (c[i] > 96 && c[i] < 123) 16 | { 17 | c[i] -= 32; 18 | } 19 | } 20 | return (c); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * cap_string - capitalize all words of a string 5 | * @str: string 6 | * Return: `str` 7 | */ 8 | 9 | char *cap_string(char *str) 10 | { 11 | int i, c; 12 | int trigger; 13 | char nots[] = ",;.!?(){}\n\t\" "; 14 | 15 | for (i = 0, trigger = 0; str[i] != '\0'; i++) 16 | { 17 | if (str[0] > 96 && str[0] < 123) 18 | trigger = 1; 19 | for (c = 0; nots[c] != '\0'; c++) 20 | { 21 | if (nots[c] == str[i]) 22 | trigger = 1; 23 | } 24 | 25 | if (trigger) 26 | { 27 | if (str[i] > 96 && str[i] < 123) 28 | { 29 | str[i] -= 32; 30 | trigger = 0; 31 | } 32 | else if (str[i] > 64 && str[i] < 91) 33 | trigger = 0; 34 | else if (str[i] > 47 && str[i] < 58) 35 | trigger = 0; 36 | } 37 | } 38 | return (str); 39 | } 40 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * leet - encode a string into 1337 5 | * @s: string 6 | * Return: encoded string `s` 7 | */ 8 | 9 | char *leet(char *s) 10 | { 11 | int i; 12 | 13 | for (i = 0; s[i] != '\0'; i++) 14 | { 15 | while (s[i] == 'a' || s[i] == 'A') 16 | s[i] = '4'; 17 | while (s[i] == 'e' || s[i] == 'E') 18 | s[i] = '3'; 19 | while (s[i] == 'o' || s[i] == 'O') 20 | s[i] = '0'; 21 | while (s[i] == 't' || s[i] == 'T') 22 | s[i] = '7'; 23 | while (s[i] == 'l' || s[i] == 'L') 24 | s[i] = '1'; 25 | } 26 | 27 | return (s); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-rot13.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * rot13 - rotate characters 13 places in the alphabet 5 | * @s: string 6 | * Return: string `s` rotated 7 | */ 8 | 9 | char *rot13(char *s) 10 | { 11 | int i; 12 | char storeh[] = "NOPQRSTUVWXYZABCDEFGHIJKLM"; 13 | char storel[] = "nopqrstuvwxyzabcdefghijklm"; 14 | 15 | for (i = 0; s[i] != '\0'; i++) 16 | { 17 | if ((s[i] > 64 && s[i] < 91) || (s[i] > 96 && s[i] < 123)) 18 | { 19 | s[i] = (s[i] - 65 > 25) ? 20 | storel[s[i] - 97] : storeh[s[i] - 65]; 21 | } 22 | } 23 | return (s); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # C - More pointers, arrays, and strings -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/_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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *_strcat(char *dest, char *src); 3 | char *_strncat(char *dest, char *src, int n); 4 | char *_strncpy(char *dest, char *src, int n); 5 | int _strcmp(char *s1, char *s2); 6 | void reverse_array(int *a, int n); 7 | char *string_toupper(char *); 8 | char *cap_string(char *); 9 | char *leet(char *); 10 | char *rot13(char *); 11 | void print_number(int n); 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/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 | ; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memset - Fills memory with a constant byte 5 | * @s: buffer array 6 | * @b: constant byte 7 | * @n: number of bytes of memory area to fill 8 | * Description: Fill the first `n` bytes of the memory area pointed to by `s` 9 | * with the constant byte `b`. 10 | * Return: Pointer to memory area `s` 11 | */ 12 | 13 | char *_memset(char *s, char b, unsigned int n) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | while (n > 0) 19 | { 20 | s[i] = b; 21 | i++; 22 | n--; 23 | } 24 | 25 | return (s); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memcpy - copy memory area 5 | * @dest: char array to copy into 6 | * @src: char array to copy from 7 | * @n: number of elements to copy 8 | * Return: pointer to `dest` 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | char *yolo; 14 | 15 | yolo = dest; 16 | while (n > 0) 17 | { 18 | *dest = *src; 19 | dest++; 20 | src++; 21 | n--; 22 | } 23 | 24 | return (yolo); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strchr - locate character in a string 5 | * @s: char array string 6 | * @c: char to look for 7 | * Return: NULL if char not found, or pointer to first occurrence of char `c` 8 | */ 9 | 10 | char *_strchr(char *s, char c) 11 | { 12 | while (*s != '\0') 13 | { 14 | if (*s == c) 15 | return (s); 16 | else if (*(s + 1) == c) 17 | return (s + 1); 18 | s++; 19 | } 20 | 21 | return (s + 1); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strspn - search a string for a set of bytes 5 | * @s: char string array 6 | * @accept: char array to check bytes with 7 | * Return: Number of bytes in the intial segment of `s` 8 | */ 9 | 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | int i; 13 | int j; 14 | int c; 15 | 16 | i = 0; 17 | c = 0; 18 | 19 | while (s[i] != '\0') 20 | { 21 | j = 0; 22 | while (accept[j] != '\0') 23 | { 24 | if (s[i] == accept[j]) 25 | { 26 | c++; 27 | break; 28 | } 29 | j++; 30 | } 31 | if (accept[j] == '\0') 32 | break; 33 | i++; 34 | } 35 | return (c); 36 | } 37 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strpbrk - Search a string for any of a set of bytes. 5 | * @s: string 6 | * @accept: string to match 7 | * Return: Pointer to the byte in `s` that matches one of the bytes in `accept` 8 | * or NULL if no such byte is found. 9 | */ 10 | 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i, j; 14 | char *p; 15 | 16 | i = 0; 17 | while (s[i] != '\0') 18 | { 19 | j = 0; 20 | while (accept[j] != '\0') 21 | { 22 | if (accept[j] == s[i]) 23 | { 24 | p = &s[i]; 25 | return (p); 26 | } 27 | j++; 28 | } 29 | i++; 30 | } 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * _strstr - locate a substring 6 | * @haystack: string 7 | * @needle: string substring 8 | * Return: Pointer to beginning of substring, or NULL if not found. 9 | */ 10 | 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | int i, j, c; 14 | 15 | i = 0; 16 | c = 0; 17 | while (haystack[i] != '\0') 18 | { 19 | j = 0; 20 | while (needle[j + c] != '\0' && haystack[i + c] != '\0' 21 | && needle[j + c] == haystack[i + c]) 22 | { 23 | if (haystack[i + c] != needle[j + c]) 24 | break; 25 | c++; 26 | } 27 | if (needle[j + c] == '\0') 28 | return (&haystack[i]); 29 | j++; 30 | i++; 31 | } 32 | 33 | return (NULL); 34 | } 35 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_chessboard - print the chessboard, 2d array 5 | * @a: 2d array of chars 6 | */ 7 | 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | int y, x; 11 | 12 | y = 0; 13 | while (y < 8) 14 | { 15 | x = 0; 16 | while (x < 8) 17 | { 18 | _putchar(a[y][x]); 19 | x++; 20 | } 21 | _putchar('\n'); 22 | y++; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * print_diagsums - Print the sum of the 2 diagonals of a square matrix of ints 5 | * @a: 2d array of int types 6 | * @size: size of array (square) 7 | */ 8 | 9 | void print_diagsums(int *a, int size) 10 | { 11 | int i, sum, sizer; 12 | 13 | i = 0; 14 | sum = 0; 15 | sizer = size * size; 16 | while (i < sizer) 17 | { 18 | if (i % (size + 1) == 0) 19 | sum += a[i]; 20 | i++; 21 | } 22 | printf("%d, ", sum); 23 | 24 | sum = 0; 25 | i = 0; 26 | while (i < sizer) 27 | { 28 | if (i % (size - 1) == 0 && i != (sizer - 1) && i != 0) 29 | sum += a[i]; 30 | i++; 31 | } 32 | printf("%d\n", sum); 33 | } 34 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/9-set_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: pointer to pointer 6 | * @to: pointer char 7 | */ 8 | 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # 0x06. C - even more pointers, arrays and strings -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_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 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *_memset(char *s, char b, unsigned int n); 3 | char *_memcpy(char *dest, char *src, unsigned int n); 4 | char *_strchr(char *s, char c); 5 | unsigned int _strspn(char *s, char *accept); 6 | char *_strpbrk(char *s, char *accept); 7 | char *_strstr(char *haystack, char *needle); 8 | void print_chessboard(char (*a)[8]); 9 | void print_diagsums(int *a, int size); 10 | void set_string(char **s, char *to); 11 | -------------------------------------------------------------------------------- /0x07-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts_recursion - Print a string 5 | * @s: string 6 | */ 7 | 8 | void _puts_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | _putchar('\n'); 12 | else 13 | { 14 | _putchar(*s); 15 | _puts_recursion(++s); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x07-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _print_rev_recursion - print a string in reverse 5 | * @s: string 6 | */ 7 | 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return; 12 | 13 | _print_rev_recursion(s + 1); 14 | _putchar(*s); 15 | } 16 | -------------------------------------------------------------------------------- /0x07-recursion/100-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * wildcmp - compare two strings with "wildcard expansion" capabilities 5 | * @s1: string 1 6 | * @s2: string 2 7 | * Return: 1 if strings can be considered identical, else 0 8 | */ 9 | 10 | int wildcmp(char *s1, char *s2) 11 | { 12 | if (*s1 == '\0' && *s2 == '\0') 13 | return (1); 14 | else if (*s1 == '\0' || *s2 == '\0') 15 | { 16 | if (*s1 == '\0' && *s2 == '*') 17 | return wildcmp(s1, ++s2); 18 | else if (*s1 == '*' && *s2 == '\0') 19 | return wildcmp(++s1, s2); 20 | return (0); 21 | } 22 | 23 | if (*s1 == *s2) 24 | { 25 | return wildcmp(++s1, ++s2); 26 | } 27 | else if (*s1 == '*') 28 | { 29 | if (*(s1 + 1) == '*') 30 | return wildcmp(++s1, s2); 31 | else 32 | { 33 | return wildcmp(s1, findsrc(s2, *(s1 + 1), 0, 0) + s2); 34 | } 35 | } 36 | else if (*s2 == '*') 37 | { 38 | if (*(s2 + 1) == '*') 39 | return wildcmp(s1, ++s2); 40 | else 41 | { 42 | return wildcmp(s1 + findsrc(s1, *(s2 + 1), 0, 0), s2); 43 | } 44 | } 45 | 46 | return (0); 47 | 48 | } 49 | 50 | int findsrc(char *s, char c, int i, int p) 51 | { 52 | if (*(s + i) == '\0') 53 | return (p + 1); 54 | else if (*(s + i) == c || *(s + i) == '*') 55 | p = i; 56 | 57 | return (findsrc(s, c, i + 1, p)); 58 | } 59 | -------------------------------------------------------------------------------- /0x07-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen_recursion - return length of a string 5 | * @s: string 6 | * Return: length of string 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | int c; 12 | 13 | c = 0; 14 | if (*s == '\0') 15 | return (0); 16 | 17 | c = 1 + _strlen_recursion(s + 1); 18 | 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * factorial - return factorial of a given number. 5 | * @n: int type number 6 | * Return: factorial of number 7 | */ 8 | 9 | int factorial(int n) 10 | { 11 | if (n < 0) 12 | return (-1); 13 | else if (n < 2) 14 | return (n); 15 | else 16 | return (n * factorial(n - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _pow_recursion - return the value of `x` raised to power of `y` 5 | * @x: number value 6 | * @y: number power 7 | * Return: x to power of y 8 | */ 9 | 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | return (-1); 14 | else if (y == 0) 15 | return (1); 16 | else if (y == 1) 17 | return (x); 18 | 19 | return (x * _pow_recursion(x, y - 1)); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _sqrt_recursion - return the natural square root of a number 5 | * @n: int number 6 | * Return: If no natural square root, return -1. Else return natural 7 | * square root 8 | */ 9 | 10 | int _sqrt_recursion(int n) 11 | { 12 | return (halp(n, 1)); 13 | } 14 | 15 | /** 16 | * halp - helper function to solve _sqrt_recursion 17 | * @c: number to determine if square root 18 | * @i: incrementer to compare against `c` 19 | * Return: square root if natural square root, or -1 if none found 20 | */ 21 | 22 | int halp(int c, int i) 23 | { 24 | int square; 25 | 26 | square = i * i; 27 | if (square == c) 28 | return (i); 29 | else if (square < c) 30 | return (halp(c, i + 1)); 31 | else 32 | return (-1); 33 | } 34 | -------------------------------------------------------------------------------- /0x07-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * is_prime_number - determine if a number is a prime number 5 | * @n: int number 6 | * Return: 1 if prime, 0 otherwise 7 | */ 8 | 9 | int is_prime_number(int n) 10 | { 11 | if (n < 2) 12 | return (0); 13 | if (n < 4) 14 | return (1); 15 | return (hai(n, 2)); 16 | } 17 | 18 | /** 19 | * _sqrt - return square root of number 20 | * @x: number 21 | * @i: number incrementer acting as divisor 22 | * Return: square root of `x` 23 | */ 24 | 25 | int _sqrt(int x, int i) 26 | { 27 | int square; 28 | 29 | square = i * i; 30 | if (square >= x) 31 | return (i); 32 | else 33 | return (_sqrt(x, i + 1)); 34 | } 35 | 36 | /** 37 | * hai - helper function, recursive steps taken 38 | * @n: number given to original function is_prime_number 39 | * @d: incrementer divisor 40 | * Return: 0 if not prime, 1 if prime 41 | */ 42 | 43 | int hai(int n, int d) 44 | { 45 | if (n % d == 0) 46 | return (0); 47 | else if (_sqrt(n, 1) < d) 48 | return (1); 49 | else 50 | return (hai(n, d + 1)); 51 | } 52 | -------------------------------------------------------------------------------- /0x07-recursion/7-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * is_palindrome - check if a string is a palindrome 5 | * @s: char array string 6 | * Return: 1 if palindrome, 0 if not 7 | */ 8 | 9 | int is_palindrome(char *s) 10 | { 11 | int length; 12 | 13 | length = get_length(s) - 1; 14 | 15 | return (my_pal(s, --length)); 16 | } 17 | 18 | /** 19 | * get_length - gets length of string 20 | * @s: string 21 | * Return: return length of string 22 | */ 23 | 24 | int get_length(char *s) 25 | { 26 | if (*s == '\0') 27 | return (1); 28 | else 29 | return (1 + get_length(++s)); 30 | } 31 | 32 | /** 33 | * my_pal - recursive check of palindrome 34 | * @s: string 35 | * @l: length of string 36 | * Return: 1 if palindrome, 0 if not 37 | */ 38 | 39 | int my_pal(char *s, int l) 40 | { 41 | if (*s == *(s + l)) 42 | { 43 | if (l <= 0) 44 | return (1); 45 | else 46 | return (my_pal(++s, l - 2)); 47 | } 48 | else 49 | return (0); 50 | } 51 | -------------------------------------------------------------------------------- /0x07-recursion/README.md: -------------------------------------------------------------------------------- 1 | # 0x07. C - Recursion -------------------------------------------------------------------------------- /0x07-recursion/_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 | -------------------------------------------------------------------------------- /0x07-recursion/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void _puts_recursion(char *s); 3 | void _print_rev_recursion(char *s); 4 | int _strlen_recursion(char *s); 5 | int factorial(int n); 6 | int _pow_recursion(int x, int y); 7 | int _sqrt_recursion(int n); 8 | int halp(int c, int i); 9 | int is_prime_number(int n); 10 | int hai(int n, int d); 11 | int _sqrt(int x, int i); 12 | int is_palindrome(char *s); 13 | int get_length(char *s); 14 | int my_pal(char *s, int l); 15 | int wildcmp(char *s1, char *s2); 16 | int findsrc(char *s, char c, int i, int p); 17 | -------------------------------------------------------------------------------- /0x08-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x08-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x08-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x08-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x08-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x08-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x08-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x08-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x08-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x08-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x08-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x08-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x08-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x08-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x08-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x08-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x08-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x08-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x08-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x08-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x08. C - Static Libraries -------------------------------------------------------------------------------- /0x08-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 | -------------------------------------------------------------------------------- /0x08-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x08-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar rc liball.a *.o 4 | -------------------------------------------------------------------------------- /0x08-static_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _islower(int c); 3 | int _isalpha(int c); 4 | int _abs(int n); 5 | int _isupper(int c); 6 | int _isdigit(int c); 7 | int _strlen(char *s); 8 | void _puts(char *s); 9 | char *_strcpy(char *dest, char *src); 10 | int _atoi(char *s); 11 | char *_strcat(char *dest, char *src); 12 | char *_strncat(char *dest, char *src, int n); 13 | char *_strncpy(char *dest, char *src, int n); 14 | int _strcmp(char *s1, char *s2); 15 | char *_memset(char *s, char b, unsigned int n); 16 | char *_memcpy(char *dest, char *src, unsigned int n); 17 | char *_strchr(char *s, char c); 18 | unsigned int _strspn(char *s, char *accept); 19 | char *_strpbrk(char *s, char *accept); 20 | char *_strstr(char *haystack, char *needle); 21 | -------------------------------------------------------------------------------- /0x08-static_libraries/libholberton.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/libholberton.a -------------------------------------------------------------------------------- /0x08-static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x08-static_libraries/quote -------------------------------------------------------------------------------- /0x09-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - print the name of the file followed by a new line 6 | * @argc: argument count 7 | * @argv: arguments passed to main as an array of strings 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | (void) argc; 14 | 15 | printf("%s\n", argv[0]); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - print the num of args you passed to it 6 | * @argc: argument count 7 | * @argv: argument vector, array of strings 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | (void) argv; 14 | 15 | printf("%d\n", argc - 1); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - print the min number of coins to make change for an amount of money 7 | * @argc: argument count 8 | * @argv: argument vector, array of strings 9 | * Return: 1 if error, 0 otherwise 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int total, count; 15 | unsigned int i; 16 | char *p; 17 | int cents[] = {25, 10, 5, 2}; 18 | 19 | if (argc != 2) 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | 25 | total = strtol(argv[1], &p, 10); 26 | count = 0; 27 | 28 | if (!*p) 29 | { 30 | while (total > 1) 31 | { 32 | for (i = 0; i < sizeof(cents[i]); i++) 33 | { 34 | if (total >= cents[i]) 35 | { 36 | count += total / cents[i]; 37 | total = total % cents[i]; 38 | } 39 | } 40 | } 41 | if (total == 1) 42 | count++; 43 | } 44 | else 45 | { 46 | printf("Error\n"); 47 | return (1); 48 | } 49 | 50 | printf("%d\n", count); 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /0x09-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - print all arguments it receives 6 | * @argc: argument count 7 | * @argv: argument vector, array of strings 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < argc; i++) 16 | { 17 | printf("%s\n", argv[i]); 18 | } 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - multiply 2 numbers passed to main, or Error 7 | * @argc: argument count 8 | * @argv: argument vector 9 | * Return: 1 if error, 0 if function runs correctly 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | (void) argc; 15 | 16 | if (argv[1] && argv[2]) 17 | { 18 | printf("%d\n", atoi(argv[1]) * atoi(argv[2])); 19 | return (0); 20 | } 21 | else 22 | printf("Error\n"); 23 | 24 | return (1); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - add 2 positive numbers and print the result 7 | * @argc: argument count 8 | * @argv: argument vector, array of strings 9 | * Description: If no number is passed to program, print 0. 10 | * If one of the numbers contain non-digits, print Error. 11 | * Return: 1 if error, 0 if function runs properly. 12 | */ 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | int total, i; 17 | char *p; 18 | int num; 19 | 20 | total = 0; 21 | if (argc > 1) 22 | { 23 | for (i = 1; argv[i]; i++) 24 | { 25 | num = strtol(argv[i], &p, 10); 26 | if (!*p) 27 | total += num; 28 | else 29 | { 30 | printf("Error\n"); 31 | return (1); 32 | } 33 | } 34 | } 35 | printf("%d\n", total); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x09-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # 0x09. C - argc && argv -------------------------------------------------------------------------------- /0x09-argc_argv/_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-argc_argv/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | -------------------------------------------------------------------------------- /0x0A-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * create_array - create an array of chars, and initialize with specific char 6 | * @size: size of array 7 | * @c: specific char 8 | * Return: char pointer to malloc created memory address or NULL if error 9 | */ 10 | 11 | char *create_array(unsigned int size, char c) 12 | { 13 | char *a; 14 | unsigned int i; 15 | 16 | if (size == 0) 17 | return (NULL); 18 | 19 | a = malloc(size * sizeof(*a)); 20 | if (a == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | a[i] = c; 25 | 26 | return (a); 27 | } 28 | -------------------------------------------------------------------------------- /0x0A-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * _strdup - Duplicate a string using malloc 6 | * @str: string to duplicate 7 | * Return: Pointer to a the new duped string 8 | */ 9 | 10 | char *_strdup(char *str) 11 | { 12 | char *a; 13 | int i, c; 14 | 15 | if (str == NULL) 16 | return (NULL); 17 | 18 | for (i = 0; str[i] != '\0'; i++) 19 | ; 20 | 21 | a = malloc(i * sizeof(*a) + 1); 22 | if (a == NULL) 23 | return (NULL); 24 | 25 | for (c = 0; c < i; c++) 26 | a[c] = str[c]; 27 | a[c] = '\0'; 28 | 29 | return (a); 30 | } 31 | -------------------------------------------------------------------------------- /0x0A-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * str_concat - concatenate two strings using malloc 6 | * @s1: string 1 7 | * @s2: string 2 8 | * Return: pointer to concat string 9 | */ 10 | 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *a; 14 | int i, j, c, d; 15 | 16 | if (s1 == NULL) 17 | s1 = ""; 18 | if (s2 == NULL) 19 | s2 = ""; 20 | 21 | for (i = 0; s1[i] != '\0'; i++) 22 | ; 23 | for (j = 0; s2[j] != '\0'; j++) 24 | ; 25 | 26 | a = malloc((i * sizeof(*s1)) + (j * sizeof(*s2)) + 1); 27 | if (a == NULL) 28 | return (NULL); 29 | 30 | for (c = 0, d = 0; c < (i + j + 1); c++) 31 | { 32 | if (c < i) 33 | a[c] = s1[c]; 34 | else 35 | a[c] = s2[d++]; 36 | } 37 | 38 | return (a); 39 | } 40 | -------------------------------------------------------------------------------- /0x0A-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - create a 2d integer grid using malloc. 6 | * Description: Each element of the grid should be initialized to 0. 7 | * @width: int size width 8 | * @height: int size height 9 | * Return: Pointer to new grid 10 | */ 11 | 12 | int **alloc_grid(int width, int height) 13 | { 14 | int **grid; 15 | int i, j; 16 | 17 | if (width + height < 2 || width < 1 || height < 1) 18 | return (NULL); 19 | 20 | grid = malloc(height * sizeof(*grid)); 21 | if (grid == NULL) 22 | return (NULL); 23 | 24 | for (i = 0; i < height; i++) 25 | { 26 | grid[i] = malloc(width * sizeof(**grid)); 27 | if (grid[i] == NULL) 28 | { 29 | for (i--; i >= 0; i--) 30 | free(grid[i]); 31 | free(grid); 32 | return (NULL); 33 | } 34 | for (j = 0; j < width; j++) 35 | grid[i][j] = 0; 36 | } 37 | 38 | return (grid); 39 | } 40 | -------------------------------------------------------------------------------- /0x0A-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * free_grid - free up a 2d grid 6 | * @grid: double pointer 2d grid 7 | * @height: height of grid 8 | */ 9 | 10 | void free_grid(int **grid, int height) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < height; i++) 15 | free(grid[i]); 16 | free(grid); 17 | } 18 | -------------------------------------------------------------------------------- /0x0A-malloc_free/5-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | /** 5 | * argstostr - concatenate all arguments of your program with newline 6 | * @ac: argument count 7 | * @av: double pointer to array of strings passed to main 8 | * Return: Null if fail, else return pointer to new string 9 | */ 10 | 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *a, *retp; 14 | int i, j, total; 15 | 16 | if (ac == 0 || av == NULL) 17 | return (NULL); 18 | 19 | for (i = 0, total = 0; i < ac; i++) 20 | { 21 | for (j = 0; *(*(av + i) + j) != '\0'; j++, total++) 22 | ; 23 | total++; 24 | } 25 | total++; 26 | 27 | a = malloc(total * sizeof(char)); 28 | if (a == NULL) 29 | return (NULL); 30 | 31 | retp = a; 32 | for (i = 0; i < ac; i++) 33 | { 34 | for (j = 0; av[i][j] != '\0'; j++) 35 | { 36 | *a = av[i][j]; 37 | a++; 38 | } 39 | *a = '\n'; 40 | a++; 41 | } 42 | 43 | return (retp); 44 | } 45 | -------------------------------------------------------------------------------- /0x0A-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0A. C - malloc, free -------------------------------------------------------------------------------- /0x0A-malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * 6 | * Return: On success 1. 7 | * On error, -1 is returned, and errno is set appropriately. 8 | */ 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x0A-malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *create_array(unsigned int size, char c); 3 | char *_strdup(char *str); 4 | char *str_concat(char *s1, char *s2); 5 | int **alloc_grid(int width, int height); 6 | void free_grid(int **grid, int height); 7 | char *argstostr(int ac, char **av); 8 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * malloc_checked - allocate memory using malloc. 5 | * Description: If malloc fails, terminate process with status 98. 6 | * @b: unsigned int memory size to allocate 7 | * Return: void pointer to malloc'd memory space 8 | */ 9 | 10 | void *malloc_checked(unsigned int b) 11 | { 12 | void *p; 13 | 14 | p = malloc(b); 15 | if (p == NULL) 16 | exit(98); 17 | 18 | return (p); 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - concatenate 2 strings, only n bytes of s2 6 | * @s1: string 1 7 | * @s2: string 2 8 | * @n: bytes to include of s2 9 | * Return: NULL if fail, else pointer to malloc memory 10 | */ 11 | 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | char *p; 15 | int strlen1, i, c; 16 | 17 | if (s1 == NULL) 18 | s1 = ""; 19 | if (s2 == NULL) 20 | s2 = ""; 21 | 22 | strlen1 = (unsigned int)_strlen(s1); 23 | p = malloc((strlen1 + n + 1) * sizeof(char)); 24 | if (p == NULL) 25 | return (NULL); 26 | for (i = 0, c = 0; i < (strlen1 + n); i++) 27 | { 28 | if (i < strlen1) 29 | p[i] = s1[i]; 30 | else 31 | p[i] = s2[c++]; 32 | } 33 | p[i] = '\0'; 34 | 35 | return (p); 36 | } 37 | 38 | /** 39 | * _strlen - find length of string 40 | * @s: string 41 | * Return: length of string 42 | */ 43 | 44 | int _strlen(char *s) 45 | { 46 | int i; 47 | 48 | for (i = 0; s[i] != '\0'; i++) 49 | ; 50 | return (i); 51 | } 52 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _realloc - reallocates a memory block 5 | * @ptr: pointer to previous memory block 6 | * @old_size: size in bytes of allocated space for `ptr` 7 | * @new_size: size in bytes for new allocated space 8 | * Return: Pointer to new memory block, or NULL if error 9 | */ 10 | 11 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 12 | { 13 | char *p, *copy; 14 | unsigned int i; 15 | 16 | if (new_size == old_size) 17 | return (ptr); 18 | if (ptr != NULL && new_size == 0) 19 | { 20 | free(ptr); 21 | return (NULL); 22 | } 23 | if (ptr == NULL) 24 | { 25 | p = malloc(new_size); 26 | if (p == NULL) 27 | return (NULL); 28 | return (p); 29 | } 30 | 31 | p = malloc(new_size); 32 | if (p == NULL) 33 | return (NULL); 34 | copy = ptr; 35 | for (i = 0; i < old_size; i++) 36 | p[i] = copy[i]; 37 | free(ptr); 38 | return (p); 39 | } 40 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _calloc - allocates memory for an array given number of elements and size 5 | * @nmemb: number of elements 6 | * @size: size of each element 7 | * Return: Null if error, else pointer to allocated memory 8 | */ 9 | 10 | void *_calloc(unsigned int nmemb, unsigned int size) 11 | { 12 | int *p; 13 | unsigned int i; 14 | 15 | if (nmemb <= 0 || size <= 0) 16 | return (NULL); 17 | 18 | p = malloc(nmemb * size); 19 | if (p == NULL) 20 | return (NULL); 21 | for (i = 0; i < nmemb; i++) 22 | p[i] = 0; 23 | 24 | return (p); 25 | } 26 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * array_range - create an array of integers inclusive of min and max 5 | * @min: min value to include 6 | * @max: max value to include 7 | * Return: pointer to newly created array 8 | */ 9 | 10 | int *array_range(int min, int max) 11 | { 12 | int *p, i; 13 | 14 | if (min > max) 15 | return (NULL); 16 | 17 | p = malloc((max - min + 1) * sizeof(*p)); 18 | if (p == NULL) 19 | return (NULL); 20 | 21 | for (i = 0; min <= max; i++, min++) 22 | p[i] = min; 23 | 24 | return (p); 25 | } 26 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0B. C - more malloc, free -------------------------------------------------------------------------------- /0x0B-more_malloc_free/_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 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #include 2 | int _putchar(char c); 3 | void *malloc_checked(unsigned int b); 4 | char *string_nconcat(char *s1, char *s2, unsigned int n); 5 | int _strlen(char *s); 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 | -------------------------------------------------------------------------------- /0x0C-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef SIZE 2 | #define SIZE 1024 3 | #endif /* SIZE */ 4 | -------------------------------------------------------------------------------- /0x0C-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI 2 | #define PI 3.14159265359 3 | #endif /* PI */ 4 | -------------------------------------------------------------------------------- /0x0C-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print the name of the file compiled from 5 | * Return: 0; 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0C-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef THREE_FUNCTION_LIKE_MACRO_H 2 | #define THREE_FUNCTION_LIKE_MACRO_H 3 | #define ABS(x) (x > 0 ? (x) : (x) * -1) 4 | #endif /* THREE_FUNCTION_LIKE_MACRO_H */ 5 | -------------------------------------------------------------------------------- /0x0C-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef FOUR_SUM_H 2 | #define FOUR_SUM_H 3 | #define SUM(x, y) ((x) + (y)) 4 | #endif /* FOUR_SUM_H */ 5 | -------------------------------------------------------------------------------- /0x0C-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # 0x0C. C - Preprocessor -------------------------------------------------------------------------------- /0x0D-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - initialize a variable of type `struct dog` 6 | * @d: struct dog 7 | * @name: string for name 8 | * @age: integer for age 9 | * @owner: string for owners name 10 | */ 11 | void init_dog(struct dog *d, char *name, float age, char *owner) 12 | { 13 | if (d != NULL) 14 | { 15 | d->name = name; 16 | d->age = age; 17 | d->owner = owner; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * print_dog - Prints a struct dog 7 | * @d: pointer to struct dog 8 | */ 9 | 10 | void print_dog(struct dog *d) 11 | { 12 | if (d != NULL) 13 | { 14 | printf("Name: "); 15 | d->name == NULL ? printf("(nil)\n") : printf("%s\n", d->name); 16 | printf("Age: %f\n", d->age); 17 | printf("Owner: "); 18 | d->owner == NULL ? printf("(nil)\n") : printf("%s\n", d->owner); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * new_dog - create a new dog 6 | * @name: char string name 7 | * @age: int age 8 | * @owner: char string owner 9 | * Return: pointer to new dog 10 | */ 11 | dog_t *new_dog(char *name, float age, char *owner) 12 | { 13 | struct dog *doge; 14 | int i, j, k; 15 | char *n, *o; 16 | 17 | doge = malloc(sizeof(struct dog)); 18 | if (doge == NULL) 19 | return (NULL); 20 | 21 | for (i = 0; name[i] != '\0'; i++) 22 | ; 23 | for (j = 0; owner[j] != '\0'; j++) 24 | ; 25 | 26 | n = malloc(sizeof(char) * i + 1); 27 | if (n == NULL) 28 | { 29 | free(doge); 30 | return (NULL); 31 | } 32 | o = malloc(sizeof(char) * j + 1); 33 | if (o == NULL) 34 | { 35 | free(n); 36 | free(doge); 37 | return (NULL); 38 | } 39 | for (k = 0; k <= i; k++) 40 | n[k] = name[k]; 41 | for (k = 0; k <= j; k++) 42 | o[k] = owner[k]; 43 | 44 | doge->name = n; 45 | doge->age = age; 46 | doge->owner = o; 47 | 48 | return (doge); 49 | } 50 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - free the dogs 6 | * @d: pointer to dog struct 7 | */ 8 | 9 | void free_dog(dog_t *d) 10 | { 11 | if (d != NULL) 12 | { 13 | free(d->name); 14 | free(d->owner); 15 | 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # 0x0D. C - Structures, typedef -------------------------------------------------------------------------------- /0x0D-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * struct dog - structure definition of a dog 6 | * @name: character string 7 | * @age: integer 8 | * @owner: character string 9 | */ 10 | typedef struct dog 11 | { 12 | char *name; 13 | float age; 14 | char *owner; 15 | } dog_t; 16 | 17 | void init_dog(struct dog *d, char *name, float age, char *owner); 18 | void print_dog(struct dog *d); 19 | dog_t *new_dog(char *name, float age, char *owner); 20 | void free_dog(dog_t *d); 21 | #endif 22 | -------------------------------------------------------------------------------- /0x0E-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - Print a name from a function pointer 5 | * @name: char string 6 | * @f: function pointer that takes a string argument 7 | */ 8 | 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | f(name); 12 | } 13 | -------------------------------------------------------------------------------- /0x0E-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - execute a function pointer on each element of an array 5 | * @array: array of integers 6 | * @size: size of array 7 | * @action: function pointer 8 | */ 9 | 10 | void array_iterator(int *array, size_t size, void (*action)(int)) 11 | { 12 | size_t i; 13 | 14 | for (i = 0; i < size; i++) 15 | action(array[i]); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - Function that searches for an integer 5 | * @array: array of integers 6 | * @size: size of array 7 | * @cmp: function pointer 8 | * Return: index of first element that matches with `cmp`, or -1 if none found 9 | */ 10 | 11 | int int_index(int *array, int size, int (*cmp)(int)) 12 | { 13 | int i; 14 | 15 | if (size < 1 || array == NULL || cmp == NULL) 16 | return (-1); 17 | 18 | for (i = 0; i < size; i++) 19 | { 20 | if (cmp(array[i])) 21 | return (i); 22 | } 23 | 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | /** 4 | * struct op - Struct op 5 | * 6 | * @op: The operator 7 | * @f: The function associated 8 | */ 9 | typedef struct op 10 | { 11 | char *op; 12 | int (*f)(int a, int b); 13 | } op_t; 14 | int (*get_op_func(char *s))(int, int); 15 | int op_add(int a, int b); 16 | int op_sub(int a, int b); 17 | int op_mul(int a, int b); 18 | int op_div(int a, int b); 19 | int op_mod(int a, int b); 20 | #endif 21 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * get_op_func - select the correct operation function asked by user 7 | * @s: operator argument 8 | * Return: function pointer corresponding to operator given 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; 21 | 22 | i = 0; 23 | while (i < 5) 24 | { 25 | if (ops[i].op[0] == s[0]) 26 | return (ops[i].f); 27 | i++; 28 | } 29 | return (NULL); 30 | } 31 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - main function 7 | * @argc: argument count 8 | * @argv: string of arguments in array 9 | * Return: 0 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int a, b; 15 | int (*o)(int, int); 16 | 17 | if (argc != 4) 18 | { 19 | printf("Error\n"); 20 | exit(98); 21 | } 22 | if (argv[2][1] != '\0') 23 | { 24 | printf("Error\n"); 25 | exit(99); 26 | } 27 | 28 | o = get_op_func(argv[2]); 29 | if (o == 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", o(a, b)); 39 | 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * op_add - addition operator 6 | * @a: first int 7 | * @b: second int 8 | * Return: sum of 2 numbers 9 | */ 10 | int op_add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | 15 | /** 16 | * op_sub - subtraction operator 17 | * @a: first in 18 | * @b: second int 19 | * Return: difference of 2 numbers 20 | */ 21 | int op_sub(int a, int b) 22 | { 23 | return (a - b); 24 | } 25 | 26 | /** 27 | * op_mul - product of 2 numbers 28 | * @a: first int 29 | * @b: second int 30 | * Return: product of the numbers 31 | */ 32 | int op_mul(int a, int b) 33 | { 34 | return (a * b); 35 | } 36 | 37 | /** 38 | * op_div - division of 2 numbers 39 | * @a: first int 40 | * @b: second int 41 | * Return: result of the division of the numbers 42 | */ 43 | int op_div(int a, int b) 44 | { 45 | if (b == 0) 46 | { 47 | printf("Error\n"); 48 | exit(100); 49 | } 50 | return (a / b); 51 | } 52 | 53 | /** 54 | * op_mod - get remainder of the division of 2 numbers 55 | * @a: first int 56 | * @b: second int 57 | * Return: remainder of division of the numbers 58 | */ 59 | int op_mod(int a, int b) 60 | { 61 | if (b == 0) 62 | { 63 | printf("Error\n"); 64 | exit(100); 65 | } 66 | return (a % b); 67 | } 68 | -------------------------------------------------------------------------------- /0x0E-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # 0x0E. C - Function Pointers! -------------------------------------------------------------------------------- /0x0E-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | #include 4 | void print_name(char *name, void (*f)(char *)); 5 | void array_iterator(int *array, size_t size, void (*action)(int)); 6 | int int_index(int *array, int size, int (*cmp)(int)); 7 | #endif 8 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - return the sum of all its parameters 5 | * @n: number of extra params 6 | * Return: total sum 7 | */ 8 | int sum_them_all(const unsigned int n, ...) 9 | { 10 | unsigned int i; 11 | va_list list; 12 | int sum; 13 | 14 | va_start(list, n); 15 | for (i = 0, sum = 0; i < n; i++) 16 | sum += va_arg(list, int); 17 | 18 | va_end(list); 19 | 20 | return (sum); 21 | } 22 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | * print_numbers - print each number with separator, followed by a newline 6 | * @separator: string to be printed between numbers 7 | * @n: number of args passed 8 | */ 9 | 10 | void print_numbers(const char *separator, const unsigned int n, ...) 11 | { 12 | unsigned int i; 13 | va_list list; 14 | 15 | va_start(list, n); 16 | for (i = 0; i < n; i++) 17 | { 18 | printf("%d", va_arg(list, int)); 19 | if (i != (n - 1) && separator != NULL) 20 | printf("%s", separator); 21 | } 22 | printf("\n"); 23 | 24 | va_end(list); 25 | } 26 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | * print_strings - print a string followed by a newline 6 | * @separator: string to be printed between strings 7 | * @n: number of strings passed to function 8 | */ 9 | 10 | void print_strings(const char *separator, const unsigned int n, ...) 11 | { 12 | unsigned int i; 13 | va_list list; 14 | char *hold; 15 | 16 | va_start(list, n); 17 | for (i = 0; i < n; i++) 18 | { 19 | hold = va_arg(list, char*); 20 | if (hold == NULL) 21 | printf("(nil)"); 22 | else 23 | printf("%s", hold); 24 | if (i != (n - 1) && separator != NULL) 25 | printf("%s", separator); 26 | } 27 | printf("\n"); 28 | 29 | va_end(list); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # 0x0F. C - Variadic functions -------------------------------------------------------------------------------- /0x0F-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | #include 4 | int sum_them_all(const unsigned int n, ...); 5 | void print_numbers(const char *separator, const unsigned int n, ...); 6 | void print_strings(const char *separator, const unsigned int n, ...); 7 | void print_all(const char * const format, ...); 8 | void _printchar(va_list list); 9 | void _printstr(va_list list); 10 | void _printfloat(va_list list); 11 | void _printint(va_list list); 12 | 13 | typedef struct checker 14 | { 15 | char *type; 16 | void (*f)(); 17 | } checker; 18 | #endif 19 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_list - print all elements of `list_t` list 6 | * @h: head of linked list 7 | * Return: number of nodes 8 | */ 9 | size_t print_list(const list_t *h) 10 | { 11 | const list_t *current; 12 | size_t c; 13 | 14 | c = 0; 15 | current = h; 16 | while (current != NULL) 17 | { 18 | printf("[%d] %s\n", current->len, current->str); 19 | current = current->next; 20 | c++; 21 | } 22 | 23 | return (c); 24 | } 25 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - Returns the number of elements in a linked `list_t` list 5 | * @h: head of linked list 6 | * Return: number of elements in list 7 | */ 8 | size_t list_len(const list_t *h) 9 | { 10 | const list_t *current; 11 | size_t c; 12 | 13 | c = 0; 14 | current = h; 15 | while (current != NULL) 16 | { 17 | current = current->next; 18 | c++; 19 | } 20 | 21 | return (c); 22 | } 23 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * before_main - Print statement within this fxn before running main fxn. 5 | */ 6 | void __attribute__((constructor)) before_main(void) 7 | { 8 | printf("You're beat! and yet, you must allow,\n"); 9 | printf("I bore my house upon my back!\n"); 10 | } 11 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * add_node - add a new node at the beginning of `list_t` list 7 | * @head: double pointer to head 8 | * @str: string to be saved in new node, must be duplicated 9 | * Return: Address of new element or NULL if failed 10 | */ 11 | list_t *add_node(list_t **head, const char *str) 12 | { 13 | list_t *new_node; 14 | int c; 15 | 16 | for (c = 0; dup_str[c] != '\0'; c++) 17 | ; 18 | new_node = malloc(sizeof(list_t)); 19 | if (new_node == NULL) 20 | { 21 | free(new_node); 22 | return (NULL); 23 | } 24 | new_node->str = strdup(str); 25 | if (new_node->str == NULL) 26 | { 27 | free(new_node); 28 | return (NULL); 29 | } 30 | new_node->len = c; 31 | new_node->next = *head; 32 | 33 | *head = new_node; 34 | return (*head); 35 | } 36 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | /** 5 | * add_node_end - add a new node at the end of `list_t` list 6 | * @head: double pointer to head 7 | * @str: string to duplicate into new node 8 | * Return: Address of the new element or NULL if failed 9 | */ 10 | list_t *add_node_end(list_t **head, const char *str) 11 | { 12 | list_t *current; 13 | list_t *new_node; 14 | int c; 15 | 16 | current = *head; 17 | while (current && current->next != NULL) 18 | current = current->next; 19 | 20 | for (c = 0; dup_str[c] != '\0'; c++) 21 | ; 22 | 23 | new_node = malloc(sizeof(list_t)); 24 | if (new_node == NULL) 25 | { 26 | free(new_node); 27 | return (NULL); 28 | } 29 | new_node->str = strdup(str); 30 | if (new_node->str == NULL) 31 | { 32 | free(new_node); 33 | return (NULL); 34 | } 35 | new_node->len = c; 36 | new_node->next = NULL; 37 | 38 | if (current) 39 | current->next = new_node; 40 | else 41 | *head = new_node; 42 | return (new_node); 43 | } 44 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - free a `list_t` list 5 | * @head: head of linked list 6 | */ 7 | void free_list(list_t *head) 8 | { 9 | list_t *current; 10 | list_t *nxt; 11 | 12 | current = head; 13 | 14 | while (current != NULL) 15 | { 16 | nxt = current->next; 17 | free(current->str); 18 | free(current); 19 | current = nxt; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x11. C - Singly linked lists -------------------------------------------------------------------------------- /0x11-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | 6 | /** 7 | * struct list_s - singly linked list 8 | * @str: string - (malloc'ed string) 9 | * @len: length of the string 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | * for Holberton project 14 | */ 15 | typedef struct list_s 16 | { 17 | char *str; 18 | unsigned int len; 19 | struct list_s *next; 20 | } list_t; 21 | 22 | size_t print_list(const list_t *h); 23 | size_t list_len(const list_t *h); 24 | list_t *add_node(list_t **head, const char *str); 25 | list_t *add_node_end(list_t **head, const char *str); 26 | void free_list(list_t *head); 27 | #endif 28 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_listint - print all elements of a `listint_t` list 6 | * @h: linked list head 7 | * Return: number of nodes 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | const listint_t *c; 12 | size_t count; 13 | 14 | for (c = h, count = 0; c != NULL; count++, c = c->next) 15 | printf("%d\n", c->n); 16 | 17 | return (count); 18 | } 19 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - find the number of elements in a linked `listint_t` list 5 | * @h: linked list head 6 | * Return: number of elements in linked list 7 | */ 8 | size_t listint_len(const listint_t *h) 9 | { 10 | const listint_t *c; 11 | size_t count; 12 | 13 | for (c = h, count = 0; c != NULL; count++, c = c->next) 14 | ; 15 | 16 | return (count); 17 | } 18 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - delete the node at index of a `listint_t` list 5 | * @head: double header to head of linked list 6 | * @index: index of node to delete, starting at 0 7 | * Return: 1 if success, -1 if failed 8 | */ 9 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 10 | { 11 | listint_t *current; 12 | listint_t *hold; 13 | unsigned int i; 14 | 15 | i = 0; 16 | current = *head; 17 | if (*head == NULL) 18 | return (-1); 19 | 20 | while (i < index) 21 | { 22 | i++; 23 | hold = current; 24 | if (current->next) 25 | current = current->next; 26 | else 27 | return (-1); 28 | } 29 | 30 | if (index == 0) 31 | *head = current->next; 32 | else if (current->next) 33 | hold->next = current->next; 34 | else 35 | hold->next = NULL; 36 | 37 | free(current); 38 | return (1); 39 | } 40 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * reverse_listint - Reverse a `listint_t` linked list 5 | * @head: double pointer to head 6 | * Description: Limited to going through loop only once. 7 | * Not allowed to use `malloc` or `free` or arrays 8 | * Can only declare a max of 2 variables. 9 | * Return: pointer to first node of reversed list 10 | */ 11 | listint_t *reverse_listint(listint_t **head) 12 | { 13 | listint_t *hold; 14 | listint_t *current; 15 | 16 | if (*head == NULL) 17 | return (NULL); 18 | 19 | current = *head; 20 | *head = current->next; 21 | hold = (*head)->next; 22 | current->next = NULL; 23 | if (*head == NULL) 24 | { 25 | *head = current; 26 | return (current); 27 | } 28 | 29 | while (hold != NULL) 30 | { 31 | (*head)->next = current; 32 | current = *head; 33 | *head = hold; 34 | hold = (*head)->next; 35 | } 36 | 37 | (*head)->next = current; 38 | return (*head); 39 | } 40 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_listint_safe - Print a `listint_t` linked list including mem addresses 6 | * @head: head of linked list 7 | * Description: Go through the list only once. 8 | * Return: number of nodes in list. If fails, exit with status 98. 9 | */ 10 | size_t print_listint_safe(const listint_t *head) 11 | { 12 | const listint_t *current; 13 | size_t count; 14 | const listint_t *hold; 15 | 16 | current = head; 17 | if (current == NULL) 18 | exit(98); 19 | 20 | count = 0; 21 | while (current != NULL) 22 | { 23 | hold = current; 24 | current = current->next; 25 | count++; 26 | printf("[%p] %d\n", (void *)hold, hold->n); 27 | 28 | if (hold < current) 29 | { 30 | printf("-> [%p] %d\n", (void *)current, current->n); 31 | break; 32 | } 33 | } 34 | 35 | return (count); 36 | } 37 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint_safe - free a `listint_t` list and set the head to null 5 | * @h: double pointer to head of linked list 6 | * Description: This function should work for circular lists 7 | * Only loop through the list once 8 | * Return: size of the list that was free'd 9 | */ 10 | size_t free_listint_safe(listint_t **h) 11 | { 12 | listint_t *current, *hold; 13 | size_t count; 14 | 15 | count = 0; 16 | current = *h; 17 | while (current != NULL) 18 | { 19 | count++; 20 | hold = current; 21 | current = current->next; 22 | free(hold); 23 | 24 | if (hold < current) 25 | break; 26 | } 27 | *h = NULL; 28 | 29 | return (count); 30 | } 31 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * find_listint_loop - find the loop in a linked list 5 | * @head: head of linked list 6 | * Description: Not allowed to use malloc, free or arrays. 7 | * Can only declare a max of 2 variables. 8 | * Return: Address of node where loop starts, or NULL if no loop found. 9 | */ 10 | listint_t *find_listint_loop(listint_t *head) 11 | { 12 | listint_t *currents, *currentf; 13 | 14 | if (head == NULL) 15 | return (NULL); 16 | 17 | currents = currentf = head; 18 | do { 19 | if (currents->next) 20 | currents = currents->next; 21 | else 22 | return (NULL); 23 | 24 | if (currentf->next->next) 25 | currentf = currentf->next->next; 26 | else 27 | return (NULL); 28 | } while (currentf != currents); 29 | 30 | currents = head; 31 | while (currentf != currents) 32 | { 33 | currentf = currentf->next; 34 | currents = currents->next; 35 | } 36 | 37 | return (currents); 38 | } 39 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - add a new node at the beginning of a `listint_t` list 5 | * @head: double pointer to head node 6 | * @n: int value to store in new node 7 | * Return: Address of new element or NULL if failed 8 | */ 9 | listint_t *add_nodeint(listint_t **head, const int n) 10 | { 11 | listint_t *hold; 12 | listint_t *new; 13 | 14 | hold = *head; 15 | new = malloc(sizeof(listint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | new->n = n; 19 | *head = new; 20 | 21 | if (hold == NULL) 22 | new->next = NULL; 23 | else 24 | new->next = hold; 25 | 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /0x12-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 of a `listint_t` list 5 | * @head: double pointer to head node 6 | * @n: int value to store in new node 7 | * Return: Address of new element or NULL if failed 8 | */ 9 | listint_t *add_nodeint_end(listint_t **head, const int n) 10 | { 11 | listint_t *current; 12 | listint_t *new; 13 | 14 | current = *head; 15 | while (current && current->next != NULL) 16 | current = current->next; 17 | 18 | new = malloc(sizeof(listint_t)); 19 | if (new == NULL) 20 | return (NULL); 21 | new->n = n; 22 | new->next = NULL; 23 | 24 | if (current) 25 | current->next = new; 26 | else 27 | *head = new; 28 | return (new); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - free a `listint_t` list 5 | * @head: head of linked list 6 | */ 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *hold; 10 | 11 | while (head != NULL) 12 | { 13 | hold = head; 14 | head = head->next; 15 | free(hold); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - free a `listint_t` list 5 | * @head: double pointer to head 6 | */ 7 | void free_listint2(listint_t **head) 8 | { 9 | listint_t *hold; 10 | 11 | if (head == NULL) 12 | return; 13 | 14 | while (*head != NULL) 15 | { 16 | hold = *head; 17 | *head = (*head)->next; 18 | free(hold); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - Delete the head node of a `listint_t` linked list 5 | * @head: double pointer to head node of linked list 6 | * Return: head nodes data `n` 7 | */ 8 | int pop_listint(listint_t **head) 9 | { 10 | int num; 11 | listint_t *hold; 12 | 13 | if (*head == NULL) 14 | return (0); 15 | 16 | hold = *head; 17 | num = hold->n; 18 | *head = hold->next; 19 | free(hold); 20 | 21 | return (num); 22 | } 23 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - Get the nth node of a `listint_t` linked list 5 | * @head: pointer to head node 6 | * @index: index to find in linked list, starting at 0 7 | * Return: pointer to node or NULL if failed 8 | */ 9 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 10 | { 11 | listint_t *current; 12 | unsigned int count; 13 | 14 | current = head; 15 | count = 0; 16 | while (count < index) 17 | { 18 | if (current) 19 | current = current->next; 20 | else 21 | return (NULL); 22 | count++; 23 | } 24 | 25 | if (current) 26 | return (current); 27 | else 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - get the sum of all the int `n` of a `listint_t` linked list 5 | * @head: pointer to head 6 | * Return: sum of all int `n` 7 | */ 8 | int sum_listint(listint_t *head) 9 | { 10 | int sum; 11 | listint_t *current; 12 | 13 | for (sum = 0, current = head; current != NULL; current = current->next) 14 | sum += current->n; 15 | 16 | return (sum); 17 | } 18 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * insert_nodeint_at_index - insert a new node at a given position 5 | * @head: double pointer to head 6 | * @index: insert node at this index, starting count at 0 7 | * @n: value to store in node 8 | * Return: Address of new node or NULL if failed 9 | */ 10 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int index, int n) 11 | { 12 | listint_t *new; 13 | listint_t *current; 14 | unsigned int count; 15 | 16 | if (head == NULL) 17 | return (NULL); 18 | 19 | current = *head; 20 | for (count = 1; current && count < index; count++) 21 | { 22 | current = current->next; 23 | if (current == NULL) 24 | return (NULL); 25 | } 26 | 27 | new = malloc(sizeof(listint_t)); 28 | if (new == NULL) 29 | { 30 | free(new); 31 | return (NULL); 32 | } 33 | new->n = n; 34 | 35 | if (index == 0) 36 | { 37 | *head = new; 38 | new->next = current; 39 | } 40 | else if (current->next) 41 | { 42 | new->next = current->next; 43 | current->next = new; 44 | } 45 | else 46 | { 47 | new->next = NULL; 48 | current->next = new; 49 | } 50 | 51 | return (new); 52 | } 53 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x12. C - More singly linked lists -------------------------------------------------------------------------------- /0x13-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * binary_to_uint - convert a binary number to an unsigned int 6 | * @b: char string 7 | * Return: converted decimal number or 0 if there is an unconvertable char 8 | */ 9 | unsigned int binary_to_uint(const char *b) 10 | { 11 | unsigned int total, power; 12 | int len; 13 | 14 | if (b == NULL) 15 | return (0); 16 | 17 | for (len = 0; b[len]; len++) 18 | { 19 | if (b[len] != '0' && b[len] != '1') 20 | return (0); 21 | } 22 | 23 | for (power = 1, total = 0, len--; len >= 0; len--, power *= 2) 24 | { 25 | if (b[len] == '1') 26 | total += power; 27 | } 28 | 29 | return (total); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_binary - print binary representation of a number 6 | * @n: decimal number to print as binary 7 | */ 8 | void print_binary(unsigned long int n) 9 | { 10 | unsigned long int temp; 11 | int shifts; 12 | 13 | if (n == 0) 14 | { 15 | printf("0"); 16 | return; 17 | } 18 | 19 | for (temp = n, shifts = 0; (temp >>= 1) > 0; shifts++) 20 | ; 21 | 22 | for (; shifts >= 0; shifts--) 23 | { 24 | if ((n >> shifts) & 1) 25 | printf("1"); 26 | else 27 | printf("0"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * get_endianness - check endianness 5 | * Return: 0 if big endian, or 1 if little endian 6 | */ 7 | int get_endianness(void) 8 | { 9 | int num; 10 | 11 | num = 1; 12 | if (*(char *)&num == 1) 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol -------------------------------------------------------------------------------- /0x13-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * get_bit - get the value of a bit at a given index 5 | * @n: number to evaluate 6 | * @index: index starting from 0, of the bit we want to get 7 | * Return: Value of bit at index, or -1 if error 8 | */ 9 | int get_bit(unsigned long int n, unsigned int index) 10 | { 11 | unsigned long int hold; 12 | 13 | if (index > 64) 14 | return (-1); 15 | 16 | hold = n >> index; 17 | 18 | return (hold & 1); 19 | } 20 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * set_bit - sets value of a bit to 1 at a given index 5 | * @n: decimal number passed by pointer 6 | * @index: index position to change, starting from 0 7 | * Return: 1 if it worked, -1 if error 8 | */ 9 | int set_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned long int p; 12 | 13 | if (index > 64) 14 | return (-1); 15 | 16 | for (p = 1; index > 0; index--, p *= 2) 17 | ; 18 | *n += p; 19 | 20 | return (1); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * clear_bit - set the value of a bit to 0 at a given index 5 | * @n: pointer to decimal number to change 6 | * @index: index position to change 7 | * Return: 1 if it worked, -1 if error 8 | */ 9 | int clear_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned long int i; 12 | unsigned int hold; 13 | 14 | if (index > 64) 15 | return (-1); 16 | hold = index; 17 | for (i = 1; hold > 0; i *= 2, hold--) 18 | ; 19 | 20 | if ((*n >> index) & 1) 21 | *n -= i; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * flip_bits - flip bits to convert one number to another number 6 | * @n: first number 7 | * @m: second number to convert to 8 | * Return: number of bits that was needed to flip 9 | */ 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 11 | { 12 | unsigned long int diff; 13 | int counter; 14 | 15 | diff = n ^ m; 16 | counter = 0; 17 | 18 | while (diff) 19 | { 20 | counter++; 21 | diff &= (diff - 1); 22 | } 23 | 24 | return (counter); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # 0x13. C - Bit manipulation -------------------------------------------------------------------------------- /0x13-bit_manipulation/_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 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | int _putchar(char c); 4 | unsigned int binary_to_uint(const char *b); 5 | void print_binary(unsigned long int n); 6 | int get_bit(unsigned long int n, unsigned int index); 7 | int set_bit(unsigned long int *n, unsigned int index); 8 | int clear_bit(unsigned long int *n, unsigned int index); 9 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 10 | int get_endianness(void); 11 | #endif 12 | -------------------------------------------------------------------------------- /0x14-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * read_textfile - Read a text file and print to POSIX stdout 5 | * @filename: char string of files name 6 | * @letters: number of letters to read and print 7 | * Return: number of letters read and printed, or 0 if error 8 | */ 9 | ssize_t read_textfile(const char *filename, size_t letters) 10 | { 11 | int fd; 12 | ssize_t rcount, wcount; 13 | char *buffer; 14 | 15 | if (filename == NULL) 16 | return (0); 17 | 18 | fd = open(filename, O_RDWR); 19 | if (fd == -1) 20 | return (0); 21 | 22 | buffer = malloc(sizeof(char) * letters); 23 | if (buffer == NULL) 24 | { 25 | free(buffer); 26 | return (0); 27 | } 28 | rcount = read(fd, buffer, letters); 29 | if (rcount == -1) 30 | return (0); 31 | 32 | wcount = write(STDOUT_FILENO, buffer, rcount); 33 | if (wcount == -1 || rcount != wcount) 34 | return (0); 35 | free(buffer); 36 | 37 | close(fd); 38 | return (wcount); 39 | } 40 | -------------------------------------------------------------------------------- /0x14-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * create_file - create a file with read/write access for user 5 | * @filename: name of file to create 6 | * @text_content: string to write to file 7 | * Return: 1 on success, -1 on failure 8 | */ 9 | int create_file(const char *filename, char *text_content) 10 | { 11 | int fd, rstatus, i; 12 | 13 | if (filename == NULL) 14 | return (-1); 15 | 16 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR); 17 | if (fd == -1) 18 | return (-1); 19 | 20 | if (text_content) 21 | { 22 | for (i = 0; text_content[i] != '\0'; i++) 23 | ; 24 | rstatus = write(fd, text_content, i); 25 | if (rstatus == -1) 26 | return (-1); 27 | } 28 | 29 | close(fd); 30 | return (1); 31 | } 32 | -------------------------------------------------------------------------------- /0x14-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * append_text_to_file - Append text to the end of a file 5 | * @filename: name of file 6 | * @text_content: string to add to end of file 7 | * Return: 1 on success, -1 on failure 8 | */ 9 | int append_text_to_file(const char *filename, char *text_content) 10 | { 11 | int fd, status, i; 12 | 13 | if (filename == NULL) 14 | return (-1); 15 | if (text_content == NULL) 16 | return (1); 17 | 18 | fd = open(filename, O_APPEND | O_WRONLY); 19 | if (fd == -1) 20 | return (-1); 21 | 22 | for (i = 0; text_content[i] != '\0'; i++) 23 | ; 24 | status = write(fd, text_content, i); 25 | if (status == -1) 26 | return (-1); 27 | 28 | close(fd); 29 | return (1); 30 | } 31 | -------------------------------------------------------------------------------- /0x14-file_io/README.md: -------------------------------------------------------------------------------- 1 | # 0x14. C - File I/O -------------------------------------------------------------------------------- /0x14-file_io/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | ssize_t read_textfile(const char *filename, size_t letters); 13 | int create_file(const char *filename, char *text_content); 14 | int append_text_to_file(const char *filename, char *text_content); 15 | #endif 16 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - Print all the values of each node in a `dlistint_t` list 5 | * @h: head pointer in doubly linked list 6 | * Return: Number of nodes in LL 7 | */ 8 | size_t print_dlistint(const dlistint_t *h) 9 | { 10 | const dlistint_t *temp; 11 | size_t count; 12 | 13 | temp = h; 14 | while (temp && temp->prev) 15 | temp = temp->prev; 16 | 17 | for (count = 0; temp; count++, temp = temp->next) 18 | printf("%d\n", temp->n); 19 | 20 | return (count); 21 | } 22 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - Find the number of nodes in a doubly linked list 5 | * @h: head pointer to LL 6 | * Return: number of nodes in LL 7 | */ 8 | size_t dlistint_len(const dlistint_t *h) 9 | { 10 | const dlistint_t *tmp; 11 | size_t count; 12 | 13 | tmp = h; 14 | while (tmp && tmp->prev) 15 | tmp = tmp->prev; 16 | 17 | for (count = 0; tmp; count++, tmp = tmp->next) 18 | ; 19 | 20 | return (count); 21 | } 22 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - Add a new node at the beginning of a `dlistint_t` list 5 | * @head: double pointer to head node 6 | * @n: value to store in node 7 | * Return: address of new node, or NULL if failed 8 | */ 9 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 10 | { 11 | dlistint_t *tmp, *new; 12 | 13 | tmp = *head; 14 | new = malloc(sizeof(dlistint_t)); 15 | if (new == NULL) 16 | return (NULL); 17 | 18 | new->n = n; 19 | new->prev = NULL; 20 | if (tmp == NULL) 21 | new->next = NULL; 22 | else 23 | { 24 | while (tmp->prev) 25 | tmp = tmp->prev; 26 | 27 | tmp->prev = new; 28 | new->next = tmp; 29 | } 30 | *head = new; 31 | 32 | return (*head); 33 | } 34 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - Add a new node at the end of a `dlistint_t` list 5 | * @head: double pointer to head node 6 | * @n: value to store in new node 7 | * Return: Address of new element, or NULL if it failed 8 | */ 9 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 10 | { 11 | dlistint_t *tmp, *new; 12 | 13 | new = malloc(sizeof(dlistint_t)); 14 | if (new == NULL) 15 | return (NULL); 16 | 17 | new->n = n; 18 | new->next = NULL; 19 | tmp = *head; 20 | while (tmp && tmp->next) 21 | tmp = tmp->next; 22 | 23 | if (tmp) 24 | { 25 | new->prev = tmp; 26 | tmp->next = new; 27 | } 28 | else 29 | { 30 | *head = new; 31 | new->prev = NULL; 32 | } 33 | 34 | return (new); 35 | } 36 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - free a `dlistint_t` doubly linked list 5 | * @head: head of LL 6 | */ 7 | void free_dlistint(dlistint_t *head) 8 | { 9 | dlistint_t *tmp, *hold; 10 | 11 | if (head == NULL) 12 | return; 13 | 14 | tmp = head; 15 | while (tmp->prev) 16 | tmp = tmp->prev; 17 | while (tmp) 18 | { 19 | hold = tmp; 20 | tmp = tmp->next; 21 | free(hold); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - get the nth node of a `dlistint_t` doubly LL 5 | * @head: pointer to head of LL 6 | * @index: node index to return 7 | * Return: node at index given or NULL if node does not exist 8 | */ 9 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 10 | { 11 | dlistint_t *tmp; 12 | unsigned int c; 13 | 14 | tmp = head; 15 | for (c = 0; tmp && c < index; tmp = tmp->next, c++) 16 | ; 17 | 18 | return (tmp); 19 | } 20 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - get the sum of all the data `n` of a `dlistint_t` LL 5 | * @head: head of doubly LL 6 | * Return: sum of all `n` or 0 if LL is empty 7 | */ 8 | int sum_dlistint(dlistint_t *head) 9 | { 10 | int sum; 11 | dlistint_t *tmp; 12 | 13 | tmp = head; 14 | for (sum = 0; tmp; tmp = tmp->next) 15 | sum += tmp->n; 16 | 17 | return (sum); 18 | } 19 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - Delete node at index of a `dlistint_t` LL 5 | * @head: double pointer to head of LL 6 | * @index: index position to remove from LL 7 | * Return: 1 if succeeded, -1 if failed 8 | */ 9 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 10 | { 11 | dlistint_t *tmp, *next, *prev; 12 | unsigned int c; 13 | 14 | if (*head == NULL) 15 | return (-1); 16 | 17 | for (tmp = *head, c = 0, prev = NULL; tmp && c < index; c++) 18 | { 19 | prev = tmp; 20 | tmp = tmp->next; 21 | } 22 | 23 | if (tmp == NULL) 24 | return (-1); 25 | 26 | next = tmp->next; 27 | if (prev == NULL) 28 | { 29 | free(tmp); 30 | if (next != NULL) 31 | { 32 | next->prev = NULL; 33 | *head = next; 34 | } 35 | else 36 | *head = NULL; 37 | } 38 | else if (next == NULL) 39 | { 40 | prev->next = NULL; 41 | free(tmp); 42 | } 43 | else 44 | { 45 | prev->next = next; 46 | next->prev = prev; 47 | free(tmp); 48 | } 49 | 50 | return (1); 51 | } 52 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x16. Doubly linked lists -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 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 Holberton 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_idx(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | #endif 33 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/0-memset.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c -Wall -Werror -Wextra -pedantic -fpic *.c 3 | gcc -shared *.o -o liball.so 4 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/3-islower.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/3-puts.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/6-abs.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x17. C - Dynamic libraries -------------------------------------------------------------------------------- /0x17-dynamic_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/_putchar.o -------------------------------------------------------------------------------- /0x17-dynamic_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | int _putchar(char c); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int _abs(int n); 7 | int _isupper(int c); 8 | int _isdigit(int c); 9 | int _strlen(char *s); 10 | void _puts(char *s); 11 | char *_strcpy(char *dest, char *src); 12 | int _atoi(char *s); 13 | char *_strcat(char *dest, char *src); 14 | char *_strncat(char *dest, char *src, int n); 15 | char *_strncpy(char *dest, char *src, int n); 16 | int _strcmp(char *s1, char *s2); 17 | char *_memset(char *s, char b, unsigned int n); 18 | char *_memcpy(char *dest, char *src, unsigned int n); 19 | char *_strchr(char *s, char c); 20 | unsigned int _strspn(char *s, char *accept); 21 | char *_strpbrk(char *s, char *accept); 22 | char *_strstr(char *haystack, char *needle); 23 | #endif 24 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/libholberton.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipyoo/holbertonschool-low_level_programming/88bef44f07976ff234fdf0dcb162aa5d604126de/0x17-dynamic_libraries/libholberton.so -------------------------------------------------------------------------------- /0x19-hash_tables/.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | a.out 3 | #* -------------------------------------------------------------------------------- /0x19-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Create a hash table 5 | * @size: size of the array 6 | * Return: new hash table or NULL if error 7 | */ 8 | hash_table_t *hash_table_create(unsigned long int size) 9 | { 10 | hash_table_t *hsh; 11 | unsigned long int i; 12 | 13 | if (size == 0) 14 | return (NULL); 15 | 16 | hsh = malloc(sizeof(hash_table_t)); 17 | if (hsh == NULL) 18 | return (NULL); 19 | 20 | hsh->array = malloc(sizeof(hash_node_t *) * size); 21 | if (hsh->array == NULL) 22 | { 23 | free(hsh); 24 | return (NULL); 25 | } 26 | hsh->size = size; 27 | 28 | for (i = 0; i < size; i++) 29 | hsh->array[i] = NULL; 30 | 31 | return (hsh); 32 | } 33 | -------------------------------------------------------------------------------- /0x19-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - hash function implementing the djb2 algorithm 5 | * @str: string to hash 6 | * Return: hashed value 7 | */ 8 | unsigned long int hash_djb2(const unsigned char *str) 9 | { 10 | unsigned long int hsh; 11 | int c; 12 | 13 | hsh = 5381; 14 | while ((c = *str++)) 15 | hsh = ((hsh << 5) + hsh) + c; /* hsh * 33 + c */ 16 | 17 | return (hsh); 18 | } 19 | -------------------------------------------------------------------------------- /0x19-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - Gives the index of a key 5 | * @key: key of key/value pair 6 | * @size: size of array of the hash table 7 | * Return: index where key/value pair should be stored in array of hash table 8 | */ 9 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 10 | { 11 | return (hash_djb2(key) % size); 12 | } 13 | -------------------------------------------------------------------------------- /0x19-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_set - Adds an element to the hash table 5 | * @ht: hash table to add to 6 | * @key: key to add 7 | * @value: value to store for key 8 | * Return: 1 if success, 0 if fail 9 | */ 10 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 11 | { 12 | hash_node_t *newpair, *tmp; 13 | unsigned long int idx; 14 | 15 | if (ht == NULL || key == NULL || value == NULL) 16 | return (0); 17 | 18 | idx = key_index((const unsigned char *)key, ht->size); 19 | tmp = ht->array[idx]; 20 | 21 | if (tmp != NULL) 22 | { 23 | while (tmp) 24 | { 25 | if (strcmp(tmp->key, key) == 0) 26 | { 27 | tmp->value = strdup(value); 28 | return (1); 29 | } 30 | tmp = tmp->next; 31 | } 32 | } 33 | 34 | newpair = malloc(sizeof(hash_node_t)); 35 | if (newpair == NULL) 36 | return (0); 37 | 38 | newpair->key = strdup(key); 39 | newpair->value = strdup(value); 40 | newpair->next = NULL; 41 | 42 | if (ht->array[idx] != NULL) 43 | newpair->next = ht->array[idx]; 44 | 45 | ht->array[idx] = newpair; 46 | 47 | return (1); 48 | } 49 | -------------------------------------------------------------------------------- /0x19-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - Retrieve a value associated with a key 5 | * @ht: hash table 6 | * @key: key to use to retrieve value 7 | * Return: value associated with key or NULL if key not found 8 | */ 9 | char *hash_table_get(const hash_table_t *ht, const char *key) 10 | { 11 | hash_node_t *tmp; 12 | unsigned long int idx; 13 | 14 | if (ht == NULL || key == NULL) 15 | return (NULL); 16 | 17 | idx = key_index((const unsigned char *)key, ht->size); 18 | tmp = ht->array[idx]; 19 | 20 | if (tmp == NULL) 21 | return (NULL); 22 | 23 | while (strcmp(tmp->key, key) != 0) 24 | tmp = tmp->next; 25 | 26 | if (tmp == NULL) 27 | return (NULL); 28 | 29 | return (tmp->value); 30 | } 31 | -------------------------------------------------------------------------------- /0x19-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - Print a hash table 5 | * @ht: hash table 6 | */ 7 | void hash_table_print(const hash_table_t *ht) 8 | { 9 | hash_node_t *tmp; 10 | unsigned long int i; 11 | int toggle; 12 | 13 | if (ht == NULL) 14 | return; 15 | 16 | printf("{"); 17 | 18 | for (i = 0, toggle = 0; i < ht->size; i++) 19 | { 20 | tmp = ht->array[i]; 21 | if (tmp != NULL) 22 | { 23 | if (toggle == 1) 24 | printf(", "); 25 | printf("'%s': '%s'", tmp->key, tmp->value); 26 | while ((tmp = tmp->next) != NULL) 27 | { 28 | printf(", "); 29 | printf("'%s': '%s'", tmp->key, tmp->value); 30 | } 31 | 32 | toggle = 1; 33 | } 34 | } 35 | 36 | printf("}\n"); 37 | } 38 | -------------------------------------------------------------------------------- /0x19-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - delete given hash table 5 | * @ht: hash table given 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | hash_node_t *tmp, *prev; 10 | unsigned long int sz, i; 11 | 12 | if (ht == NULL) 13 | return; 14 | if (ht->array == NULL) 15 | { 16 | free(ht); 17 | return; 18 | } 19 | 20 | for (i = 0, sz = ht->size; i < sz; i++) 21 | { 22 | tmp = ht->array[i]; 23 | while (tmp) 24 | { 25 | prev = tmp; 26 | tmp = tmp->next; 27 | free(prev->key); 28 | free(prev->value); 29 | free(prev); 30 | } 31 | } 32 | 33 | free(ht->array); 34 | free(ht); 35 | } 36 | -------------------------------------------------------------------------------- /0x19-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | 0x19. C - Hash Tables -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/0-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/0-bubble_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * bubble_sort - Sort an array of integers in ascending order 5 | * using bubble sort algorithm. 6 | * @array: array of integers to sort 7 | * @size: size of array 8 | */ 9 | void bubble_sort(int *array, size_t size) 10 | { 11 | size_t i, swapped; 12 | int tmp; 13 | 14 | if (size == 0 || array == NULL) 15 | return; 16 | 17 | swapped = 1; 18 | while (swapped) 19 | { 20 | swapped = 0; 21 | 22 | for (i = 0; i < size - 1; i++) 23 | { 24 | if (array[i] > array[i + 1]) 25 | { 26 | tmp = array[i + 1]; 27 | array[i + 1] = array[i]; 28 | array[i] = tmp; 29 | swapped = 1; 30 | print_array(array, size); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/1-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/1-insertion_sort_list.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * get_head - Get the head of a doubly linked list 5 | * @tmp: node in linked list 6 | * Return: head of linked list 7 | */ 8 | listint_t *get_head(listint_t *tmp) 9 | { 10 | while (tmp->prev) 11 | tmp = tmp->prev; 12 | 13 | return (tmp); 14 | } 15 | 16 | /** 17 | * insertion_sort_list - Sort a doubly linked list of integers 18 | * in ascending order using insertion sort algorithm. 19 | * @list: doubly linked list 20 | */ 21 | void insertion_sort_list(listint_t **list) 22 | { 23 | listint_t *tmp, *hold, *ptmp; 24 | 25 | if (list == NULL) 26 | return; 27 | tmp = get_head(*list); 28 | 29 | for (tmp = tmp->next; tmp;) 30 | { 31 | hold = tmp->next; 32 | while (tmp->prev && tmp->n < tmp->prev->n) 33 | { 34 | ptmp = tmp->prev; 35 | ptmp->next = tmp->next; 36 | tmp->prev = ptmp->prev; 37 | ptmp->prev = tmp; 38 | tmp->next = ptmp; 39 | if (ptmp->next) 40 | ptmp->next->prev = ptmp; 41 | if (tmp->prev) 42 | tmp->prev->next = tmp; 43 | print_list(get_head(*list)); 44 | } 45 | tmp = hold; 46 | } 47 | *list = get_head(*list); 48 | } 49 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/10-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/100-shell_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * shell_sort - sort an array of integers in ascending order 5 | * @array: array of integers 6 | * @size: size of array of integers 7 | */ 8 | void shell_sort(int *array, size_t size) 9 | { 10 | size_t gap; 11 | 12 | if (size <= 1 || array == NULL) 13 | return; 14 | 15 | gap = 1; 16 | while ((gap * 3 + 1) < size) 17 | gap = gap * 3 + 1; 18 | 19 | for (; gap > 0; gap /= 3) 20 | { 21 | insertion_sort(array, (int)size, (int)gap); 22 | print_array(array, size); 23 | } 24 | } 25 | 26 | /** 27 | * insertion_sort - sort an array of integers based on a gap for shell sort 28 | * @array: array of integers 29 | * @size: size of array of integers 30 | * @diff: gap given by shell sort 31 | */ 32 | void insertion_sort(int *array, int size, int diff) 33 | { 34 | int i, j, hold; 35 | 36 | for (i = diff; i < size; i++) 37 | { 38 | for (j = i; j - diff >= 0; j -= diff) 39 | { 40 | if (array[j] < array[j - diff]) 41 | { 42 | hold = array[j]; 43 | array[j] = array[j - diff]; 44 | array[j - diff] = hold; 45 | } 46 | else 47 | break; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/103-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(nlog(n)) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/104-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(nlog(n)) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/11-O: -------------------------------------------------------------------------------- 1 | O(n^2) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/12-O: -------------------------------------------------------------------------------- 1 | O(2^n) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/13-O: -------------------------------------------------------------------------------- 1 | O(n^2) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/14-O: -------------------------------------------------------------------------------- 1 | O(n^2) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/15-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | O(n) 3 | O(n) 4 | O(n) 5 | O(1) 6 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/16-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n) 3 | O(n) 4 | O(n) 5 | O(1) 6 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/17-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n) 3 | O(n) 4 | O(n) 5 | O(1) 6 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/18-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | O(n) 3 | O(n) 4 | O(n) 5 | O(1) 6 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/19-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | O(1) 3 | O(n) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n^2) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/2-selection_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * selection_sort - sort an array of integers in ascending order 5 | * @array: array of integers 6 | * @size: size of array 7 | */ 8 | void selection_sort(int *array, size_t size) 9 | { 10 | size_t i, j, min; 11 | int hold; 12 | 13 | if (size == 0 || array == NULL) 14 | return; 15 | 16 | for (i = 0; i < size - 1; i++) 17 | { 18 | min = i; 19 | for (j = i; j < size; j++) 20 | { 21 | if (array[j] < array[min]) 22 | min = j; 23 | } 24 | if (i != min) 25 | { 26 | hold = array[i]; 27 | array[i] = array[min]; 28 | array[min] = hold; 29 | print_array(array, size); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/20-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | O(1) 3 | O(n) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/21-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | O(n) 3 | O(1) 4 | O(n) 5 | O(1) 6 | O(n) 7 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/7-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/8-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/9-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/README.md: -------------------------------------------------------------------------------- 1 | 0x1A. C - Sorting algorithms & Big O -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * print_array - Prints an array of integers 6 | * 7 | * @array: The array to be printed 8 | * @size: Number of elements in @array 9 | */ 10 | void print_array(const int *array, size_t size) 11 | { 12 | size_t i; 13 | 14 | i = 0; 15 | while (array && i < size) 16 | { 17 | if (i > 0) 18 | printf(", "); 19 | printf("%d", array[i]); 20 | ++i; 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "sort.h" 3 | 4 | /** 5 | * print_list - Prints a list of integers 6 | * 7 | * @list: The list to be printed 8 | */ 9 | void print_list(const listint_t *list) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (list) 15 | { 16 | if (i > 0) 17 | printf(", "); 18 | printf("%d", list->n); 19 | ++i; 20 | list = list->next; 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/sort.h: -------------------------------------------------------------------------------- 1 | #ifndef SORT_H 2 | #define SORT_H 3 | 4 | #include 5 | 6 | /** 7 | * struct listint_s - Doubly linked list node 8 | * 9 | * @n: Integer stored in the node 10 | * @prev: Pointer to prev element of list 11 | * @next: Pointer to the next element of the list 12 | */ 13 | typedef struct listint_s 14 | { 15 | const int n; 16 | struct listint_s *prev; 17 | struct listint_s *next; 18 | } listint_t; 19 | 20 | void print_array(const int *array, size_t size); 21 | void print_list(const listint_t *list); 22 | listint_t *get_head(listint_t *tmp); 23 | void insertion_sort(int *array, int size, int diff); 24 | 25 | void bubble_sort(int *array, size_t size); 26 | void insertion_sort_list(listint_t **list); 27 | void selection_sort(int *array, size_t size); 28 | void quick_sort(int *array, size_t size); 29 | void shell_sort(int *array, size_t size); 30 | void heap_sort(int *array, size_t size); 31 | void merge_sort(int *array, size_t size); 32 | #endif 33 | -------------------------------------------------------------------------------- /0x1B-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | 3 | all: main.o holberton.o 4 | $(CC) -o holberton main.o holberton.o 5 | 6 | clean: 7 | rm *.o 8 | -------------------------------------------------------------------------------- /0x1B-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | SRC=main.c holberton.c 3 | 4 | all: $(SRC) 5 | $(CC) $(SRC) -o holberton 6 | -------------------------------------------------------------------------------- /0x1B-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | SRC=main.c holberton.c 3 | OBJ=$(SRC:.c=.o) 4 | NAME=holberton 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1B-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | SRC=main.c holberton.c 3 | OBJ=$(SRC:.c=.o) 4 | NAME=holberton 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: 17 | $(RM) *~ $(NAME) 18 | $(RM) $(OBJ) 19 | 20 | re: oclean all 21 | -------------------------------------------------------------------------------- /0x1B-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | SRC=main.c holberton.c 3 | OBJ=$(SRC:.c=.o) 4 | NAME=holberton 5 | RM=rm -f 6 | CFLAGS=-Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(CFLAGS) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: 18 | $(RM) *~ $(NAME) 19 | $(RM) $(OBJ) 20 | 21 | re: oclean all 22 | -------------------------------------------------------------------------------- /0x1B-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # 0x1B. Makefiles -------------------------------------------------------------------------------- /0x1B-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_holberton(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1B-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_holberton(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-binary_trees/0-binary_tree_node.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | *binary_tree_node - Making the initial node 4 | *@parent: The parent node 5 | *@value: The value being stored in the node 6 | *Return: The returning the node 7 | */ 8 | binary_tree_t *binary_tree_node(binary_tree_t *parent, int value) 9 | { 10 | binary_tree_t *node; 11 | 12 | node = malloc(sizeof(binary_tree_t)); 13 | if (node == NULL) 14 | return (NULL); 15 | node->parent = parent; 16 | node->n = value; 17 | node->left = NULL; 18 | node->right = NULL; 19 | return (node); 20 | } 21 | -------------------------------------------------------------------------------- /0x1C-binary_trees/1-binary_tree_insert_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_insert_left - Insert a node as the left-child of another node. 5 | * If parent already has a left-child, new node must take its place, and 6 | * the old left child must be set as the left-child of the new node. 7 | * @parent: pointer to node to insert the left-child in 8 | * @value: value to store in new node 9 | * Return: pointer to the created node or NULL on failure 10 | */ 11 | binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value) 12 | { 13 | binary_tree_t *new_node, *tmp; 14 | 15 | if (parent == NULL) 16 | return (NULL); 17 | 18 | new_node = malloc(sizeof(binary_tree_t)); 19 | if (new_node == NULL) 20 | return (NULL); 21 | new_node->n = value; 22 | new_node->parent = parent; 23 | new_node->right = NULL; 24 | 25 | tmp = parent->left; 26 | parent->left = new_node; 27 | 28 | if (tmp != NULL) 29 | { 30 | new_node->left = tmp; 31 | tmp->parent = new_node; 32 | } 33 | else 34 | new_node->left = NULL; 35 | 36 | return (new_node); 37 | } 38 | -------------------------------------------------------------------------------- /0x1C-binary_trees/10-binary_tree_depth.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_depth - measure the depth of a node in a binary tree. 5 | * Depth is measured from root parent node going down. 6 | * @node: pointer to node to measure depth of 7 | * Return: depth of tree from given node 8 | */ 9 | size_t binary_tree_depth(const binary_tree_t *node) 10 | { 11 | if (node == NULL || node->parent == NULL) 12 | return (0); 13 | 14 | return (binary_tree_depth(node->parent) + 1); 15 | } 16 | -------------------------------------------------------------------------------- /0x1C-binary_trees/103-binary_tree_rotate_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | #include 3 | /** 4 | *binary_tree_rotate_left - left rotate the tree 5 | *@tree: The tree to be rotated 6 | *Return: The new tree 7 | */ 8 | binary_tree_t *binary_tree_rotate_left(binary_tree_t *tree) 9 | { 10 | binary_tree_t *temp, *new_root; 11 | 12 | temp = NULL; 13 | if (tree == NULL) 14 | return (NULL); 15 | if (tree->right->left != NULL) 16 | temp = tree->right->left; 17 | if (tree->right == NULL) 18 | return (tree); 19 | new_root = tree->right; 20 | tree->right->left = tree; 21 | tree->parent = tree->right; 22 | tree->right->parent = NULL; 23 | tree->right = temp; 24 | if (temp != NULL) 25 | temp->parent = tree; 26 | return (new_root); 27 | } 28 | -------------------------------------------------------------------------------- /0x1C-binary_trees/104-binary_tree_rotate_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | *binary_tree_rotate_right - Rotating the tree to the right 4 | *@tree: The tree to be evaluated and modified 5 | *Return: Newly modified tree 6 | */ 7 | binary_tree_t *binary_tree_rotate_right(binary_tree_t *tree) 8 | { 9 | binary_tree_t *temp, *new_root; 10 | 11 | temp = NULL; 12 | if (tree == NULL) 13 | return (NULL); 14 | if (tree->left->right != NULL) 15 | temp = tree->left->right; 16 | if (tree->left == NULL) 17 | return (tree); 18 | new_root = tree->left; 19 | tree->left->right = tree; 20 | tree->parent = tree->left; 21 | tree->left->parent = NULL; 22 | tree->left = temp; 23 | if (temp != NULL) 24 | temp->parent = tree; 25 | return (new_root); 26 | } 27 | -------------------------------------------------------------------------------- /0x1C-binary_trees/11-binary_tree_size.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_size - measure the size of a binary tree from a given node 5 | * @tree: root node of tree to measure from 6 | * Return: size of tree from a given node 7 | */ 8 | size_t binary_tree_size(const binary_tree_t *tree) 9 | { 10 | size_t count; 11 | 12 | if (tree == NULL) 13 | return (0); 14 | 15 | count = binary_tree_size(tree->left) + 1; 16 | count += binary_tree_size(tree->right); 17 | 18 | return (count); 19 | } 20 | -------------------------------------------------------------------------------- /0x1C-binary_trees/112-array_to_bst.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * array_to_bst - build a Binary Search Tree from an array 5 | * @array: array to convert to BST 6 | * @size: size of array 7 | * Return: pointer to root node, or NULL on failure 8 | */ 9 | bst_t *array_to_bst(int *array, size_t size) 10 | { 11 | size_t i; 12 | bst_t *root; 13 | 14 | if (array == NULL || size == 0) 15 | return (NULL); 16 | 17 | for (i = 0, root = NULL; i < size; i++) 18 | bst_insert(&root, array[i]); 19 | 20 | return (root); 21 | } 22 | -------------------------------------------------------------------------------- /0x1C-binary_trees/113-bst_search.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | #include 3 | /** 4 | * bst_search - Search for a value in a Binary Search Tree 5 | * @tree: pointer to root node of BST 6 | * @value: value to look for 7 | * Return: pointer to node containing value, else NULL 8 | */ 9 | bst_t *bst_search(const bst_t *tree, int value) 10 | { 11 | if (tree == NULL) 12 | return (NULL); 13 | if (tree->n == value) 14 | return ((bst_t *)tree); 15 | if (value < tree->n) 16 | return (bst_search(tree->left, value)); 17 | else 18 | return (bst_search(tree->right, value)); 19 | } 20 | -------------------------------------------------------------------------------- /0x1C-binary_trees/115-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | O(log(n)) 3 | O(log(n)) 4 | -------------------------------------------------------------------------------- /0x1C-binary_trees/12-binary_tree_leaves.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | *binary_tree_leaves - Function that counts how many leaves are in the tree 4 | *@tree: The tree to be evaluated 5 | *Return: The addition of both sides of the tree 6 | */ 7 | size_t binary_tree_leaves(const binary_tree_t *tree) 8 | { 9 | size_t left, right; 10 | 11 | left = right = 0; 12 | if (tree == NULL) 13 | return (0); 14 | if (tree->left == NULL && tree->right == NULL) 15 | return (1); 16 | left = binary_tree_leaves(tree->left); 17 | right = binary_tree_leaves(tree->right); 18 | return (left + right); 19 | } 20 | -------------------------------------------------------------------------------- /0x1C-binary_trees/13-binary_tree_nodes.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | *binary_tree_nodes - Count the nodes that have at least one child 4 | *@tree: The tree to be evaluated 5 | *Return: 0 when tree is NULL or a recursive function call 6 | */ 7 | size_t binary_tree_nodes(const binary_tree_t *tree) 8 | { 9 | if (tree != NULL && (tree->left != NULL || tree->right != NULL)) 10 | return (1 + binary_tree_nodes(tree->left) + 11 | binary_tree_nodes(tree->right)); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x1C-binary_trees/14-binary_tree_balance.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_balance - measure the balance factor of a binary tree 5 | * @tree: pointer to root node of tree to measure 6 | * Return: balance factor left height minus right height, or 0 if tree is NULL 7 | */ 8 | int binary_tree_balance(const binary_tree_t *tree) 9 | { 10 | int left, right; 11 | 12 | if (tree == NULL) 13 | return (0); 14 | 15 | left = custom_binary_tree_height(tree->left); 16 | right = custom_binary_tree_height(tree->right); 17 | 18 | return (left - right); 19 | } 20 | 21 | /** 22 | * custom_binary_tree_height - Measure the height 23 | * of a binary tree from a given node 24 | * @tree: pointer to node of tree to measure 25 | * Description: Edited to work with balance factor function 26 | * Return: height of tree or -1 if NULL 27 | */ 28 | int custom_binary_tree_height(const binary_tree_t *tree) 29 | { 30 | int left, right; 31 | 32 | if (tree == NULL) 33 | return (-1); 34 | 35 | if (tree->left == NULL && tree->right == NULL) 36 | return (0); 37 | 38 | left = custom_binary_tree_height(tree->left) + 1; 39 | right = custom_binary_tree_height(tree->right) + 1; 40 | 41 | if (left > right) 42 | return (left); 43 | else 44 | return (right); 45 | } 46 | -------------------------------------------------------------------------------- /0x1C-binary_trees/15-binary_tree_is_full.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_is_full - checking if the binary tree is full 4 | * @tree: The tree to be evaluated 5 | * Return: 1 if the node is a leaf and returning the highest node; 6 | */ 7 | int binary_tree_is_full(const binary_tree_t *tree) 8 | { 9 | int right, left; 10 | 11 | right = left = 0; 12 | if (tree == NULL) 13 | return (0); 14 | if (tree->left == NULL && tree->right == NULL) 15 | return (1); 16 | if (tree->left == NULL && tree->right != NULL) 17 | return (0); 18 | if (tree->left != NULL && tree->right == NULL) 19 | return (0); 20 | left = binary_tree_is_full(tree->left); 21 | right = binary_tree_is_full(tree->right); 22 | if (right == 1 && left == 1) 23 | return (1); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x1C-binary_trees/17-binary_tree_sibling.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_sibling - find the sibling of a node 5 | * @node: pointer to node to find sibling of 6 | * Return: pointer to sibling node, else NULL 7 | */ 8 | binary_tree_t *binary_tree_sibling(binary_tree_t *node) 9 | { 10 | if (node == NULL || node->parent == NULL || 11 | node->parent->left == NULL || node->parent->right == NULL) 12 | return (NULL); 13 | 14 | if (node->parent->left->n == node->n) 15 | return (node->parent->right); 16 | else 17 | return (node->parent->left); 18 | } 19 | -------------------------------------------------------------------------------- /0x1C-binary_trees/18-binary_tree_uncle.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | *binary_tree_uncle - function that finds the uncle node or the node that 4 | *shares the same sibling as the parent node. 5 | *@node: The node to be evaluated 6 | *Return: The uncle node or NULL 7 | */ 8 | binary_tree_t *binary_tree_uncle(binary_tree_t *node) 9 | { 10 | if (node == NULL) 11 | return (NULL); 12 | if (node->parent == NULL) 13 | return (NULL); 14 | if (node->parent->parent == NULL) 15 | return (NULL); 16 | if (node->parent->parent->right != NULL && 17 | node->parent->parent->right != node->parent) 18 | return (node->parent->parent->right); 19 | else if (node->parent->parent->left != NULL && 20 | node->parent->parent->left != node->parent) 21 | return (node->parent->parent->left); 22 | return (NULL); 23 | } 24 | -------------------------------------------------------------------------------- /0x1C-binary_trees/2-binary_tree_insert_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | *binary_tree_insert_right - inserting to the right of the binary tree 4 | *@parent: The parent node of what is being inserted 5 | *@value: The value stored in the struct 6 | *Return: The newly inserted node or NULL if the parent is NULL 7 | */ 8 | binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value) 9 | { 10 | binary_tree_t *node, *temp; 11 | 12 | if (parent == NULL) 13 | return (NULL); 14 | node = malloc(sizeof(binary_tree_t)); 15 | if (node == NULL) 16 | return (NULL); 17 | node->n = value; 18 | node->parent = parent; 19 | temp = parent->right; 20 | if (temp != NULL) 21 | { 22 | node->right = temp; 23 | temp->parent = node; 24 | } 25 | else 26 | node->right = NULL; 27 | parent->right = node; 28 | node->left = NULL; 29 | return (node); 30 | } 31 | -------------------------------------------------------------------------------- /0x1C-binary_trees/3-binary_tree_delete.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_delete - delete an entire binary tree 5 | * @tree: pointer to root node of tree to delete 6 | */ 7 | void binary_tree_delete(binary_tree_t *tree) 8 | { 9 | if (tree == NULL) 10 | return; 11 | 12 | binary_tree_delete(tree->left); 13 | binary_tree_delete(tree->right); 14 | free(tree); 15 | } 16 | -------------------------------------------------------------------------------- /0x1C-binary_trees/4-binary_tree_is_leaf.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_leaf - check if a node is a leaf 5 | * @node: pointer to the node to check 6 | * Return: 1 if node is a leaf, 0 otherwise 7 | */ 8 | int binary_tree_is_leaf(const binary_tree_t *node) 9 | { 10 | if (node == NULL) 11 | return (0); 12 | if (node->left == NULL && node->right == NULL) 13 | return (1); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x1C-binary_trees/5-binary_tree_is_root.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | *binary_tree_is_root - Function that checks if a given node is a root 4 | *@node: a pointer that points to the value of what we want to check 5 | *Return: 1 if the node is a root or else 0 6 | */ 7 | int binary_tree_is_root(const binary_tree_t *node) 8 | { 9 | if (node == NULL) 10 | return (0); 11 | if (node->parent == NULL) 12 | return (1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x1C-binary_trees/6-binary_tree_preorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | *binary_tree_preorder - Going through a binary tree using pre-order traversal 4 | *@tree: The binary tree to traverse and print 5 | *@func: A function used to print the number stored in the node 6 | */ 7 | void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int)) 8 | { 9 | if (func == NULL) 10 | return; 11 | if (tree == NULL) 12 | return; 13 | func(tree->n); 14 | binary_tree_preorder(tree->left, func); 15 | binary_tree_preorder(tree->right, func); 16 | } 17 | -------------------------------------------------------------------------------- /0x1C-binary_trees/7-binary_tree_inorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | *binary_tree_inorder - Go through a binary tree using in-order traversal 4 | *@tree: The tree to be printed in order 5 | *@func: A pointer to a function used to print the numbers 6 | */ 7 | void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int)) 8 | { 9 | if (tree == NULL || func == NULL) 10 | return; 11 | binary_tree_inorder(tree->left, func); 12 | func(tree->n); 13 | binary_tree_inorder(tree->right, func); 14 | } 15 | -------------------------------------------------------------------------------- /0x1C-binary_trees/8-binary_tree_postorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_postorder - Go through a binary tree using post-order traversal 5 | * @tree: pointer to root node of tree to traverse 6 | * @func: pointer to function to call for each node 7 | */ 8 | void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (tree == NULL || func == NULL) 11 | return; 12 | 13 | binary_tree_postorder(tree->left, func); 14 | binary_tree_postorder(tree->right, func); 15 | func(tree->n); 16 | } 17 | -------------------------------------------------------------------------------- /0x1C-binary_trees/9-binary_tree_height.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_height - Measure the height of a binary tree from a given node 5 | * @tree: pointer to node of tree to measure 6 | * Return: height of tree or 0 if NULL 7 | */ 8 | size_t binary_tree_height(const binary_tree_t *tree) 9 | { 10 | size_t left, right; 11 | 12 | if (tree == NULL) 13 | return (0); 14 | 15 | if (tree->left == NULL && tree->right == NULL) 16 | return (0); 17 | 18 | left = binary_tree_height(tree->left) + 1; 19 | right = binary_tree_height(tree->right) + 1; 20 | 21 | if (left > right) 22 | return (left); 23 | else 24 | return (right); 25 | } 26 | -------------------------------------------------------------------------------- /0x1C-binary_trees/README.md: -------------------------------------------------------------------------------- 1 | # 0x1C. C - Binary trees -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../binary_trees.h" 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | binary_tree_t *root; 12 | 13 | root = binary_tree_node(NULL, 98); 14 | 15 | root->left = binary_tree_node(root, 12); 16 | root->left->left = binary_tree_node(root->left, 6); 17 | root->left->right = binary_tree_node(root->left, 16); 18 | 19 | root->right = binary_tree_node(root, 402); 20 | root->right->left = binary_tree_node(root->right, 256); 21 | root->right->right = binary_tree_node(root->right, 512); 22 | 23 | binary_tree_print(root); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 12); 16 | root->right = binary_tree_node(root, 402); 17 | binary_tree_print(root); 18 | printf("\n"); 19 | binary_tree_insert_left(root->right, 128); 20 | binary_tree_insert_left(root, 54); 21 | binary_tree_print(root); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/10-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t depth; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | depth = binary_tree_depth(root); 23 | printf("Depth of %d: %lu\n", root->n, depth); 24 | depth = binary_tree_depth(root->right); 25 | printf("Depth of %d: %lu\n", root->right->n, depth); 26 | depth = binary_tree_depth(root->left->right); 27 | printf("Depth of %d: %lu\n", root->left->right->n, depth); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/101-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * print_num - Prints a number 7 | * 8 | * @n: Number to be printed 9 | */ 10 | void print_num(int n) 11 | { 12 | printf("%d\n", n); 13 | } 14 | 15 | /** 16 | * main - Entry point 17 | * 18 | * Return: Always 0 (Success) 19 | */ 20 | int main(void) 21 | { 22 | binary_tree_t *root; 23 | 24 | root = binary_tree_node(NULL, 98); 25 | root->left = binary_tree_node(root, 12); 26 | root->right = binary_tree_node(root, 402); 27 | root->left->left = binary_tree_node(root->left, 6); 28 | root->left->right = binary_tree_node(root->left, 56); 29 | root->right->left = binary_tree_node(root->right, 256); 30 | root->right->right = binary_tree_node(root->right, 512); 31 | 32 | binary_tree_print(root); 33 | binary_tree_levelorder(root, &print_num); 34 | binary_tree_delete(root); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/11-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t size; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_insert_left(root->left, 10); 21 | binary_tree_print(root); 22 | 23 | size = binary_tree_size(root); 24 | printf("Size of %d: %lu\n", root->n, size); 25 | size = binary_tree_size(root->right); 26 | printf("Size of %d: %lu\n", root->right->n, size); 27 | size = binary_tree_size(root->left->right); 28 | printf("Size of %d: %lu\n", root->left->right->n, size); 29 | size = binary_tree_size(root->left); 30 | printf("Size of %d: %lu\n", root->left->n, size); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/110-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int bst; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 128); 18 | root->left->right = binary_tree_node(root->left, 54); 19 | root->right->right = binary_tree_node(root, 402); 20 | root->left->left = binary_tree_node(root->left, 10); 21 | 22 | binary_tree_print(root); 23 | bst = binary_tree_is_bst(root); 24 | printf("Is %d bst: %d\n", root->n, bst); 25 | bst = binary_tree_is_bst(root->left); 26 | printf("Is %d bst: %d\n", root->left->n, bst); 27 | 28 | root->right->left = binary_tree_node(root->right, 100); 29 | root->right->left->left = binary_tree_node(root->right->left, 99); 30 | 31 | binary_tree_print(root); 32 | bst = binary_tree_is_bst(root); 33 | printf("Is %d bst: %d\n", root->n, bst); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/111-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | bst_t *root; 13 | bst_t *node; 14 | 15 | root = NULL; 16 | node = bst_insert(&root, 98); 17 | printf("Inserted: %d\n", node->n); 18 | node = bst_insert(&root, 402); 19 | printf("Inserted: %d\n", node->n); 20 | node = bst_insert(&root, 12); 21 | printf("Inserted: %d\n", node->n); 22 | binary_tree_print(root); 23 | node = bst_insert(&root, 46); 24 | printf("Inserted: %d\n", node->n); 25 | node = bst_insert(&root, 128); 26 | printf("Inserted: %d\n", node->n); 27 | node = bst_insert(&root, 256); 28 | printf("Inserted: %d\n", node->n); 29 | node = bst_insert(&root, 512); 30 | printf("Inserted: %d\n", node->n); 31 | node = bst_insert(&root, 1); 32 | printf("Inserted: %d\n", node->n); 33 | node = bst_insert(&root, 128); 34 | printf("Node should be nil -> %p\n", (void *)node); 35 | binary_tree_print(root); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/112-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../binary_trees.h" 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: 0 on success, error code on failure 8 | */ 9 | int main(void) 10 | { 11 | bst_t *tree; 12 | int array[] = { 13 | 79, 47, 68, 87, 84, 91, 21, 32, 34, 2, 14 | 20, 22, 98, 1, 62, 95 15 | }; 16 | size_t n = sizeof(array) / sizeof(array[0]); 17 | 18 | tree = array_to_bst(array, n); 19 | if (!tree) 20 | return (1); 21 | binary_tree_print(tree); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/113-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: 0 on success, error code on failure 9 | */ 10 | int main(void) 11 | { 12 | bst_t *tree; 13 | int array[] = { 14 | 79, 47, 68, 87, 84, 91, 21, 32, 34, 2, 15 | 20, 22, 98, 1, 62, 95 16 | }; 17 | size_t n = sizeof(array) / sizeof(array[0]); 18 | bst_t *node; 19 | 20 | tree = array_to_bst(array, n); 21 | if (!tree) 22 | return (1); 23 | binary_tree_print(tree); 24 | node = bst_search(tree, 32); 25 | printf("Found: %d\n", node->n); 26 | binary_tree_print(node); 27 | node = bst_search(tree, 512); 28 | printf("Node should be nil -> %p\n", (void *)node); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/114-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: 0 on success, error code on failure 9 | */ 10 | int main(void) 11 | { 12 | bst_t *tree; 13 | int array[] = { 14 | 79, 47, 68, 87, 84, 91, 21, 32, 34, 2, 15 | 20, 22, 98, 1, 62, 95 16 | }; 17 | size_t n = sizeof(array) / sizeof(array[0]); 18 | 19 | tree = array_to_bst(array, n); 20 | if (!tree) 21 | return (1); 22 | binary_tree_print(tree); 23 | 24 | tree = bst_remove(tree, 79); 25 | printf("Removed 79...\n"); 26 | binary_tree_print(tree); 27 | 28 | tree = bst_remove(tree, 21); 29 | printf("Removed 21...\n"); 30 | binary_tree_print(tree); 31 | 32 | tree = bst_remove(tree, 68); 33 | printf("Removed 68...\n"); 34 | binary_tree_print(tree); 35 | binary_tree_delete(tree); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/12-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t leaves; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | leaves = binary_tree_leaves(root); 23 | printf("Leaves in %d: %lu\n", root->n, leaves); 24 | leaves = binary_tree_leaves(root->right); 25 | printf("Leaves in %d: %lu\n", root->right->n, leaves); 26 | leaves = binary_tree_leaves(root->left->right); 27 | printf("Leaves in %d: %lu\n", root->left->right->n, leaves); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/13-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t nodes; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | nodes = binary_tree_nodes(root); 23 | printf("Nodes in %d: %lu\n", root->n, nodes); 24 | nodes = binary_tree_nodes(root->right); 25 | printf("Nodes in %d: %lu\n", root->right->n, nodes); 26 | nodes = binary_tree_nodes(root->left->right); 27 | printf("Nodes in %d: %lu\n", root->left->right->n, nodes); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/14-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int balance; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_insert_right(root->right, 501); 21 | binary_tree_insert_left(root, 45); 22 | binary_tree_insert_right(root->left, 50); 23 | binary_tree_insert_left(root->left->left, 10); 24 | binary_tree_insert_left(root->left->left->left, 8); 25 | binary_tree_print(root); 26 | 27 | balance = binary_tree_balance(root); 28 | printf("Balance of %d: %+d\n", root->n, balance); 29 | balance = binary_tree_balance(root->right); 30 | printf("Balance of %d: %+d\n", root->right->n, balance); 31 | balance = binary_tree_balance(root->left->left->right); 32 | printf("Balance of %d: %+d\n", root->left->left->right->n, balance); 33 | balance = binary_tree_balance(root); 34 | printf("Balance of %d: %+d\n", root->n, balance); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/16-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int perfect; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | root->left->left = binary_tree_node(root->left, 10); 21 | root->right->left = binary_tree_node(root->right, 10); 22 | 23 | binary_tree_print(root); 24 | perfect = binary_tree_is_perfect(root); 25 | printf("Perfect: %d\n\n", perfect); 26 | 27 | root->right->right->left = binary_tree_node(root->right->right, 10); 28 | binary_tree_print(root); 29 | perfect = binary_tree_is_perfect(root); 30 | printf("Perfect: %d\n\n", perfect); 31 | 32 | root->right->right->right = binary_tree_node(root->right->right, 10); 33 | binary_tree_print(root); 34 | perfect = binary_tree_is_perfect(root); 35 | printf("Perfect: %d\n", perfect); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/18-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | binary_tree_t *uncle; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 128); 18 | root->left->right = binary_tree_node(root->left, 54); 19 | root->right->right = binary_tree_node(root->right, 402); 20 | root->left->left = binary_tree_node(root->left, 10); 21 | root->right->left = binary_tree_node(root->right, 110); 22 | root->right->right->left = binary_tree_node(root->right->right, 200); 23 | root->right->right->right = binary_tree_node(root->right->right, 512); 24 | 25 | binary_tree_print(root); 26 | uncle = binary_tree_uncle(root->right->left); 27 | printf("Uncle of %d: %d\n", root->right->left->n, uncle->n); 28 | uncle = binary_tree_uncle(root->left->right); 29 | printf("Uncle of %d: %d\n", root->left->right->n, uncle->n); 30 | uncle = binary_tree_uncle(root->left); 31 | printf("Uncle of %d: %p\n", root->left->n, (void *)uncle); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 12); 16 | root->right = binary_tree_node(root, 402); 17 | binary_tree_insert_left(root->right, 128); 18 | binary_tree_insert_left(root, 54); 19 | binary_tree_print(root); 20 | binary_tree_delete(root); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int ret; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_left(root->right, 128); 19 | binary_tree_insert_left(root, 54); 20 | binary_tree_print(root); 21 | 22 | ret = binary_tree_is_leaf(root); 23 | printf("Is %d a leaf: %d\n", root->n, ret); 24 | ret = binary_tree_is_leaf(root->right); 25 | printf("Is %d a leaf: %d\n", root->right->n, ret); 26 | ret = binary_tree_is_leaf(root->right->left); 27 | printf("Is %d a leaf: %d\n", root->right->left->n, ret); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int ret; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | ret = binary_tree_is_root(root); 23 | printf("Is %d a root: %d\n", root->n, ret); 24 | ret = binary_tree_is_root(root->right); 25 | printf("Is %d a root: %d\n", root->right->n, ret); 26 | ret = binary_tree_is_root(root->right->right); 27 | printf("Is %d a root: %d\n", root->right->right->n, ret); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * print_num - Prints a number 7 | * 8 | * @n: Number to be printed 9 | */ 10 | void print_num(int n) 11 | { 12 | printf("%d\n", n); 13 | } 14 | 15 | /** 16 | * main - Entry point 17 | * 18 | * Return: Always 0 (Success) 19 | */ 20 | int main(void) 21 | { 22 | binary_tree_t *root; 23 | 24 | root = binary_tree_node(NULL, 98); 25 | root->left = binary_tree_node(root, 12); 26 | root->right = binary_tree_node(root, 402); 27 | root->left->left = binary_tree_node(root->left, 6); 28 | root->left->right = binary_tree_node(root->left, 56); 29 | root->left->left->right = binary_tree_node(root->left, 40); 30 | root->right->left = binary_tree_node(root->right, 256); 31 | root->right->right = binary_tree_node(root->right, 512); 32 | root->right->right->right = binary_tree_node(root->right, 1000); 33 | 34 | binary_tree_print(root); 35 | binary_tree_preorder(root, &print_num); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * print_num - Prints a number 7 | * 8 | * @n: Number to be printed 9 | */ 10 | void print_num(int n) 11 | { 12 | printf("%d\n", n); 13 | } 14 | 15 | /** 16 | * main - Entry point 17 | * 18 | * Return: Always 0 (Success) 19 | */ 20 | int main(void) 21 | { 22 | binary_tree_t *root; 23 | 24 | root = binary_tree_node(NULL, 98); 25 | root->left = binary_tree_node(root, 12); 26 | root->right = binary_tree_node(root, 402); 27 | root->left->left = binary_tree_node(root->left, 6); 28 | root->left->right = binary_tree_node(root->left, 56); 29 | root->right->left = binary_tree_node(root->right, 256); 30 | root->right->right = binary_tree_node(root->right, 512); 31 | 32 | binary_tree_print(root); 33 | binary_tree_inorder(root, &print_num); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/8-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * print_num - Prints a number 7 | * 8 | * @n: Number to be printed 9 | */ 10 | void print_num(int n) 11 | { 12 | printf("%d\n", n); 13 | } 14 | 15 | /** 16 | * main - Entry point 17 | * 18 | * Return: Always 0 (Success) 19 | */ 20 | int main(void) 21 | { 22 | binary_tree_t *root; 23 | 24 | root = binary_tree_node(NULL, 98); 25 | root->left = binary_tree_node(root, 12); 26 | root->right = binary_tree_node(root, 402); 27 | root->left->left = binary_tree_node(root->left, 6); 28 | root->left->right = binary_tree_node(root->left, 56); 29 | root->right->left = binary_tree_node(root->right, 256); 30 | root->right->right = binary_tree_node(root->right, 512); 31 | 32 | binary_tree_print(root); 33 | binary_tree_postorder(root, &print_num); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-binary_trees/mains/9-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t height; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | height = binary_tree_height(root); 23 | printf("Height from %d: %lu\n", root->n, height); 24 | height = binary_tree_height(root->right); 25 | printf("Height from %d: %lu\n", root->right->n, height); 26 | height = binary_tree_height(root->left->right); 27 | printf("Height from %d: %lu\n", root->left->right->n, height); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - sequentially search an array for a value. 5 | * Must also print each value compared while searching. 6 | * @array: pointer to first element in array to search in 7 | * @size: number of elements in array 8 | * @value: value to search for 9 | * Return: index of matched value in array or -1 if error or not found 10 | */ 11 | int linear_search(int *array, size_t size, int value) 12 | { 13 | size_t i; 14 | 15 | if (array == NULL || size == 0) 16 | return (-1); 17 | 18 | for (i = 0; i < size; i++) 19 | { 20 | printf("Value checked array[%lu] = [%d]\n", i, array[i]); 21 | if (value == array[i]) 22 | return (i); 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | # 0x1D. C - Search Algorithms -------------------------------------------------------------------------------- /0x1D-search_algorithms/mains/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 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/mains/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, 4, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS 2 | #define SEARCH_ALGOS 3 | #include 4 | 5 | /* helpers */ 6 | void print_array(int *array, size_t inc, size_t size); 7 | 8 | /* main functions */ 9 | int linear_search(int *array, size_t size, int value); 10 | int binary_search(int *array, size_t size, int value); 11 | #endif 12 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/README.md: -------------------------------------------------------------------------------- 1 | # 0x1E. C - Huffman codes & Red-Black trees -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/heap/binary_tree_node.c: -------------------------------------------------------------------------------- 1 | #include "heap.h" 2 | 3 | /** 4 | * binary_tree_node - Create a Heap node 5 | * @parent: pointer to parent node 6 | * @data: data to be stored in node 7 | * Return: pointer to new created node or NULL if fail 8 | */ 9 | binary_tree_node_t *binary_tree_node(binary_tree_node_t *parent, void *data) 10 | { 11 | binary_tree_node_t *new_node; 12 | 13 | new_node = malloc(sizeof(binary_tree_node_t)); 14 | if (new_node == NULL) 15 | return (NULL); 16 | 17 | new_node->parent = parent; 18 | new_node->data = data; 19 | new_node->left = NULL; 20 | new_node->right = NULL; 21 | 22 | return (new_node); 23 | } 24 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/heap/heap_create.c: -------------------------------------------------------------------------------- 1 | #include "heap.h" 2 | 3 | /** 4 | * heap_create - Creates a Heap data structure. Set root to NULL and size to 0. 5 | * @data_cmp: pointer to a comparison function 6 | * Return: pointer to the created `heap_t` structure, or Null on fail 7 | */ 8 | heap_t *heap_create(int (*data_cmp)(void *, void *)) 9 | { 10 | heap_t *new_heap; 11 | 12 | new_heap = malloc(sizeof(heap_t)); 13 | if (new_heap == NULL) 14 | return (NULL); 15 | 16 | new_heap->size = 0; 17 | new_heap->root = NULL; 18 | new_heap->data_cmp = data_cmp; 19 | 20 | return (new_heap); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/heap/heap_delete.c: -------------------------------------------------------------------------------- 1 | #include "heap.h" 2 | 3 | /** 4 | * heap_delete - Deallocates a heap 5 | * @heap: pointer to heap 6 | * @free_data: pointer to function that will be used to free contents of a node 7 | */ 8 | void heap_delete(heap_t *heap, void (*free_data)(void *)) 9 | { 10 | if (heap == NULL) 11 | { 12 | if (heap->root == NULL) 13 | return; 14 | free(heap); 15 | } 16 | 17 | recurse_delete_heap(heap->root, free_data); 18 | free(heap); 19 | } 20 | 21 | /** 22 | * recurse_delete_heap - delete heap helper function to recurse down heap 23 | * @node: pointer to currently focused node 24 | * @free_data: pointer to function that will be used to free contents of a node 25 | */ 26 | void recurse_delete_heap(binary_tree_node_t *node, void (*free_data)(void *)) 27 | { 28 | if (node == NULL) 29 | return; 30 | 31 | if (node->left != NULL) 32 | recurse_delete_heap(node->left, free_data); 33 | if (node->right != NULL) 34 | recurse_delete_heap(node->right, free_data); 35 | if (free_data != NULL) 36 | free_data(node); 37 | free(node); 38 | } 39 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/heap/helpers.c: -------------------------------------------------------------------------------- 1 | #include "heap.h" 2 | 3 | /** 4 | * get_power - helper method to find power of 2 based on heap size 5 | * @heap: pointer to heap in which the node has to be inserted 6 | * Return: power of 2 based on heap size 7 | */ 8 | size_t get_power(heap_t *heap) 9 | { 10 | size_t power, position; 11 | 12 | power = 1; 13 | position = heap->size + 1; 14 | while ((power * 2) <= position) 15 | power *= 2; 16 | 17 | return (power); 18 | } 19 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/huffman.h: -------------------------------------------------------------------------------- 1 | #ifndef _HUFFMAN_H_ 2 | #define _HUFFMAN_H_ 3 | 4 | #include 5 | #include "heap/heap.h" 6 | 7 | /** 8 | * struct symbol_s - Stores a char and its associated frequency 9 | * 10 | * @data: The character 11 | * @freq: The associated frequency 12 | */ 13 | typedef struct symbol_s 14 | { 15 | char data; 16 | size_t freq; 17 | } symbol_t; 18 | 19 | /* helpers */ 20 | int symbol_cmp(void *p1, void *p2); 21 | 22 | /* functions */ 23 | symbol_t *symbol_create(char data, size_t freq); 24 | heap_t *huffman_priority_queue(char *data, size_t *freq, size_t size); 25 | #endif 26 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/mains/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "heap.h" 4 | 5 | /** 6 | * int_cmp - Compares two integers 7 | * 8 | * @p1: First pointer 9 | * @p2: Second pointer 10 | * 11 | * Return: Difference between the two strings 12 | */ 13 | int int_cmp(void *p1, void *p2) 14 | { 15 | int *n1, *n2; 16 | 17 | n1 = (int *)p1; 18 | n2 = (int *)p2; 19 | return (*n1 - *n2); 20 | } 21 | 22 | /** 23 | * main - Entry point 24 | * 25 | * Return: EXIT_SUCCESS 26 | */ 27 | int main(void) 28 | { 29 | heap_t *heap; 30 | 31 | heap = heap_create(int_cmp); 32 | if (heap == NULL) 33 | { 34 | fprintf(stderr, "Failed to create the heap\n"); 35 | return (EXIT_FAILURE); 36 | } 37 | printf("Heap size: %lu\n", heap->size); 38 | printf("Heap root: %p\n", (void *)heap->root); 39 | return (EXIT_SUCCESS); 40 | } 41 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/mains/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "heap.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_node_t *node; 13 | int n, n2; 14 | 15 | n = 5; 16 | n2 = 10; 17 | node = binary_tree_node(NULL, &n); 18 | if (node == NULL) 19 | { 20 | fprintf(stderr, "Failed to create a node\n"); 21 | return (EXIT_FAILURE); 22 | } 23 | printf("Node(%p): data(%d) left(%p) right(%p) parent(%p)\n", 24 | (void *)node, *((int *)node->data), (void *)node->left, 25 | (void *)node->right, (void *)node->parent); 26 | 27 | node->left = binary_tree_node(node, &n2); 28 | if (node == NULL) 29 | { 30 | fprintf(stderr, "Failed to create a node\n"); 31 | return (EXIT_FAILURE); 32 | } 33 | printf("Node(%p): data(%d) left(%p) right(%p) parent(%p)\n", 34 | (void *)node->left, *((int *)node->left->data), (void *)node->left->left, 35 | (void *)node->left->right, (void *)node->left->parent); 36 | 37 | return (EXIT_SUCCESS); 38 | } 39 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/mains/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "huffman.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | symbol_t *symbol; 13 | 14 | symbol = symbol_create('d', 3); 15 | if (symbol == NULL) 16 | { 17 | fprintf(stderr, "Failed to create a symbol\n"); 18 | return (EXIT_FAILURE); 19 | } 20 | printf("Symbol: data(%c) frequency(%lu)\n", symbol->data, symbol->freq); 21 | 22 | symbol = symbol_create('H', 98); 23 | if (symbol == NULL) 24 | { 25 | fprintf(stderr, "Failed to create a symbol\n"); 26 | return (EXIT_FAILURE); 27 | } 28 | printf("Symbol: data(%c) frequency(%lu)\n", symbol->data, symbol->freq); 29 | 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/rb_trees.h: -------------------------------------------------------------------------------- 1 | #ifndef _RB_TREES_H_ 2 | #define _RB_TREES_H_ 3 | 4 | /** 5 | * enum rb_color_e - Possible color of a Red-Black tree 6 | * 7 | * @RED: 0 -> Red node 8 | * @BLACK: 1 -> Black node 9 | */ 10 | typedef enum rb_color_e 11 | { 12 | RED = 0, 13 | BLACK, 14 | DOUBLE_BLACK 15 | } rb_color_t; 16 | 17 | /** 18 | * struct rb_tree_s - Red-Black tree node structure 19 | * 20 | * @n: Integer stored in the node 21 | * @parent: Pointer to the parent node 22 | * @left: Pointer to the left child node 23 | * @right: Pointer to the right child node 24 | * @color: Color of the node (RED or BLACK) 25 | */ 26 | typedef struct rb_tree_s 27 | { 28 | int n; 29 | rb_color_t color; 30 | struct rb_tree_s *parent; 31 | struct rb_tree_s *left; 32 | struct rb_tree_s *right; 33 | } rb_tree_t; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /0x1E-huffman_rb_trees/symbol.c: -------------------------------------------------------------------------------- 1 | #include "huffman.h" 2 | 3 | /** 4 | * symbol_create - creates a `symbol_t` data structure 5 | * @data: data to be stored in struct 6 | * @freq: associated frequency 7 | * Return: pointer to the created struct, or NULL 8 | */ 9 | symbol_t *symbol_create(char data, size_t freq) 10 | { 11 | symbol_t *cr; 12 | 13 | cr = malloc(sizeof(symbol_t)); 14 | if (cr == NULL) 15 | return (NULL); 16 | cr->data = data; 17 | cr->freq = freq; 18 | 19 | return (cr); 20 | } 21 | --------------------------------------------------------------------------------