├── test.c ├── 0x1D-search_algorithms ├── 2-O ├── 3-O ├── 5-O ├── 6-O ├── 4-O ├── search_algos.h ├── 0-linear.c ├── 1-binary.c └── README.md ├── _config.yml ├── 0x13-bit_manipulation ├── 101-password ├── 100-get_endianness.c ├── 3-set_bit.c ├── 2-get_bit.c ├── 4-clear_bit.c ├── holberton.h ├── 0-binary_to_uint.c ├── 5-flip_bits.c └── 1-print_binary.c ├── 0x16-doubly_linked_lists ├── 102-result ├── palidrome.py ├── 4-free_dlistint.c ├── 1-dlistint_len.c ├── 6-sum_dlistint.c ├── 0-print_dlistint.c ├── 5-get_dnodeint.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 8-delete_dnodeint.c ├── lists.h └── 7-insert_dnodeint.c ├── 0x1C-binary_trees ├── 135-O ├── 115-O ├── 125-O ├── 4-binary_tree_is_leaf.c ├── 10-binary_tree_depth.c ├── 5-binary_tree_is_root.c ├── 3-binary_tree_delete.c ├── 6-binary_tree_preorder.c ├── 8-binary_tree_postorder.c ├── 7-binary_tree_inorder.c ├── 17-binary_tree_sibling.c ├── 11-binary_tree_size.c ├── 113-bst_search.c ├── 0-binary_tree_node.c ├── 112-array_to_bst.c ├── 103-binary_tree_rotate_left.c ├── 104-binary_tree_rotate_right.c ├── 9-binary_tree_height.c ├── 12-binary_tree_leaves.c ├── 14-binary_tree_balance.c ├── 13-binary_tree_nodes.c ├── 110-binary_tree_is_bst.c ├── 15-binary_tree_is_full.c ├── 1-binary_tree_insert_left.c ├── 18-binary_tree_uncle.c ├── 111-bst_insert.c ├── 2-binary_tree_insert_right.c ├── README.md ├── 102-binary_tree_is_complete.c ├── 100-binary_trees_ancestor.c ├── 101-binary_tree_levelorder.c └── 16-binary_tree_is_perfect.c ├── 0x06-pointers_arrays_strings ├── 101-crackme_password ├── 9-set_string ├── 8-print_diagsums ├── 9-set_string.c ├── _putchar.c ├── 0-memset.c ├── 9-main.c ├── 7-print_chessboard.c ├── 2-strchr.c ├── 1-memcpy.c ├── holberton.h ├── 3-strspn.c ├── 8-print_diagsums.c ├── 8-main.c ├── 5-strstr.c └── 4-strpbrk.c ├── 0x1A-sorting_algorithms ├── 0-O ├── 1-O ├── 2-O ├── 3-O ├── print_list.c ├── print_array.c ├── 2-selection_sort.c ├── 0-bubble_sort.c ├── sort.h └── 1-insertion_sort_list.c ├── 0x00-hello_world ├── 1-compiler ├── 2-assembler ├── 3-name ├── 0-preprocessor ├── 100-intel ├── sizeof ├── 4-puts.c ├── 5-printf.c ├── 101-quote.c └── 6-size.c ├── 0x1B-makefiles ├── 0-Makefile ├── 1-Makefile ├── m.h ├── 2-Makefile ├── main.c ├── 3-Makefile ├── 100-Makefile ├── 4-Makefile ├── 5-island_perimeter_option.py ├── 5-island_perimeter.py └── holberton.c ├── 0x17-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── libholberton.so ├── 100-operations.so ├── holberton.h └── README.md ├── 0x0C-preprocessor ├── 1-pi.h ├── 4-sum.h ├── 0-object_like_macro.h ├── 3-function_like_macro.h ├── 2-main.c └── README.md ├── 0x08-static_libraries ├── quote ├── 6-abs.o ├── main.o ├── 0-memset.o ├── 0-strcat.o ├── 1-memcpy.o ├── 100-atoi.o ├── 2-strchr.o ├── 2-strlen.o ├── 3-puts.o ├── 3-strcmp.o ├── 3-strspn.o ├── 5-strstr.o ├── 9-strcpy.o ├── _putchar.o ├── 0-isupper.o ├── 1-isdigit.o ├── 1-strncat.o ├── 2-strncpy.o ├── 3-islower.o ├── 4-isalpha.o ├── 4-strpbrk.o ├── libholberton.a ├── create_static_lib.sh ├── main.c ├── 1-isdigit.c ├── 0-isupper.c ├── 2-strlen.c ├── 6-abs.c ├── _putchar.c ├── 3-islower.c ├── 3-puts.c ├── 0-memset.c ├── 2-strchr.c ├── 4-isalpha.c ├── 3-strcmp.c ├── 1-memcpy.c ├── 9-strcpy.c ├── 3-strspn.c ├── 2-strncpy.c ├── 5-strstr.c ├── 0-strcat.c ├── 1-strncat.c ├── 4-strpbrk.c ├── README.md ├── holberton.h └── 100-atoi.c ├── 0x12-more_singly_linked_lists ├── .gitignore ├── 4-free_listint.c ├── 1-listint_len.c ├── 8-sum_listint.c ├── 0-print_listint.c ├── 6-pop_listint.c ├── 2-add_nodeint.c ├── 5-free_listint2.c ├── 100-reverse_listint.c ├── 7-get_nodeint.c ├── 3-add_nodeint_end.c ├── 10-delete_nodeint.c ├── 9-insert_nodeint.c └── lists.h ├── 0x09-argc_argv ├── holberton.h ├── 0-whatsmyname.c ├── 1-args.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── .gitmodules ├── 0x04-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 2-strlen.c ├── 3-puts.c ├── 6-puts2.c ├── 7-puts_half.c ├── 4-print_rev.c ├── holberton.h ├── 9-strcpy.c ├── 8-print_array.c ├── 101-keygen.c ├── swap_vowel.c ├── 5-rev_string.c └── 100-atoi.c ├── 0x02-functions_nested_loops ├── 10-add.c ├── 6-abs.c ├── 3-islower.c ├── holberton.h ├── 7-print_last_digit.c ├── 2-print_alphabet_x10.c ├── 1-alphabet.c ├── 101-natural.c ├── 4-isalpha.c ├── 5-sign.c ├── 8-24_hours.c ├── 0-holberton.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 9-times_table.c ├── 11-print_to_98.c ├── 100-times_table.c └── 104-fibonacci.c ├── 0x03-more_functions_nested_loops ├── 2-mul.c ├── 1-isdigit.c ├── 0-isupper.c ├── 3-print_numbers.c ├── 6-print_line.c ├── holberton.h ├── 4-print_most_numbers.c ├── 8-print_square.c ├── 5-more_numbers.c ├── 7-print_diagonal.c ├── 100-prime_factor.c ├── 10-print_triangle.c ├── 9-fizz_buzz.c └── 101-print_number.c ├── 0x11-singly_linked_lists ├── 4-free_list.c ├── 100-first.c ├── 1-list_len.c ├── 0-print_list.c ├── 2-add_node.c ├── lists.h ├── 3-add_node_end.c └── README.md ├── 0x0D-structures_typedef ├── 5-free_dog.c ├── 1-init_dog.c ├── 2-print_dog.c ├── dog.h ├── 4-new_dog.c └── README.md ├── 0x07-recursion ├── 1-print_rev_recursion.c ├── 3-factorial.c ├── 2-strlen_recursion.c ├── 4-pow_recursion.c ├── 0-puts_recursion.c ├── holberton.h ├── 6-is_prime_number.c ├── 5-sqrt_recursion.c └── 7-is_palindrome.c ├── 0x01-variables_if_else_while ├── 5-print_numbers.c ├── 7-print_tebahpla.c ├── 2-print_alphabet.c ├── 4-print_alphabt.c ├── 8-print_base16.c ├── 3-print_alphabets.c ├── 6-print_numberz.c ├── 0-positive_or_negative.c ├── 9-print_comb.c ├── 1-last_digit.c ├── 10-print_comb2.c ├── 102-print_comb5.c ├── 100-print_comb3.c └── 101-print_comb4.c ├── 0x0E-function_pointers ├── 0-print_name.c ├── function_pointers.h ├── 3-calc.h ├── 3-get_op_func.c ├── 1-array_iterator.c ├── 2-int_index.c ├── 3-main.c └── 3-op_functions.c ├── 0x19-hash_tables ├── 2-key_index.c ├── 1-djb2.c ├── 6-hash_table_delete.c ├── 5-hash_table_print.c ├── 4-hash_table_get.c ├── 0-hash_table_create.c ├── 3-hash_table_set.c └── hash_tables.h ├── 0x05-pointers_arrays_strings ├── 5-string_toupper.c ├── 4-rev_array.c ├── 3-strcmp.c ├── 101-magic.c ├── 2-strncpy.c ├── 7-leet.c ├── holberton.h ├── 8-rot13.c ├── 0-strcat.c ├── 1-strncat.c ├── 6-cap_string.c └── 100-print_number.c ├── 0x0B-more_malloc_free ├── 0-malloc_checked.c ├── holberton.h ├── 2-calloc.c ├── 3-array_range.c ├── 100-realloc.c ├── 1-string_nconcat.c └── README.md ├── 0x14-file_io ├── holberton.h ├── 1-create_file.c ├── 2-append_text_to_file.c ├── 0-read_textfile.c └── 3-cp.c ├── 0x0A-malloc_free ├── holberton.h ├── 4-free_grid.c ├── 0-create_array.c ├── 3-alloc_grid.c ├── 1-strdup.c ├── 5-argstostr.c └── 2-str_concat.c ├── 0x0F-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── variadic_functions.h └── 3-print_all.c └── 0x18-stacks_queues_lifo_fifo ├── README.md └── monty.h /test.c: -------------------------------------------------------------------------------- 1 | Monty 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /0x13-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x1D-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1C-binary_trees/135-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n) 3 | O(n) 4 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/0-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/1-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n^2) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1C-binary_trees/115-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | O(log(n)) 3 | O(log(n)) 4 | -------------------------------------------------------------------------------- /0x1C-binary_trees/125-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | O(log(n)) 3 | O(log(n)) 4 | -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE >> c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x1B-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc main.c notrebloh.c -o notrebloh 3 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -shared -fPIC -o liball.so *.c 3 | -------------------------------------------------------------------------------- /0x1B-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c notrebloh.c 3 | all: 4 | $(CC) $(SRC) -o notrebloh 5 | -------------------------------------------------------------------------------- /0x00-hello_world/sizeof: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x00-hello_world/sizeof -------------------------------------------------------------------------------- /0x0C-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x08-static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/quote -------------------------------------------------------------------------------- /0x0C-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x08-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x08-static_libraries/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/main.o -------------------------------------------------------------------------------- /0x0C-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | 3 | !*.* 4 | 5 | !*/ 6 | 7 | *~ 8 | **/*~ 9 | **/\#* 10 | 11 | **/a.out -------------------------------------------------------------------------------- /0x08-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x08-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x08-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x08-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x08-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x08-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x08-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x08-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x08-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x08-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x08-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x08-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x08-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x08-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x08-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x08-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x08-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x08-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x08-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x08-static_libraries/libholberton.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x08-static_libraries/libholberton.a -------------------------------------------------------------------------------- /0x09-argc_argv/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | int _putchar(char c); 5 | int atoi(char *s); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /0x0C-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | #define ABS(x) ((x < 0) ? (-x) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/libholberton.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x17-dynamic_libraries/libholberton.so -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/9-set_string: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x06-pointers_arrays_strings/9-set_string -------------------------------------------------------------------------------- /0x17-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x17-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "0x18-stacks_queues_lifo_fifo/monty"] 2 | path = 0x18-stacks_queues_lifo_fifo/monty 3 | url = https://github.com/flourishcodes/Monty 4 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-print_diagsums: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AdekunleAdeniran/low_level_programming/HEAD/0x06-pointers_arrays_strings/8-print_diagsums -------------------------------------------------------------------------------- /0x08-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar -rc liball.a *.o 4 | ar -t liball.a 5 | ranlib liball.a 6 | -------------------------------------------------------------------------------- /0x1B-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_notrebloh(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1B-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | NAME = notrebloh 2 | 3 | CC = gcc 4 | 5 | SRC = main.c notrebloh.c 6 | 7 | OBJ = $(SRC:.c=.o) 8 | 9 | all: $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | -------------------------------------------------------------------------------- /0x08-static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | int main(void) 4 | { 5 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 6 | return (0); 7 | } 8 | -------------------------------------------------------------------------------- /0x1B-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_notrebloh(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * reset_to_98 - updates values 4 | *@n: int type 5 | * 6 | */ 7 | 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main -main block 4 | * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | puts("\"Programming is like building a multilingual puzzle"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main -main block 4 | * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | printf("with proper grammar, but the outcome is a piece of art,\n"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | # include "notrebloh.h" 2 | 3 | /** 4 | * add - get the last digit 5 | * 6 | * @a: int type 7 | * @b: int type 8 | * 9 | * Return: sum of two intergers 10 | */ 11 | 12 | int add(int a, int b) 13 | { 14 | return (a + b); 15 | } 16 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * mul - multiply 2 integers 5 | * @a: int type number 6 | * @b: int type number 7 | * Return: result of multiplication 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS 2 | #define SEARCH_ALGOS 3 | 4 | #include 5 | #include 6 | 7 | int linear_search(int *array, size_t size, int value); 8 | int binary_search(int *array, size_t size, int value); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x08-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _isdigit - Check for digits (0 through 9) 5 | * @c: int type param 6 | * Return: 1 if digit, else 0 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c > 47 && c < 58) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0C-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - program to print the file name 4 | * Description: print file name from which file is compiled from 5 | * Return: always a success 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _isdigit - Check for digits (0 through 9) 5 | * @c: int type param 6 | * Return: 1 if digit, else 0 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c > 47 && c < 58) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * swap_int - change the values of ints 5 | * @a: int type 6 | * @b: int type 7 | * Return: Return swapped values 8 | */ 9 | void swap_int(int *a, int *b) 10 | { 11 | int c; 12 | 13 | c = *a; 14 | *a = *b; 15 | *b = c; 16 | } 17 | -------------------------------------------------------------------------------- /0x08-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _isupper - function that checks for uppercase characters 5 | * @c: int type number 6 | * Return: 1 if uppercase, else 0 7 | */ 8 | int _isupper(int c) 9 | { 10 | if (c > 64 && c < 91) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strlen - return length of a string 5 | * 6 | * @s: char type 7 | * Return: length of string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int a; 13 | 14 | for (a = 0; s[a] != '\0'; a++) 15 | { 16 | 17 | } 18 | return (a); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _abs - compute the absolute value of an integer 5 | * 6 | * @a: int type 7 | * 8 | * Return: return absolute value of integer 9 | */ 10 | 11 | int _abs(int a) 12 | { 13 | if (a < 0) 14 | return (a * -1); 15 | else 16 | return (a); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _abs - compute the absolute value of an integer 5 | * 6 | * @a: int type 7 | * 8 | * Return: return absolute value of integer 9 | */ 10 | 11 | int _abs(int a) 12 | { 13 | if (a < 0) 14 | return (a * -1); 15 | else 16 | return (a); 17 | } 18 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _isupper - function that checks for uppercase characters 5 | * @c: int type number 6 | * Return: 1 if uppercase, else 0 7 | */ 8 | int _isupper(int c) 9 | { 10 | if (c > 64 && c < 91) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strlen - return length of a string 5 | * 6 | * @s: char type 7 | * Return: length of string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int a; 13 | 14 | for (a = 0; s[a] != '\0'; a++) 15 | { 16 | 17 | } 18 | return (a); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/9-set_string.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * set_string - use a pointer to derefrence another string 5 | * @s: pointer to pointer of string 6 | * @to: pointer to string 7 | * Return: always success 8 | * 9 | */ 10 | 11 | void set_string(char **s, char *to) 12 | { 13 | *s = to; 14 | } 15 | -------------------------------------------------------------------------------- /0x1B-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | NAME = notrebloh 2 | 3 | CC = gcc 4 | 5 | SRC = main.c notrebloh.c 6 | 7 | OBJ = $(SRC:.c=.o) 8 | 9 | RM = rm -f 10 | 11 | all: $(OBJ) 12 | $(CC) $(OBJ) -o $(NAME) 13 | 14 | clean: 15 | $(RM) *~ $(NAME) 16 | 17 | oclean: 18 | $(RM) $(OBJ) 19 | 20 | fclean: clean oclean 21 | 22 | re: oclean all 23 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_list - function to free nodes 4 | * @head: pointer to nodes 5 | * Return: Always Successful 6 | */ 7 | void free_list(list_t *head) 8 | { 9 | while (head != NULL) 10 | { 11 | free(head->str); 12 | free(head); 13 | head = head->next; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * free_dog - function to free space 5 | * @d: pointer to dog typedef type 6 | * Return: Always success 7 | */ 8 | 9 | void free_dog(dog_t *d) 10 | { 11 | if (d == NULL) 12 | return; 13 | free(d->name); 14 | free(d->owner); 15 | free(d); 16 | } 17 | -------------------------------------------------------------------------------- /0x07-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _print_rev_recursion - reverse a string 5 | * 6 | * @s: pointer to string to be reversed 7 | * Return: return reversed 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-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _islower - checks if character is lowercase, return 1 is yes and 0 if not. 5 | * @c: is char type 6 | * Return: 1 if c is lower and 0 otherwise. 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | if (c >= 97 && c <= 122) 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all numbers of base 10, starting from 0. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int a; 10 | 11 | for (a = 0; a < 10; a++) 12 | { 13 | printf("%i", a); 14 | } 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _islower - checks if character is lowercase, return 1 is yes and 0 if not. 5 | * @c: is char type 6 | * Return: 1 if c is lower and 0 otherwise. 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | if (c >= 97 && c <= 122) 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_numbers - print the numbers from 0 to 9 5 | * Description: You can only use _putchar twice 6 | */ 7 | void print_numbers(void) 8 | { 9 | int a; 10 | 11 | a = 0; 12 | while (a < 10) 13 | { 14 | _putchar(a + '0'); 15 | a++; 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _puts - change the values of ints 5 | * @str: char type 6 | * Description: take string and print to standard output 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int a; 12 | 13 | for (a = 0; str[a] != '\0'; a++) 14 | { 15 | _putchar(str[a]); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * get_endianness - function to check endianness of machine 5 | * Return: 1 if Little Endian or 0 if big Endian 6 | */ 7 | int get_endianness(void) 8 | { 9 | unsigned int i = 1; 10 | char *c = (char *)&i; 11 | 12 | if (*c) 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _puts - change the values of ints 5 | * @str: char type 6 | * Description: take string and print to standard output 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int a; 12 | 13 | for (a = 0; str[a] != '\0'; a++) 14 | { 15 | _putchar(str[a]); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | /** 4 | * print_name - function to print name 5 | * @name: pointer of char type 6 | * @f: pointer to function 7 | * Return: Always successful 8 | */ 9 | 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (f == NULL) 13 | return; 14 | f(name); 15 | } 16 | -------------------------------------------------------------------------------- /0x07-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * factorial - find factorial of numbers; 5 | * @n: int type 6 | * Return: returns -1 if number less than zero, else return factorial 7 | */ 8 | 9 | int factorial(int n) 10 | { 11 | if (n < 0) 12 | return (-1); 13 | if (n >= 1) 14 | return (n * factorial(n - 1)); 15 | else 16 | return (1); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - program to print name of program 5 | * @argc: size of argv array and number of command line arguments 6 | * @argv: An array of size argc 7 | * Return: always successful 8 | */ 9 | int main(int argc __attribute__((unused)), char **argv) 10 | { 11 | printf("%s\n", argv[0]); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x1C-binary_trees/4-binary_tree_is_leaf.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_leaf - check if leaf 5 | * @node: node to check 6 | * Return: 1 if leaf, 0 if not 7 | */ 8 | int binary_tree_is_leaf(const binary_tree_t *node) 9 | { 10 | if (!node) 11 | return (0); 12 | if (!(node->left) && !(node->right)) 13 | return (1); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print the alphabet in reverse order in lowercase. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char alpha; 10 | 11 | for (alpha = 'z'; alpha >= 'a'; alpha--) 12 | { 13 | putchar(alpha); 14 | } 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | /** 3 | * main - program to print out number of arguments 4 | * @argc: size of argv array and number of command line arguments 5 | * @argv: An array of size argc 6 | * Return: always successful 7 | */ 8 | 9 | int main(int argc, char **argv __attribute__((unused))) 10 | { 11 | printf("%d\n", argc - 1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | #include "string.h" 3 | #include "unistd.h" 4 | /** 5 | * main - main block 6 | * Return: 1 7 | */ 8 | int main(void) 9 | { 10 | int len; 11 | len = strlen("and that piece of art is useful\" - Dora Korpar, 2015-10-19\n"); 12 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", len); 13 | return (1); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * *_memset - set memory function 5 | * @s: pointer to array 6 | * @b: char type 7 | * @n: unsigned int 8 | * Return: s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | int x; 14 | 15 | for (x = 0; n > 0; x++) 16 | { 17 | s[x] = b; 18 | n--; 19 | } 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Use `putchar` function to print the alphabet in lowercase. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char alpha = 'a'; 10 | 11 | while (alpha <= 'z') 12 | { 13 | putchar(alpha); 14 | alpha++; 15 | } 16 | putchar('\n'); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/palidrome.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | def palidrome(num): 3 | n = str(num) 4 | return (n == n[::-1]) 5 | 6 | def highp(low, high): 7 | total = [] 8 | for i in range(high, low, -1): 9 | for j in range(high, low, -1): 10 | prod = i * j 11 | if palidrome(prod): 12 | total.append(prod) 13 | print (max(total)) 14 | 15 | highp(99, 999) 16 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_line - draw a straight line 5 | * @n: number of times line is printed 6 | * Description: Can only use _putchar to print 7 | */ 8 | void print_line(int n) 9 | { 10 | int c; 11 | 12 | c = 0; 13 | while (c < n) 14 | { 15 | _putchar('_'); 16 | 17 | c++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _isupper(int c); 3 | int _isdigit(int c); 4 | int mul(int a, int b); 5 | void print_numbers(void); 6 | void print_most_numbers(void); 7 | void more_numbers(void); 8 | void print_line(int n); 9 | void print_diagonal(int n); 10 | void print_square(int size); 11 | void print_triangle(int size); 12 | void print_number(int n); 13 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * *_memset - set memory function 5 | * @s: pointer to array 6 | * @b: char type 7 | * @n: unsigned int 8 | * Return: s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | int x; 14 | 15 | for (x = 0; n > 0; x++) 16 | { 17 | s[x] = b; 18 | n--; 19 | } 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int); 8 | int print_last_digit(int); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int, int); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_listint - function to free heap 4 | * @head: pointer to struct of listint type 5 | * Return: always successful 6 | */ 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *temp = NULL; 10 | 11 | while (head != NULL) 12 | { 13 | temp = head; 14 | head = head->next; 15 | free(temp); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_most_numbers - Print numbers 0 to 9, minus 2 and 4. 5 | * Description: use only _putchar 6 | */ 7 | void print_most_numbers(void) 8 | { 9 | int a; 10 | 11 | a = 0; 12 | while (a < 10) 13 | { 14 | if (a != 2 && a != 4) 15 | _putchar(a + '0'); 16 | 17 | a++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strlen_recursion - find length of a string 5 | * @s: pointer to string to be counted 6 | * Return: return count 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | int count; 12 | 13 | if (*s == '\0') 14 | { 15 | return (0); 16 | } 17 | else 18 | count = 1; 19 | return (count + _strlen_recursion(s + 1)); 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | 4 | int _putchar(char c); 5 | 6 | void print_name(char *name, void (*f)(char *)); 7 | 8 | void array_iterator(int *array, size_t size, void (*action)(int)); 9 | 10 | int int_index(int *array, int size, int (*cmp)(int)); 11 | 12 | int (*get_op_func(char *s))(int, int); 13 | 14 | 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_last_digit - selects the last digit of an integer 5 | * @n: int type 6 | * 7 | * Return: the absolute value of last digit 8 | */ 9 | 10 | int print_last_digit(int n) 11 | { 12 | int m; 13 | 14 | if (n >= 0) 15 | m = n % 10; 16 | else 17 | m = (n % 10) * -1; 18 | _putchar(m + '0'); 19 | return (m); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _pow_recursion - take x to power y 5 | * @x: int type 6 | * @y: int type 7 | * Return: return success 8 | */ 9 | 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | { 14 | return (-1); 15 | } 16 | else if (y == 0) 17 | { 18 | return (1); 19 | } 20 | else 21 | return (x * (_pow_recursion(x, y - 1))); 22 | } 23 | -------------------------------------------------------------------------------- /0x19-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * key_index - C function to find index of a key 4 | * @key: pointer to key 5 | * @size: size of the array of the hash table 6 | * Return: index of key 7 | */ 8 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 9 | { 10 | unsigned long int hash; 11 | 12 | hash = hash_djb2(key) % size; 13 | return (hash); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * puts2 - print one char out of 2 of a string 5 | * @str: char array string type 6 | * Description: Print 1st char, then 3rd, then 5th, etc.. 7 | */ 8 | 9 | void puts2(char *str) 10 | { 11 | int a; 12 | 13 | for (a = 0; str[a] != '\0'; a++) 14 | { 15 | if (a % 2 == 0) 16 | _putchar(str[a]); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * myStartupFun - function to run command before main function 6 | * Return: always successful 7 | */ 8 | void myStartupFun(void) __attribute__ ((constructor)); 9 | 10 | void myStartupFun(void) 11 | { 12 | printf("You're beat! and yet, you must allow,\n" 13 | "I bore my house upon my back!\n"); 14 | } 15 | -------------------------------------------------------------------------------- /0x1B-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | NAME = notrebloh 2 | 3 | CC = gcc 4 | 5 | SRC = main.c notrebloh.c 6 | 7 | OBJ = $(SRC:.c=.o) 8 | 9 | .PHONY: all clean oclean fclean re 10 | 11 | CFLAGS = -Wall -Werror -Wextra -pedantic 12 | 13 | all: m.h $(OBJ) 14 | $(CC) $(OBJ) -o $(NAME) 15 | 16 | clean: 17 | $(RM) -f *~ $(NAME) 18 | 19 | oclean: 20 | $(RM) -f $(OBJ) 21 | 22 | fclean: clean oclean 23 | 24 | re: oclean all 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * string_toupper - change a string to upper case 5 | * @s: pointer to a string 6 | * Return: s 7 | */ 8 | 9 | char *string_toupper(char *s) 10 | { 11 | int i = 0; 12 | 13 | while (s[i] != '\0') 14 | { 15 | if (s[i] >= 'a' && s[i] <= 'z') 16 | { 17 | s[i] = s[i] - 32; 18 | } 19 | ++i; 20 | } 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * *malloc_checked - allocate memory with malloc 8 | * @b: unsigned int type 9 | * Return: return pointer 10 | */ 11 | void *malloc_checked(unsigned int b) 12 | { 13 | int *ptr; 14 | 15 | ptr = malloc(b); 16 | if (ptr == NULL) 17 | exit(98); 18 | return (ptr); 19 | } 20 | -------------------------------------------------------------------------------- /0x1C-binary_trees/10-binary_tree_depth.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_depth - calculate levels from parent 5 | * @node: node 6 | * Return: levels node is from parent 7 | */ 8 | size_t binary_tree_depth(const binary_tree_t *node) 9 | { 10 | if (!node) 11 | return (0); 12 | 13 | if (node && (node->parent)) 14 | return (1 + binary_tree_depth(node->parent)); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_alphabet_x10 - print list of alphabets 10 times 5 | * 6 | * Return: 0 7 | */ 8 | 9 | void print_alphabet_x10(void) 10 | { 11 | char c; 12 | int d = 0; 13 | 14 | while (d < 10) 15 | { 16 | c = 'a'; 17 | while (c <= 'z') 18 | { 19 | _putchar(c); 20 | c++; 21 | } 22 | _putchar('\n'); 23 | d++; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/9-main.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for notrebloh 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 | -------------------------------------------------------------------------------- /0x08-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strchr - checks for character in a string 5 | * @s: pointer to array 6 | * @c: char type 7 | * 8 | * Return: arrayed at check 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | while (*s != '\0') 14 | { 15 | if (*s == c) 16 | return (s); 17 | s++; 18 | } 19 | if (c == '\0') 20 | return (s); 21 | s = 0; 22 | return (s); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | /** 4 | * listint_len - function to print all elements 5 | * @h: listint_t type of node struct 6 | * Return: Always successful 7 | */ 8 | size_t listint_len(const listint_t *h) 9 | { 10 | unsigned int count; 11 | 12 | count = 0; 13 | while (h != NULL) 14 | { 15 | h = h->next; 16 | count++; 17 | } 18 | return (count); 19 | } 20 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_dlistint - function to free allocated memory on heap 4 | * @head: pointer to head of doubly linked list 5 | * 6 | */ 7 | void free_dlistint(dlistint_t *head) 8 | { 9 | dlistint_t *temp; 10 | 11 | if (head == NULL) 12 | return; 13 | while (head != NULL) 14 | { 15 | temp = head; 16 | head = head->next; 17 | free(temp); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_alphabet - use only user defined variables to create output 5 | * 6 | * Description: Write program to print "a to z" using _putchar 7 | * Return: always success 8 | */ 9 | 10 | void print_alphabet(void) 11 | { 12 | char c = 'a'; 13 | 14 | while (c <= 'z') 15 | { 16 | _putchar(c); 17 | c++; 18 | } 19 | _putchar('\n'); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * puts_half - returns half of string 5 | * @str: type char 6 | * 7 | * Return: half of string 8 | */ 9 | 10 | 11 | void puts_half(char *str) 12 | { 13 | int a; 14 | 15 | for (a = 0; str[a] != '\0'; a++) 16 | { 17 | 18 | } 19 | a++; 20 | for (a /= 2; str[a] != '\0'; a++) 21 | { 22 | _putchar(str[a]); 23 | } 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_chessboard - print a chess board 5 | * @a: array pointer to chess board 6 | * Return: chessboard 7 | * 8 | */ 9 | 10 | void print_chessboard(char (*a)[8]) 11 | { 12 | int x, y; 13 | 14 | for (x = 0; x < 8; x++) 15 | { 16 | for (y = 0; y < 8; y++) 17 | { 18 | _putchar(a[x][y]); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | int _putchar(char c); 5 | 6 | void *malloc_checked(unsigned int b); 7 | 8 | char *string_nconcat(char *s1, char *s2, unsigned int n); 9 | 10 | void *_calloc(unsigned int nmemb, unsigned int size); 11 | 12 | int *array_range(int min, int max); 13 | 14 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /0x1B-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | NAME = notrebloh 2 | 3 | CC = gcc 4 | 5 | SRC = main.c notrebloh.c 6 | 7 | OBJ = $(SRC:.c=.o) 8 | 9 | RM = rm -f 10 | 11 | CFLAGS = -Wall -Werror -Wextra -pedantic 12 | 13 | all: $(OBJ) 14 | $(CC) $(OBJ) -o $(NAME) 15 | 16 | %.o:%.c 17 | $(CC) $(CFLAGS) -c -o $@ $< 18 | 19 | clean: 20 | $(RM) *~ $(NAME) 21 | 22 | oclean: 23 | $(RM) $(OBJ) 24 | 25 | fclean: clean oclean 26 | 27 | re: oclean all 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print the alphabet on a line omitting e and q 5 | * using putchar only twice 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char alpha = 'a'; 11 | while (alpha <= 'z') 12 | { 13 | putchar(alpha); 14 | alpha++; 15 | if (alpha == 'e' || alpha == 'q') 16 | alpha++; 17 | } 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strchr - checks for character in a string 5 | * @s: pointer to array 6 | * @c: char type 7 | * 8 | * Return: arrayed at check 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | while (*s != '\0') 14 | { 15 | if (*s == c) 16 | return (s); 17 | s++; 18 | } 19 | if (c == '\0') 20 | return (s); 21 | s = 0; 22 | return (s); 23 | } 24 | -------------------------------------------------------------------------------- /0x19-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_djb2 - C function to implement the djb2 algorithm 4 | * @str: const unsigned char type 5 | * Return: always successful 6 | */ 7 | unsigned long int hash_djb2(const unsigned char *str) 8 | { 9 | unsigned long hash = 5381; 10 | int count; 11 | 12 | while ((count = *str++)) 13 | hash = ((hash << 5) + hash) + count; /*hash * 33 + count*/ 14 | return (hash); 15 | } 16 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "sort.h" 3 | 4 | /** 5 | * print_list - Prints a list of integers 6 | * 7 | * @list: The list to be printed 8 | */ 9 | void print_list(const listint_t *list) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (list) 15 | { 16 | if (i > 0) 17 | printf(", "); 18 | printf("%d", list->n); 19 | ++i; 20 | list = list->next; 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x1C-binary_trees/5-binary_tree_is_root.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_root - C function to check if node is root of a binary tree 5 | * @node: Pointer to node to check 6 | * Return: 0 if false, 1 if true 7 | */ 8 | int binary_tree_is_root(const binary_tree_t *node) 9 | { 10 | if (node == NULL) 11 | return (0); 12 | 13 | if (node->parent == NULL) 14 | return (1); 15 | else 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - program to print arguments on a new line 4 | * @argc: size of argv array and number of command line arguments 5 | * @argv: An array of size argc 6 | * Return: always successful 7 | */ 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | int count; 12 | 13 | for (count = 0; count < argc; count++) 14 | { 15 | printf("%s\n", argv[count]); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * set_bit - function to set value of bit at given index 5 | * @n: binary number given 6 | * @index: index given 7 | * Return: 1 if successful or -1 if failed 8 | */ 9 | 10 | int set_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (index < sizeof(n) * 8) 13 | { 14 | *n = *n | (1 << index); 15 | return (1); 16 | } 17 | return (-1); 18 | } 19 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * dlistint_len - function to print the length of a doubly linked list 4 | * @h: pointer of const dlistint_t type 5 | * Return: return the length of the doubly linked list 6 | */ 7 | 8 | size_t dlistint_len(const dlistint_t *h) 9 | { 10 | unsigned int length = 0; 11 | 12 | while (h != NULL) 13 | { 14 | h = h->next; 15 | length++; 16 | } 17 | return (length); 18 | } 19 | -------------------------------------------------------------------------------- /0x1C-binary_trees/3-binary_tree_delete.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_delete - delete entire binary tree 5 | * @tree: pointer to tree 6 | */ 7 | void binary_tree_delete(binary_tree_t *tree) 8 | { 9 | if (tree == NULL) 10 | return; 11 | 12 | /*recursively search for all nodes left and right and delete them*/ 13 | binary_tree_delete(tree->left); 14 | binary_tree_delete(tree->right); 15 | 16 | free(tree); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Program to sum integers 4 | * 5 | * Description: check for all natural numbers in range of 6 | * 3 and 5 and sum them 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int a, b; 13 | 14 | for (a = 0; a < 1024; a++) 15 | { 16 | 17 | if (a % 3 == 0 || a % 5 == 0) 18 | { 19 | b += a; 20 | } 21 | } 22 | printf("%d\n", b); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x08-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _isalpha - checks if character is letter, lowercase or uppercase 5 | * and return 1 if yes and 0 if no. 6 | * 7 | * @c: char type 8 | * 9 | * Return: 1 if c is letter, lowercase or uppercase 10 | * and 0 otherwise. 11 | */ 12 | 13 | int _isalpha(int c) 14 | { 15 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 16 | return (1); 17 | else 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * sum_listint - function to sum int values of nodes 4 | * @head: pointer to head node 5 | * Return: always successful 6 | */ 7 | int sum_listint(listint_t *head) 8 | { 9 | unsigned int sum; 10 | 11 | if (head == NULL) 12 | return (0); 13 | sum = 0; 14 | while (head != NULL) 15 | { 16 | sum += head->n; 17 | head = head->next; 18 | } 19 | return (sum); 20 | } 21 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * sum_dlistint - function to sum the elements of a doubly linked list 4 | * @head: pointer of type dlistint_t to head of list 5 | * Return: sum of all elements in a linked list 6 | */ 7 | int sum_dlistint(dlistint_t *head) 8 | { 9 | unsigned int sum = 0; 10 | 11 | while (head != NULL) 12 | { 13 | sum += head->n; 14 | head = head->next; 15 | } 16 | return (sum); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _isalpha - checks if character is letter, lowercase or uppercase 5 | * and return 1 if yes and 0 if no. 6 | * 7 | * @c: char type 8 | * 9 | * Return: 1 if c is letter, lowercase or uppercase 10 | * and 0 otherwise. 11 | */ 12 | 13 | int _isalpha(int c) 14 | { 15 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 16 | return (1); 17 | else 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-file_io/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef notrebloh_H 2 | #define notrebloh_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | ssize_t read_textfile(const char *filename, size_t letters); 12 | 13 | int create_file(const char *filename, char *text_content); 14 | 15 | int append_text_to_file(const char *filename, char *text_content); 16 | 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * print_listint - function to print all elements 6 | * @h: listint_t type of node struct 7 | * Return: Always successful 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | unsigned int count; 12 | 13 | count = 0; 14 | while (h != NULL) 15 | { 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | count++; 19 | } 20 | return (count); 21 | } 22 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * pop_listint - function to remove node from top 4 | * @head: pointer to head node 5 | * Return: always successful 6 | */ 7 | int pop_listint(listint_t **head) 8 | { 9 | listint_t *top = NULL; 10 | int value = 0; 11 | 12 | if (*head == NULL) 13 | { 14 | return (0); 15 | } 16 | top = *head; 17 | value = top->n; 18 | *head = top->next; 19 | free(top); 20 | return (value); 21 | } 22 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | printf("Size of a char: %d byte(s)\n", sizeof(char)); 9 | printf("Size of an int: %d byte(s)\n", sizeof(int)); 10 | printf("Size of a long int: %d byte(s)\n", sizeof(long int)); 11 | printf("Size of a long long int: %d byte(s)\n", sizeof(long long int)); 12 | printf("Size of a float: %d byte(s)\n", sizeof(float)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all numbers of base64 in lowercase. 5 | * You can only use `putchar`, and only 3 times. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | 12 | for (a = 0; a < 10; a++) 13 | { 14 | putchar(a + '0'); 15 | } 16 | for (a = 'a'; a <= 'f'; a++) 17 | { 18 | putchar(a); 19 | } 20 | putchar('\n'); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * get_bit - function to get bit at index 4 | * @n: unsigned long int type 5 | * @index: unsigned int type 6 | * Return: always successful 7 | */ 8 | int get_bit(unsigned long int n, unsigned int index) 9 | { 10 | unsigned long int check = 1; 11 | 12 | if (index > sizeof(n) * 8) 13 | return (-1); 14 | check <<= index; 15 | if (check & n) 16 | return (1); 17 | else 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_sign - check if n is +, - or 0 5 | * 6 | * @n: int type 7 | * 8 | * Return: 1 if n is > 0, 0 is zero and -1 if < 0. 9 | */ 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('0'); 21 | return (0); 22 | } 23 | else 24 | { 25 | _putchar('-'); 26 | return (-1); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_square - print hashes in pattern of size 5 | * @size: int type 6 | * 7 | * Return: prints hashes in squares 8 | */ 9 | 10 | void print_square(int size) 11 | { 12 | int a, b; 13 | 14 | for (b = 1; b <= size; b++) 15 | { 16 | for (a = 1; a <= size; a++) 17 | { 18 | _putchar('#'); 19 | } 20 | _putchar('\n'); 21 | } 22 | if (size < 1) 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * reverse_array - reverse an array of int 5 | * @a: pointer to an array 6 | * @n: int type for size of array 7 | * Return: reversed array 8 | */ 9 | 10 | void reverse_array(int *a, int n) 11 | { 12 | int i, j, temp; 13 | 14 | for (i = 0, j = n - 1; j >= i; i++, j--) 15 | { 16 | temp = a[i]; 17 | a[i] = a[j]; 18 | a[j] = temp; 19 | } 20 | for (i = 0; i < n; i++) 21 | ; 22 | } 23 | -------------------------------------------------------------------------------- /0x08-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strcmp - return length of a string 5 | * 6 | * @s1: char type 7 | * @s2: char type 8 | * Return: positive negative or zero 9 | */ 10 | 11 | int _strcmp(char *s1, char *s2) 12 | { 13 | int a; 14 | 15 | for (a = 0; s1[a] != '\0'; a++) 16 | ; 17 | for (a = 0; s2[a] != '\0'; a++) 18 | { 19 | if (s1[a] != s2[a]) 20 | { 21 | return (s1[a] - s2[a]); 22 | } 23 | } 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _puts_recursion - recursion function to print string 5 | * @s: pointer of char type to input string 6 | * description: print a string without loops 7 | * 8 | * Return: Always success 9 | */ 10 | 11 | 12 | void _puts_recursion(char *s) 13 | { 14 | if (*s == '\0') 15 | { 16 | _putchar('\n'); 17 | return; 18 | } 19 | else 20 | { 21 | _putchar(*s); 22 | _puts_recursion(s + 1); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x08-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * *_memcpy - copy into memory location 5 | * @dest: char type pointer to destination array 6 | * @src: char type pointer to source array 7 | * @n: unsigned int type 8 | * Return: pointer to destination 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | int x; 14 | 15 | for (x = 0; n > 0; x++) 16 | { 17 | dest[x] = src[x]; 18 | n--; 19 | } 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - function to print the elements of a doubly linked list 5 | * @h: pointer of const dlistint_t type 6 | * Return: return list and number of elements 7 | */ 8 | 9 | size_t print_dlistint(const dlistint_t *h) 10 | { 11 | unsigned int count = 0; 12 | 13 | while (h != NULL) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | count++; 18 | } 19 | return (count); 20 | } 21 | -------------------------------------------------------------------------------- /0x1C-binary_trees/6-binary_tree_preorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_preorder - print preorder traversal (all left nodes, then right) 5 | * @tree: pointer to node 6 | * @func: function to print 7 | */ 8 | void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (!tree || !func) 11 | return; 12 | 13 | func(tree->n); 14 | binary_tree_preorder(tree->left, func); 15 | binary_tree_preorder(tree->right, func); 16 | } 17 | -------------------------------------------------------------------------------- /0x1C-binary_trees/8-binary_tree_postorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_postorder - print postorder traversal (left, right, then parent) 5 | * @tree: pointer to node 6 | * @func: function to print 7 | */ 8 | void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (!tree || !func) 11 | return; 12 | 13 | binary_tree_postorder(tree->left, func); 14 | binary_tree_postorder(tree->right, func); 15 | func(tree->n); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strcmp - return length of a string 5 | * 6 | * @s1: char type 7 | * @s2: char type 8 | * Return: positive negative or zero 9 | */ 10 | 11 | int _strcmp(char *s1, char *s2) 12 | { 13 | int a; 14 | 15 | for (a = 0; s1[a] != '\0'; a++) 16 | ; 17 | for (a = 0; s2[a] != '\0'; a++) 18 | { 19 | if (s1[a] != s2[a]) 20 | { 21 | return (s1[a] - s2[a]); 22 | } 23 | } 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * init_dog - initialize function for struct dog 6 | * @d: pointer to dog struct 7 | * @name: char type 8 | * @age: float type 9 | * @owner: char type 10 | * Return: return pointer to struct 11 | */ 12 | 13 | void init_dog(struct dog *d, char *name, float age, char *owner) 14 | { 15 | if (d == NULL) 16 | return; 17 | d->name = name; 18 | d->age = age; 19 | d->owner = owner; 20 | } 21 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * print_array - Prints an array of integers 6 | * 7 | * @array: The array to be printed 8 | * @size: Number of elements in @array 9 | */ 10 | void print_array(const int *array, size_t size) 11 | { 12 | size_t i; 13 | 14 | i = 0; 15 | while (array && i < size) 16 | { 17 | if (i > 0) 18 | printf(", "); 19 | printf("%d", array[i]); 20 | ++i; 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_rev - change the values of ints 5 | * @s: char type 6 | * Description: take string and print to standard output 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int a; 12 | 13 | for (a = 0; s[a] != '\0'; a++) 14 | { 15 | 16 | } 17 | a--; /* read the output from the last and print it out*/ 18 | while (s[a] != 0) 19 | { 20 | _putchar(s[a]); 21 | a--; 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * *_memcpy - copy into memory location 5 | * @dest: char type pointer to destination array 6 | * @src: char type pointer to source array 7 | * @n: unsigned int type 8 | * Return: pointer to destination 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | int x; 14 | 15 | for (x = 0; n > 0; x++) 16 | { 17 | dest[x] = src[x]; 18 | n--; 19 | } 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | int _putchar(char c); 5 | void reset_to_98(int *n); 6 | void swap_int(int *a, int *b); 7 | int _strlen(char *s); 8 | void _puts(char *str); 9 | void print_rev(char *s); 10 | void rev_string(char *s); 11 | void puts2(char *str); 12 | void puts_half(char *str); 13 | void print_array(int *a, int n); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | int _isdigit(int c); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x08-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strcpy - function to copy string from source to destination 5 | *@dest: pointer to destinatioin of string to be copied 6 | *@src: pointer to source of string to be copied 7 | * Return: return the value at dest 8 | */ 9 | 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int a; 13 | 14 | for (a = 0; src[a] != '\0'; a++) 15 | { 16 | dest[a] = src[a]; 17 | } 18 | dest[a] = '\0'; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x0A-malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | int _putchar(char c); 5 | 6 | char *create_array(unsigned int size, char c); 7 | 8 | char *_strdup(char *str); 9 | 10 | int _strlen(char *s); 11 | 12 | char *str_concat(char *s1, char *s2); 13 | 14 | char *_strncat(char *dest, char *src, int n); 15 | 16 | int **alloc_grid(int width, int height); 17 | 18 | void free_grid(int **grid, int height); 19 | 20 | char *argstostr(int ac, char **av); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * more_numbers - print 10 times the numbers from 0 to 14 5 | * Description: Can only use _putchar 3 times 6 | */ 7 | void more_numbers(void) 8 | { 9 | int a, b; 10 | 11 | a = 0; 12 | while (a < 10) 13 | { 14 | b = 0; 15 | while (b < 15) 16 | { 17 | if (b > 9) 18 | _putchar(b / 10 + '0'); 19 | 20 | _putchar(b % 10 + '0'); 21 | b++; 22 | } 23 | a++; 24 | _putchar('\n'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_diagonal - print backslash diagonally 5 | * @n: int type 6 | * 7 | * Return: prints lines and space to the value of n 8 | */ 9 | 10 | void print_diagonal(int n) 11 | { 12 | int a, b; 13 | 14 | for (a = 1; a <= n; a++) 15 | { 16 | for (b = 2; b <= a; b++) 17 | { 18 | _putchar(' '); 19 | } 20 | _putchar('\\'); 21 | _putchar('\n'); 22 | } 23 | if (n < 1) 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strcpy - function to copy string from source to destination 5 | *@dest: pointer to destinatioin of string to be copied 6 | *@src: pointer to source of string to be copied 7 | * Return: return the value at dest 8 | */ 9 | 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int a; 13 | 14 | for (a = 0; src[a] != '\0'; a++) 15 | { 16 | dest[a] = src[a]; 17 | } 18 | dest[a] = '\0'; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-recursion/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | 3 | void _puts_recursion(char *s); 4 | 5 | void _print_rev_recursion(char *s); 6 | 7 | int _strlen_recursion(char *s); 8 | 9 | int factorial(int n); 10 | 11 | int _pow_recursion(int x, int y); 12 | 13 | int _sqrt_recursion(int n); 14 | 15 | int is_prime_number(int n); 16 | 17 | int is_palindrome(char *s); 18 | 19 | int if_natural_square(int a, int b); 20 | 21 | int check_prime(int a, int b); 22 | 23 | int charcheck(char *str, int length); 24 | -------------------------------------------------------------------------------- /0x0A-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * free_grid - function to free memory 7 | * @grid: pointer to grid of int type 8 | * @height: int type 9 | * Return: clean up memory 10 | */ 11 | 12 | void free_grid(int **grid, int height) 13 | { 14 | int y; 15 | 16 | if (grid == NULL || grid == 0) 17 | { 18 | return; 19 | } 20 | for (y = 0; y < height; y++) 21 | { 22 | free(grid[y]); 23 | } 24 | free(grid); 25 | } 26 | -------------------------------------------------------------------------------- /0x1C-binary_trees/7-binary_tree_inorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_inorder - C function to print Tree in order traversal 5 | * @tree: tree to be printed 6 | * @func: function pointer to tree 7 | * Return: return printed data 8 | */ 9 | void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int)) 10 | { 11 | if (!tree || !func) 12 | return; 13 | binary_tree_inorder(tree->left, func); 14 | func(tree->n); 15 | binary_tree_inorder(tree->right, func); 16 | } 17 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print the alphabet in lowercase and uppercase on same line 5 | * using putchar only thrice, followed by new line 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char alpha = 'a'; 11 | while (alpha <= 'z') 12 | { 13 | putchar(alpha); 14 | alpha++; 15 | } 16 | alpha = 'A'; 17 | while (alpha <= 'Z') 18 | { 19 | putchar(alpha); 20 | alpha++; 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all numbers of base 10, starting from 0. 5 | * You are not allowed to use any variable of type char. 6 | * You can only use `putchar` to print to console. 7 | * You can only use `putchar` twice. 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int a; 13 | 14 | for (a = 0; a < 10; a++) 15 | { 16 | putchar(a + '0'); 17 | } 18 | putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print largest prime 5 | * 6 | * description: find the largest divisible number 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | long number; 13 | long divisor; 14 | 15 | number = 612852475143; 16 | 17 | for (divisor = 2; divisor < number; divisor++) 18 | { 19 | if (number % divisor == 0) 20 | { 21 | number /= divisor; 22 | } 23 | } 24 | printf("%ld\n", number); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - function to get length of node 5 | * @h: pointer to nodes 6 | * Return: Always succesful 7 | */ 8 | size_t list_len(const list_t *h) 9 | { 10 | const list_t *temp; 11 | unsigned int count; 12 | 13 | temp = malloc(sizeof(list_t)); 14 | if (temp == NULL) 15 | return (0); 16 | temp = h; 17 | count = 0; 18 | while (temp != NULL) 19 | { 20 | temp = temp->next; 21 | count++; 22 | } 23 | return (count); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * jack_bauer - print every minute of the day 5 | * 6 | * 7 | * Return: all the minutes 8 | */ 9 | 10 | void jack_bauer(void) 11 | { 12 | int a, b; 13 | 14 | for (a = 0; a <= 23; a++) 15 | { 16 | for (b = 0; b <= 59; b++) 17 | { 18 | _putchar(a / 10 + '0'); 19 | _putchar(a % 10 + '0'); 20 | _putchar(':'); 21 | _putchar(b / 10 + '0'); 22 | _putchar(b % 10 + '0'); 23 | _putchar('\n'); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-holberton.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * main - Entry Point.use only user defined variable to create output. 5 | * 6 | * Description: Print notrebloh using predefined functions 7 | * 8 | * Return: 0 9 | */ 10 | 11 | int main(void) 12 | { 13 | _putchar('H'); 14 | _putchar('o'); 15 | _putchar('l'); 16 | _putchar('b'); 17 | _putchar('e'); 18 | _putchar('r'); 19 | _putchar('t'); 20 | _putchar('o'); 21 | _putchar('n'); 22 | _putchar('\n'); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | 3 | char *_memset(char *s, char b, unsigned int n); 4 | 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | 7 | char *_strchr(char *s, char c); 8 | 9 | unsigned int _strspn(char *s, char *accept); 10 | 11 | char *_strpbrk(char *s, char *accept); 12 | 13 | char *_strstr(char *haystack, char *needle); 14 | 15 | void print_chessboard(char (*a)[8]); 16 | 17 | void print_diagsums(int *a, int size); 18 | 19 | void set_string(char **s, char *to); 20 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * *add_nodeint - function to add node from top 4 | * @head: listint_t type of node struct pointer to head 5 | * @n: const int type 6 | * Return: Always successful 7 | */ 8 | listint_t *add_nodeint(listint_t **head, const int n) 9 | { 10 | listint_t *top = NULL; 11 | 12 | top = malloc(sizeof(listint_t)); 13 | if (top == NULL) 14 | return (NULL); 15 | top->n = n; 16 | top->next = *head; 17 | *head = top; 18 | return (*head); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - program to multiply two numbers 7 | * @argc: size of argv array and number of command line arguments 8 | * @argv: An array of size argc 9 | * Return: always successful 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | if (argc != 3) 15 | { 16 | printf("Error\n"); 17 | return (1); 18 | } 19 | else 20 | printf("%d\n", atoi(argv[1]) * atoi(argv[2])); 21 | 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_listint2 - function to free heap memory used 4 | * @head: pointer to head of struct listint_t type 5 | * Return: always successful 6 | */ 7 | void free_listint2(listint_t **head) 8 | { 9 | listint_t *temp = NULL; 10 | listint_t *temp1 = NULL; 11 | 12 | if (head == NULL) 13 | return; 14 | temp = *head; 15 | while (temp != NULL) 16 | { 17 | temp1 = temp; 18 | temp = temp->next; 19 | free(temp1); 20 | } 21 | *head = NULL; 22 | } 23 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * clear_bit - function to clearbit 4 | * @n: unsigned long int type 5 | * @index: index to clear 6 | * Return: 1 if successful or -1 if failed 7 | */ 8 | 9 | int clear_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned long int check = 1; 12 | unsigned int size = sizeof(n) * 8; 13 | 14 | if (*n == '\0') 15 | return (-1); 16 | if (index > size) 17 | return (-1); 18 | check <<= index; 19 | *n &= ~check; 20 | return (1); 21 | } 22 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * print_dog - print struct dog 5 | * @d: pointer to struct dog 6 | * Return: return pointer to struct 7 | */ 8 | 9 | void print_dog(struct dog *d) 10 | { 11 | if (d == NULL) 12 | return; 13 | if (d->name == NULL) 14 | printf("Name: (nil)\n"); 15 | else 16 | printf("Name: %s\n", d->name); 17 | printf("Age: %f\n", d->age); 18 | if (d->owner == NULL) 19 | printf("Owner: (nil)\n"); 20 | else 21 | printf("Owner: %s\n", d->owner); 22 | } 23 | -------------------------------------------------------------------------------- /0x1C-binary_trees/17-binary_tree_sibling.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_sibling - C function to find sibling of a node in binary tree 4 | * @node: node to use for searching 5 | * Return: data at sibling 6 | */ 7 | binary_tree_t *binary_tree_sibling(binary_tree_t *node) 8 | { 9 | if (!node || !node->parent) 10 | return (NULL); 11 | 12 | if (node->parent->left == node) 13 | return (node->parent->right); 14 | else if (node->parent->right == node) 15 | return (node->parent->left); 16 | else 17 | return (NULL); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strspn - function to find size of array containing words 5 | * @s: pointer array to be searched 6 | * @accept: pointer array with char to be searched 7 | * Return: length of characters found 8 | */ 9 | 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | int i, j; 13 | 14 | for (i = 0; s[i]; i++) 15 | { 16 | for (j = 0; accept[j]; j++) 17 | { 18 | if (accept[j] == s[i]) 19 | break; 20 | } 21 | if (!accept[j]) 22 | break; 23 | } 24 | return (i); 25 | } 26 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | #include 4 | #include 5 | /** 6 | * struct op - Struct op 7 | * 8 | * @op: The operator 9 | * @f: The function associated 10 | */ 11 | typedef struct op 12 | { 13 | char *op; 14 | int (*f)(int a, int b); 15 | } op_t; 16 | 17 | int op_add(int a, int b); 18 | 19 | int op_sub(int a, int b); 20 | 21 | int op_mul(int a, int b); 22 | 23 | int op_div(int a, int b); 24 | 25 | int op_mod(int a, int b); 26 | 27 | int (*get_op_func(char *s))(int, int); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - main block 6 | * Description: Get a random number and print the number 7 | * and if it is positive, negative, or zero 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | if (n > 0) 17 | printf("%i is positive\n", n); 18 | else if (n < 0) 19 | printf("%i is negative\n", n); 20 | else 21 | printf("%i is zero\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strspn - function to find size of array containing words 5 | * @s: pointer array to be searched 6 | * @accept: pointer array with char to be searched 7 | * Return: length of characters found 8 | */ 9 | 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | int i, j; 13 | 14 | for (i = 0; s[i]; i++) 15 | { 16 | for (j = 0; accept[j]; j++) 17 | { 18 | if (accept[j] == s[i]) 19 | break; 20 | } 21 | if (!accept[j]) 22 | break; 23 | } 24 | return (i); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include "stdio.h" 3 | 4 | /** 5 | * print_diagsums- sum diagonal values 6 | * @a: pointer to array of int type 7 | * @size: int type 8 | * Return: sum of two sides 9 | * 10 | */ 11 | 12 | void print_diagsums(int *a, int size) 13 | { 14 | int x, y, suma, sumb; 15 | 16 | suma = 0; 17 | sumb = 0; 18 | 19 | for (x = 0; x < size; x++) 20 | suma += a[(size + 1) * x]; 21 | for (y = 0; y < size; y++) 22 | sumb += a[(size - 1) * (y + 1)]; 23 | printf("%d, %d\n", suma, sumb); 24 | } 25 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_list - function to print node contents 4 | * @h: pointer to node struct 5 | * Return: Always Successful 6 | */ 7 | size_t print_list(const list_t *h) 8 | { 9 | unsigned int i; 10 | const list_t *temp = NULL; 11 | 12 | i = 0; 13 | temp = h; 14 | while (temp != NULL) 15 | { 16 | if (temp->str == NULL) 17 | { 18 | printf("[0] (nil)\n"); 19 | } 20 | else 21 | printf("[%d] %s\n", temp->len, temp->str); 22 | temp = temp->next; 23 | i++; 24 | } 25 | return (i); 26 | } 27 | -------------------------------------------------------------------------------- /0x1C-binary_trees/11-binary_tree_size.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_size - C function to print the size of a Node in a tree 4 | * @tree: pointer to tree/node to be checked 5 | * Return: size of tree 6 | */ 7 | 8 | size_t binary_tree_size(const binary_tree_t *tree) 9 | { 10 | size_t height_l, height_r; 11 | 12 | if (tree == NULL) 13 | return (0); 14 | 15 | height_l = tree->left ? binary_tree_size(tree->left) : 0; 16 | height_r = tree->right ? binary_tree_size(tree->right) : 0; 17 | 18 | 19 | return (height_l + height_r + 1); 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | * get_op_func - function to select operator 6 | * 7 | * @s: chat type 8 | * Return: always successful 9 | */ 10 | 11 | int (*get_op_func(char *s))(int, int) 12 | { 13 | op_t ops[] = { 14 | {"+", op_add}, 15 | {"-", op_sub}, 16 | {"*", op_mul}, 17 | {"/", op_div}, 18 | {"%", op_mod}, 19 | {NULL, NULL} 20 | }; 21 | int i; 22 | 23 | i = 0; 24 | while (ops[i].op != NULL && strcmp(ops[i].op, s) != 0) 25 | { 26 | i++; 27 | } 28 | return (ops[i].f); 29 | } 30 | -------------------------------------------------------------------------------- /0x1C-binary_trees/113-bst_search.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * bst_search - search if node with given value exists in tree 5 | * @tree: tree 6 | * @value: value to search for 7 | * Return: pointer to node with that value, or NULL if not found 8 | */ 9 | bst_t *bst_search(const bst_t *tree, int value) 10 | { 11 | if (!tree) 12 | return (NULL); 13 | 14 | if (tree->n == value) 15 | return ((bst_t *)tree); 16 | 17 | if (value < tree->n) 18 | return (bst_search(tree->left, value)); 19 | else 20 | return (bst_search(tree->right, value)); 21 | } 22 | -------------------------------------------------------------------------------- /0x08-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strncpy - function to copy string from source to destination 5 | * @dest: pointer to destinatioin of string to be copied 6 | * @src: pointer to source of string to be copied 7 | * @n: int type to determine size of dest to copy into 8 | * Return: return dest 9 | */ 10 | 11 | 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | int i; 15 | 16 | for (i = 0; i < n && src[i] != '\0'; i++) 17 | dest[i] = src[i]; 18 | for ( ; i < n; i++) 19 | dest[i] = '\0'; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | 3 | /** 4 | * main - prints the first 50 number 5 | * 6 | * Description: fibobnachi numbers starting from one 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | long int fib1 = 0; 13 | long int fib2 = 1; 14 | long int fib3; 15 | int counter = 1; 16 | 17 | while (counter <= 50) 18 | { 19 | fib3 = fib2 + fib1; 20 | counter++; 21 | printf("%ld", fib3); 22 | if (counter <= 50) 23 | printf(", "); 24 | fib1 = fib2; 25 | fib2 = fib3; 26 | } 27 | printf("\n"); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strncpy - function to copy string from source to destination 5 | * @dest: pointer to destinatioin of string to be copied 6 | * @src: pointer to source of string to be copied 7 | * @n: int type to determine size of dest to copy into 8 | * Return: return dest 9 | */ 10 | 11 | 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | int i; 15 | 16 | for (i = 0; i < n && src[i] != '\0'; i++) 17 | dest[i] = src[i]; 18 | for ( ; i < n; i++) 19 | dest[i] = '\0'; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * leet - replace letters with numbers 5 | * @s: pointer to an array of words 6 | * 7 | * Return: s 8 | */ 9 | 10 | char *leet(char *s) 11 | { 12 | int a, b; 13 | 14 | char test[] = "aeotl"; 15 | char test1[] = "AEOTL"; 16 | char res[] = "43071"; 17 | 18 | for (a = 0; s[a] != '\0'; a++) 19 | { 20 | for (b = 0; test[b] != '\0' && test1[b] != '\0'; b++) 21 | { 22 | if (s[a] == test[b] || s[a] == test1[b]) 23 | { 24 | s[a] = res[b]; 25 | } 26 | } 27 | } 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | 3 | char *_strncat(char *dest, char *src, int n); 4 | 5 | char *_strncpy(char *dest, char *src, int n); 6 | 7 | int _strcmp(char *s1, char *s2); 8 | 9 | void reverse_array(int *a, int n); 10 | 11 | char *string_toupper(char *); 12 | 13 | char *cap_string(char *); 14 | 15 | char *leet(char *); 16 | 17 | char *rot13(char *); 18 | 19 | int _strlen(char *s); 20 | 21 | void print_number(int n); 22 | 23 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 24 | 25 | void print_buffer(char *b, int size); 26 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | * sum_them_all - variadic function to add number 6 | * @n: const unsigned int type 7 | * Return: sum of numbers 8 | */ 9 | 10 | int sum_them_all(const unsigned int n, ...) 11 | { 12 | unsigned int sum = 0; 13 | unsigned int i; 14 | va_list addlist; 15 | 16 | va_start(addlist, n); 17 | 18 | if (n == 0) 19 | return (0); 20 | for (i = 0; i < n; i++) 21 | { 22 | sum += va_arg(addlist, unsigned int); 23 | } 24 | va_end(addlist); 25 | 26 | return (sum); 27 | } 28 | -------------------------------------------------------------------------------- /0x1C-binary_trees/0-binary_tree_node.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_node - Function to made a binary tree 5 | * @parent: parent node 6 | * @value: value of parent node 7 | * Return: always successful 8 | */ 9 | 10 | binary_tree_t *binary_tree_node(binary_tree_t *parent, int value) 11 | { 12 | binary_tree_t *newNode = malloc(sizeof(binary_tree_t)); 13 | 14 | if (newNode == NULL) 15 | return (NULL); 16 | 17 | newNode->n = value; 18 | newNode->parent = parent; 19 | newNode->left = NULL; 20 | newNode->right = NULL; 21 | 22 | return (newNode); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-stacks_queues_lifo_fifo/README.md: -------------------------------------------------------------------------------- 1 | ## 0x18. C - Stacks, Queues - LIFO, FIFO 2 | 3 | **What you should learn from this project** 4 | At the end of this project you are expected to be able to explain to 5 | anyone, without the help of Google: 6 | 7 | * What is LIFO and FIFO 8 | * What is a stack, and when to use it 9 | * What is a queue, and when to use it 10 | * What are the common implementations of stacks and queues 11 | * What are the most common use cases of stacks and queues 12 | * What is the proper way to use global variables 13 | * How to work with git submodules 14 | 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | 3 | /** 4 | * main - sum the even numbers below 5 | * 4000000 in fibobnachi sequence 6 | * Description: fibobnachi numbers starting from one 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | long int fib1 = 0; 13 | long int fib2 = 0; 14 | long int fib3 = 1; 15 | long int sum = 0; 16 | 17 | while (fib2 < 4000000) 18 | { 19 | fib2 = fib1 + fib3; 20 | fib1 = fib3; 21 | fib3 = fib2; 22 | if (fib2 % 2 == 0) 23 | { 24 | sum = sum + fib2; 25 | } 26 | } 27 | printf("%ld\n", sum); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-rot13.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * rot13 - caesers cipher 5 | * @s: pointer to an array of words 6 | * 7 | * Return: s 8 | */ 9 | 10 | char *rot13(char *s) 11 | { 12 | int a, b; 13 | 14 | char test[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 15 | char res[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 16 | 17 | for (a = 0; s[a] != '\0'; a++) 18 | { 19 | for (b = 0; test[b] != '\0'; b++) 20 | { 21 | if (s[a] == test[b]) 22 | { 23 | s[a] = res[b]; 24 | break; 25 | } 26 | } 27 | } 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef notrebloh_H 2 | #define notrebloh_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int _putchar(char c); 9 | 10 | unsigned int binary_to_uint(const char *b); 11 | 12 | void print_binary(unsigned long int n); 13 | 14 | int get_bit(unsigned long int n, unsigned int index); 15 | 16 | int set_bit(unsigned long int *n, unsigned int index); 17 | 18 | int clear_bit(unsigned long int *n, unsigned int index); 19 | 20 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 21 | 22 | int get_endianness(void); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /0x1B-makefiles/5-island_perimeter_option.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | 4 | def island_perimeter(grid): 5 | """Python Function to print perimeter of an island""" 6 | perimeter = 0 7 | for line in grid: 8 | perimeter += line.count(1) * 4 9 | for line in grid: 10 | for i in range(len(line)-1): 11 | if line[i] == 1 and line[i+1] == 1: 12 | perimeter -= 2 13 | 14 | for i in range(len(grid)-1): 15 | for j in range(len(grid[0])): 16 | if grid[i][j] == 1 and grid[i+1][j] == 1: 17 | perimeter -= 2 18 | 19 | return perimeter 20 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * *reverse_listint - function to reverse order of nodes 4 | * @head: pointer to head 5 | * Return: always successful 6 | */ 7 | listint_t *reverse_listint(listint_t **head) 8 | { 9 | listint_t *forward; 10 | listint_t *back = NULL; 11 | 12 | if (head == NULL) 13 | return (NULL); 14 | forward = *head; 15 | while (forward != NULL) 16 | { 17 | forward = forward->next; 18 | forward = (*head)->next; 19 | (*head)->next = back; 20 | back = *head; 21 | *head = forward; 22 | } 23 | *head = back; 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x1C-binary_trees/112-array_to_bst.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * array_to_bst - builds binary search tree from given array; 5 | * utilizes bst_insert function that would insert into binary search tree 6 | * 7 | * @array: values for binary search tree nodes 8 | * @size: number of elements in array 9 | * Return: pointer to root of tree 10 | */ 11 | bst_t *array_to_bst(int *array, size_t size) 12 | { 13 | size_t idx; 14 | bst_t *root = NULL; 15 | 16 | if (!array) 17 | return (NULL); 18 | 19 | for (idx = 0; idx < size; idx++) 20 | bst_insert(&root, array[idx]); 21 | 22 | return (root); 23 | } 24 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle to the terminal 5 | * @size: the size of the triangle 6 | * 7 | */ 8 | 9 | void print_triangle(int size) 10 | { 11 | int x, y, space; 12 | 13 | if (size <= 0) 14 | { 15 | _putchar('\n'); 16 | return; 17 | } 18 | 19 | for (x = 0; x < size; x++) 20 | { 21 | space = size - x; 22 | for (y = 1; y <= size; y++) 23 | { 24 | if (y < space) 25 | { 26 | _putchar(' '); 27 | } 28 | else 29 | { 30 | _putchar('#'); 31 | } 32 | } 33 | _putchar('\n'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-binary_trees/103-binary_tree_rotate_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_rotate_left - rotate root node to left 5 | * @tree: root 6 | * Return: pointer to new root node 7 | */ 8 | binary_tree_t *binary_tree_rotate_left(binary_tree_t *tree) 9 | { 10 | if (!tree) 11 | return (NULL); 12 | 13 | if (tree->right) 14 | { 15 | tree->parent = tree->right; 16 | tree->right = (tree->parent)->left; 17 | if (tree->right) 18 | (tree->right)->parent = tree; 19 | (tree->parent)->left = tree; 20 | (tree->parent)->parent = NULL; 21 | return (tree->parent); 22 | } 23 | return (tree); 24 | } 25 | -------------------------------------------------------------------------------- /0x1C-binary_trees/104-binary_tree_rotate_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_rotate_right - rotate root node to right 5 | * @tree: root 6 | * Return: pointer to new root node 7 | */ 8 | binary_tree_t *binary_tree_rotate_right(binary_tree_t *tree) 9 | { 10 | if (!tree) 11 | return (NULL); 12 | 13 | if (tree->left) 14 | { 15 | tree->parent = tree->left; 16 | tree->left = (tree->parent)->right; 17 | if (tree->left) 18 | (tree->left)->parent = tree; 19 | (tree->parent)->right = tree; 20 | (tree->parent)->parent = NULL; 21 | return (tree->parent); 22 | } 23 | return (tree); 24 | } 25 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * get_nodeint_at_index - function to return index of node 4 | * @index: index of node to be returned 5 | * @head: pointer to head of node 6 | * Return: always successful 7 | */ 8 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 9 | { 10 | listint_t *tmp; 11 | unsigned int count; 12 | 13 | if (head == NULL) 14 | return (NULL); 15 | count = 0; 16 | while (head != NULL) 17 | { 18 | if (index == count) 19 | { 20 | tmp = head; 21 | return (tmp); 22 | } 23 | count++; 24 | head = head->next; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x19-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_delete - Hash function to delete a table 4 | * @ht: table to be deleted 5 | * Return: always successful 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | unsigned int index; 10 | hash_node_t *temp; 11 | 12 | for (index = 0; index < ht->size; index++) 13 | { 14 | while (ht->array[index]) 15 | { 16 | temp = ht->array[index]->next; 17 | free(ht->array[index]->value); 18 | free(ht->array[index]->key); 19 | free(ht->array[index]); 20 | ht->array[index] = temp; 21 | } 22 | } 23 | free(ht->array); 24 | free(ht); 25 | } 26 | -------------------------------------------------------------------------------- /0x0E-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | /** 4 | * array_iterator - function to iterate through arrays 5 | * @array: pointer to array of int type 6 | * @size: type of size_t typedef 7 | * @action: pointer to function 8 | * Return: always successful 9 | */ 10 | 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | unsigned int count; 14 | 15 | if (action == NULL) 16 | return; 17 | if (size <= 0) 18 | return; 19 | if (array == NULL) 20 | return; 21 | 22 | for (count = 0; count < size; count++) 23 | { 24 | action(array[count]); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | 4 | /** 5 | * print_array - print `n` elements of an array of integers 6 | * @a: int type array pointer 7 | * @n: int type integer 8 | * Description: Numbers must be separated by comma and space. 9 | * Numbers should be displayed in the same order they are stored in array. 10 | * You can only use _putchar to print. 11 | */ 12 | 13 | void print_array(int *a, int n) 14 | { 15 | int b; 16 | 17 | for (b = 0; b < n; b++) 18 | { 19 | printf("%d", a[b]); 20 | if (b < (n - 1)) 21 | { 22 | printf(", "); 23 | } 24 | } 25 | printf("\n"); 26 | } 27 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * struct dog - struct for dog info 6 | * @name: type char 7 | * @age: float type 8 | * @owner: char type 9 | * 10 | *Description: structure to store dog info 11 | */ 12 | typedef struct dog 13 | { 14 | char *name; 15 | float age; 16 | char *owner; 17 | } dog_t; 18 | 19 | void init_dog(struct dog *d, char *name, float age, char *owner); 20 | 21 | void print_dog(struct dog *d); 22 | 23 | dog_t *new_dog(char *name, float age, char *owner); 24 | 25 | void free_dog(dog_t *d); 26 | 27 | int _strlen(char *s); 28 | 29 | char *_strcpy(char *dest, char *src); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * *add_node - function to add node to start 4 | * @head: pointer to pointer of list_t struct type 5 | * @str: pointer to string input 6 | * Return: Always Successful 7 | */ 8 | list_t *add_node(list_t **head, const char *str) 9 | { 10 | list_t *temp = NULL; 11 | unsigned int counter; 12 | 13 | temp = malloc(sizeof(list_t)); 14 | if (temp == NULL) 15 | return (NULL); 16 | counter = 0; 17 | while (str[counter] != '\0') 18 | { 19 | counter++; 20 | } 21 | temp->len = counter; 22 | temp->str = strdup(str); 23 | temp->next = *head; 24 | *head = temp; 25 | return (*head); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | /** 4 | * binary_to_uint - function to convert binary to unsigned int 5 | * @b: pointer to char string input 6 | * Return: always successful 7 | */ 8 | unsigned int binary_to_uint(const char *b) 9 | { 10 | unsigned int count = 0; 11 | unsigned int check = 1; 12 | int x; 13 | 14 | if (b == NULL) 15 | return (0); 16 | x = 0; 17 | while (b[x] != '\0') 18 | { 19 | if (b[x] != '0' && b[x] != '1') 20 | { 21 | return (0); 22 | } 23 | count <<= 1; 24 | if (b[x] == '1') 25 | { 26 | count ^= check; 27 | } 28 | x++; 29 | } 30 | return (count); 31 | } 32 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - Function to perform linear search on array 5 | * @array: Pointer to array to be searched 6 | * @size: Size of array to be search of type size_t 7 | * @value: value to be search inside array 8 | * Return: Return index of value or -1 if not found 9 | */ 10 | int linear_search(int *array, size_t size, int value) 11 | { 12 | size_t indx; 13 | 14 | for (indx = 0; indx < size; indx++) 15 | { 16 | printf("Value checked array[%lu] = [%d]\n", indx, array[indx]); 17 | if (array[indx] == value) 18 | { 19 | return (indx); 20 | } 21 | } 22 | return (-1); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible combinations fo single-digit numbers. 5 | * Numbers must be separated by commas and a space. 6 | * You can only use `putchar` to print to console 7 | * You can only use `putchar` up to four times. 8 | * You are not allowed to use any variable of type `char`. 9 | * Return: 0 10 | */ 11 | int main(void) 12 | { 13 | int i = 0; 14 | 15 | while (i < 10) 16 | { 17 | putchar(i + '0'); 18 | if (i < 9) 19 | { 20 | putchar(','); 21 | putchar(' '); 22 | } 23 | i++; 24 | } 25 | putchar('\n'); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * check_prime - check for number is prime 5 | * @a: int type 6 | * @b: int type 7 | * Return: always success 8 | */ 9 | int check_prime(int a, int b) 10 | { 11 | if (a == b) 12 | return (1); 13 | else if (b % a == 0) 14 | return (0); 15 | else 16 | return (check_prime(a + 1, b)); 17 | } 18 | 19 | /** 20 | * is_prime_number - return prime number or not 21 | * @n: int type 22 | * Return: always success 23 | * 24 | */ 25 | 26 | int is_prime_number(int n) 27 | { 28 | if (n <= 1) 29 | return (0); 30 | if (n == 0) 31 | return (0); 32 | return (check_prime(2, n)); 33 | } 34 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * *_calloc - function to allocates memory 7 | * @nmemb: unsigned int type 8 | * @size: unsigned int type 9 | * Return: return pointer to array 10 | */ 11 | void *_calloc(unsigned int nmemb, unsigned int size) 12 | { 13 | char *ptr; 14 | unsigned int count; 15 | 16 | if (nmemb == 0 || size == 0) 17 | return (NULL); 18 | ptr = malloc(nmemb * size); 19 | if (ptr == NULL) 20 | { 21 | return (NULL); 22 | } 23 | count = 0; 24 | while (count < nmemb * size) 25 | { 26 | ptr[count] = 0; 27 | count++; 28 | } 29 | return (ptr); 30 | } 31 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | 3 | /** 4 | * main - FizzBuzz 5 | * 6 | * Description: print numbers 1 - 100, replace multiples of 3 with Fizz 7 | * multiples of 5 with Buzz and multiples of both with FizzBuzz 8 | * 9 | * Return: 0 10 | */ 11 | 12 | int main(void) 13 | { 14 | int a; 15 | 16 | for (a = 1; a <= 100; a++) 17 | { 18 | if ((a % 3 == 0) && (a % 5 == 0)) 19 | printf("FizzBuzz"); 20 | else if (a % 3 == 0) 21 | printf("Fizz"); 22 | else if (a % 5 == 0) 23 | printf("Buzz"); 24 | else 25 | printf("%d", a); 26 | if (a < 100) 27 | printf(" "); 28 | } 29 | printf("\n"); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x0A-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | 5 | 6 | /** 7 | * create_array - create an arry of size with char c 8 | * @size: unsigned int type 9 | * @c: char type 10 | * Return: Return pointer to array created 11 | */ 12 | 13 | char *create_array(unsigned int size, char c) 14 | { 15 | char *s; 16 | unsigned int i; 17 | 18 | if (size == 0) 19 | { 20 | return (NULL); 21 | } 22 | 23 | s = malloc((size) * sizeof(char)); 24 | if (s == NULL) 25 | { 26 | return (NULL); 27 | } 28 | i = 0; 29 | while (i < size) 30 | { 31 | s[i] = c; 32 | i++; 33 | } 34 | s[i] = '\0'; 35 | return (s); 36 | } 37 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * *array_range - make memory for an array 8 | * @min: int type 9 | * @max: int type 10 | * Return: return pointer to array 11 | */ 12 | 13 | int *array_range(int min, int max) 14 | { 15 | int *ptr; 16 | int arr; 17 | 18 | if (min > max) 19 | { 20 | return (NULL); 21 | } 22 | 23 | ptr = malloc(sizeof(int) * (max - min + 1)); 24 | if (ptr == NULL) 25 | { 26 | return (NULL); 27 | } 28 | arr = 0; 29 | while (min <= max) 30 | { 31 | ptr[arr] = min; 32 | min++; 33 | arr++; 34 | } 35 | return (ptr); 36 | } 37 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * countSetBits - function to count set bits 4 | * @n: unsigned long int type 5 | * Return: return count of set bits 6 | */ 7 | unsigned int countSetBits(unsigned long int n) 8 | { 9 | unsigned int count = 0; 10 | 11 | while (n) 12 | { 13 | count += n & 1; 14 | n >>= 1; 15 | } 16 | return (count); 17 | } 18 | /** 19 | * flip_bits - function to return number of bits 20 | * @n: unsigned long int type 21 | * @m: unsigned long int type 22 | * Return: always successful 23 | */ 24 | 25 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 26 | { 27 | return (countSetBits(n ^ m)); 28 | } 29 | -------------------------------------------------------------------------------- /0x0E-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | /** 4 | * int_index - function to return index of compared int 5 | * @array: pointer to array of int type 6 | * @size: size of array of int type 7 | * @cmp: pointer to function of int type 8 | * 9 | * Return: return index number of comp int 10 | */ 11 | int int_index(int *array, int size, int (*cmp)(int)) 12 | { 13 | int ind; 14 | 15 | if (array == NULL) 16 | return (-1); 17 | if (size <= 0) 18 | return (-1); 19 | if (cmp == NULL) 20 | return (-1); 21 | for (ind = 0; ind < size; ind++) 22 | { 23 | if (cmp(array[ind])) 24 | return (ind); 25 | } 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/2-selection_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | /** 3 | * selection_sort - sorts a list using selection sort 4 | * 5 | * @array: The list to be sorted 6 | * @size: The size of the list 7 | */ 8 | void selection_sort(int *array, size_t size) 9 | { 10 | size_t i = 0, j = 0, m = 0, t = 0; 11 | 12 | if (array == NULL || size == 0) 13 | return; 14 | for ( ; i < size - 1; i++) 15 | { 16 | m = i; 17 | for (j = i + 1; j < size; j++) 18 | { 19 | if (array[j] < array[m]) 20 | m = j; 21 | } 22 | if (array[m] == array[i]) 23 | continue; 24 | t = array[m]; 25 | array[m] = array[i]; 26 | array[i] = t; 27 | print_array(array, size); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - main block 6 | * Description: Print statements based on the last digit of the random number. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | int m; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | m = n % 10; 17 | 18 | if (m > 5) 19 | printf("Last digit of %d is %d and is greater than 5\n", 20 | n, m); 21 | else if (m == 0) 22 | printf("Last digit of %d is %d and is 0\n", n, m); 23 | else 24 | printf("Last digit of %d is %d and is less than 6 and not 0\n", 25 | n, m); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | #include 3 | #include "variadic_functions.h" 4 | 5 | /** 6 | * print_numbers - variadic function to print numbers 7 | * @separator: const char type pointer to string between numbers 8 | * @n: const unsigned int type 9 | * Return: Always successful 10 | */ 11 | 12 | void print_numbers(const char *separator, const unsigned int n, ...) 13 | { 14 | va_list va; 15 | unsigned int i; 16 | 17 | va_start(va, n); 18 | for (i = 0; i < n; i++) 19 | { 20 | printf("%d", va_arg(va, int)); 21 | if (separator != NULL && (i < (n - 1))) 22 | printf("%s", separator); 23 | } 24 | va_end(va); 25 | printf("\n"); 26 | } 27 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * get_dnodeint_at_index - function to get the node at index 4 | * @head: pointer of dlistint_t type to head of list 5 | * @index: unsigned int type for number of index 6 | * Return: node at inded if successful, NULL if failed 7 | */ 8 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 9 | { 10 | dlistint_t *temp; 11 | unsigned int count = 0; 12 | 13 | if (head == NULL) 14 | return (NULL); 15 | while (head != NULL) 16 | { 17 | if (count == index) 18 | { 19 | temp = head; 20 | return (temp); 21 | } 22 | count++; 23 | head = head->next; 24 | } 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /0x1B-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | 4 | def island_perimeter(grid): 5 | """Python function to return the perimeter of an island""" 6 | 7 | p = 0 8 | for x in range(len(grid)): 9 | for y in range(len(grid[x])): 10 | if grid[x][y] == 1: 11 | if x + 1 == len(grid) or grid[x + 1][y] == 0: 12 | p += 1 13 | if y + 1 == len(grid[0]) or grid[x][y + 1] == 0: 14 | p += 1 15 | if x - 1 == -1 or grid[x - 1][y] == 0: 16 | p += 1 17 | if y - 1 == -1 or grid[x][y - 1] == 0: 18 | p += 1 19 | return(p) 20 | -------------------------------------------------------------------------------- /0x1C-binary_trees/9-binary_tree_height.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_height - Measures the height of a binary tree 4 | * 5 | * @tree: Pointer to the node to measures the height 6 | * 7 | * Return: The height of the tree starting at @node 8 | */ 9 | size_t binary_tree_height(const binary_tree_t *tree) 10 | { 11 | size_t height_l; 12 | size_t height_r; 13 | 14 | if (tree == NULL) 15 | return (0); 16 | 17 | /*recursively check the left and right node if exist*/ 18 | height_l = tree->left ? 1 + binary_tree_height(tree->left) : 0; 19 | height_r = tree->right ? 1 + binary_tree_height(tree->right) : 0; 20 | return (height_l > height_r ? height_l : height_r); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for notrebloh 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-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * if_natural_square - check if a has natural square 5 | * @a: int type 6 | * @b: int type 7 | * Return: return success 8 | */ 9 | int if_natural_square(int a, int b) 10 | { 11 | if (a == (b * b)) 12 | return (b); 13 | if (b * b > a) 14 | return (-1); 15 | return (if_natural_square(a, b + 1)); 16 | } 17 | 18 | /** 19 | * _sqrt_recursion - check if number has natural sqrt 20 | * @n: int type 21 | * Return: return success 22 | */ 23 | int _sqrt_recursion(int n) 24 | { 25 | if (n == 1) 26 | return (1); 27 | if (n == 0) 28 | return (0); 29 | if (n < 1) 30 | return (-1); 31 | return (if_natural_square(n, 1)); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * print_binary - function to convert number to binary 5 | * @n: unsigned long int type; 6 | * Return: always successful 7 | */ 8 | void print_binary(unsigned long int n) 9 | { 10 | unsigned long int check; 11 | unsigned int size = sizeof(n) * 8; 12 | unsigned int count; 13 | unsigned int flag = 1; 14 | 15 | count = 0; 16 | while (count < size) 17 | { 18 | check = (n << 1); 19 | check >>= 1; 20 | if (n != check) 21 | { 22 | flag = 0; 23 | write(1, "1", 1); 24 | } 25 | else if (!flag) 26 | { 27 | write(1, "0", 1); 28 | } 29 | n <<= 1; 30 | count++; 31 | } 32 | if (flag == 1) 33 | write(1, "0", 1); 34 | } 35 | -------------------------------------------------------------------------------- /0x19-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_print - function to print a hash table in C 4 | * @ht: hash table to be printed 5 | * Return: printed elements of hash table 6 | */ 7 | void hash_table_print(const hash_table_t *ht) 8 | { 9 | unsigned long int index = 0; 10 | unsigned int check = 0; 11 | hash_node_t *temp; 12 | 13 | if (!ht) 14 | return; 15 | printf("{"); 16 | while (index < ht->size) 17 | { 18 | temp = ht->array[index]; 19 | while (temp != NULL) 20 | { 21 | if (check == 1) 22 | printf(", "); 23 | printf("'%s': '%s'", temp->key, temp->value); 24 | check = 1; 25 | temp = temp->next; 26 | } 27 | index++; 28 | } 29 | printf("}\n"); 30 | } 31 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/10-print_comb2.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print numbers from 0 to 99. 5 | * Numbers must be separated by commas followed by a space. 6 | * You can only use `putchar` to print to console. 7 | * You can only use `putchar` up to 5 times. 8 | * You are not allowed to use variables of type `char`. 9 | * Return: 0 10 | */ 11 | int main(void) 12 | { 13 | int a; 14 | int b; 15 | int c; 16 | 17 | for (a = 0; a < 100; a++) 18 | { 19 | b = a / 10; 20 | c = a % 10; 21 | 22 | putchar(b + '0'); 23 | putchar(c + '0'); 24 | 25 | if (a < 99) 26 | { 27 | putchar(','); 28 | putchar(' '); 29 | } 30 | } 31 | putchar('\n'); 32 | 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/0-bubble_sort.c: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | /** 4 | * bubble_sort - C function that uses uses the Bubble sort method in an array 5 | * @array: array of integers to sort 6 | * @size: the size of the array to be sorted 7 | * Return: sorted array 8 | */ 9 | 10 | void bubble_sort(int *array, size_t size) 11 | { 12 | size_t check = size; 13 | size_t i, temp = 0; 14 | 15 | if (array == NULL || size == 0) 16 | return; 17 | while (check > 0) 18 | { 19 | for (i = 0; i < size - 1; i++) 20 | { 21 | if (array[i] > array[i + 1]) 22 | { 23 | temp = array[i + 1]; 24 | array[i + 1] = array[i]; 25 | array[i] = temp; 26 | print_array(array, size); 27 | } 28 | } 29 | check--; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - program to generate random password 6 | * 7 | * description: find checksum value from objdump 8 | * 9 | * Return: Return generated password 10 | */ 11 | 12 | int main(void) 13 | { 14 | int sum, i, r; 15 | 16 | char a[63] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 17 | char password[100]; 18 | 19 | srand(time(NULL)); 20 | sum = 0; 21 | i = 0; 22 | 23 | while (sum < (2772 - 122)) 24 | { 25 | r = rand() % 62; 26 | password[i] = a[r]; 27 | sum += password[i]; 28 | i++; 29 | } 30 | r = 2772 - sum; 31 | password[i] = r; 32 | printf("%s\n", password); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x1C-binary_trees/12-binary_tree_leaves.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * is_leaf - check if no children 5 | * @tree: node 6 | * Return: 1 if leaf, 0 if not 7 | */ 8 | size_t is_leaf(const binary_tree_t *tree) 9 | { 10 | if (!tree) 11 | return (0); 12 | if (!(tree->left) && !(tree->right)) 13 | return (1); 14 | return (0); 15 | } 16 | 17 | /** 18 | * binary_tree_leaves - calculate total leaves 19 | * @tree: node 20 | * Return: total leaves 21 | */ 22 | size_t binary_tree_leaves(const binary_tree_t *tree) 23 | { 24 | size_t leaves = 0; 25 | 26 | if (!tree) 27 | return (0); 28 | 29 | leaves += binary_tree_leaves(tree->left); 30 | leaves += binary_tree_leaves(tree->right); 31 | return (leaves + is_leaf(tree)); 32 | } 33 | -------------------------------------------------------------------------------- /0x14-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * create_file - function that creates file 4 | * @filename: pointer to filename to be created of char const type 5 | * @text_content: string to be written into file 6 | * Return: always successfull 7 | */ 8 | int create_file(const char *filename, char *text_content) 9 | { 10 | int fd, length, fdwrite; 11 | 12 | fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600); 13 | if (fd == -1) 14 | return (-1); 15 | 16 | if (text_content == NULL) 17 | text_content = ""; 18 | length = 0; 19 | while (text_content[length] != '\0') 20 | { 21 | length++; 22 | } 23 | fdwrite = write(fd, text_content, length); 24 | if (fdwrite == -1) 25 | return (-1); 26 | close(fd); 27 | return (1); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - program to multiply two numbers 6 | * @argc: size of argv array and number of command line arguments 7 | * @argv: An array of size argc 8 | * Return: always successful 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int count, count1, sum; 13 | 14 | sum = 0; 15 | if (argc < 1) 16 | { 17 | printf("0"); 18 | } 19 | for (count = 1; count < argc; count++) 20 | { 21 | sum += atoi(argv[count]); 22 | for (count1 = 0; argv[count][count1] != '\0'; count1++) 23 | { 24 | if (!(isdigit(argv[count][count1]))) 25 | { 26 | printf("Error\n"); 27 | return (0); 28 | } 29 | } 30 | } 31 | printf("%d\n", sum); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x08-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * *_strstr - find a string in another string 5 | * @haystack: pointer to array to be searched 6 | * @needle: pointer to array to search for 7 | * Return: return pointer to location of string 8 | */ 9 | 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | const char *s, *t; 13 | 14 | if (!haystack || !needle) 15 | return (0); 16 | 17 | if (!*needle) 18 | return ((char *)haystack); 19 | for (; *haystack; haystack++) 20 | { 21 | if (*haystack == *needle) 22 | { 23 | t = haystack; 24 | s = needle; 25 | for (; *t; s++, t++) 26 | { 27 | if (*t != *s) 28 | break; 29 | } 30 | if (!*s) 31 | return ((char *)haystack); 32 | } 33 | } 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * *add_nodeint_end - function to add node from top 5 | * @head: listint_t type of node struct pointer to head 6 | * @n: const int type 7 | * Return: Always successful 8 | */ 9 | listint_t *add_nodeint_end(listint_t **head, const int n) 10 | { 11 | listint_t *top = NULL; 12 | listint_t *temp = NULL; 13 | 14 | top = malloc(sizeof(listint_t)); 15 | if (top == NULL) 16 | return (NULL); 17 | top->n = n; 18 | if (*head == NULL) 19 | { 20 | *head = top; 21 | top->next = NULL; 22 | return (*head); 23 | } 24 | 25 | temp = *head; 26 | while (temp->next != NULL) 27 | { 28 | temp = temp->next; 29 | } 30 | temp->next = top; 31 | top->next = NULL; 32 | 33 | return (*head); 34 | } 35 | -------------------------------------------------------------------------------- /0x1C-binary_trees/14-binary_tree_balance.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | #define MAX(a, b) ((a) >= (b) ? (a) : (b)) 4 | 5 | /** 6 | * height - calculate height 7 | * @tree: left or right subtree 8 | * Return: height 9 | */ 10 | int height(const binary_tree_t *tree) 11 | { 12 | if (!tree) 13 | return (0); 14 | 15 | if (tree || (tree->left || tree->right)) 16 | return (1 + MAX(height(tree->left), 17 | height(tree->right))); 18 | 19 | return (0); 20 | } 21 | 22 | /** 23 | * binary_tree_balance - calculate left height minus right height 24 | * @tree: node 25 | * Return: balance_factor 26 | */ 27 | int binary_tree_balance(const binary_tree_t *tree) 28 | { 29 | if (!tree) 30 | return (0); 31 | 32 | return (height(tree->left) - height(tree->right)); 33 | } 34 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * *_strstr - find a string in another string 5 | * @haystack: pointer to array to be searched 6 | * @needle: pointer to array to search for 7 | * Return: return pointer to location of string 8 | */ 9 | 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | const char *s, *t; 13 | 14 | if (!haystack || !needle) 15 | return (0); 16 | 17 | if (!*needle) 18 | return ((char *)haystack); 19 | for (; *haystack; haystack++) 20 | { 21 | if (*haystack == *needle) 22 | { 23 | t = haystack; 24 | s = needle; 25 | for (; *t; s++, t++) 26 | { 27 | if (*t != *s) 28 | break; 29 | } 30 | if (!*s) 31 | return ((char *)haystack); 32 | } 33 | } 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x08-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strlen - return length of a string 5 | * 6 | * @s: char type 7 | * Return: length of string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int a; 13 | 14 | for (a = 0; s[a] != '\0'; a++) 15 | { 16 | 17 | } 18 | return (a); 19 | } 20 | /** 21 | * _strcat - function to concatnate strings 22 | * 23 | * @dest: destination for concatnation 24 | * 25 | * @src: source of string 26 | * 27 | * Return: dest 28 | */ 29 | 30 | char *_strcat(char *dest, char *src) 31 | { 32 | int dest_len; 33 | int a; 34 | 35 | dest_len = _strlen(dest); 36 | 37 | for (a = 0 ; src[a] != '\0' ; a++) 38 | dest[dest_len + a] = src[a]; 39 | 40 | dest[dest_len + a] = '\0'; 41 | 42 | return (dest); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /0x1C-binary_trees/13-binary_tree_nodes.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * is_leaf - check if no children 4 | * @tree: node 5 | * Return: 1 if leaf, 0 if not 6 | */ 7 | size_t is_leaf(const binary_tree_t *tree) 8 | { 9 | if (!tree) 10 | return (0); 11 | if (!(tree->left) && !(tree->right)) 12 | return (1); 13 | return (0); 14 | } 15 | 16 | /** 17 | * binary_tree_nodes - C function to count nodes with at least one child 18 | * @tree: pointer to tree 19 | * Return: number of nodes 20 | */ 21 | size_t binary_tree_nodes(const binary_tree_t *tree) 22 | { 23 | size_t nodes = 0; 24 | 25 | if (!tree) 26 | return (0); 27 | 28 | nodes += binary_tree_nodes(tree->left); 29 | nodes += binary_tree_nodes(tree->right); 30 | 31 | return (nodes + (!is_leaf(tree))); 32 | } 33 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strlen - return length of a string 5 | * 6 | * @s: char type 7 | * Return: length of string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int a; 13 | 14 | for (a = 0; s[a] != '\0'; a++) 15 | { 16 | 17 | } 18 | return (a); 19 | } 20 | /** 21 | * _strcat - function to concatnate strings 22 | * 23 | * @dest: destination for concatnation 24 | * 25 | * @src: source of string 26 | * 27 | * Return: dest 28 | */ 29 | 30 | char *_strcat(char *dest, char *src) 31 | { 32 | int dest_len; 33 | int a; 34 | 35 | dest_len = _strlen(dest); 36 | 37 | for (a = 0 ; src[a] != '\0' ; a++) 38 | dest[dest_len + a] = src[a]; 39 | 40 | dest[dest_len + a] = '\0'; 41 | 42 | return (dest); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /0x08-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strlen - return length of a string 5 | * 6 | * @s: char type 7 | * Return: length of string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int b; 13 | 14 | for (b = 0; s[b] != '\0'; b++) 15 | { 16 | 17 | } 18 | return (b); 19 | } 20 | 21 | /** 22 | * _strncat - function to concatnate strings with n bytes 23 | * 24 | * @dest: destination for concatnation 25 | * 26 | * @src: source of string 27 | * @n: int type for size of byte 28 | * Return: dest 29 | */ 30 | 31 | char *_strncat(char *dest, char *src, int n) 32 | { 33 | int dest_len; 34 | int a; 35 | 36 | dest_len = _strlen(dest); 37 | for (a = 0; a < n && src[a] != '\0'; a++) 38 | dest[dest_len + a] = src[a]; 39 | return (dest); 40 | } 41 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strlen - return length of a string 5 | * 6 | * @s: char type 7 | * Return: length of string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int b; 13 | 14 | for (b = 0; s[b] != '\0'; b++) 15 | { 16 | 17 | } 18 | return (b); 19 | } 20 | 21 | /** 22 | * _strncat - function to concatnate strings with n bytes 23 | * 24 | * @dest: destination for concatnation 25 | * 26 | * @src: source of string 27 | * @n: int type for size of byte 28 | * Return: dest 29 | */ 30 | 31 | char *_strncat(char *dest, char *src, int n) 32 | { 33 | int dest_len; 34 | int a; 35 | 36 | dest_len = _strlen(dest); 37 | for (a = 0; a < n && src[a] != '\0'; a++) 38 | dest[dest_len + a] = src[a]; 39 | return (dest); 40 | } 41 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | /** 5 | * print_strings - variadic function to print strings 6 | * @separator: const char type pointer to string between numbers 7 | * @n: const unsigned int type 8 | * Return: Always successful 9 | */ 10 | void print_strings(const char *separator, const unsigned int n, ...) 11 | { 12 | unsigned int i; 13 | va_list arg; 14 | char *test; 15 | 16 | va_start(arg, n); 17 | 18 | for (i = 0; i < n; i++) 19 | { 20 | test = va_arg(arg, char*); 21 | if (test) 22 | { 23 | printf("%s", test); 24 | } 25 | else 26 | printf("(nil)"); 27 | if (separator != NULL && (i < (n - 1))) 28 | printf("%s", separator); 29 | } 30 | va_end(arg); 31 | printf("\n"); 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * times_table - produce the nine times table 5 | * 6 | * 7 | * 8 | * Return: returns the table arranged in order 9 | */ 10 | 11 | void times_table(void) 12 | { 13 | int a, b; 14 | 15 | a = 0; 16 | while (a <= 9) 17 | { 18 | b = 0; 19 | while (b <= 9) 20 | { 21 | if ((a * b) > 9) 22 | { 23 | _putchar(((a * b) / 10) + '0'); 24 | _putchar(((a * b) % 10) + '0'); 25 | } 26 | else if (b != 0) 27 | { 28 | _putchar(' '); 29 | _putchar((a * b) + '0'); 30 | } 31 | else 32 | { 33 | _putchar((a * b) + '0'); 34 | } 35 | if (b != 9) 36 | { 37 | _putchar(','); 38 | _putchar(' '); 39 | } 40 | b++; 41 | } 42 | a++; 43 | _putchar('\n'); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * cap_string - capitalize words in a string 4 | * @s: pointer to an array of words 5 | * 6 | * Return: Return capitalized words 7 | */ 8 | 9 | char *cap_string(char *s) 10 | { 11 | int a, b; 12 | 13 | char spec[13] = {' ', '\t', '\n', ',', ';', '.', '!', '?', '"', 14 | '(', ')', '{', '}'}; 15 | 16 | for (a = 0; s[a] != '\0'; a++) 17 | { 18 | for (b = 0; spec[b] != '\0'; b++) 19 | { 20 | if (a == 0) 21 | { 22 | if (s[a] >= 'a' && s[a] <= 'z') 23 | { 24 | s[a] = s[a] - 32; 25 | } 26 | } 27 | if (s[a] == spec[b]) 28 | { 29 | if (s[a + 1] >= 'a' && s[a + 1] <= 'z') 30 | { 31 | s[a + 1] = s[a + 1] - 32; 32 | } 33 | } 34 | } 35 | } 36 | return (s); 37 | } 38 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints multiple combinations 4 | * Description: Write a program that prints all possible 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int a, b, c, d; 10 | 11 | for (a = '0'; a <= '9'; a++) 12 | { 13 | for (b = '0'; b <= '9'; b++) 14 | { 15 | for (c = '0'; c <= '9'; c++) 16 | { 17 | for (d = '0'; d <= '9'; d++) 18 | { 19 | if ((a < c) || (a == c && b < d)) 20 | { 21 | putchar(a); 22 | putchar(b); 23 | putchar(' '); 24 | putchar(c); 25 | putchar(d); 26 | if (!(a == '9' && b == '8')) 27 | { 28 | putchar(','); 29 | putchar(' '); 30 | } 31 | } 32 | } 33 | } 34 | } 35 | } 36 | putchar('\n'); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x14-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * append_text_to_file - function to append to file 4 | * @filename: name of file to append 5 | * @text_content: name of file written 6 | * Return: always successful 7 | */ 8 | int append_text_to_file(const char *filename, char *text_content) 9 | { 10 | int fdopen, fdwrite, length; 11 | 12 | if (filename == NULL) 13 | return (-1); 14 | 15 | fdopen = open(filename, O_WRONLY | O_APPEND); 16 | if (fdopen == -1) 17 | { 18 | return (-1); 19 | } 20 | if (text_content == NULL) 21 | text_content = ""; 22 | length = 0; 23 | while (text_content[length] != '\0') 24 | { 25 | length++; 26 | } 27 | fdwrite = write(fdopen, text_content, length); 28 | if (fdwrite == -1) 29 | return (-1); 30 | close(fdopen); 31 | return (1); 32 | } 33 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - function to add node at the head of a doubly linked list 5 | * @head: double pointer of dlistint_t type to head of list 6 | * @n: int of const type for data inside list 7 | * Return: doubly linked list if successful or NULL if unsuccessful 8 | */ 9 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 10 | { 11 | dlistint_t *temp, *node = malloc(sizeof(dlistint_t)); 12 | 13 | 14 | if (node == NULL) 15 | return (NULL); 16 | node->n = n; 17 | node->prev = NULL; 18 | node->next = NULL; 19 | 20 | if (*head == NULL) 21 | { 22 | node->next = NULL; 23 | *head = node; 24 | return (*head); 25 | } 26 | temp = *head; 27 | node->next = temp; 28 | temp->prev = node; 29 | *head = node; 30 | return (node); 31 | } 32 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_dnodeint_end - function to add node to the end of doubly linked list 4 | * @head: double pointer of dlistint_t type to head of list 5 | * @n: int of const type of data for list 6 | * Return: node if successful or NULL if empty 7 | */ 8 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 9 | { 10 | dlistint_t *node = malloc(sizeof(dlistint_t)); 11 | dlistint_t *temp = *head; 12 | 13 | if (node == NULL) 14 | return (NULL); 15 | node->n = n; 16 | node->prev = NULL; 17 | node->next = NULL; 18 | if (*head == NULL) 19 | { 20 | *head = node; 21 | return (*head); 22 | } 23 | while (temp->next != NULL) 24 | { 25 | temp = temp->next; 26 | } 27 | node->prev = temp; 28 | temp->next = node; 29 | return (node); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/sort.h: -------------------------------------------------------------------------------- 1 | #ifndef SORT_H 2 | #define SORT_H 3 | 4 | #include 5 | #include 6 | 7 | 8 | /** 9 | * struct listint_s - Doubly linked list node 10 | * 11 | * @n: Integer stored in the node 12 | * @prev: Pointer to the previous element of the list 13 | * @next: Pointer to the next element of the list 14 | */ 15 | typedef struct listint_s 16 | { 17 | const int n; 18 | struct listint_s *prev; 19 | struct listint_s *next; 20 | } listint_t; 21 | 22 | void print_array(const int *array, size_t size); 23 | 24 | void print_list(const listint_t *list); 25 | 26 | void bubble_sort(int *array, size_t size); 27 | 28 | void insertion_sort_list(listint_t **list); 29 | 30 | void selection_sort(int *array, size_t size); 31 | 32 | void quick_sort(int *array, size_t size); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /0x08-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strchr - check for character in an array 5 | * @s: pointer to array 6 | * @c: char type 7 | * 8 | * Return: arrayed at check 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | while (*s != '\0') 14 | { 15 | if (*s == c) 16 | return (s); 17 | s++; 18 | } 19 | if (c == '\0') 20 | return (s); 21 | s = 0; 22 | return (s); 23 | } 24 | /** 25 | * *_strpbrk - find first appearance of a char in an array 26 | * 27 | * @s: pointer array to be searched 28 | * @accept: pointer array with char to be searched 29 | * Return: pointer to array 30 | */ 31 | 32 | char *_strpbrk(char *s, char *accept) 33 | { 34 | for (; *s; s++) 35 | { 36 | if (_strchr(accept, *s) != 0) 37 | return ((char *) s); 38 | } 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_H 2 | #define LIST_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for notrebloh project 16 | */ 17 | typedef struct list_s 18 | { 19 | char *str; 20 | unsigned int len; 21 | struct list_s *next; 22 | } list_t; 23 | 24 | int _putchar(char c); 25 | 26 | size_t print_list(const list_t *h); 27 | 28 | size_t list_len(const list_t *h); 29 | 30 | list_t *add_node(list_t **head, const char *str); 31 | 32 | list_t *add_node_end(list_t **head, const char *str); 33 | 34 | void free_list(list_t *head); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * delete_nodeint_at_index - functioon to delete node at nth position 4 | * @head: pointer to head 5 | * @index: point of node deletion 6 | * Return: Always successful 7 | */ 8 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 9 | { 10 | listint_t *temp = *head; 11 | listint_t *temp1; 12 | unsigned int count = 0; 13 | 14 | if (head == NULL || *head == NULL) 15 | return (-1); 16 | 17 | if (index == 0) 18 | { 19 | *head = temp->next; 20 | free(temp); 21 | return (1); 22 | } 23 | while (count != (index - 1)) 24 | { 25 | temp = temp->next; 26 | count++; 27 | if (temp == NULL) 28 | return (-1); 29 | } 30 | temp1 = temp->next; 31 | temp->next = temp1->next; 32 | free(temp1); 33 | return (1); 34 | } 35 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strchr - check for character in an array 5 | * @s: pointer to array 6 | * @c: char type 7 | * 8 | * Return: arrayed at check 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | while (*s != '\0') 14 | { 15 | if (*s == c) 16 | return (s); 17 | s++; 18 | } 19 | if (c == '\0') 20 | return (s); 21 | s = 0; 22 | return (s); 23 | } 24 | /** 25 | * *_strpbrk - find first appearance of a char in an array 26 | * 27 | * @s: pointer array to be searched 28 | * @accept: pointer array with char to be searched 29 | * Return: pointer to array 30 | */ 31 | 32 | char *_strpbrk(char *s, char *accept) 33 | { 34 | for (; *s; s++) 35 | { 36 | if (_strchr(accept, *s) != 0) 37 | return ((char *) s); 38 | } 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x08-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x08. C - Static libraries 2 | 3 | 0. A library is not a luxury but one of the necessities of life mandatory 4 | Create the static library libnotrebloh.a containing 5 | 6 | 1. Without libraries what have we? We have no past and no future mandatory 7 | Create a script called create_static_lib.sh that creates a static library 8 | called liball.a from all the .c files that are in the current directory. 9 | 10 | 2. Either write something worth reading or do something worth writing mandatory 11 | Write a blog post on C static libraries. It should cover: 12 | 13 | Why use libraries 14 | How they work 15 | How to create them 16 | How to use them 17 | Your posts should have examples and at least one picture, at the top. 18 | Publish your blog post on Medium or LinkedIn, and share it at least on 19 | Twitter and LinkedIn. -------------------------------------------------------------------------------- /0x0A-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * **alloc_grid - function to allocate memory to grid 7 | * @width: int type 8 | * @height: int type 9 | * Return: grid of 0s 10 | */ 11 | int **alloc_grid(int width, int height) 12 | { 13 | int x, y; 14 | int **ptr; 15 | 16 | if (width <= 0 || height <= 0) 17 | { 18 | return (NULL); 19 | } 20 | ptr = malloc(height * sizeof(int *)); 21 | if (ptr == NULL) 22 | { 23 | return (NULL); 24 | } 25 | for (x = 0; x < height; x++) 26 | { 27 | ptr[x] = malloc(width * sizeof(int)); 28 | if (ptr[x] == NULL) 29 | { 30 | for (y = 0; y < x; y++) 31 | free(ptr[y]); 32 | free(ptr); 33 | return (NULL); 34 | } 35 | for (y = 0; y < width; y++) 36 | { 37 | ptr[x][y] = 0; 38 | } 39 | } 40 | return (ptr); 41 | } 42 | -------------------------------------------------------------------------------- /0x19-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_get - function to retrieve value at key 4 | * @ht: is the hash table 5 | * @key: is the key being searched 6 | * Return: associated value if found or NULL if not 7 | */ 8 | char *hash_table_get(const hash_table_t *ht, const char *key) 9 | { 10 | unsigned long int index = 0; 11 | 12 | if (!ht || !ht->array || !key || strlen(key) == 0) 13 | { 14 | return (NULL); 15 | } 16 | index = key_index((const unsigned char *) key, ht->size); 17 | if (ht->array[index] == NULL) 18 | return (NULL); 19 | while (index < ht->size) 20 | { 21 | while (ht->array[index]) 22 | { 23 | if (strcmp(ht->array[index]->key, key) == 0) 24 | return (ht->array[index]->value); 25 | ht->array[index] = ht->array[index]->next; 26 | } 27 | index++; 28 | } 29 | return (NULL); 30 | } 31 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/swap_vowel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | static inline int is_vowel(int c) 5 | { 6 | c = tolower(c); 7 | return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y'); 8 | } 9 | void rev_string(char *s) 10 | { 11 | char *p1 = s; 12 | char *p2 = p1 + strlen(p1); 13 | char c; 14 | while (p1 < p2) 15 | { 16 | while (p1 < p2 && !is_vowel(*p1)) 17 | p1++; 18 | while (p1 < p2 && !is_vowel(*p2)) 19 | p2--; 20 | if (p1 != p2) 21 | { 22 | c = *p1; 23 | *p1++ = *p2; 24 | *p2-- = c; 25 | } 26 | } 27 | } 28 | int main(void) { 29 | char s[10] = "Betty"; 30 | 31 | printf("%s\n", s); 32 | rev_string(s); 33 | printf("%s\n", s); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-print_number.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * print_number - function to print integers 4 | * 5 | * @n: int type 6 | * Return: return integer values 7 | */ 8 | 9 | void print_number(int n) 10 | { 11 | int a, remainder, length, count; 12 | int m = n; 13 | int pcounter = 0; 14 | int power; 15 | 16 | if (n == 0) 17 | { 18 | _putchar('0'); 19 | } 20 | if (n < 0) 21 | { 22 | _putchar('-'); 23 | } 24 | for (length = 0; n != 0; length++) 25 | { 26 | n /= 10; 27 | } 28 | for (power = 1; pcounter < (length - 1); pcounter++) 29 | { 30 | power *= 10; 31 | } 32 | for (count = 0; count < length; count++) 33 | { 34 | a = m; 35 | if (m < 0) 36 | _putchar(((m / power) * -1) + '0'); 37 | else 38 | _putchar((m / power) + '0'); 39 | remainder = a % power; 40 | power /= 10; 41 | m = remainder; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0x19-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_create - C function to create a hash table 4 | * @size: size of hash table, unsigned long int type 5 | * Return: returns a pointer to newly created hash table 6 | */ 7 | 8 | hash_table_t *hash_table_create(unsigned long int size) 9 | { 10 | unsigned long int count; 11 | hash_table_t *table = NULL; 12 | 13 | /* allocate memory for hash table*/ 14 | table = malloc(sizeof(hash_table_t)); 15 | if (table == NULL) 16 | return (NULL); 17 | 18 | table->size = size; 19 | /*allocate memory for node in hash table*/ 20 | table->array = malloc(sizeof(hash_node_t *) * size); 21 | if (table->array == NULL) 22 | { 23 | free(table); 24 | return (NULL); 25 | } 26 | for (count = 0; count < size; count++) 27 | { 28 | table->array[count] = NULL; 29 | } 30 | return (table); 31 | } 32 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * print_number - function to print integers 4 | * 5 | * @n: int type 6 | * Return: return integer values 7 | */ 8 | 9 | void print_number(int n) 10 | { 11 | int a, remainder, length, count; 12 | int m = n; 13 | int pcounter = 0; 14 | int power; 15 | 16 | if (n == 0) 17 | { 18 | _putchar('0'); 19 | } 20 | if (n < 0) 21 | { 22 | _putchar('-'); 23 | } 24 | for (length = 0; n != 0; length++) 25 | { 26 | n /= 10; 27 | } 28 | for (power = 1; pcounter < (length - 1); pcounter++) 29 | { 30 | power *= 10; 31 | } 32 | for (count = 0; count < length; count++) 33 | { 34 | a = m; 35 | if (m < 0) 36 | _putchar(((m / power) * -1) + '0'); 37 | else 38 | _putchar((m / power) + '0'); 39 | remainder = a % power; 40 | power /= 10; 41 | m = remainder; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * _strlen - returns the length of the string -1 4 | * @s: the string to get the length 5 | * 6 | * Return: lenght of the string 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int counter = 0; 11 | int a = 0; 12 | 13 | while (s[a] != '\0') 14 | { 15 | ++counter; 16 | ++a; 17 | } 18 | --counter; 19 | return (counter); 20 | } 21 | 22 | /** 23 | * rev_string - prints a string in reverse order 24 | * @s: the string to count down from 25 | * 26 | * Description: Reverses the string in memory 27 | * Return: void 28 | */ 29 | void rev_string(char *s) 30 | { 31 | 32 | int b, c; 33 | int length = _strlen(s); 34 | char holder; 35 | 36 | b = 0; 37 | c = length; 38 | while (c >= b) 39 | { 40 | holder = s[b]; 41 | s[b] = s[c]; 42 | s[c] = holder; 43 | c--; 44 | b++; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "3-calc.h" 3 | #include 4 | 5 | /** 6 | * main - Entry point of program 7 | * @argc: size of array argv and number of command line arguments 8 | * @argv: An array of size argc 9 | * Return: Always successful 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int (*operator)(int, int); 15 | int num1, num2; 16 | 17 | if (argc != 4) 18 | { 19 | printf("Error\n"); 20 | exit(98); 21 | } 22 | operator = get_op_func(argv[2]); 23 | num1 = atoi(argv[1]); 24 | num2 = atoi(argv[3]); 25 | 26 | if (operator == NULL) 27 | { 28 | printf("Error\n"); 29 | exit(99); 30 | } 31 | 32 | if ((argv[2][0] == '%' || argv[2][0] == '/') && atoi(argv[3]) == 0) 33 | { 34 | printf("Error\n"); 35 | exit(100); 36 | } 37 | 38 | printf("%d\n", operator(num1, num2)); 39 | 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | 4 | #include 5 | 6 | int _putchar(char c); 7 | /** 8 | * struct pter - pointer to print all 9 | * @test: pointer to input for test 10 | * @printer: pointer to function 11 | */ 12 | typedef struct pter 13 | { 14 | char *test; 15 | void (*printer)(); 16 | } pt; 17 | 18 | void print_int(va_list arg); 19 | 20 | void print_char(va_list arg); 21 | 22 | void print_float(va_list arg); 23 | 24 | void print_string(va_list arg); 25 | 26 | void print_all(const char * const format, ...); 27 | 28 | int sum_them_all(const unsigned int n, ...); 29 | 30 | void print_numbers(const char *separator, const unsigned int n, ...); 31 | 32 | void print_strings(const char *separator, const unsigned int n, ...); 33 | 34 | void print_all(const char * const format, ...); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /0x1C-binary_trees/110-binary_tree_is_bst.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * is_bst - recursively check subtrees (left data < right data) 5 | * @tree: root 6 | * @l: min value 7 | * @r: max value 8 | * Return: 1 if yes, 0 if not 9 | */ 10 | int is_bst(const binary_tree_t *tree, 11 | const binary_tree_t *l, 12 | const binary_tree_t *r) 13 | { 14 | if (!tree) 15 | return (1); 16 | if (l && l->n >= tree->n) 17 | return (0); 18 | if (r && r->n <= tree->n) 19 | return (0); 20 | return (is_bst(tree->left, l, tree) && 21 | is_bst(tree->right, tree, r)); 22 | } 23 | 24 | /** 25 | * binary_tree_is_bst - check if binary search tree (left data < right data) 26 | * @tree: root 27 | * Return: 1 if yes, 0 if not 28 | */ 29 | int binary_tree_is_bst(const binary_tree_t *tree) 30 | { 31 | if (!tree) 32 | return (0); 33 | 34 | return (is_bst(tree, NULL, NULL)); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-binary_trees/15-binary_tree_is_full.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * is_leaf - check if no children 4 | * @tree: node 5 | * Return: 1 if leaf, 0 if not 6 | */ 7 | size_t is_leaf(const binary_tree_t *tree) 8 | { 9 | if (!tree) 10 | return (0); 11 | if (!(tree->left) && !(tree->right)) 12 | return (1); 13 | return (0); 14 | } 15 | 16 | /** 17 | * binary_tree_is_full - C function to check if binary tree is full 18 | * @tree: pointer to tree/node to be checked 19 | * Return: 1 if true, 0 if false 20 | */ 21 | int binary_tree_is_full(const binary_tree_t *tree) 22 | { 23 | int nodes_l, nodes_r; 24 | 25 | if (!tree) 26 | return (0); 27 | if (is_leaf(tree)) 28 | return (1); 29 | 30 | nodes_l = binary_tree_is_full(tree->left); 31 | nodes_r = binary_tree_is_full(tree->right); 32 | 33 | if (nodes_l == 0 || nodes_r == 0) 34 | return (0); 35 | return (1); 36 | } 37 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | #include 5 | /** 6 | * *_realloc - reallocate memory size function 7 | * @ptr: pointer to address of old memory location 8 | * @old_size: unsigned int type of old memory size 9 | * @new_size: unsigned int type for new memory size 10 | * Return: return pointer to array 11 | */ 12 | 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 14 | { 15 | char *s; 16 | 17 | if (new_size > old_size) 18 | { 19 | s = malloc(new_size); 20 | free(ptr); 21 | return (s); 22 | } 23 | if (new_size == old_size) 24 | { 25 | return (ptr); 26 | } 27 | if (ptr == NULL) 28 | { 29 | s = malloc(new_size); 30 | free(ptr); 31 | return (s); 32 | } 33 | if (new_size == 0 && ptr != NULL) 34 | { 35 | free(ptr); 36 | return (NULL); 37 | } 38 | return (ptr); 39 | } 40 | -------------------------------------------------------------------------------- /0x14-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * read_textfile - function to read and write a file 4 | * @filename: const char type pointer to file to be read 5 | * @letters: size_t type 6 | * Return: always successful 7 | */ 8 | ssize_t read_textfile(const char *filename, size_t letters) 9 | { 10 | int fd; 11 | ssize_t fdread, fdwrite, fdclose; 12 | char *space; 13 | 14 | if (filename == NULL) 15 | return (0); 16 | space = malloc(sizeof(char) * letters); 17 | if (space == NULL) 18 | { 19 | return (-1); 20 | } 21 | 22 | fd = open(filename, O_RDONLY); 23 | if (fd == -1) 24 | return (0); 25 | fdread = read(fd, space, letters); 26 | if (fdread == -1) 27 | return (-1); 28 | fdwrite = write(STDOUT_FILENO, space, fdread); 29 | 30 | if (fdwrite == -1) 31 | return (-1); 32 | fdclose = close(fd); 33 | if (fdclose == -1) 34 | return (-1); 35 | return (fdread); 36 | } 37 | -------------------------------------------------------------------------------- /0x0A-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * _strlen - return length of a string 7 | * 8 | * @s: char type 9 | * Return: length of string 10 | */ 11 | int _strlen(char *s) 12 | { 13 | int a; 14 | 15 | for (a = 0; s[a] != '\0'; a++) 16 | { 17 | 18 | } 19 | return (a); 20 | } 21 | 22 | /** 23 | * *_strdup - function to return pointer to string 24 | * @str: pointer to string array input 25 | * Return: pointer to string created 26 | */ 27 | 28 | char *_strdup(char *str) 29 | { 30 | char *ptr; 31 | int size; 32 | int x; 33 | 34 | if (str == NULL) 35 | { 36 | return (NULL); 37 | } 38 | size = _strlen(str) + 1; 39 | ptr = malloc(size * sizeof(char)); 40 | 41 | x = 0; 42 | while (x < size) 43 | { 44 | if (ptr == NULL) 45 | { 46 | return (NULL); 47 | } 48 | ptr[x] = str[x]; 49 | x++; 50 | } 51 | return (ptr); 52 | } 53 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * *add_node_end - function to add node to start 4 | * @head: pointer to pointer of list_t struct type 5 | * @str: pointer to string input 6 | * Return: Always Successful 7 | */ 8 | list_t *add_node_end(list_t **head, const char *str) 9 | { 10 | list_t *temp; 11 | list_t *first; 12 | unsigned int counter; 13 | 14 | counter = 0; 15 | while (str[counter] != '\0') 16 | { 17 | counter++; 18 | } 19 | 20 | first = malloc(sizeof(list_t)); 21 | if (first == NULL) 22 | return (NULL); 23 | first->str = strdup(str); 24 | first->len = counter; 25 | 26 | if (*head == NULL) 27 | { 28 | *head = first; 29 | first->next = NULL; 30 | return (*head); 31 | } 32 | temp = *head; 33 | while (temp->next != NULL) 34 | { 35 | temp = temp->next; 36 | } 37 | temp->next = first; 38 | first->next = NULL; 39 | 40 | return (*head); 41 | } 42 | -------------------------------------------------------------------------------- /0x1C-binary_trees/1-binary_tree_insert_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_insert_left - C function to insert node to left side of tree 4 | * @parent: pointer to node addition 5 | * @value: value to be added 6 | * Return: return pointer to new node added 7 | */ 8 | 9 | binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value) 10 | { 11 | binary_tree_t *newNode; 12 | 13 | if (parent == NULL) 14 | return (NULL); 15 | 16 | newNode = malloc(sizeof(binary_tree_t)); 17 | 18 | if (newNode == NULL) 19 | return (NULL); 20 | 21 | newNode->n = value; 22 | newNode->parent = parent; 23 | newNode->right = NULL; 24 | 25 | if (parent->left == NULL) 26 | { 27 | parent->left = newNode; 28 | newNode->left = NULL; 29 | } 30 | else 31 | { 32 | newNode->left = parent->left; 33 | parent->left->parent = newNode; 34 | parent->left = newNode; 35 | } 36 | return (newNode); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /0x1C-binary_trees/18-binary_tree_uncle.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_sibling - C function to find sibling of a node in binary tree 5 | * @node: node to use for searching 6 | * Return: data at sibling 7 | */ 8 | binary_tree_t *binary_tree_sibling(binary_tree_t *node) 9 | { 10 | if (!node || !node->parent) 11 | return (NULL); 12 | 13 | if (node->parent->left == node) 14 | return (node->parent->right); 15 | else if (node->parent->right == node) 16 | return (node->parent->left); 17 | else 18 | return (NULL); 19 | } 20 | 21 | /** 22 | * binary_tree_uncle - C function to find uncle of a node in binary tree 23 | * @node: node to use for searching 24 | * Return: data of uncle node 25 | */ 26 | binary_tree_t *binary_tree_uncle(binary_tree_t *node) 27 | { 28 | if (!node || !node->parent || !((node->parent)->parent)) 29 | return (NULL); 30 | 31 | return (binary_tree_sibling(node->parent)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-static_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | int _putchar(char c); 5 | 6 | int _islower(int c); 7 | 8 | int _isalpha(int c); 9 | 10 | int _abs(int n); 11 | 12 | int _isupper(int c); 13 | 14 | int _isdigit(int c); 15 | 16 | int _strlen(char *s); 17 | 18 | void _puts(char *s); 19 | 20 | char *_strcpy(char *dest, char *src); 21 | 22 | int _atoi(char *s); 23 | 24 | char *_strcat(char *dest, char *src); 25 | 26 | char *_strncat(char *dest, char *src, int n); 27 | 28 | char *_strncpy(char *dest, char *src, int n); 29 | 30 | int _strcmp(char *s1, char *s2); 31 | 32 | char *_memset(char *s, char b, unsigned int n); 33 | 34 | char *_memcpy(char *dest, char *src, unsigned int n); 35 | 36 | char *_strchr(char *s, char c); 37 | 38 | unsigned int _strspn(char *s, char *accept); 39 | 40 | char *_strpbrk(char *s, char *accept); 41 | 42 | char *_strstr(char *haystack, char *needle); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_FILE 2 | #define HEADER_FILE 3 | 4 | int _putchar(char c); 5 | 6 | int _islower(int c); 7 | 8 | int _isalpha(int c); 9 | 10 | int _abs(int n); 11 | 12 | int _isupper(int c); 13 | 14 | int _isdigit(int c); 15 | 16 | int _strlen(char *s); 17 | 18 | void _puts(char *s); 19 | 20 | char *_strcpy(char *dest, char *src); 21 | 22 | int _atoi(char *s); 23 | 24 | char *_strcat(char *dest, char *src); 25 | 26 | char *_strncat(char *dest, char *src, int n); 27 | 28 | char *_strncpy(char *dest, char *src, int n); 29 | 30 | int _strcmp(char *s1, char *s2); 31 | 32 | char *_memset(char *s, char b, unsigned int n); 33 | 34 | char *_memcpy(char *dest, char *src, unsigned int n); 35 | 36 | char *_strchr(char *s, char c); 37 | 38 | unsigned int _strspn(char *s, char *accept); 39 | 40 | char *_strpbrk(char *s, char *accept); 41 | 42 | char *_strstr(char *haystack, char *needle); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /0x08-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * _isdigit - Check for digits (0 through 9) 4 | * @c: int type param 5 | * Return: 1 if digit, else 0 6 | */ 7 | int _isdigit(int c) 8 | { 9 | if (c > 47 && c < 58) 10 | return (1); 11 | 12 | return (0); 13 | } 14 | 15 | 16 | /** 17 | * _atoi - program to converst ASCII to integer 18 | * @s: pointer to array of character 19 | * Return: Always successful 20 | */ 21 | 22 | int _atoi(char *s) 23 | { 24 | int digit; 25 | int count = 0; 26 | int sign = 1; 27 | unsigned int value = 0; 28 | unsigned int pos = 1; 29 | 30 | for (digit = 0; ((s[digit] < '0') || (s[digit] > '9')); digit++) 31 | { 32 | if (s[digit] == '-') 33 | sign *= -1; 34 | } 35 | for (; _isdigit(s[digit]); digit++) 36 | { 37 | count++; 38 | } 39 | while (count > 0) 40 | { 41 | value += ((s[digit - 1] - 48) * pos); 42 | pos *= 10; 43 | count--; 44 | digit--; 45 | } 46 | return (value * sign); 47 | } 48 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * _isdigit - Check for digits (0 through 9) 4 | * @c: int type param 5 | * Return: 1 if digit, else 0 6 | */ 7 | int _isdigit(int c) 8 | { 9 | if (c > 47 && c < 58) 10 | return (1); 11 | 12 | return (0); 13 | } 14 | 15 | 16 | /** 17 | * _atoi - program to converst ASCII to integer 18 | * @s: pointer to array of character 19 | * Return: Always successful 20 | */ 21 | 22 | int _atoi(char *s) 23 | { 24 | int digit; 25 | int count = 0; 26 | int sign = 1; 27 | unsigned int value = 0; 28 | unsigned int pos = 1; 29 | 30 | for (digit = 0; ((s[digit] < '0') || (s[digit] > '9')); digit++) 31 | { 32 | if (s[digit] == '-') 33 | sign *= -1; 34 | } 35 | for (; _isdigit(s[digit]); digit++) 36 | { 37 | count++; 38 | } 39 | while (count > 0) 40 | { 41 | value += ((s[digit - 1] - 48) * pos); 42 | pos *= 10; 43 | count--; 44 | digit--; 45 | } 46 | return (value * sign); 47 | } 48 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * delete_dnodeint_at_index - function to delete node at index 4 | * @head: double pointer of dlistint_t type to head of linked list 5 | * @index: index at which node will be deleted 6 | * Return: 1 if successful or -1 if failed 7 | */ 8 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 9 | { 10 | dlistint_t *temp1, *temp = *head; 11 | unsigned int count = 0; 12 | 13 | if (*head == NULL || head == NULL) 14 | return (-1); 15 | if (index == 0) 16 | { 17 | *head = temp->next; 18 | if (temp->next != NULL) 19 | temp->next->prev = NULL; 20 | free(temp); 21 | return (1); 22 | } 23 | 24 | while (count < (index - 1)) 25 | { 26 | if (temp == NULL) 27 | return (-1); 28 | temp = temp->next; 29 | count++; 30 | } 31 | temp1 = temp->next->next; 32 | if (temp->next->next != NULL) 33 | temp->next->next->prev = temp; 34 | free(temp->next); 35 | temp->next = temp1; 36 | return (1); 37 | } 38 | -------------------------------------------------------------------------------- /0x0C-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # 0x0C. C - Preprocessor 2 | 3 | What you should learn from this project 4 | At the end of this project you are expected to be able to explain to anyone, 5 | without the help of Google: 6 | 1. What are macros and how to use them 7 | 2. What are the most common predefined macros 8 | 3. How to include guard your header files 9 | 10 | ## Exercises 11 | 12 | 0. Object-like Macro 13 | Create a header file that defines a macro named SIZE as an abbreviation for the 14 | token 1024. 15 | 16 | 1. Pi mandatory 17 | Create a header file that defines a macro named PI as an abbreviation for the 18 | token 3.14159265359. 19 | 20 | 2. File name mandatory 21 | Write a program that prints the name of the file it was compiled from, followed 22 | by a new line. 23 | 24 | 3. Function-like macro mandatory 25 | Write a function-like macro ABS(x) that computes the absolute value of a 26 | number x. 27 | 28 | 4. SUM mandatory 29 | Write a function-like macro SUM(x, y) that computes the sum of the numbers x 30 | and y -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/1-insertion_sort_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | /** 5 | * insertion_sort_list - C function to insert node via sort 6 | * @list: Double pointer to list of struct listint_t type 7 | * Return: Return sorted list 8 | */ 9 | void insertion_sort_list(listint_t **list) 10 | { 11 | listint_t *cNode = NULL, *pNode = NULL; 12 | 13 | if (list == NULL || *list == NULL || (*list)->next == NULL) 14 | return; 15 | cNode = *list; 16 | while (cNode != NULL) 17 | { 18 | pNode = cNode; 19 | while (pNode->prev != NULL && pNode->n < pNode->prev->n) 20 | { 21 | pNode->prev->next = pNode->next; 22 | if (pNode->next != NULL) 23 | pNode->next->prev = pNode->prev; 24 | pNode->next = pNode->prev; 25 | pNode->prev = pNode->prev->prev; 26 | pNode->next->prev = pNode; 27 | if (pNode->prev == NULL) 28 | *list = pNode; 29 | else 30 | pNode->prev->next = pNode; 31 | print_list(*list); 32 | } 33 | cNode = cNode->next; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible combinations of two digits. 5 | * Numbers must be separated by commas and a space. 6 | * 01 and 10 are considered as the same combination of the two digits. 7 | * Print only the smallest combination of two digits. 8 | * Numbers should be printed in ascending order, with two digits. 9 | * You can only use `putchar`. 10 | * You can only use `putchar` up to 5 times. 11 | * You are not allowed to use any variable of type `char`. 12 | * Return: 0 13 | */ 14 | int main(void) 15 | { 16 | int a, b, c; 17 | 18 | a = 0; 19 | 20 | while (a < 100) 21 | { 22 | b = a % 10; /* singles digit */ 23 | c = a / 10; /* doubles digit */ 24 | 25 | if (c < b) 26 | { 27 | putchar(c + '0'); 28 | putchar(b + '0'); 29 | 30 | if (a < 89) 31 | { 32 | putchar(','); 33 | putchar(' '); 34 | } 35 | } 36 | 37 | a++; 38 | } 39 | putchar('\n'); 40 | 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /0x1C-binary_trees/111-bst_insert.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * bst_insert - insert given node into tree 5 | * @tree: double pointer to root node of tree or NULL if no tree 6 | * @value: value for new node 7 | * Return: pointer to created node 8 | */ 9 | bst_t *bst_insert(bst_t **tree, int value) 10 | { 11 | binary_tree_t *newNode, *tmp, *parent; 12 | 13 | if (!tree) 14 | return (NULL); 15 | 16 | if (!(*tree)) 17 | { 18 | /*use previous helper function to malloc and set node value*/ 19 | *tree = binary_tree_node(*tree, value); 20 | return (*tree); 21 | } 22 | 23 | tmp = *tree; 24 | while (tmp) 25 | { 26 | parent = tmp; 27 | if (value < tmp->n) 28 | tmp = tmp->left; 29 | else if (value > tmp->n) 30 | tmp = tmp->right; 31 | else if (value == tmp->n) 32 | return (NULL); 33 | } 34 | newNode = binary_tree_node(*tree, value); 35 | if (value < parent->n) 36 | parent->left = newNode; 37 | else 38 | parent->right = newNode; 39 | newNode->parent = parent; 40 | return (newNode); 41 | } 42 | -------------------------------------------------------------------------------- /0x1C-binary_trees/2-binary_tree_insert_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_insert_right - C function to add Binary Nodeto treee 4 | * @parent: Pointer to where new node will be added 5 | * @value: value to be inserted 6 | * Return: return pointer to New node 7 | */ 8 | binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value) 9 | { 10 | binary_tree_t *newNode; 11 | 12 | if (parent == NULL) 13 | return (NULL); 14 | newNode = malloc(sizeof(binary_tree_t)); 15 | if (newNode == NULL) 16 | return (NULL); 17 | newNode->n = value; 18 | newNode->parent = parent; 19 | newNode->left = NULL; 20 | 21 | /*check if parent-right is empty, put node here*/ 22 | if (parent->right == NULL) 23 | { 24 | parent->right = newNode; /*assign parent-right to newNode*/ 25 | newNode->right = NULL; /*assign newNode-left to NULL*/ 26 | } 27 | else 28 | { 29 | newNode->right = parent->right; 30 | newNode->right->parent = newNode; 31 | parent->right = newNode; 32 | } 33 | return (newNode); 34 | } 35 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | /** 4 | * op_add - add function 5 | * @a: int type 6 | * @b: int type 7 | * Return: sum of two int 8 | */ 9 | int op_add(int a, int b) 10 | { 11 | return (a + b); 12 | } 13 | 14 | /** 15 | * op_sub - substract function 16 | * @a: int type 17 | * @b: int type 18 | * Return: subtract two ints 19 | */ 20 | int op_sub(int a, int b) 21 | { 22 | return (a - b); 23 | } 24 | 25 | /** 26 | * op_mul - multiply function 27 | * @a: int type 28 | * @b: int type 29 | * Return: multiply two ints 30 | */ 31 | int op_mul(int a, int b) 32 | { 33 | return (a * b); 34 | } 35 | 36 | /** 37 | * op_div - divide two ints 38 | * @a: int type 39 | * @b: int type 40 | * Return: divide two ints 41 | */ 42 | int op_div(int a, int b) 43 | { 44 | return (a / b); 45 | } 46 | 47 | /** 48 | * op_mod - modulos of two ints 49 | * @a: int type 50 | * @b: int type 51 | * Return: modulus of two ints 52 | */ 53 | int op_mod(int a, int b) 54 | { 55 | return (a % b); 56 | } 57 | -------------------------------------------------------------------------------- /0x18-stacks_queues_lifo_fifo/monty.h: -------------------------------------------------------------------------------- 1 | #ifndef MONTY_H 2 | #define MONTY_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct stack_s - doubly linked list representation of a stack (or queue) 10 | * @n: integer 11 | * @prev: points to the previous element of the stack (or queue) 12 | * @next: points to the next element of the stack (or queue) 13 | * 14 | * Description: doubly linked list node structure 15 | * for stack, queues, LIFO, FIFO notrebloh project 16 | */ 17 | typedef struct stack_s 18 | { 19 | int n; 20 | struct stack_s *prev; 21 | struct stack_s *next; 22 | } stack_t; 23 | 24 | /** 25 | * struct instruction_s - opcoode and its function 26 | * @opcode: the opcode 27 | * @f: function to handle the opcode 28 | * 29 | * Description: opcode and its function 30 | * for stack, queues, LIFO, FIFO notrebloh project 31 | */ 32 | typedef struct instruction_s 33 | { 34 | char *opcode; 35 | void (*f)(stack_t **stack, unsigned int line_number); 36 | } instruction_t; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_below_98 - check for numbers below 98 5 | * @a: int type 6 | * 7 | * 8 | * Return: return all values including 98 9 | */ 10 | 11 | void print_below_98(int a) 12 | { 13 | int c; 14 | 15 | for (c = a; c <= 98; c++) 16 | { 17 | if (c != 98) 18 | printf("%d, ", c); 19 | else 20 | printf("%d\n", c); 21 | } 22 | } 23 | 24 | /** 25 | * print_above_98 - check for numbers above 98 26 | * @d: int type 27 | * 28 | * 29 | * Return: return all values including 98 30 | */ 31 | 32 | void print_above_98(int d) 33 | { 34 | int b; 35 | 36 | for (b = d; b >= 98; b--) 37 | { 38 | if (b != 98) 39 | printf("%d, ", b); 40 | else 41 | printf("%d\n", b); 42 | } 43 | } 44 | 45 | /** 46 | * print_to_98 - check for numbers below 98 47 | * @n: int type 48 | * 49 | * 50 | * Return: return all values including 98 51 | */ 52 | 53 | 54 | void print_to_98(int n) 55 | { 56 | if (n <= 98) 57 | print_below_98(n); 58 | else 59 | print_above_98(n); 60 | } 61 | -------------------------------------------------------------------------------- /0x07-recursion/7-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | 3 | /** 4 | * _strlen_recursion - find length of a string 5 | * @s: pointer to string to be counted 6 | * Return: return count 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | return (0); 13 | else 14 | return (1 + _strlen_recursion(s + 1)); 15 | } 16 | 17 | /** 18 | * charcheck - resolve palidrome 19 | * @str: pointer to string to be counted 20 | * @length: int type 21 | * Return: return palidrome 22 | */ 23 | 24 | int charcheck(char *str, int length) 25 | { 26 | if (length <= 0) 27 | return (1); 28 | if (str[0] == str[length - 1]) 29 | { 30 | return (charcheck(str + 1, length - 2)); 31 | } 32 | else 33 | return (0); 34 | } 35 | 36 | /** 37 | * is_palindrome - check if palidrome 38 | * @s: pointer to string to be counted 39 | * Return: return value 40 | */ 41 | 42 | int is_palindrome(char *s) 43 | { 44 | int length = _strlen_recursion(s); 45 | 46 | if (length <= 1) 47 | return (1); 48 | else 49 | return (charcheck(s, length)); 50 | } 51 | -------------------------------------------------------------------------------- /0x0A-malloc_free/5-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * *argstostr - convert arguments on command line to strings 7 | * @ac: int type 8 | * @av: pointer to array 9 | * Return: arguments as strings 10 | */ 11 | 12 | char *argstostr(int ac, char **av) 13 | { 14 | int size, count, count1, count2 = 0; 15 | char *ptr; 16 | 17 | if (ac == 0 || av == NULL) 18 | { 19 | return (NULL); 20 | } 21 | 22 | for (count = 0; count < ac; count++) 23 | { 24 | for (count1 = 0; av[count][count1] != '\0'; count1++) 25 | { 26 | size += 1; 27 | } 28 | size += 1; 29 | } 30 | size += 1; 31 | 32 | ptr = malloc(sizeof(char) * size); 33 | if (ptr == NULL) 34 | { 35 | free(ptr); 36 | return (NULL); 37 | } 38 | for (count = 0; count < ac; count++) 39 | { 40 | for (count1 = 0; av[count][count1] != '\0'; count1++) 41 | { 42 | ptr[count2] = av[count][count1]; 43 | count2++; 44 | } 45 | ptr[count2] = '\n'; 46 | count2++; 47 | } 48 | ptr[count2] = '\0'; 49 | return (ptr); 50 | } 51 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * print_times_table - takes an input and prints out the times 4 | * @n: int type 5 | * Return: return table of values 6 | */ 7 | 8 | void print_times_table(int n) 9 | { 10 | int a, b; 11 | 12 | if (n < 15 || n < 0) 13 | 14 | for (a = 0; a <= n; a++) 15 | { 16 | for (b = 0; b <= n; b++) 17 | { 18 | if (b == 0) 19 | _putchar(((a * b) % 10) + '0'); 20 | else if ((a * b) <= 9) 21 | { 22 | _putchar(','); 23 | _putchar(' '); 24 | _putchar(' '); 25 | _putchar(' '); 26 | _putchar((a * b) + '0'); 27 | } 28 | else if ((a * b) <= 99) 29 | { 30 | _putchar(','); 31 | _putchar(' '); 32 | _putchar(' '); 33 | _putchar(((a * b) / 10) + '0'); 34 | _putchar(((a * b) % 10) + '0'); 35 | } 36 | else 37 | { 38 | _putchar(','); 39 | _putchar(' '); 40 | _putchar(((a * b) / 100) + '0'); 41 | _putchar((((a * b) / 10) % 10) + '0'); 42 | _putchar(((a * b) % 10) + '0'); 43 | } 44 | } 45 | _putchar('\n'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible different combinations of 3 digits. 5 | * Numbers must be separated by commas and a space. 6 | * The 3 digits must be different. 7 | * 012, 120, 102, 021, 201, 210 are considered the same combination. 8 | * print only the smallest combination of 3 digits. 9 | * Numbers should be printed in ascending order. 10 | * You can only use `putchar` to print to console. 11 | * You can only use `putchar` up to 6 times. 12 | * You are not allowed to use any variables of type `char`. 13 | * Return: 0 14 | */ 15 | int main(void) 16 | { 17 | int a, b, c, d; 18 | 19 | for (a = 0; a < 1000; a++) 20 | { 21 | b = a / 100; /* hundreds */ 22 | c = (a / 10) % 10; /* tens */ 23 | d = a % 10; /* singles */ 24 | 25 | if (b < c && c < d) 26 | { 27 | putchar(b + '0'); 28 | putchar(c + '0'); 29 | putchar(d + '0'); 30 | 31 | if (a < 700) 32 | { 33 | putchar(','); 34 | putchar(' '); 35 | } 36 | } 37 | } 38 | putchar('\n'); 39 | 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the first 98 number 5 | * 6 | * Description: fibobnachi numbers starting from one 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | long fib1, fib1a, fib1b, fib2, fib2a, fib2b, fib3, fib3a, fib3b; 13 | long divi = 10000000000; 14 | 15 | fib1 = 0; 16 | fib2 = 1; 17 | int counter = 1; 18 | 19 | for (counter = 0; counter < 91; counter++) 20 | { 21 | fib3 = fib2 + fib1; 22 | fib1 = fib2; 23 | fib2 = fib3; 24 | printf("%ld, ", fib3); 25 | } 26 | fib1a = fib1 / divi; 27 | fib1b = fib1 % divi; 28 | fib2a = fib2 / divi; 29 | fib2b = fib2 % divi; 30 | for (; counter < 98; counter++) 31 | { 32 | fib3a = fib1a + fib2a; 33 | fib3b = fib1b + fib2b; 34 | if (fib3b > divi) 35 | { 36 | fib3b = fib3b % divi; 37 | fib3a++; 38 | } 39 | if (counter != 97) 40 | { 41 | printf("%ld%ld, ", fib3a, fib3b); 42 | fib1a = fib2a; 43 | fib1b = fib2b; 44 | fib2a = fib3a; 45 | fib2b = fib3b; 46 | } 47 | else 48 | printf("%ld%ld\n", fib3a, fib3b); 49 | } 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * string_nconcat - function to concatnate strings with n bytes 6 | * @s1: destination for concatnation 7 | * @s2: source of string 8 | * @n: int type for size of byte 9 | * Return: pointer to new memory allocated 10 | */ 11 | 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | int count, count1; 15 | int sign = n; 16 | char *ptr; 17 | int len1, len2; 18 | 19 | if (s1 == NULL) 20 | s1 = ""; 21 | if (s2 == NULL) 22 | s2 = ""; 23 | 24 | for (len1 = 0; s1[len1] != '\0'; len1++) 25 | ; 26 | for (len2 = 0; s2[len2] != '\0'; len2++) 27 | ; 28 | 29 | if (sign >= len2) 30 | { 31 | sign = len2; 32 | ptr = malloc(sizeof(char) * (len1 + len2 + 1)); 33 | } 34 | else 35 | ptr = malloc(sizeof(char) * (len1 + n + 1)); 36 | if (ptr == NULL) 37 | return (NULL); 38 | for (count = 0; count < len1; count++) 39 | { 40 | ptr[count] = s1[count]; 41 | } 42 | for (count1 = 0; count1 < sign; count1++) 43 | { 44 | ptr[count++] = s2[count1]; 45 | } 46 | ptr[count++] = '\0'; 47 | return (ptr); 48 | } 49 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct dlistint_s - doubly linked list 10 | * @n: integer 11 | * @prev: points to the previous node 12 | * @next: points to the next node 13 | * 14 | * Description: doubly linked list node structure 15 | * for notrebloh project 16 | */ 17 | typedef struct dlistint_s 18 | { 19 | int n; 20 | struct dlistint_s *prev; 21 | struct dlistint_s *next; 22 | } dlistint_t; 23 | 24 | size_t print_dlistint(const dlistint_t *h); 25 | 26 | size_t dlistint_len(const dlistint_t *h); 27 | 28 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 29 | 30 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 31 | 32 | void free_dlistint(dlistint_t *head); 33 | 34 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 35 | 36 | int sum_dlistint(dlistint_t *head); 37 | 38 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 39 | 40 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /0x19-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_set - function that adds an element to a hash table 4 | * @ht: is the hash table to be updated 5 | * @key: key cannot be empty 6 | * @value: value to be associated to the key 7 | * Return: 1 if successful, 0 otherwise 8 | */ 9 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 10 | { 11 | unsigned long int index = 0; 12 | hash_node_t *new, *temp; 13 | 14 | if (!ht || !ht->array || !key || !value || strlen(key) == 0) 15 | { 16 | return (0); 17 | } 18 | if (strcmp("", key) == 0) 19 | return (0); 20 | 21 | index = key_index((const unsigned char *) key, ht->size); 22 | new = malloc(sizeof(hash_node_t)); 23 | if (new == NULL) 24 | return (0); 25 | temp = ht->array[index]; 26 | while (temp != NULL) 27 | { 28 | if (strcmp(temp->key, key) == 0) 29 | { 30 | free(temp->value); 31 | free(new); 32 | temp->value = strdup(value); 33 | return (1); 34 | } 35 | temp = temp->next; 36 | } 37 | new->key = strdup(key); 38 | new->value = strdup(value); 39 | new->next = ht->array[index]; 40 | ht->array[index] = new; 41 | return (1); 42 | } 43 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * insert_nodeint_at_index - function to insert node at nth location 4 | * @head: pointer to head of struct listint_t type 5 | * @idx: unsigned int for location to add node 6 | * @n: int type for value of new node 7 | * Return: always successful 8 | */ 9 | 10 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 11 | { 12 | listint_t *node = NULL; 13 | listint_t *temp = NULL; 14 | unsigned int count = 0; 15 | 16 | if (head == NULL) 17 | return (NULL); 18 | node = malloc(sizeof(listint_t)); 19 | if (node == NULL) 20 | return (NULL); 21 | node->n = n; 22 | node->next = NULL; 23 | temp = *head; 24 | if (temp == NULL) 25 | { 26 | *head = node; 27 | node->next = NULL; 28 | return (node); 29 | } 30 | if (idx == 0) 31 | { 32 | node->next = *head; 33 | *head = node; 34 | return (node); 35 | } 36 | while (count != (idx - 1)) 37 | { 38 | temp = temp->next; 39 | count++; 40 | if (temp == NULL) 41 | { 42 | free(node); 43 | return (NULL); 44 | } 45 | } 46 | node->next = temp->next; 47 | temp->next = node; 48 | return (node); 49 | } 50 | -------------------------------------------------------------------------------- /0x1C-binary_trees/README.md: -------------------------------------------------------------------------------- 1 | ## 0x1C. C - Binary trees 2 | 3 | ![](http://new.gctm-resources.org/migrating/644-01.png) 4 | 5 | **What you should learn from this project** 6 | 7 | At the end of this project you are expected to be able to explain to anyone, without 8 | the help of Google: 9 | 10 | * What is a binary tree 11 | * What is the difference between a binary tree and a Binary Search Tree 12 | * What is the possible gain in terms of time complexity compared to linked lists 13 | * What are the depth, the height, the size of a binary tree 14 | * What are the different traversal methods to go through a binary tree 15 | * What is a complete, a full, a perfect, a balanced binary tree 16 | 17 | ### Environment 18 | * Language: C (version C89) 19 | * OS: Ubuntu 14.04 LTS 20 | * Compiler: gcc 4.8.4 21 | * Style guidelines: [Betty style](https://github.com/notreblohschool/Betty/wiki) 22 | --- 23 | ### Authors 24 | Adekunle Adeniran [![M](https://upload.wikimedia.org/wikipedia/fr/thumb/c/c8/Twitter_Bird.svg/30px-Twitter_Bird.svg\.png)](https://twitter.com/flourishcodes) 25 | Melissa Ng [![M](https://upload.wikimedia.org/wikipedia/fr/thumb/c/c8/Twitter_Bird.svg/30px-Twitter_Bird.svg\.png)](https://twitter.com/MelissaNg__) -------------------------------------------------------------------------------- /0x1C-binary_trees/102-binary_tree_is_complete.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * _nodes - C function to count nodes with at least one child 4 | * @tree: pointer to tree 5 | * Return: number of nodes 6 | */ 7 | size_t _nodes(const binary_tree_t *tree) 8 | { 9 | if (tree == NULL) 10 | return (0); 11 | return (1 + _nodes(tree->left) + _nodes(tree->right)); 12 | } 13 | 14 | /** 15 | * isComplete - C Function helper to check if binary tree is complete 16 | * @index: index of root 17 | * @tree: pointer to node 18 | * @number_nodes: total nodes in tree 19 | * Return: 1 if complete, 0 if not 20 | */ 21 | int isComplete(const binary_tree_t *tree, size_t index, size_t number_nodes) 22 | { 23 | if (tree == NULL) 24 | return (1); 25 | 26 | if (index >= number_nodes) 27 | return (0); 28 | return (isComplete(tree->left, (2 * index + 1), number_nodes) && 29 | isComplete(tree->right, (2 * index + 2), number_nodes)); 30 | } 31 | 32 | /** 33 | * binary_tree_is_complete - C function to check if binary tree is complete 34 | * @tree: pointer to node of tree to check 35 | * Return: 1 if complete or 0 if not 36 | */ 37 | int binary_tree_is_complete(const binary_tree_t *tree) 38 | { 39 | if (tree == NULL) 40 | return (0); 41 | return (isComplete(tree, 0, _nodes(tree))); 42 | } 43 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | /** 3 | * print_array - Function to print contents of array 4 | * @array: Pointer to array to be printed 5 | * @size: size of array to be printed of type size_t 6 | * Return: always successful 7 | */ 8 | void print_array(int *array, size_t size) 9 | { 10 | size_t idx; 11 | 12 | printf("Searching in array: "); 13 | for (idx = 0; idx < size; idx++) 14 | { 15 | if (idx > 0 && idx < size) 16 | printf(", "); 17 | printf("%d", array[idx]); 18 | } 19 | printf("\n"); 20 | } 21 | /** 22 | * binary_search - Function to perform binary search 23 | * @array: Pointer to array to be searched 24 | * @size: size of array to be searched 25 | * @value: value to be searched for 26 | * Return: Return index of value if found 27 | */ 28 | int binary_search(int *array, size_t size, int value) 29 | { 30 | size_t start; 31 | size_t mid; 32 | size_t end; 33 | 34 | if (array) 35 | { 36 | start = 0; 37 | end = size - 1; 38 | while (start <= end) 39 | { 40 | print_array(array + start, end + 1 - start); 41 | mid = (start + end) / 2; 42 | if (array[mid] < value) 43 | start = mid + 1; 44 | else if (array[mid] > value) 45 | end = mid - 1; 46 | else 47 | return (mid); 48 | } 49 | } 50 | return (-1); 51 | } 52 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * insert_dnodeint_at_index - function to insert node at given index 4 | * @h: double pointer type to head of list 5 | * @idx: index at which node will be added 6 | * @n: int type for data to be added 7 | * Return: node if successful, NULL if failed 8 | */ 9 | 10 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 11 | { 12 | dlistint_t *node = malloc(sizeof(dlistint_t)); 13 | dlistint_t *temp; 14 | unsigned int count = 0; 15 | 16 | if (node == NULL) 17 | return (NULL); 18 | node->n = n; 19 | node->prev = NULL; 20 | node->next = NULL; 21 | 22 | if (*h == NULL) 23 | { 24 | *h = node; 25 | node->next = NULL; 26 | return (node); 27 | } 28 | temp = *h; 29 | if (idx == 0) 30 | { 31 | node->next = temp; 32 | temp->prev = node; 33 | *h = node; 34 | return (node); 35 | } 36 | 37 | while (count != (idx - 1)) 38 | { 39 | temp = temp->next; 40 | count++; 41 | if (temp == NULL) 42 | { 43 | free(node); 44 | return (NULL); 45 | } 46 | } 47 | node->next = temp->next; 48 | node->prev = temp; 49 | if (temp->next == NULL) 50 | { 51 | temp->next = node; 52 | } 53 | else 54 | { 55 | temp->next->prev = node; 56 | temp->next = node; 57 | } 58 | return (node); 59 | } 60 | -------------------------------------------------------------------------------- /0x1C-binary_trees/100-binary_trees_ancestor.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_depth - calculate levels from parent 5 | * @node: node 6 | * Return: levels node is from parent 7 | */ 8 | size_t binary_tree_depth(const binary_tree_t *node) 9 | { 10 | if (!node) 11 | return (0); 12 | 13 | if (node && (node->parent)) 14 | return (1 + binary_tree_depth(node->parent)); 15 | 16 | return (0); 17 | } 18 | /** 19 | * binary_trees_ancestor - C function to find common ancestor of two nodes 20 | * @first: pointer to first node 21 | * @second: pointer to second node 22 | * Return: pointer to ancestor node 23 | */ 24 | binary_tree_t *binary_trees_ancestor( 25 | const binary_tree_t *first, const binary_tree_t *second) 26 | { 27 | binary_tree_t *left, *right; 28 | 29 | if (!first || !second) 30 | return (NULL); 31 | if (first == second) 32 | return ((binary_tree_t *)first); 33 | if (first->parent == second->parent) 34 | return (first->parent); 35 | 36 | left = binary_trees_ancestor(first->parent, second); 37 | right = binary_trees_ancestor(second->parent, first); 38 | 39 | if (binary_tree_depth(first) > binary_tree_depth(second)) 40 | return (left); 41 | else if (binary_tree_depth(second) > binary_tree_depth(first)) 42 | return (right); 43 | return (binary_trees_ancestor(first->parent, second->parent)); 44 | } 45 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct listint_s - singly linked list 10 | * @n: integer 11 | * @next: points to the next node 12 | * 13 | * Description: singly linked list node structure 14 | * for notrebloh project 15 | */ 16 | 17 | typedef struct listint_s 18 | { 19 | int n; 20 | struct listint_s *next; 21 | } listint_t; 22 | 23 | int _putchar(char c); 24 | 25 | size_t print_listint(const listint_t *h); 26 | 27 | size_t listint_len(const listint_t *h); 28 | 29 | listint_t *add_nodeint(listint_t **head, const int n); 30 | 31 | listint_t *add_nodeint_end(listint_t **head, const int n); 32 | 33 | void free_listint(listint_t *head); 34 | 35 | void free_listint2(listint_t **head); 36 | 37 | int pop_listint(listint_t **head); 38 | 39 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 40 | 41 | int sum_listint(listint_t *head); 42 | 43 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 44 | 45 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 46 | 47 | listint_t *reverse_listint(listint_t **head); 48 | 49 | size_t print_listint_safe(const listint_t *head); 50 | 51 | size_t free_listint_safe(listint_t **h); 52 | 53 | listint_t *find_listint_loop(listint_t *head); 54 | #endif 55 | -------------------------------------------------------------------------------- /0x19-hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef HASH_TABLES_H 2 | #define HASH_TABLES_H 3 | 4 | #include 5 | #include 6 | #include 7 | /** 8 | * struct hash_node_s - Node of a hash table 9 | * 10 | * @key: The key, string 11 | * The key is unique in the HashTable 12 | * @value: The value corresponding to a key 13 | * @next: A pointer to the next node of the List 14 | */ 15 | typedef struct hash_node_s 16 | { 17 | char *key; 18 | char *value; 19 | struct hash_node_s *next; 20 | } hash_node_t; 21 | 22 | /** 23 | * struct hash_table_s - Hash table data structure 24 | * 25 | * @size: The size of the array 26 | * @array: An array of size @size 27 | * Each cell of this array is a pointer to the first node of a linked list, 28 | * because we want our HashTable to use a Chaining collision handling 29 | */ 30 | typedef struct hash_table_s 31 | { 32 | unsigned long int size; 33 | hash_node_t **array; 34 | } hash_table_t; 35 | 36 | hash_table_t *hash_table_create(unsigned long int size); 37 | 38 | unsigned long int hash_djb2(const unsigned char *str); 39 | 40 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 41 | 42 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 43 | 44 | char *hash_table_get(const hash_table_t *ht, const char *key); 45 | 46 | void hash_table_print(const hash_table_t *ht); 47 | 48 | void hash_table_delete(hash_table_t *ht); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x11. C - Singly linked lists 2 | 3 | ## What you should learn from this project 4 | 5 | At the end of this project you are expected to be able to explain to anyone, without the help of Google: 6 | 7 | When and why using linked lists vs arrays 8 | How to build and use linked lists 9 | 10 | ## Exercises: 11 | 12 | 0. Print list 13 | Write a function that prints all the elements of a list_t list. 14 | Prototype: size_t print_list(const list_t *h); 15 | Return: the number of nodes 16 | Format: see example 17 | If str is NULL, print [0] (nil) 18 | You are allowed to use printf 19 | 20 | 1. List length 21 | Write a function that returns the number of elements in a linked list_t list. 22 | Prototype: size_t list_len(const list_t *h); 23 | 24 | 2. Add node 25 | Write a function that adds a new node at the beginning of a list_t list. 26 | Prototype: list_t *add_node(list_t **head, const char *str); 27 | Return: the address of the new element, or NULL if it failed 28 | str needs to be duplicated 29 | You are allowed to use strdup 30 | 31 | 3. Add node at the end 32 | Write a function that adds a new node at the end of a list_t list. 33 | Prototype: list_t *add_node_end(list_t **head, const char *str); 34 | Return: the address of the new element, or NULL if it failed 35 | str needs to be duplicated 36 | You are allowed to use strdup 37 | 38 | 4. Free list mandatory 39 | Write a function that free a list_t list. 40 | Prototype: void free_list(list_t *head); -------------------------------------------------------------------------------- /0x0A-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | #include 3 | #include 4 | /** 5 | * _strlen - return length of a string 6 | * 7 | * @s: char type 8 | * Return: length of string 9 | */ 10 | int _strlen(char *s) 11 | { 12 | int a; 13 | 14 | for (a = 0; s[a] != '\0'; a++) 15 | ; 16 | return (a); 17 | } 18 | /** 19 | * _strncat - function to concatnate strings with n bytes 20 | * 21 | * @dest: destination for concatnation 22 | * @src: source of string 23 | * @n: int type for size of byte 24 | * Return: dest 25 | */ 26 | char *_strncat(char *dest, char *src, int n) 27 | { 28 | int dest_len, a; 29 | 30 | dest_len = _strlen(dest); 31 | for (a = 0; a < n && src[a] != '\0'; a++) 32 | dest[dest_len + a] = src[a]; 33 | return (dest); 34 | } 35 | 36 | /** 37 | * *str_concat - function to allocate space for sting concatnation 38 | * @s1: array pointer to destination of string 39 | * @s2: array pointer to source of string 40 | * Return: return pointer to copy of string 41 | */ 42 | 43 | char *str_concat(char *s1, char *s2) 44 | { 45 | char *ptr; 46 | int size1, size2; 47 | 48 | if (s1 == NULL) 49 | { 50 | s1 = ""; 51 | } 52 | if (s2 == NULL) 53 | { 54 | s2 = ""; 55 | } 56 | size1 = _strlen(s1); 57 | size2 = _strlen(s2); 58 | 59 | ptr = malloc(((size1 + size2) + 1) * sizeof(char)); 60 | if (ptr == NULL) 61 | { 62 | return (NULL); 63 | } 64 | _strncat(ptr, s1, size1); 65 | _strncat(ptr, s2, size2); 66 | ptr += '\0'; 67 | return (ptr); 68 | } 69 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x17. C - Dynamic libraries 2 | 3 | At the end of this project you are expected to be able to explain to anyone, without the help of Google: 4 | 5 | * What is a dymanic library, how does it work, how to create one, and how to use it 6 | * What is the environment variable $LD_LIBRARY_PATH and how to use it 7 | * What are the differences between static and shared libraries 8 | * Basic usage nm, ldd, ldconfig 9 | 10 | **Exercises:** 11 | 12 | **0. A library is not a luxury but one of the necessities of life** 13 | 14 | Create the dynamic library libnotrebloh.so containing all the functions listed below: 15 | 16 | **1. Without libraries what have we? We have no past and no future** 17 | 18 | Create a script that creates a dynamic library called liball.so from all the .c files that are in the current directory. 19 | 20 | **2. Either write something worth reading or do something worth writing** 21 | 22 | Write a blog post describing the differences between static and dynamic libraries. It should cover: 23 | 24 | * Why using libraries in general 25 | * How do they work 26 | * How to create them (Linux only) 27 | * How to use them (Linux only) 28 | * What are the differences between static and dynamic libraries 29 | * What are the advantages and drawbacks of each of them 30 | 31 | Your posts should have examples and at least one picture, at the top. Publish your blog post on Medium or LinkedIn, and share it at least on Twitter and LinkedIn. 32 | 33 | ## Author 34 | Adekunle Adeniran 35 | -------------------------------------------------------------------------------- /0x14-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "notrebloh.h" 2 | /** 3 | * main - C program that copied file content from one to another 4 | * @argc: size of array argv and number of command line arguments 5 | * @av: An array of size argc 6 | * Return: Always successful 7 | */ 8 | int main(int argc, char *av[]) 9 | { 10 | int file1, file2, fileread, filewrite; 11 | char buffer[1024]; 12 | 13 | if (argc != 3) 14 | { 15 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"); 16 | exit(97); 17 | } 18 | file1 = open(av[1], O_RDONLY); 19 | if (file1 == -1) 20 | { 21 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", av[1]); 22 | exit(98); 23 | } 24 | file2 = open(av[2], O_TRUNC | O_CREAT | O_WRONLY, 0664); 25 | if (file2 == -1) 26 | { 27 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", av[2]); 28 | exit(99); 29 | } 30 | do { 31 | fileread = read(file1, buffer, 1024); 32 | if (fileread == -1) 33 | { 34 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", av[1]); 35 | exit(98); 36 | } 37 | filewrite = write(file2, buffer, fileread); 38 | if (filewrite == -1) 39 | { 40 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", av[2]); 41 | exit(99); 42 | } 43 | } while (fileread == 1024); 44 | 45 | if (close(file1) == -1) 46 | { 47 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file1); 48 | exit(100); 49 | } 50 | if (close(file2) == -1) 51 | { 52 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file2); 53 | exit(100); 54 | } 55 | return (0); 56 | } 57 | -------------------------------------------------------------------------------- /0x1B-makefiles/holberton.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_notrebloh - print the logo of notrebloh 5 | * 6 | * Return: Nothing 7 | * 8 | * Learn more: https://www.notreblohschool.com 9 | */ 10 | void print_notrebloh(void) 11 | { 12 | printf("j#0000000000000000000000000000000000000\n"); 13 | printf("j#000000000000000000@Q**g00000000000000\n"); 14 | printf("j#0000000000000000*]++]4000000000000000\n"); 15 | printf("j#000000000000000k]++]++*N#000000000000\n"); 16 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 17 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 18 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 19 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 20 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 21 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 22 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 23 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 24 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 25 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 26 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 27 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 28 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 29 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 30 | printf("j#00000000000w]++++]*0##000000000000000\n"); 31 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 32 | printf("j#00000000000000000we]+]Q00000000000000\n"); 33 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 34 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 35 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 36 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 37 | } 38 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * _strlen - return length of a string 7 | * 8 | * @s: char type 9 | * Return: length of string 10 | */ 11 | 12 | int _strlen(char *s) 13 | { 14 | int a; 15 | 16 | for (a = 0; s[a] != '\0'; a++) 17 | { 18 | 19 | } 20 | return (a); 21 | } 22 | /** 23 | * _strcpy - function to copy string from source to destination 24 | *@dest: pointer to destinatioin of string to be copied 25 | *@src: pointer to source of string to be copied 26 | * Return: return the value at dest 27 | */ 28 | 29 | char *_strcpy(char *dest, char *src) 30 | { 31 | int a; 32 | 33 | for (a = 0; src[a] != '\0'; a++) 34 | { 35 | dest[a] = src[a]; 36 | } 37 | dest[a] = '\0'; 38 | return (dest); 39 | } 40 | /** 41 | * *new_dog - function to create new dog struct 42 | * @name: char type 43 | * @age: float type 44 | * @owner: char type 45 | * Return: Always success 46 | */ 47 | 48 | dog_t *new_dog(char *name, float age, char *owner) 49 | { 50 | dog_t *newd; 51 | 52 | newd = malloc(sizeof(struct dog)); 53 | if (newd == NULL) 54 | return (NULL); 55 | 56 | if (name == NULL) 57 | { 58 | free(newd); 59 | return (NULL); 60 | } 61 | newd->name = malloc(sizeof(char) * (_strlen(name) + 1)); 62 | if (newd->name == NULL) 63 | return (NULL); 64 | _strcpy(newd->name, name); 65 | 66 | newd->age = age; 67 | 68 | if (owner == NULL) 69 | { 70 | free(newd->owner); 71 | free(newd); 72 | return (NULL); 73 | } 74 | newd->owner = malloc(sizeof(char) * (_strlen(owner) + 1)); 75 | if (newd->owner == NULL) 76 | return (NULL); 77 | _strcpy(newd->owner, owner); 78 | 79 | return (newd); 80 | } 81 | -------------------------------------------------------------------------------- /0x1C-binary_trees/101-binary_tree_levelorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_height - Measures the height of a binary tree 4 | * 5 | * @tree: Pointer to the node to measures the height 6 | * 7 | * Return: The height of the tree starting at @node 8 | */ 9 | size_t binary_tree_height(const binary_tree_t *tree) 10 | { 11 | size_t height_l, height_r; 12 | 13 | if (tree == NULL || (tree->left == NULL && tree->right == NULL)) 14 | return (0); 15 | 16 | height_l = binary_tree_height(tree->left); 17 | height_r = binary_tree_height(tree->right); 18 | if (height_l > height_r) 19 | return (height_l + 1); 20 | return (height_r + 1); 21 | } 22 | /** 23 | * printLevel - C Function to print values at all levels of binary tree 24 | * @tree: pointer to tree/nodes whose values will be printed 25 | * @func: pointer to value to be printed 26 | * @level: level at which values will be printed 27 | * Return: printed values 28 | */ 29 | void printLevel(const binary_tree_t *tree, void (*func)(int), size_t level) 30 | { 31 | if (tree == NULL) 32 | return; 33 | if (level == 1) 34 | func(tree->n); 35 | else if (level > 1) 36 | { 37 | printLevel(tree->left, func, level - 1); 38 | printLevel(tree->right, func, level - 1); 39 | } 40 | } 41 | /** 42 | * binary_tree_levelorder - C function to print in level order 43 | * @tree: pointer to node to be printed 44 | * @func: function pointer 45 | * Return: printed data 46 | */ 47 | void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int)) 48 | { 49 | size_t height, count; 50 | 51 | if (tree == NULL || func == NULL) 52 | return; 53 | height = binary_tree_height(tree) + 1; 54 | for (count = 1; count <= height; count++) 55 | printLevel(tree, func, count); 56 | } 57 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # 0x0D. C - Structures, typedef 2 | 3 | What you should learn from this project 4 | At the end of this project you are expected to be able to explain to anyone, 5 | without the help of Google: 6 | 1. What are structures, when, why and how to use them 7 | 2. How to use typedef 8 | 9 | ## Exercises 10 | 11 | 0. Django 12 | Define a new type struct dog with the following elements: 13 | name, type = char * 14 | age, type = float 15 | owner, type = char * 16 | 17 | 1. A dog is the only thing on earth that loves you more than you love yourself 18 | Write a function that initialize a variable of type struct dog 19 | Prototype: void init_dog(struct dog *d, char *name, float age, char *owner); 20 | 21 | 2. A dog will teach you unconditional love. If you can have that in your life, 22 | things won't be too bad 23 | Write a function that prints a struct dog 24 | Prototype: void print_dog(struct dog *d); 25 | Format: see example bellow 26 | You are allowed to use the standard library 27 | If an element of d is NULL, print (nil) instead of this element. 28 | (if name is NULL, print Name: (nil)) 29 | If d is NULL print nothing. 30 | 31 | 3. Outside of a dog, a book is a man's best friend. Inside of a dog it's too 32 | dark to read 33 | Define a new type dog_t as a new name for the type struct dog. 34 | 35 | 4. A door is what a dog is perpetually on the wrong side of 36 | Write a function that creates a new dog. 37 | Prototype: dog_t *new_dog(char *name, float age, char *owner); 38 | You have to store a copy of name and owner 39 | Return NULL if the function fails 40 | 41 | 5. How many legs does a dog have if you call his tail a leg? Four. Saying that a 42 | tail is a leg doesn't make it a leg mandatory 43 | Write a function that frees dogs. 44 | Prototype: void free_dog(dog_t *d); -------------------------------------------------------------------------------- /0x0F-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include "stdio.h" 3 | 4 | /** 5 | * print_int - function to print ints 6 | * @arg: va_list type 7 | * Return: always successful 8 | */ 9 | void print_int(va_list arg) 10 | { 11 | printf("%d", va_arg(arg, int)); 12 | } 13 | 14 | /** 15 | * print_char - function to print char 16 | * @arg: va_list type 17 | * Return: always successful 18 | */ 19 | 20 | void print_char(va_list arg) 21 | { 22 | printf("%c", va_arg(arg, int)); 23 | } 24 | /** 25 | * print_float - function to print floats 26 | * @arg: va_list type 27 | * Return: always successful 28 | */ 29 | void print_float(va_list arg) 30 | { 31 | printf("%f", va_arg(arg, double)); 32 | } 33 | 34 | /** 35 | * print_string - function to print string 36 | * @arg: va_list type 37 | * Return: always successful 38 | */ 39 | void print_string(va_list arg) 40 | { 41 | char *pr; 42 | 43 | pr = va_arg(arg, char*); 44 | if (pr == NULL) 45 | { 46 | pr = "(nil)"; 47 | } 48 | printf("%s", pr); 49 | } 50 | 51 | /** 52 | * print_all - function to print all inputs 53 | * @format: const pointer to functionof type char 54 | * Return: always successful 55 | */ 56 | void print_all(const char * const format, ...) 57 | { 58 | int i, j; 59 | va_list arg; 60 | char *seperator; 61 | 62 | pt types[] = { 63 | {"c", print_char}, 64 | {"i", print_int}, 65 | {"f", print_float}, 66 | {"s", print_string}, 67 | {NULL, NULL} 68 | }; 69 | 70 | va_start(arg, format); 71 | 72 | i = 0; 73 | seperator = ""; 74 | while (format && format[i]) 75 | { 76 | j = 0; 77 | while (types[j].test != NULL) 78 | { 79 | if (format[i] == types[j].test[0]) 80 | { 81 | printf("%s", seperator); 82 | types[j].printer(arg); 83 | seperator = ", "; 84 | } 85 | j++; 86 | } 87 | i++; 88 | } 89 | printf("\n"); 90 | va_end(arg); 91 | } 92 | -------------------------------------------------------------------------------- /0x1C-binary_trees/16-binary_tree_is_perfect.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_height - Measures the height of a binary tree 5 | * @tree: Pointer to the node to measures the height 6 | * Return: The height of the tree starting at @node 7 | */ 8 | size_t binary_tree_height(const binary_tree_t *tree) 9 | { 10 | size_t height_l; 11 | size_t height_r; 12 | 13 | if (tree == NULL) 14 | return (0); 15 | 16 | /*recursively check the left and right node if exist*/ 17 | height_l = tree->left ? 1 + binary_tree_height(tree->left) : 0; 18 | height_r = tree->right ? 1 + binary_tree_height(tree->right) : 0; 19 | return (height_l > height_r ? height_l : height_r); 20 | } 21 | 22 | /** 23 | * binary_tree_size - C function to print the size of a Node in a tree 24 | * @tree: pointer to tree/node to be checked 25 | * Return: size of tree 26 | */ 27 | 28 | size_t binary_tree_size(const binary_tree_t *tree) 29 | { 30 | size_t size = 0; 31 | 32 | if (!tree) 33 | return (0); 34 | 35 | size += (binary_tree_size(tree->right) 36 | + binary_tree_size(tree->left) + 1); 37 | return (size); 38 | } 39 | 40 | /** 41 | * to_power - a to power b (2 ^ height of tree) 42 | * @a: 2 43 | * @b: height of tree 44 | * Return: number of nodes at last level for bianry tree to be "perfect" 45 | */ 46 | size_t to_power(size_t a, size_t b) 47 | { 48 | if (b == 0) 49 | return (1); 50 | if (a == 1) 51 | return (b); 52 | return (a * to_power(a, b - 1)); 53 | } 54 | 55 | /** 56 | * binary_tree_is_perfect - checks if perfectly filled like a triangle 57 | * @tree: tree 58 | * Return: 1 if perfect, 0 if not 59 | */ 60 | int binary_tree_is_perfect(const binary_tree_t *tree) 61 | { 62 | size_t expected_num_nodes; 63 | 64 | if (!tree) 65 | return (0); 66 | 67 | expected_num_nodes = (to_power(2, (binary_tree_height(tree)) + 1) - 1); 68 | return (binary_tree_size(tree) == expected_num_nodes); 69 | } 70 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | ## 0x1D. C - Search Algorithms 2 | 3 | **What you should learn from this project** 4 | 5 | At the end of this project you are expected to be able to explain to 6 | anyone, without the help of Google: 7 | 8 | * What is a search algorithm 9 | * What is a linear search 10 | * What is a binary search 11 | * What is the best search algorithm to use depending on your needs 12 | 13 | **0. Linear search** 14 | 15 | Write a function that searches for a value in an array of integers using 16 | the Linear search algorithm 17 | 18 | * Prototype : int linear_search(int *array, size_t size, int value); 19 | * Where array is a pointer to the first element of the array to search in 20 | size is the number of elements in array 21 | * And value is the value to search for 22 | * Your function must return the first index where value is located 23 | * If value is not present in array or if array is NULL, your function 24 | must return -1 25 | * Every time you compare a value in the array to the value you are searching, 26 | you have to print this value (see example below) 27 | 28 | **1. Binary search** 29 | 30 | Write a function that searches for a value in a sorted array of integers 31 | using the Binary search algorithm 32 | 33 | * Prototype : int binary_search(int *array, size_t size, int value); 34 | * Where array is a pointer to the first element of the array to search in 35 | size is the number of elements in array 36 | * And value is the value to search for 37 | * Your function must return the index where value is located 38 | * You can assume that array will be sorted in ascending order 39 | * You can assume that value won’t appear more than once in array 40 | * If value is not present in array or if array is NULL, your function 41 | must return -1 42 | * You must print the array being searched every time it changes. 43 | (e.g. at the beginning and when you search a subarray) -------------------------------------------------------------------------------- /0x0B-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0B. C - More malloc, free 2 | What you should learn from this project 3 | At the end of this project you are expected to be able to explain to anyone, 4 | without the help of Google: 5 | 6 | How to use the exit function 7 | What are the functions callo and realloc from the standard library and how to 8 | use them 9 | 10 | ## Quiz Questions 11 | 12 | 0. Trust no one 13 | Write a function that allocates memory using malloc. 14 | Prototype: void *malloc_checked(unsigned int b); 15 | Returns a pointer to the allocated memory 16 | if malloc fails, the malloc_checked function should cause normal process 17 | termination with a status value of 98 18 | 19 | 1. string_nconcat 20 | Write a function that concatenates two strings. 21 | Prototype: char *string_nconcat(char *s1, char *s2, unsigned int n); 22 | The returned pointer shall point to a newly allocated space in memory, 23 | which contains s1, followed by the first n bytes of s2, and null terminated 24 | If the function fails, it should return NULL 25 | If n is greater or equal to the length of s2 then use the entire string s2 26 | if NULL is pass, treat it as an empty string 27 | 28 | 2. _calloc 29 | Write a function that allocates memory for an array, using malloc. 30 | Prototype: void *_calloc(unsigned int nmemb, unsigned int size); 31 | The _calloc function allocates memory for an array of nmemb elements of size 32 | bytes each and returns a pointer to the allocated memory. 33 | The memory is set to zero 34 | If nmemb or size is 0, then _calloc returns NULL 35 | If malloc fails, then _calloc returns NULL 36 | 37 | 3. array_range 38 | Write a function that creates an array of integers. 39 | Prototype: int *array_range(int min, int max); 40 | The array created should contain all the values from min (included) to max 41 | (included), ordered from min to max 42 | Return: the pointer to the newly created array 43 | If min > max, return NULL 44 | If malloc fails, return NULL -------------------------------------------------------------------------------- /0x09-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # 0x09. C - argc, argv 2 | 3 | At the end of this project you are expected to be able to explain to anyone, 4 | without the help of Google: 5 | 6 | How to use arguments passed to your program 7 | What are two prototypes of main that you know of, and in which case do you use 8 | one or the other 9 | How to use __attribute__((unused)) or (void) to compile functions with unused 10 | variables or parameters 11 | 12 | ## Exercises 13 | 0. It ain't what they call you, it's what you answer to mandatory 14 | Write a program that prints its name, followed by a new line. 15 | If you rename the program, it will print the new name, without 16 | having to compile it again 17 | You should not remove the path before the name of the program 18 | 19 | 1. Silence is argument carried out by other means 20 | Write a program that prints the number of arguments passed into it. 21 | Your program should print a number, followed by a new line 22 | 23 | 2. The best argument against democracy is a five-minute conversation with the 24 | average voter 25 | Write a program that prints all arguments it receives. 26 | All arguments should be printed, including the first one 27 | Only print one argument per line, ending with a new line 28 | 29 | 3. Neither irony or sarcasm is argument 30 | Write a program that multiplies two numbers. 31 | Your program should print the result of the multiplication, followed by a 32 | new line 33 | You can assume that the two numbers and result of the multiplication can 34 | be stored in an integer 35 | If the program does not receive two arguments, your program should print Error, 36 | followed by a new line, and return 1 37 | 38 | 4. To infinity and beyond 39 | Write a program that adds positive numbers. 40 | Print the result, followed by a new line 41 | If no number is passed to the program, print 0, followed by a new line 42 | If one of the number contains symbols that are not digits, print Error, 43 | followed by a new line, and return 1 44 | You can assume that numbers and the addition of all the numbers can be stored 45 | in an int --------------------------------------------------------------------------------