├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c ├── README.md ├── c └── main.c ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 10-print_comb2.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c ├── README.md └── a.out ├── 0x02-functions_nested_loops ├── 0-holberton.c ├── 1-alphabet.c ├── 1-main.c ├── 10-add.c ├── 10-main.c ├── 11-main.c ├── 11-print_to_98.c ├── 111-putchar_print_to_98.c ├── 2-main.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 3-main.c ├── 4-isalpha.c ├── 4-main.c ├── 5-main.c ├── 5-sign.c ├── 6-abs.c ├── 6-main.c ├── 7-main.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-main.c ├── 9-times_table.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x03-more_functions_nested_loops ├── 0-isupper.c ├── 0-main.c ├── 1-isdigit.c ├── 1-main.c ├── 10-main.c ├── 10-print_triangle.c ├── 101-main.c ├── 101-print_number.c ├── 2-main.c ├── 2-mul.c ├── 3-main.c ├── 3-print_numbers.c ├── 4-main.c ├── 4-print_most_numbers.c ├── 5-main.c ├── 5-more_numbers.c ├── 6-main.c ├── 6-print_line.c ├── 7-main.c ├── 7-print_diagonal.c ├── 8-main.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x04-pointers_arrays_strings ├── 0-main.c ├── 0-reset_to_98.c ├── 1-main.c ├── 1-swap.c ├── 100-atoi.c ├── 100-main.c ├── 101-crackme ├── 101-keygen.c ├── 102-keygenOriginalAttempt.c ├── 2-main.c ├── 2-strlen.c ├── 3-main.c ├── 3-puts.c ├── 4-main.c ├── 4-print_rev.c ├── 5-main.c ├── 5-rev_string.c ├── 6-main.c ├── 6-puts2.c ├── 7-main.c ├── 7-puts_half.c ├── 8-main.c ├── 8-print_array.c ├── 9-main.c ├── 9-strcpy.c ├── README.md └── holberton.h ├── 0x05-pointers_arrays_strings ├── 0-main.c ├── 0-strcat.c ├── 1-main.c ├── 1-strncat.c ├── 100-main.c ├── 100-print_number.c ├── 101-magic.c ├── 102-infinite_add.c ├── 102-main.c ├── 103-main.c ├── 103-print_buffer.c ├── 2-main.c ├── 2-strncpy.c ├── 3-main.c ├── 3-strcmp.c ├── 4-main.c ├── 4-rev_array.c ├── 5-main.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── 7-main.c ├── 8-main.c ├── 8-rot13.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x06-pointers_arrays_strings ├── #crackme# ├── 0-main.c ├── 0-memset.c ├── 1-main.c ├── 1-memcpy.c ├── 101-crackme_password ├── 101result ├── 2-main.c ├── 2-strchr.c ├── 3-main.c ├── 3-strspn.c ├── 4-main.c ├── 4-strpbrk.c ├── 5-main.c ├── 5-strstr.c ├── 7-main.c ├── 7-print_chessboard.c ├── 8-main.c ├── 8-print_diagsums.c ├── 9-main.c ├── 9-set_string.c ├── README.md ├── _putchar.c ├── a.out ├── crackme.dump ├── crackme2 ├── hexFromCrackMe ├── hexParser ├── hexParser.c ├── hexToParse └── holberton.h ├── 0x07-recursion ├── 0-main.c ├── 0-puts_recursion.c ├── 1-main.c ├── 1-print_rev_recursion.c ├── 100-main.c ├── 100-wildcmp.c ├── 2-main.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 3-main.c ├── 4-main.c ├── 4-pow_recursion.c ├── 5-main.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 6-main.c ├── 7-is_palindrome.c ├── 7-main.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x08-static_libraries ├── README.md ├── create_static_lib.sh ├── holberton.h └── libholberton.a ├── 0x09-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── 0x0A-malloc_free ├── 0-create_array.c ├── 0-main.c ├── 1-main.c ├── 1-strdup.c ├── 100-strtow.c ├── 2-main.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── 4-main.c ├── 5-argstostr.c ├── 5-main.c ├── README.md └── holberton.h ├── 0x0B-more_malloc_free ├── 0-malloc_checked.c ├── 1-main.c ├── 1-string-nconcat.c ├── 1-string_nconcat.c ├── 100-main.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 2-main.c ├── 3-array_range.c ├── 3-main.c ├── README.md └── holberton.h ├── 0x0C-preprocessor ├── 0-main.c ├── 0-object_like_macro.h ├── 1-main.c ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 3-main.c ├── 4-main.c ├── 4-sum.h └── README.md ├── 0x0D-structures_typedef ├── 0-main.c ├── 1-init_dog.c ├── 1-main.c ├── 2-main.c ├── 2-print_dog.c ├── 3-main.c ├── 4-main.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0E-function_pointers ├── 0-main.c ├── 0-print_name.c ├── 1-array_iterator.c ├── 1-main.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 2-main.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x0F-variadic_functions ├── 0-main.c ├── 0-sum_them_all.c ├── 1-main.c ├── 1-print_numbers.c ├── 1-real-print_numbers ├── 100-hello_holberton.asm ├── 2-main.c ├── 2-print_strings.c ├── 2-real-print_strings.c ├── 3-main.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x11-singly_linked_lists ├── 0-main.c ├── 0-print_list.c ├── 1-list_len.c ├── 1-main.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 2-main.c ├── 3-add_node_end.c ├── 3-main.c ├── 4-free_list.c ├── 4-main.c ├── README.md └── lists.h ├── 0x12-more_singly_linked_lists ├── 0-main.c ├── 0-print_listint.c ├── 1-listint_len.c ├── 1-main.c ├── 10-delete_nodeint.c ├── 10-main.c ├── 100-reverse_listint.c ├── 101-main.c ├── 101-print_listint_safe.c ├── 103-find_loop.c ├── 103-main.c ├── 2-add_nodeint.c ├── 2-main.c ├── 3-add_nodeint_end.c ├── 3-main.c ├── 4-free_listint.c ├── 4-main.c ├── 5-free_listint2.c ├── 5-main.c ├── 6-main.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 7-main.c ├── 8-main.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── 9-main.c ├── README.md └── lists.h ├── 0x13-bit_manipulation ├── 0-binary_to_uint.c ├── 0-main.c ├── 1-main.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 100-main.c ├── 101-password ├── 2-get_bit.c ├── 2-main.c ├── 3-main.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 4-main.c ├── 5-flip_bits.c ├── 5-main.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x14-file_io ├── 0-main.c ├── 0-read_textfile.c ├── 1-create_file.c ├── 1-main.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 2-main.c ├── 3-cp.c ├── README.md ├── RequiesCat ├── holberton.h ├── incitatous └── super_long_file ├── 0x16-doubly_linked_lists ├── 0-main.c ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 1-main.c ├── 100-password ├── 102-result ├── 2-add_dnodeint.c ├── 2-main.c ├── 3-add_dnodeint_end.c ├── 3-main.c ├── 4-free_dlistint.c ├── 4-main.c ├── 5-get_dnodeint.c ├── 5-main.c ├── 6-main.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 7-main.c ├── 8-delete_dnodeint.c ├── 8-main.c ├── README.md ├── a.out ├── b ├── e ├── generate_palindrome.c ├── h ├── j ├── k └── lists.h ├── 0x17-dynamic_libraries ├── 0-main.c ├── 1-create_dynamic_lib.sh ├── 1-isdigit.c ├── 1-memcpy.c ├── 1-strncat.c ├── 100-atoi.c ├── 100-operations.c ├── 100-operations.so ├── 101-make_me_win.sh ├── 2-strchr.c ├── 2-strlen.c ├── 2-strncpy.c ├── 3-islower.c ├── 3-puts.c ├── 3-strcmp.c ├── 3-strspn.c ├── 4-isalpha.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 6-abs.c ├── 9-strcpy.c ├── README.md ├── _putchar.c ├── custom_check_injection.c ├── holberton.h ├── libholberton.so ├── libinjection.so └── testing_ops.py ├── 0x19-hash_tables ├── .gitignore ├── 0-hash_table_create.c ├── 0-main.c ├── 1-djb2.c ├── 1-main.c ├── 2-key_index.c ├── 2-main.c ├── 3-hash_table_set.c ├── 3-main.c ├── 4-hash_table_get.c ├── 4-main.c ├── 5-hash_table_print.c ├── 5-main.c ├── 6-hash_table_delete.c ├── 6-main.c ├── LICENSE.md ├── README.md └── hash_tables.h ├── 0x1A-sorting_algorithms ├── 0-O ├── 0-bubble_sort.c ├── 0-main.c ├── 1-O ├── 1-insertion_sort_list.c ├── 1-main.c ├── 1000-main.c ├── 1000-sort_deck.c ├── 2-O ├── 2-main.c ├── 2-selection_sort.c ├── 3-O ├── 3-main.c ├── 3-quick_sort.c ├── README.md ├── deck ├── deck.h ├── print_array.c ├── print_list.c └── sort.h ├── 0x1B-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_efficient_sol.py ├── 5-island_perimeter.py ├── 5-main.py ├── 5-off_main.py ├── README.md ├── holberton.c ├── m.h └── main.c ├── 0x1C-binary_trees ├── 0-binary_tree_node.c ├── 1-binary_tree_insert_left.c ├── 10-binary_tree_depth.c ├── 11-binary_tree_size.c ├── 12-binary_tree_leaves.c ├── 13-binary_tree_nodes.c ├── 14-binary_tree_balance.c ├── 15-binary_tree_is_full.c ├── 16-binary_tree_is_perfect.c ├── 17-binary_tree_sibling.c ├── 18-binary_tree_uncle.c ├── 2-binary_tree_insert_right.c ├── 3-binary_tree_delete.c ├── 4-binary_tree_is_leaf.c ├── 5-binary_tree_is_root.c ├── 6-binary_tree_preorder.c ├── 7-binary_tree_inorder.c ├── 8-binary_tree_postorder.c ├── 9-binary_tree_height.c ├── README.md ├── binary_tree_print.c ├── binary_trees.h └── tests │ ├── 0-main.c │ ├── 1-main.c │ ├── 10-main.c │ ├── 12-main.c │ ├── 13-main.c │ ├── 14-main.c │ ├── 15-main.c │ ├── 16-main.c │ ├── 17-main.c │ ├── 18-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ ├── 6-main.c │ ├── 7-main.c │ ├── 8-main.c │ └── 9-main.c ├── 0x1D-search_algorithms ├── 0-linear.c ├── 0-main.c ├── 1-binary.c ├── 1-main.c ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h ├── LICENSE.md └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E -o c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point for function 4 | * 5 | * Description: Places our quote to stderr 6 | * instead of stdout 7 | * 8 | * Return: 1 always (success) 9 | */ 10 | int main(void) 11 | { 12 | char *quote = "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n"; 13 | 14 | write(2, quote, 59); 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -o cisfun $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * @void: no paramater 5 | * 6 | * Description: Uses puts to output 7 | * 8 | * "Programming is like building a multilingual puzzle 9 | * 10 | * Return: Always 0 (Success) 11 | */ 12 | int main(void) 13 | { 14 | char *holbertonStr = "\"Programming is like building a multilingual puzzle"; 15 | 16 | puts(holbertonStr); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | char *grammar = "with proper grammar, but the outcome is a piece of art,"; 10 | 11 | printf("%s\n", grammar); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Description: prints sizes of different types in c 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | printf("Size of a char: %lu byte(s)\n", sizeof(char)); 12 | printf("Size of an int: %lu byte(s)\n", sizeof(int)); 13 | printf("Size of a long int: %lu byte(s)\n", sizeof(long)); 14 | printf("Size of a long long int: %lu byte(s)\n", sizeof(double)); 15 | printf("Size of a float: %lu byte(s)\n", sizeof(float)); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | Here we will be using some shell scripts as well as C to look at compiler, preprocessor, and assembly results. 2 | -------------------------------------------------------------------------------- /0x00-hello_world/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | printf("Hello World!\n"); 5 | return (0); 6 | } 7 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /* more headers goes there */ 5 | /** 6 | * main - entry point for function 7 | * 8 | * Description: decides if a random number is odd or even 9 | * 10 | * Return: always 0 (success) 11 | */ 12 | /* betty style doc for function main goes there */ 13 | int main(void) 14 | { 15 | int n; 16 | 17 | srand(time(0)); 18 | n = rand() - RAND_MAX / 2; 19 | if (n > 0) 20 | printf("%i is positive\n", n); 21 | else if (n == 0) 22 | printf("%i is zero\n", n); 23 | else 24 | printf("%i is negative\n", n); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /* more headers goes there */ 5 | /** 6 | * main - entry point function 7 | * 8 | * Description: prints only last digit of num 9 | * stored in n 10 | * 11 | * Return: always 0 (success) 12 | */ 13 | /* betty style doc for function main goes there */ 14 | int main(void) 15 | { 16 | int n, q; 17 | 18 | srand(time(0)); 19 | n = rand() - RAND_MAX / 2; 20 | q = n % 10; 21 | 22 | printf("Last digit of %i is %i ", n, q); 23 | if (q > 5) 24 | printf("and is greater than 5\n"); 25 | else if (q < 6 && q != 0) 26 | printf("and is less than 6 and not 0\n"); 27 | else if (q == 0) 28 | printf("and is 0\n"); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/10-print_comb2.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry function for program 4 | * 5 | * Description: print '00', '01'..'99' 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int i, q; 12 | 13 | for (i = 48; i < 58; i++) 14 | { 15 | for (q = 48; q < 58; q++) 16 | { 17 | putchar(i); 18 | putchar(q); 19 | if (i == 57 && q == 57) 20 | continue; 21 | putchar(44); 22 | putchar(32); 23 | } 24 | } 25 | putchar(10); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point of function 4 | * 5 | * Description - print 00, 01..89 skip duplicates 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int a, b; 12 | 13 | a = '0'; 14 | while (a < '9') 15 | { 16 | b = a + 1; 17 | while (b <= '9') 18 | { 19 | putchar(a); 20 | putchar(b++); 21 | if (a != '8') 22 | { 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | } 27 | a++; 28 | } 29 | putchar('\n'); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point of function 4 | * 5 | * Description - print 00, 01..89 skip duplicates 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int a, b, c; 12 | 13 | a = '0'; 14 | while (a <= '7') 15 | { 16 | b = a + 1; 17 | while (b <= '8') 18 | { 19 | c = b + 1; 20 | while (c <= '9') 21 | { 22 | putchar(a); 23 | putchar(b); 24 | putchar(c); 25 | if (a != '7') 26 | { 27 | putchar(','); 28 | putchar(' '); 29 | } 30 | c++; 31 | } 32 | b++; 33 | } 34 | a++; 35 | } 36 | putchar('\n'); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point fucntion 4 | * 5 | * Description: prints all lowercase characters, followed by a new line 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 97; i <= 122; i++) 14 | putchar(i); 15 | putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program 4 | * 5 | * Description: print lower and uppercase chars 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 97; i < 123; i++) 14 | putchar(i); 15 | for (i = 65; i < 91; i++) 16 | putchar(i); 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for function 4 | * 5 | * Description: prints alphabet - q&e 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 97; i < 123; i++) 14 | { 15 | if (i == 101 || i == 113) 16 | continue; 17 | putchar(i); 18 | } 19 | putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program 4 | * 5 | * Description: prints numbers 0-9 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < 10; i++) 14 | printf("%i", i); 15 | putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program 4 | * 5 | * Description: use putchar to print ints 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 48; i < 58; i++) 14 | putchar(i); 15 | putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program 4 | * 5 | * Description: print alphabet in reverse 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 122; i > 96; i--) 14 | putchar(i); 15 | putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program 4 | * 5 | * Description - print hexadecimal 6 | * 7 | * Return: always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 48; i < 58; i++) 14 | putchar(i); 15 | for (i = 97; i < 103; i++) 16 | putchar(i); 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program 4 | * 5 | * Description: print all ints followed by ', ' 6 | * using putchar 7 | * 8 | * Return: always 0 (success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 48; i < 58; i++) 15 | { 16 | putchar(i); 17 | if (i == 57) 18 | continue; 19 | putchar(44); 20 | putchar(32); 21 | } 22 | putchar(10); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | Here we will be learning about the use of if statements, for loops, while loops, do-while loops, logical & arithmetic operators. 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x01-variables_if_else_while/a.out -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-holberton.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * main - entry point for function 4 | * 5 | * Return: always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | _putchar('H'); 10 | _putchar('o'); 11 | _putchar('l'); 12 | _putchar('b'); 13 | _putchar('e'); 14 | _putchar('r'); 15 | _putchar('t'); 16 | _putchar('o'); 17 | _putchar('n'); 18 | _putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_alphabet - print alphabet out 4 | * 5 | * Return: void 6 | */ 7 | void print_alphabet(void) 8 | { 9 | int ch = 'a'; 10 | 11 | while (ch <= 'z') 12 | { 13 | _putchar(ch++); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * main - entry point for function 4 | * 5 | * Return: always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | print_alphabet(); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | /** 2 | * add - adds two numbers 3 | * 4 | * @a: first num to add to b 5 | * @b: second num to add to a 6 | * 7 | * Return: the result 8 | */ 9 | int add(int a, int b) 10 | { 11 | return (a + b); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = add(89, 9); 14 | printf("%d\n", n); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_to_98(0); 11 | print_to_98(98); 12 | print_to_98(111); 13 | print_to_98(81); 14 | print_to_98(-10); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * print_to_98 - prints n..98 4 | * 5 | * @n: number to start at 6 | * 7 | * Return: always void 8 | */ 9 | void print_to_98(int n) 10 | { 11 | while (n >= 98 || n < 98) 12 | { 13 | printf("%d", n); 14 | if (n == 98) 15 | break; 16 | printf(", "); 17 | (n > 98) ? n-- : n++; 18 | } 19 | putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/111-putchar_print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_to_98 - prints n..98 4 | * 5 | * @n: number to start at 6 | * 7 | * Return: always void 8 | */ 9 | void print_to_98(int n) 10 | { 11 | int p1, p2, p3, tempN; 12 | 13 | while (n >= 98 || n < 98) 14 | { 15 | if (n < 0) 16 | _putchar('-'); 17 | tempN = n; 18 | p3 = tempN % 10; 19 | tempN = tempN / 10; 20 | p2 = tempN % 10; 21 | tempN = tempN / 10; 22 | p1 = tempN % 10; 23 | if (n <= 0) 24 | { 25 | p1 = p1 * -1; 26 | p2 = p2 * -1; 27 | p3 = p3 * -1; 28 | } 29 | 30 | _putchar((p1 == 0) ? 0 : p1 + '0'); 31 | _putchar((p1 == 0 && p2 == 0) ? 0 : p2 + '0'); 32 | _putchar(p3 + '0'); 33 | 34 | if (n == 98) 35 | break; 36 | _putchar(','); 37 | _putchar(' '); 38 | (n > 98) ? n-- : n++; 39 | } 40 | _putchar('\n'); 41 | } 42 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * main - entry point for function 4 | * 5 | * Return: always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | print_alphabet_x10(); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_alphabet_x10 - print alphabet 10 times 4 | * 5 | * Return: always void 6 | */ 7 | void print_alphabet_x10(void) 8 | { 9 | int i; 10 | 11 | i = 0; 12 | 13 | while (i < 10) 14 | { 15 | int ch = 'a'; 16 | 17 | while (ch <= 'z') 18 | { 19 | _putchar(ch++); 20 | } 21 | _putchar('\n'); 22 | i++; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _islower - print alphabet out 3 | * 4 | * @c: character 5 | * 6 | * Return: 1 if lower, 0 otherwise 7 | */ 8 | int _islower(int c) 9 | { 10 | if (c >= 'a' && c <= 'z') 11 | return (1); 12 | else 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * main - check the code for Holberton School students. 3 | * 4 | * Return: Always 0. 5 | */ 6 | int main(void) 7 | { 8 | int r; 9 | 10 | r = _islower('H'); 11 | _putchar(r + '0'); 12 | r = _islower('o'); 13 | _putchar(r + '0'); 14 | r = _islower(108); 15 | _putchar(r + '0'); 16 | _putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _isalpha - checks if a char is alpha or not 3 | * 4 | * @c: character parameter to check 5 | * 6 | * Return: 1 if letter, 0 otherwise 7 | */ 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 11 | return (1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = _isalpha('H'); 13 | _putchar(r + '0'); 14 | r = _isalpha('o'); 15 | _putchar(r + '0'); 16 | r = _isalpha(108); 17 | _putchar(r + '0'); 18 | r = _isalpha(';'); 19 | _putchar(r + '0'); 20 | _putchar('\n'); 21 | return (0); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = print_sign(98); 13 | _putchar(','); 14 | _putchar(' '); 15 | _putchar(r + '0'); 16 | _putchar('\n'); 17 | r = print_sign(0); 18 | _putchar(','); 19 | _putchar(' '); 20 | _putchar(r + '0'); 21 | _putchar('\n'); 22 | r = print_sign(0xff); 23 | _putchar(','); 24 | _putchar(' '); 25 | _putchar(r + '0'); 26 | _putchar('\n'); 27 | r = print_sign(-1); 28 | _putchar(','); 29 | _putchar(' '); 30 | _putchar(r + '0'); 31 | _putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_sign - checks if a char is alpha or not 5 | * 6 | * @n: int parameter to check 7 | * 8 | * Return: 1 if n>0, 0 if n=0, -1 if n<0 9 | */ 10 | int print_sign(int n) 11 | { 12 | if (n > 0) 13 | { 14 | _putchar('+'); 15 | return (1); 16 | } 17 | else if (n < 0) 18 | { 19 | _putchar('-'); 20 | return (-1); 21 | } 22 | else 23 | { 24 | _putchar('0'); 25 | return (0); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _abs - calculates absolute value 3 | * 4 | * @n: integer parameter 5 | * 6 | * Return: absolute value of n 7 | */ 8 | int _abs(int n) 9 | { 10 | if (n < 0) 11 | return (-(n)); 12 | else 13 | return (n); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _abs(-1); 14 | printf("%d\n", r); 15 | r = _abs(0); 16 | printf("%d\n", r); 17 | r = _abs(1); 18 | printf("%d\n", r); 19 | r = _abs(-98); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | print_last_digit(98); 13 | print_last_digit(0); 14 | r = print_last_digit(-1024); 15 | _putchar('0' + r); 16 | _putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_last_digit - prints last digit 4 | * 5 | * @n: integer to get last digit from 6 | * 7 | * Return: the last digit 8 | */ 9 | int print_last_digit(int n) 10 | { 11 | int r; 12 | 13 | r = n % 10; 14 | 15 | if (r <= 0) 16 | r = -1 * r; 17 | _putchar(r + '0'); 18 | return (r); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * jack_bauer - prints 00:00 - 23:59 4 | * 5 | * Return: void (always) 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int hourTens, hourOnes, minutesTens, minutesOnes; 10 | 11 | hourTens = 0; 12 | while (hourTens <= 2) 13 | { 14 | hourOnes = 0; 15 | while (hourOnes <= 9) 16 | { 17 | if (hourOnes > 3 && hourTens == 2) 18 | break; 19 | minutesTens = 0; 20 | while (minutesTens <= 5) 21 | { 22 | minutesOnes = 0; 23 | while (minutesOnes <= 9) 24 | { 25 | _putchar(hourTens + '0'); 26 | _putchar(hourOnes + '0'); 27 | _putchar(':'); 28 | _putchar(minutesTens + '0'); 29 | _putchar(minutesOnes + '0'); 30 | _putchar('\n'); 31 | minutesOnes++; 32 | } 33 | minutesTens++; 34 | } 35 | hourOnes++; 36 | } 37 | hourTens++; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | times_table(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * times_table - prints out 0..9 times table 4 | * 5 | * Return: always void 6 | */ 7 | void times_table(void) 8 | { 9 | int i, n, pTens, pOnes, lineTotal; 10 | 11 | i = 0; 12 | pTens = 0; 13 | pOnes = 0; 14 | while (i < 10) 15 | { 16 | n = 0; 17 | lineTotal = 0; 18 | while (n < 10) 19 | { 20 | lineTotal = n * i; 21 | 22 | pOnes = lineTotal % 10; /* grab last digit */ 23 | pTens = (lineTotal - pOnes) / 10; 24 | /* ^^grab first digit of two digit number^^ */ 25 | if (pTens == 0 && n > 0) 26 | _putchar(' '); 27 | else if (n > 0) 28 | _putchar(pTens + '0'); 29 | _putchar(pOnes + '0'); 30 | if (n == 9) 31 | break; 32 | _putchar(','); 33 | _putchar(' '); 34 | n++; 35 | } 36 | _putchar('\n'); 37 | i++; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # 0x02 Functions, & Nested Loops 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using nested loops as well as functions to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | 8 | - 9 | What are nested loops and how to use them 10 | - What is a function and how do you use functions 11 | - What is the difference between a declaration and a definition of a function 12 | - What is a prototype 13 | - Scope of variables 14 | - What are the `gcc` flags `-Wall -Werror -pedantic -Wextra` 15 | - What are header files and how to to use them with `#include` 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int); 8 | int print_last_digit(int); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int, int); 12 | void print_to_98(int n); 13 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _isupper - check if ch is uppercase 3 | * 4 | * @c: (ch) integer to check 5 | * 6 | * Return: 1 if c is upper, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | if (c >= 65 && c <= 90) 11 | return (1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = 'A'; 14 | printf("%c: %d\n", c, _isupper(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isupper(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _isdigit - checks if int is digit or not 3 | * 4 | * @c: int c, represents a char 5 | * 6 | * Return: 1 if c is digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= 48 && c <= 57) /* ascii 0..9 values */ 11 | return (1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = '0'; 14 | printf("%c: %d\n", c, _isdigit(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isdigit(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_triangle(2); 11 | print_triangle(10); 12 | print_triangle(1); 13 | print_triangle(0); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_triangle - prints a triangle 4 | * 5 | * @size: sizexsize right triangle 6 | * 7 | * Return: always void 8 | */ 9 | void print_triangle(int size) 10 | { 11 | int i, n, spaceC; 12 | 13 | if (size < 1) 14 | { 15 | _putchar('\n'); 16 | return; 17 | } 18 | i = 0; 19 | spaceC = size - 1; 20 | while (i < size) 21 | { 22 | n = 0; 23 | while (n < size) 24 | _putchar((n++ < spaceC) ? ' ' : '#'); 25 | _putchar('\n'); 26 | spaceC--; 27 | i++; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/101-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | print_number(INT_MIN); 12 | _putchar('\n'); 13 | print_number(402); 14 | _putchar('\n'); 15 | print_number(1024); 16 | _putchar('\n'); 17 | print_number(0); 18 | _putchar('\n'); 19 | print_number(-98); 20 | _putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | void print_unsigned_int(unsigned int n); 3 | /** 4 | * print_number - print number n with putchar 5 | * 6 | * @n: number to print 7 | * 8 | * Return: always void 9 | */ 10 | void print_number(int n) 11 | { 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | print_unsigned_int(-(unsigned int)n); 16 | } 17 | else 18 | print_unsigned_int(n); 19 | } 20 | /** 21 | * print_unsigned_int - prints an unsigned integer 22 | * 23 | * @n: an unsigned integer to print 24 | * 25 | * Return: always void 26 | */ 27 | void print_unsigned_int(unsigned int n) 28 | { 29 | if (n / 10 != 0) 30 | { 31 | print_unsigned_int(n / 10); 32 | } 33 | _putchar((n % 10) + '0'); 34 | } 35 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", mul(98, 1024)); 12 | printf("%d\n", mul(-402, 4096)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | /** 2 | * mul - multiply two nums together 3 | * 4 | * @a: integer to multiply by b 5 | * @b: integer to multiply by a 6 | * 7 | * Return: result of a*b 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_numbers - print numbers 0..9\n 4 | * 5 | * Return: always void 6 | */ 7 | void print_numbers(void) 8 | { 9 | int i = 48; /* ascii: 0 */ 10 | 11 | while (i <= 57) /* ascii: 9 */ 12 | { 13 | _putchar(i++); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_most_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_most_numbers - print numbers 0..9\n 4 | * 5 | * Description: skips 2 & 4 6 | * 7 | * Return: always void 8 | */ 9 | void print_most_numbers(void) 10 | { 11 | int i = 48; /* ascii: 0 */ 12 | 13 | while (i <= 57) /* ascii: 9 */ 14 | { 15 | if (i == 50 || i == 52) /* skip 2 & 4 */ 16 | { 17 | i++; 18 | continue; 19 | } 20 | _putchar(i++); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | more_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * more_numbers - print 0..14 x 10 4 | * 5 | * Return: always void 6 | */ 7 | void more_numbers(void) 8 | { 9 | int i, n; 10 | 11 | i = 0; 12 | while (i < 10) 13 | { 14 | n = 0; 15 | while (n <= 14) 16 | { 17 | if (n >= 10) 18 | _putchar((n / 10) + '0'); 19 | _putchar((n % 10) + '0'); 20 | n++; 21 | } 22 | _putchar('\n'); 23 | i++; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * main - check the code for Holberton School students. 4 | * 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | print_line(0); 10 | print_line(2); 11 | print_line(10); 12 | print_line(-4); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_line - prints a line n'_' long 4 | * 5 | * @n: number of times to print '_' 6 | * 7 | * Return: always void 8 | */ 9 | void print_line(int n) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (n > 0 && i < n) 15 | { 16 | _putchar('_'); 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_diagonal(0); 11 | print_diagonal(2); 12 | print_diagonal(10); 13 | print_diagonal(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_diagonal - prints a diagonal line n long 4 | * 5 | * @n: length of line 6 | * 7 | * Return: always void 8 | */ 9 | void print_diagonal(int n) 10 | { 11 | int i, spaceC; 12 | 13 | i = 0; 14 | while (n > 0 && i < n) 15 | { 16 | spaceC = 0; 17 | while (spaceC < i) 18 | { 19 | _putchar(' '); 20 | spaceC++; 21 | } 22 | _putchar('\\'); 23 | _putchar('\n'); 24 | i++; 25 | } 26 | if (n < 1) 27 | _putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_square(2); 11 | print_square(10); 12 | print_square(0); 13 | return (0); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_square - prints an nxn square 4 | * 5 | * @size: length & width of square 6 | * 7 | * Return: always void 8 | */ 9 | void print_square(int size) 10 | { 11 | int i, n; 12 | 13 | i = 0; 14 | while (size > 0 && i < size) 15 | { 16 | n = 0; 17 | while (n < size) 18 | { 19 | _putchar('#'); 20 | n++; 21 | } 22 | _putchar('\n'); 23 | i++; 24 | } 25 | if (size <= 0) 26 | _putchar('\n'); 27 | } 28 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | void fizz_buzz(void); 3 | /** 4 | * main - entry point for function 5 | * 6 | * Return: always 0 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 1; 13 | while (i <= 100) 14 | { 15 | if ((i % 3 == 0) && (i % 5 == 0)) 16 | printf("FizzBuzz"); 17 | else if (i % 3 == 0) 18 | printf("Fizz"); 19 | else if (i % 5 == 0) 20 | printf("Buzz"); 21 | else 22 | printf("%d", i); 23 | if (i == 100) 24 | break; 25 | putchar(' '); 26 | i++; 27 | } 28 | putchar('\n'); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # 0x03. C - More functions, more nested loops 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using more nested loops as well as functions to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What are nested loops and how to use them 8 | - What is a function and how do you use functions 9 | - What is the difference between a declaration and a definition of a function 10 | - What is a prototype 11 | - Scope of variables 12 | - What are the `gcc` flags `-Wall -Werror -pedantic -Wextra` 13 | - What are header files and how to to use them with `#include` 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x03-more_functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _isupper(int c); 3 | int _isdigit(int c); 4 | int mul(int a, int b); 5 | void print_numbers(void); 6 | void print_most_numbers(void); 7 | void more_numbers(void); 8 | void print_line(int n); 9 | void print_diagonal(int n); 10 | void print_square(int size); 11 | void print_triangle(int size); 12 | void print_number(int n); 13 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = 402; 14 | printf("n=%d\n", n); 15 | reset_to_98(&n); 16 | printf("n=%d\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | /** 2 | * reset_to_98 - updates pointer's value to 98 3 | * 4 | * @n: integer pointer to update 5 | * 6 | * Return: always void 7 | */ 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int a; 12 | int b; 13 | 14 | a = 98; 15 | b = 42; 16 | printf("a=%d, b=%d\n", a, b); 17 | swap_int(&a, &b); 18 | printf("a=%d, b=%d\n", a, b); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | /** 2 | * swap_int - swap to integer values 3 | * 4 | * @a: first value to swap 5 | * @b: second value to swap 6 | * 7 | * Return: always void 8 | */ 9 | void swap_int(int *a, int *b) 10 | { 11 | int temp; 12 | 13 | temp = *a; 14 | *a = *b; 15 | *b = temp; 16 | } 17 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _atoi - converts string to integer 4 | * 5 | * @s: string to convert from 6 | * 7 | * Return: integer from conversion 8 | */ 9 | int _atoi(char *s) 10 | { 11 | int sign = 1; 12 | unsigned int total = 0; 13 | char working = 0; 14 | 15 | while (*s) 16 | { 17 | if (*s == '-') 18 | sign = sign * -1; 19 | if (*s >= '0' && *s <= '9') 20 | { 21 | working = 1; 22 | total = total * 10 + *s - '0'; 23 | } 24 | else if (*s < '0' || *s > '9') 25 | { 26 | if (working) 27 | break; 28 | } 29 | s++; 30 | } 31 | if (sign < 0) 32 | total = (-(total)); 33 | return (total); 34 | } 35 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = _atoi("98"); 14 | printf("%d\n", n); 15 | n = _atoi("-402"); 16 | printf("%d\n", n); 17 | n = _atoi(" ------++++++-----+++++--98"); 18 | printf("%d\n", n); 19 | n = _atoi("214748364"); 20 | printf("%d\n", n); 21 | n = _atoi("0"); 22 | printf("%d\n", n); 23 | n = _atoi("Suite 402"); 24 | printf("%d\n", n); 25 | n = _atoi(" + + - -98 Battery Street; San Francisco, CA 94111 - USA "); 26 | printf("%d\n", n); 27 | n = _atoi("---++++ -++ Sui - te - 402 #cisfun :)"); 28 | printf("%d\n", n); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/101-crackme: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x04-pointers_arrays_strings/101-crackme -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *str; 12 | int len; 13 | 14 | str = "Holberton!"; 15 | len = _strlen(str); 16 | printf("%d\n", len); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strlen - gets length of string 3 | * 4 | * @s: string to check length of 5 | * 6 | * Return: length of string as int 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int counter; 11 | 12 | counter = 0; 13 | while (*(s + counter) != '\0') 14 | counter++; 15 | return (counter); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "I do not fear computers. I fear the lack of them - Isaac Asimov"; 13 | _puts(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _puts - puts string to stdout 4 | * 5 | * @str: string to print to stdout 6 | * 7 | * Return: always void 8 | */ 9 | void _puts(char *str) 10 | { 11 | while (*str) 12 | _putchar(*str++); 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "I do not fear computers. I fear the lack of them - Isaac Asimov"; 13 | print_rev(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_rev - prints str in reverse 4 | * 5 | * @s: string to print in reverse 6 | * 7 | * Return: always void 8 | */ 9 | void print_rev(char *s) 10 | { 11 | int counter, strLength; 12 | 13 | counter = 0; 14 | while (*(s + counter) != '\0') 15 | counter++; 16 | strLength = counter; 17 | counter = 0; 18 | while (*(s + counter) != '\0') 19 | _putchar(*(s + (strLength - (counter++ + 1)))); 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[10] = "Holberton"; 12 | 13 | printf("%s\n", s); 14 | rev_string(s); 15 | printf("%s\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | /** 2 | * rev_string - reverses given string 3 | * 4 | * @s: string to reverse 5 | * 6 | * Return: always void 7 | */ 8 | void rev_string(char *s) 9 | { 10 | char *end = s; 11 | char store; 12 | 13 | while (*end) 14 | end++; /* set pos of end pointer to point to last element */ 15 | end = end - 1; /* set end pos to end -1 b/c end is really '\0' */ 16 | while (s < end) /* stop once we get to last address */ 17 | { 18 | store = *s; /* set store ch to s[currentPos] */ 19 | *s++ = *end; /* set position of s = pos end, and increment s */ 20 | *end-- = store; /* set pos of end to stored char and increment */ 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "AlternateString"; 13 | puts2(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * puts2 - prints alternate characters 4 | * 5 | * @str: string to alternate print 6 | * 7 | * Return: always void 8 | */ 9 | void puts2(char *str) 10 | { 11 | int counter = 0; 12 | 13 | while (*str) 14 | { 15 | if (counter++ % 2 == 0) 16 | _putchar(*str); 17 | str++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char *str; 11 | 12 | str = "Print Only Half"; 13 | puts_half(str); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * puts_half - print second half of str 4 | * 5 | * @str: string to cut in half & print 6 | * 7 | * Return: always void 8 | */ 9 | void puts_half(char *str) 10 | { 11 | int length, charsToPrint = 0; 12 | 13 | while (*(str + length) != '\0') 14 | length++; 15 | if (!(length % 2)) /* if uneven number */ 16 | charsToPrint = (length - 1) / 2; 17 | else 18 | charsToPrint = length / 2; 19 | charsToPrint++; 20 | while (charsToPrint < length) 21 | _putchar(*(str + charsToPrint++)); 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int array[5]; 11 | 12 | array[0] = 98; 13 | array[1] = 402; 14 | array[2] = -198; 15 | array[3] = 298; 16 | array[4] = -1024; 17 | print_array(array, 5); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * print_array - prints an array of ints 4 | * 5 | * @a: array of integers to print 6 | * @n: size of array 7 | * 8 | * Return: always void 9 | */ 10 | void print_array(int *a, int n) 11 | { 12 | int counter = 0; 13 | 14 | while (counter < n) 15 | { 16 | if (counter + 1 != n) 17 | printf("%d, ", a[counter++]); 18 | else 19 | printf("%d", a[counter++]); 20 | } 21 | putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/9-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98]; 12 | char *p; 13 | 14 | p = _strcpy(s1, "First, solve the problem. Then, write the code\n"); 15 | printf("%s", s1); 16 | printf("%s", p); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strcpy - copies string from src to dest 3 | * 4 | * @dest: pointer to destination of string 5 | * @src: pointer to source string to copy from 6 | * 7 | * Return: pointer to dest 8 | */ 9 | char *_strcpy(char *dest, char *src) 10 | { 11 | char *temp = dest; 12 | 13 | while (*src) 14 | *dest++ = *src++; 15 | return (temp); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # 0x04-pointers, arrays, & strings 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using pointers, arrays, and strings to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What are pointers and how to use them 8 | - What are arrays and how to use them 9 | - What are the differences between pointers and arrays 10 | - How to use strings and how to manipulate them 11 | - Scope of variables 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /0x04-pointers_arrays_strings/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 | void reset_to_98(int *n); 14 | void swap_int(int *a, int *b); 15 | int _strlen(char *s); 16 | void _puts(char *str); 17 | void print_rev(char *s); 18 | void rev_string(char *s); 19 | void puts2(char *str); 20 | void puts_half(char *str); 21 | void print_array(int *a, int n); 22 | char *_strcpy(char *dest, char*src); 23 | int _atoi(char *s); 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *p; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | p = _strcat(s1, s2); 18 | printf("%s", s1); 19 | printf("%s", s2); 20 | printf("%s", p); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strcat - concatanates two strings 3 | * @dest: destination string 4 | * @src: string to add to end of dest 5 | * 6 | * Return: char pointer to dest 7 | */ 8 | char *_strcat(char *dest, char *src) 9 | { 10 | int srcLength = 0; 11 | int counter = 0; 12 | char *temp = dest; 13 | char *tempSource = src; 14 | 15 | while (*dest) 16 | dest++; 17 | while (*src++) 18 | srcLength++; 19 | while (counter < srcLength) 20 | { 21 | *dest++ = *tempSource++; 22 | counter++; 23 | } 24 | *dest = '\0'; 25 | return (temp); 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *p; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | p = _strncat(s1, s2, 1); 18 | printf("%s\n", s1); 19 | printf("%s", s2); 20 | printf("%s\n", p); 21 | p = _strncat(s1, s2, 1024); 22 | printf("%s", s1); 23 | printf("%s", s2); 24 | printf("%s", p); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strncat - concatanates two strings 3 | * 4 | * @dest: destination string 5 | * @src: string to add to end of dest 6 | * @n: num of bytes to take from src 7 | * 8 | * Description: only takes n bytes from src 9 | * 10 | * Return: char pointer to dest 11 | */ 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int counter = 0, srcLength = 0; 15 | char *temp = dest; 16 | 17 | while (*(src + srcLength) != '\0') 18 | srcLength++; 19 | while (*dest) 20 | dest++; 21 | if (n > srcLength) 22 | n = srcLength; 23 | while (counter < n) 24 | { 25 | *dest++ = *src++; 26 | counter++; 27 | } 28 | *dest = '\0'; 29 | return (temp); 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_number(98); 11 | _putchar('\n'); 12 | print_number(402); 13 | _putchar('\n'); 14 | print_number(1024); 15 | _putchar('\n'); 16 | print_number(0); 17 | _putchar('\n'); 18 | print_number(-98); 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | void print_unsigned_int(unsigned int n); 3 | /** 4 | * print_number - print number n with putchar 5 | * 6 | * @n: number to print 7 | * 8 | * Return: always void 9 | */ 10 | void print_number(int n) 11 | { 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | print_unsigned_int(-(unsigned int)n); 16 | } 17 | else 18 | print_unsigned_int(n); 19 | } 20 | /** 21 | * print_unsigned_int - prints an unsigned integer 22 | * 23 | * @n: an unsigned integer to print 24 | * 25 | * Return: always void 26 | */ 27 | void print_unsigned_int(unsigned int n) 28 | { 29 | if (n / 10 != 0) 30 | { 31 | print_unsigned_int(n / 10); 32 | } 33 | _putchar((n % 10) + '0'); 34 | } 35 | -------------------------------------------------------------------------------- /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 + 3) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/103-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char buffer[] = "This is a string!\0And this is the rest of the #buffer :)\1\2\3\4\5\6\7#cisfun\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x20\x21\x34\x56#pointersarefun #infernumisfun\n"; 12 | printf("%s\n", buffer); 13 | printf("---------------------------------\n"); 14 | print_buffer(buffer, sizeof(buffer)); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strncpy - copies string, up to n bytes 3 | * 4 | * @dest: destination str to copy into 5 | * @src: string to copy to dest 6 | * @n: number of bytes to copy from src 7 | * 8 | * Return: char pointer to beginning of dest 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | char *temp = dest; 13 | int counter = 0, srcLength = 0; 14 | 15 | if (n <= 0) 16 | return (dest); 17 | while (src[srcLength] != '\0') 18 | srcLength++; 19 | srcLength++; 20 | if (n > srcLength) 21 | n = srcLength; 22 | 23 | while (counter < n) 24 | { 25 | dest[counter] = src[counter]; 26 | counter++; 27 | } 28 | return (temp); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | #include 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char s1[] = "Hel"; 13 | char s2[] = "Wo"; 14 | 15 | printf("%d\n", strcmp(s1, s2)); 16 | printf("%d\n", strcmp(s2, s1)); 17 | printf("%d\n", strcmp(s1, s1)); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | /** 2 | * reverse_array - reverses an array 3 | * 4 | * @a: an array of ints 5 | * @n: size of array 6 | * 7 | * Return: always void 8 | */ 9 | void reverse_array(int *a, int n) 10 | { 11 | int start = 0; 12 | int store; 13 | 14 | while (start < n - 1) 15 | { 16 | store = a[n - 1]; 17 | a[n - 1] = a[start]; 18 | a[start] = store; 19 | n--; 20 | start++; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Holberton School!\n"; 12 | char *p; 13 | 14 | p = string_toupper(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | /** 2 | * string_toupper - converts string to upper case 3 | * 4 | * @str: string to convert 5 | * 6 | * Return: char pointer to converted string 7 | */ 8 | char *string_toupper(char *str) 9 | { 10 | char *start = str; 11 | 12 | while (*str) 13 | { 14 | if (*str >= 'a' && *str <= 'z') 15 | *str -= 32; 16 | str++; 17 | } 18 | return (start); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\n"; 12 | char *p; 13 | 14 | p = leet(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher.\n"; 12 | char *p; 13 | 14 | p = rot13(s); 15 | printf("%s", p); 16 | printf("------------------------------------\n"); 17 | printf("%s", s); 18 | printf("------------------------------------\n"); 19 | p = rot13(s); 20 | printf("%s", p); 21 | printf("------------------------------------\n"); 22 | printf("%s", s); 23 | printf("------------------------------------\n"); 24 | p = rot13(s); 25 | printf("%s", p); 26 | printf("------------------------------------\n"); 27 | printf("%s", s); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # 0x05- more pointers, arrays, & strings 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using pointers, arrays, and strings to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What are pointers and how to use them 8 | - What are arrays and how to use them 9 | - What are the differences between pointers and arrays 10 | - How to use strings and how to manipulate them 11 | - Scope of variables 12 | - Build our own print buffer 13 | - Build our own string addition into buffer 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/#crackme#: -------------------------------------------------------------------------------- 1 | :help 2 | 3 | 4 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _memset - fills n bytes of memory 3 | * 4 | * @s: pointer to start of memory area 5 | * @b: constant byte to fill with 6 | * @n: num bytes to fill in 7 | * 8 | * Return: pointer to beginning of memory area s 9 | */ 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | unsigned int i = 0; 13 | char *start = s; 14 | 15 | while (i < n) 16 | { 17 | *s++ = b; 18 | i++; 19 | } 20 | return (start); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _memcpy - copys n bytes of memory 3 | * 4 | * @dest: pointer to dest str 5 | * @src: source to be copied from 6 | * @n: num bytes to copy from src 7 | * 8 | * Return: pointer to beginning of memory area dest 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i = 0; 13 | char *start = dest; 14 | 15 | while (i < n) 16 | { 17 | *dest++ = *src++; 18 | i++; 19 | } 20 | return (start); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101result: -------------------------------------------------------------------------------- 1 | abc123 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello"; 12 | char *f; 13 | 14 | f = _strchr(s, '\0'); 15 | printf("%s\n", f); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _strchr - finds if char occurs in string 4 | * 5 | * @s: string to check 6 | * @c: character to find in s 7 | * 8 | * Return: pointer to first instance of c, otherwise NULL 9 | */ 10 | char *_strchr(char *s, char c) 11 | { 12 | int i = 0; 13 | 14 | while (*(s + i)) 15 | { 16 | if (*(s + i) == c) 17 | return (s + i); 18 | i++; 19 | } 20 | if (*(s + i) == c) 21 | return (s + i); 22 | return (NULL); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "oleh"; 13 | unsigned int n; 14 | 15 | n = _strspn(s, f); 16 | printf("%u\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strspn - finds length of prefix substring 3 | * 4 | * @s: string to check 5 | * @accept: substring of chars 6 | * 7 | * Return: n bytes in intial segment of s which 8 | * consist only of bytes from accept 9 | */ 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | int i; 13 | int consistC = 0; 14 | int prevC; 15 | 16 | while (*s) 17 | { 18 | i = 0; 19 | prevC = consistC; 20 | while (*(accept + i) != '\0') 21 | { 22 | if (*(accept + i) == *s) 23 | consistC++; 24 | i++; 25 | } 26 | if (prevC == consistC) /* didn't equal a char from accept */ 27 | break; 28 | s++; 29 | } 30 | return (consistC); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "world"; 13 | char *t; 14 | 15 | t = _strpbrk(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strpbrk - searches string for set of bytes 3 | * 4 | * @s: string to check 5 | * @accept: substring of chars 6 | * 7 | * Return: pointer to first occurance 8 | */ 9 | char *_strpbrk(char *s, char *accept) 10 | { 11 | int i; 12 | 13 | while (*s) 14 | { 15 | i = 0; 16 | while (*(accept + i) != '\0') 17 | { 18 | if (*(accept + i) == *s) 19 | return (s); 20 | i++; 21 | } 22 | s++; 23 | } 24 | return (0); /* didn't find an occurance */ 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "First, solve the problem. Then, write the code."; 12 | char *f = ""; 13 | char *t; 14 | 15 | t = _strstr(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char board[8][8] = { 12 | {'r', 'k', 'b', 'q', 'k', 'b', 'k', 'r'}, 13 | {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, 14 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 15 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 16 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 17 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 18 | {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, 19 | {'R', 'K', 'B', 'Q', 'K', 'B', 'K', 'R'}, 20 | }; 21 | print_chessboard(board); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_chessboard - prints out a chessboard 4 | * 5 | * @a: chessboard to print, 2D array [8] columns 6 | * 7 | * Return: always void 8 | */ 9 | void print_chessboard(char (*a)[8]) 10 | { 11 | int subC, primC = 0; 12 | 13 | while (primC < 8) 14 | { 15 | subC = 0; 16 | while (subC < 8) 17 | _putchar(a[primC][subC++]); 18 | _putchar('\n'); 19 | primC++; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int c3[3][3] = { 12 | {0, 1, 5}, 13 | {10, 11, 12}, 14 | {1000, 101, 102}, 15 | }; 16 | int c5[5][5] = { 17 | {0, 1, 5, 12124, 1234}, 18 | {10, 11, 12, 123521, 12512}, 19 | {1000, 101, 102, 12545, 214543435}, 20 | {100, 1012451, 11102, 12545, 214543435}, 21 | {10, 12401, 10452, 11542545, 1214543435}, 22 | }; 23 | print_diagsums((int *)c3, 3); 24 | print_diagsums((int *)c5, 5); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * print_diagsums - prints diagonal sums of square matrix 4 | * 5 | * @a: square matrix of integers 6 | * @size: col & row size of matrix 7 | * 8 | * Return: always void 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int primC, sum1, sum2, diagC1, diagC2, row; 13 | 14 | primC = 0, sum1 = 0, sum2 = 0, diagC1 = 0, diagC2 = 0, row = 0; 15 | while (primC < size * size) 16 | { 17 | if (primC == diagC1 + (row * size)) 18 | sum1 += a[primC]; 19 | if (primC == (size - diagC2 - 1) + (row * size)) 20 | sum2 += a[primC]; 21 | if (((primC + 1) % size) == 0) 22 | row++, diagC1++, diagC2++; 23 | primC++; 24 | } 25 | printf("%d, ", sum1); 26 | printf("%d\n", sum2); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/9-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s0 = "Anne Cognet"; 12 | char *s1 = "Asaia Palacios"; 13 | 14 | printf("%s, %s\n", s0, s1); 15 | set_string(&s1, s0); 16 | printf("%s, %s\n", s0, s1); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/9-set_string.c: -------------------------------------------------------------------------------- 1 | /** 2 | * set_string - sets a pointer to a string 3 | * 4 | * @s: pointer to pointer of type char 5 | * @to: pointer to beginning of string to set 6 | * 7 | * Return: always void 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # 0x06- Even more pointers, arrays, & strings 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using pointers, arrays, and strings to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What are pointers to pointers and how to use them 8 | - What are multidimensional arrays and how to use them 9 | - What are the most common C standard library functions to manipulate strings 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x06-pointers_arrays_strings/a.out -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/crackme2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x06-pointers_arrays_strings/crackme2 -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/hexParser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x06-pointers_arrays_strings/hexParser -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/hexParser.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | FILE *pF; 6 | char buff[1000]; 7 | 8 | pF = fopen("hexToParse", "r"); 9 | if (!pF) 10 | return (-1); 11 | while (fgets(buff, 1000, pF) != NULL) {} 12 | fclose(pF); 13 | printf("%s", buff); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x07-recursion/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _puts_recursion("Betty Holberton"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x07-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _puts_recursion - puts a string to stdout recursively 4 | * 5 | * @s: string to put to stdout 6 | * 7 | * Return: always void 8 | */ 9 | void _puts_recursion(char *s) 10 | { 11 | if (!*s) /* if s points to null char */ 12 | { 13 | _putchar('\n'); 14 | return; 15 | } 16 | _putchar(*s); 17 | _puts_recursion(s + 1); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-recursion/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _print_rev_recursion("\nColton Walker"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x07-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _print_rev_recursion - puts a string to stdout in reverse 4 | * 5 | * @s: string to put to stdout 6 | * 7 | * Return: always void 8 | */ 9 | void _print_rev_recursion(char *s) 10 | { 11 | if (!*s) /* if s points to null char */ 12 | return; 13 | _print_rev_recursion(s + 1); 14 | _putchar(*s); 15 | } 16 | -------------------------------------------------------------------------------- /0x07-recursion/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = _strlen_recursion("Corbin Coleman"); 14 | printf("%d\n", n); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x07-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strlen_recursion - gets strlen of s via recursive algorithm 3 | * 4 | * @s: string to check length of 5 | * 6 | * Return: int containing length of string 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (!*s) 11 | return (0); 12 | return (1 + _strlen_recursion(s + 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x07-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | /** 2 | * factorial - returns factorial of n using recursion 3 | * 4 | * @n: int paramater to return factorial of 5 | * 6 | * Return: factorial of n 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n < 0) /* error */ 11 | return (-1); 12 | if (n <= 1) /* base case */ 13 | return (1); 14 | return (n * factorial(n - 1)); 15 | } 16 | -------------------------------------------------------------------------------- /0x07-recursion/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = factorial(1); 14 | printf("%d\n", r); 15 | r = factorial(5); 16 | printf("%d\n", r); 17 | r = factorial(10); 18 | printf("%d\n", r); 19 | r = factorial(-1024); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-recursion/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _pow_recursion(1, 10); 14 | printf("%d\n", r); 15 | r = _pow_recursion(1024, 0); 16 | printf("%d\n", r); 17 | r = _pow_recursion(2, 16); 18 | printf("%d\n", r); 19 | r = _pow_recursion(5, 2); 20 | printf("%d\n", r); 21 | r = _pow_recursion(5, -2); 22 | printf("%d\n", r); 23 | r = _pow_recursion(-5, 3); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _pow_recursion - returns val of x^^y 3 | * 4 | * @x: value to raise to power of y 5 | * @y: power to exponent x 6 | * 7 | * Return: int of x^^y 8 | */ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) /* error case */ 12 | return (-1); 13 | if (y == 0) /* edge case */ 14 | return (1); 15 | if (y == 1) /* base case */ 16 | return (x); 17 | return (x *= _pow_recursion(x, y - 1)); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-recursion/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _sqrt_recursion(0); 14 | printf("%d\n", r); 15 | r = _sqrt_recursion(1024); 16 | printf("%d\n", r); 17 | r = _sqrt_recursion(16); 18 | printf("%d\n", r); 19 | r = _sqrt_recursion(17); 20 | printf("%d\n", r); 21 | r = _sqrt_recursion(25); 22 | printf("%d\n", r); 23 | r = _sqrt_recursion(-1); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | int _find_sqrt(int prev, int find); 2 | /** 3 | * _sqrt_recursion - finds square root of n, recursively 4 | * 5 | * @n: int to find sqroot from 6 | * 7 | * Return: sqroot, -1 if no natural sqroot for n 8 | */ 9 | int _sqrt_recursion(int n) 10 | { 11 | if (n <= 0) /* error case */ 12 | return (-1); 13 | return (_find_sqrt(1, n)); 14 | } 15 | /** 16 | * _find_sqrt - finds square root recursively, needs prev param 17 | * 18 | * @prev: previous result of function 19 | * @find: constant int to find sq root for 20 | * 21 | * Return: square root of find, or -1 if not found 22 | */ 23 | int _find_sqrt(int prev, int find) 24 | { 25 | if (prev > find) /* didn't find a nr, there was remainder */ 26 | return (-1); 27 | if (prev * prev == find) /* natural root found */ 28 | return (prev); 29 | return (_find_sqrt(prev + 1, find)); /* not super efficient */ 30 | } 31 | -------------------------------------------------------------------------------- /0x07-recursion/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_prime_number(1); 14 | printf("%d\n", r); 15 | r = is_prime_number(1024); 16 | printf("%d\n", r); 17 | r = is_prime_number(16); 18 | printf("%d\n", r); 19 | r = is_prime_number(17); 20 | printf("%d\n", r); 21 | r = is_prime_number(25); 22 | printf("%d\n", r); 23 | r = is_prime_number(-1); 24 | printf("%d\n", r); 25 | r = is_prime_number(113); 26 | printf("%d\n", r); 27 | r = is_prime_number(7919); 28 | printf("%d\n", r); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x07-recursion/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_palindrome("level"); 14 | printf("%d\n", r); 15 | r = is_palindrome("redder"); 16 | printf("%d\n", r); 17 | r = is_palindrome("holberton"); 18 | printf("%d\n", r); 19 | r = is_palindrome("step on no pets"); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-recursion/README.md: -------------------------------------------------------------------------------- 1 | # 0x07-C-Recursion 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using recursion to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What is recursion and how to use it to implement various algorithms 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /0x07-recursion/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x08-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x08- C - Static Libraries 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using static libraries to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What is a static library, how does it work, how to create one, and how to use it 8 | - Basic usage of `ar`, `ranlib`, `nm` 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /0x08-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c *.c 3 | ar rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x08-static_libraries/libholberton.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x08-static_libraries/libholberton.a -------------------------------------------------------------------------------- /0x09-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program 4 | * 5 | * @argc: count of args present 6 | * @argv: array of char * pointing to args 7 | * 8 | * Return: always 0 (success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | if (argc < 1) 13 | return (-1); 14 | printf("%s\n", argv[0]); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program 4 | * 5 | * @argc: count of args present 6 | * @argv: array of char * pointing to args 7 | * 8 | * Return: always 0 (success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | if (!*argv) 13 | return (-1); 14 | printf("%d\n", argc - 1); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program 4 | * 5 | * @argc: count of args present 6 | * @argv: array of char * pointing to args 7 | * 8 | * Return: always 0 (success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < argc; i++) 15 | printf("%s\n", argv[i]); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - entry point for program 5 | * 6 | * @argc: count of args present 7 | * @argv: array of char * pointing to args 8 | * 9 | * Return: always 0 (success) 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, product = 1; 14 | 15 | if (argc != 3) 16 | { 17 | printf("Error\n"); 18 | return (1); 19 | } 20 | for (i = 1; i < argc; i++) 21 | product *= atoi(argv[i]); 22 | printf("%d\n", product); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x09-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - entry point for program 5 | * 6 | * @argc: count of args present 7 | * @argv: array of char * pointing to args 8 | * 9 | * Return: always 0 (success) 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, sum = 0; 14 | 15 | for (i = 1; i < argc; i++) 16 | { 17 | if (!atoi(argv[i])) /* non numeric */ 18 | { 19 | printf("Error\n"); 20 | return (1); 21 | } 22 | sum += atoi(argv[i]); 23 | } 24 | printf("%d\n", sum); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # 0x09- C - argC and argV 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using argC and argV to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - How to use arguments passed to your program 8 | - What are two prototypes of `main` that you know of, and in which case do you use one or the other 9 | - How to use `__attribute__((unused))` or `(void)` to compile functions with unused variables or parameters 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /0x0A-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * create_array - creates an array in memory and initializes it 4 | * 5 | * @size: unsigned int, size of array to be created 6 | * @c: char to fill array with upon initialization 7 | * 8 | * Return: pointer to beginning of array 9 | */ 10 | char *create_array(unsigned int size, char c) 11 | { 12 | char *arr; 13 | unsigned int i = 0; 14 | 15 | if (size <= 0) 16 | return (NULL); 17 | arr = malloc(sizeof(char) * size); 18 | if (arr == NULL) 19 | return (NULL); 20 | while (i < size) 21 | { 22 | *(arr + i) = c; 23 | i++; 24 | } 25 | return (arr); 26 | } 27 | -------------------------------------------------------------------------------- /0x0A-malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = _strdup("Holberton"); 15 | if (s == NULL) 16 | { 17 | printf("failed to allocate memory\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0A-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _strdup - duplicates a string in memory 4 | * 5 | * @str: string to duplicate 6 | * 7 | * Return: pointer to beginning of duplicated string 8 | */ 9 | char *_strdup(char *str) 10 | { 11 | char *dup, *strT; 12 | char *dupStart; 13 | int strSize = 0; 14 | 15 | if (str == NULL) /* null string */ 16 | return (NULL); 17 | strT = str; 18 | while (*(str + strSize)) 19 | strSize++; 20 | dup = malloc(sizeof(*str) * (strSize + 1)); 21 | if (dup == NULL) 22 | return (NULL); 23 | dupStart = dup; 24 | while (*strT) 25 | *dup++ = *strT++; 26 | *dup = '\0'; /* null byte */ 27 | return (dupStart); 28 | } 29 | -------------------------------------------------------------------------------- /0x0A-malloc_free/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = str_concat("Betty ", "Holberton"); 15 | if (s == NULL) 16 | { 17 | printf("failed\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0A-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * str_concat - concatenates two strings, makes new string 5 | * 6 | * @s1: string 1 to concatenate with string 2 7 | * @s2: string 2 to concatenate with string 1 8 | * 9 | * Return: pointer to new string, or NULL on failure 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | int s1Size = 0, s2Size = 0; 14 | char *conc, *concStart; 15 | 16 | if (s1 == NULL) 17 | s1 = ""; 18 | if (s2 == NULL) 19 | s2 = ""; 20 | while (*(s1 + s1Size)) 21 | s1Size++; 22 | while (*(s2 + s2Size)) 23 | s2Size++; 24 | conc = malloc(sizeof(char) * (s1Size + s2Size + 1)); 25 | if (conc == NULL) 26 | return (NULL); 27 | concStart = conc; 28 | while (*s1) 29 | *conc++ = *s1++; 30 | while (*s2) 31 | *conc++ = *s2++; 32 | *conc = '\0'; 33 | return (concStart); 34 | } 35 | -------------------------------------------------------------------------------- /0x0A-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * alloc_grid - allocates memory for a 2D array of ints 4 | * 5 | * @width: number of columns for 2D array 6 | * @height: number of rows for 2D array 7 | * 8 | * Return: pointer to a 2D array, NULL on error 9 | */ 10 | int **alloc_grid(int width, int height) 11 | { 12 | int **arr, i = 0, n = 0; 13 | 14 | if (width <= 0 || height <= 0) 15 | return (NULL); 16 | arr = malloc(sizeof(int *) * height); 17 | if (arr == NULL) 18 | return (NULL); 19 | while (i < height) 20 | { 21 | arr[i] = malloc(sizeof(int) * width); 22 | if (arr[i] == NULL) 23 | { 24 | while (i >= 0) 25 | { 26 | i--; 27 | free(arr[i]); 28 | } 29 | free(arr); 30 | return (NULL); 31 | } 32 | i++; 33 | } 34 | for (i = 0; i < height; i++) 35 | for (n = 0; n < width; n++) 36 | arr[i][n] = 0; 37 | return (arr); 38 | } 39 | -------------------------------------------------------------------------------- /0x0A-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * free_grid - frees a 2D array of ints previously malloc'ed 4 | * 5 | * @grid: 2D array of ints to free 6 | * @height: height of grid 7 | * 8 | * Return: always void 9 | */ 10 | void free_grid(int **grid, int height) 11 | { 12 | int i = 0; 13 | 14 | if (grid == NULL) 15 | return; 16 | while (i < height) 17 | free((int *)grid[i++]); 18 | free(grid); 19 | } 20 | -------------------------------------------------------------------------------- /0x0A-malloc_free/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char *av[]) 11 | { 12 | char *s; 13 | 14 | s = argstostr(ac, av); 15 | if (s == NULL) 16 | { 17 | return (1); 18 | } 19 | printf("%s", s); 20 | free(s); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x0A-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0A - C - Using Malloc() and Free() 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using malloc and free to understand memory allocation and to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What is the difference between automatic and dynamic allocation 8 | - What is `malloc` and `free` and how to use them 9 | - Why and when use `malloc` 10 | - How to use `valgrind` to check for memory leak 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * malloc_checked - performs a malloc, if failed exits 4 | * 5 | * @b: number of bytes to allocate 6 | * 7 | * Return: pointer to beginning of new memory 8 | */ 9 | void *malloc_checked(unsigned int b) 10 | { 11 | void *ret; 12 | 13 | ret = malloc(b); 14 | if (ret == NULL) 15 | exit(98); 16 | return (ret); 17 | } 18 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *concat; 13 | 14 | concat = string_nconcat("Holberton ", "School !!!", 6); 15 | printf("%s\n", concat); 16 | free(concat); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0B-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0B - C - More Malloc() and Free() 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using malloc, realloc, callalloc, exit, and free to understand memory allocation and to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - How to use the `exit` function 8 | - What are the functions `calloc` and `realloc` from the standard library and how to use them 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /0x0C-preprocessor/0-main.c: -------------------------------------------------------------------------------- 1 | #include "0-object_like_macro.h" 2 | #include "0-object_like_macro.h" 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = 98 + SIZE; 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0C-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _OBJECT_LIKE_MACRO_H_ 2 | #define _OBJECT_LIKE_MACRO_H_ 3 | #define SIZE 1024 4 | #endif /* _OBJECT_LIKE_MACRO_H_ */ 5 | -------------------------------------------------------------------------------- /0x0C-preprocessor/1-main.c: -------------------------------------------------------------------------------- 1 | #include "1-pi.h" 2 | #include "1-pi.h" 3 | #include 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | float a; 13 | float r; 14 | 15 | r = 98; 16 | a = PI * r * r; 17 | printf("%.3f\n", a); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef _PI_H 2 | #define _PI_H 3 | #define PI 3.14159265359 4 | #endif /* _PI_H */ 5 | -------------------------------------------------------------------------------- /0x0C-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints file name of file 4 | * 5 | * Return: always 0 6 | */ 7 | int main(void) 8 | { 9 | printf("%s\n", __FILE__); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x0C-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNC_LIKE_MACRO_H 2 | #define _FUNC_LIKE_MACRO_H 3 | #define ABS(x) (((x) < 0) ? -(x) : (x)) 4 | #endif /* _FUNC_LIKE_MACRO_H */ 5 | -------------------------------------------------------------------------------- /0x0C-preprocessor/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "3-function_like_macro.h" 3 | #include "3-function_like_macro.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = ABS(-98) * 10; 16 | j = ABS(98) * 10; 17 | printf("%d, %d\n", i, j); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-preprocessor/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "4-sum.h" 3 | #include "4-sum.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = SUM(98, 1024); 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0C-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUM_H_ 2 | #define _SUM_H_ 3 | #define SUM(x, y) ((x) + (y)) 4 | #endif /* _SUM_H_ */ 5 | -------------------------------------------------------------------------------- /0x0C-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # 0x0C. C - Preprocessor 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using macros to understand the preprocessing step and to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What are macros and how to use them 8 | - What are the most common predefined macros 9 | - How to include guard your header files 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Django"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Jay"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #define NULL (void *)0 3 | /** 4 | * init_dog - initializes dog structure 5 | * 6 | * @d: struct to initialize 7 | * @name: dog's name 8 | * @age: dog's age 9 | * @owner: dog's owner 10 | * 11 | * Return: always void 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 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | init_dog(&my_dog, "Django", 3.5, "Jay"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Django"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Jay"; 16 | print_dog(&my_dog); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | #define NILL "(nil)" 4 | /** 5 | * print_dog - prints contents of struct dog 6 | * 7 | * @d: pointer to struct of type struct dog 8 | * 9 | * Return: always void 10 | */ 11 | void print_dog(struct dog *d) 12 | { 13 | if (d == NULL) 14 | return; 15 | if (d->name == NULL) 16 | d->name = NILL; 17 | if (d->owner == NULL) 18 | d->owner = NILL; 19 | printf("Name: %s\nAge: %0.6f\nOwner: %s\n", d->name, d->age, d->owner); 20 | } 21 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t my_dog; 12 | 13 | my_dog.name = "Django"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Jay"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Django", 3.5, "Jay"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * free_dog - frees a dog structure 5 | * 6 | * @d: pointer to struct to free 7 | * 8 | * Return: always void 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d == NULL) 13 | return; 14 | if (d->name != NULL) 15 | free(d->name); 16 | if (d->owner != NULL) 17 | free(d->owner); 18 | free(d); 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # 0x0D. C - Structures, typedef 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using structures and typedef to understand more about header files, data structures, types, and to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What are structures, when, why and how to use them 8 | - How to use `typedef` 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /0x0D-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOG_H_ 2 | #define _DOG_H_ 3 | /** 4 | * struct dog - structure for a basic dog 5 | * @name: name of dog 6 | * @age: age of dog 7 | * @owner: owner of dog 8 | * 9 | * Description: data structure for dog 10 | */ 11 | struct dog 12 | { 13 | char *name; 14 | float age; 15 | char *owner; 16 | }; 17 | typedef struct dog dog_t; 18 | void init_dog(struct dog *d, char *name, float age, char *owner); 19 | void print_dog(struct dog *d); 20 | dog_t *new_dog(char *name, float age, char *owner); 21 | void free_dog(dog_t *d); 22 | #endif /* _DOG_H_ */ 23 | -------------------------------------------------------------------------------- /0x0E-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * print_name - prints a name given a function 4 | * 5 | * @name: pointer to name to print 6 | * @f: function to print name 7 | * 8 | * Return: always void 9 | */ 10 | void print_name(char *name, void (*f)(char *name)) 11 | { 12 | if (name == NULL || f == NULL) 13 | return; 14 | f(name); 15 | } 16 | -------------------------------------------------------------------------------- /0x0E-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * array_iterator - iterates over an array and performs an action on it 4 | * @array: array to iterate through 5 | * @size: number of elements of array 6 | * @action: action (function) to perform on array elements 7 | * 8 | * Return: always void 9 | */ 10 | void array_iterator(int *array, size_t size, void (*action)(int param)) 11 | { 12 | unsigned int i = 0; 13 | 14 | if (array == NULL || action == NULL) 15 | return; 16 | while (i < size) 17 | { 18 | action(array[i]); 19 | i++; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x0E-function_pointers/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_elem - prints an integer 6 | * @elem: the integer to print 7 | * 8 | * Return: Nothing. 9 | */ 10 | void print_elem(int elem) 11 | { 12 | printf("%d\n", elem); 13 | } 14 | 15 | /** 16 | * print_elem_hex - prints an integer, in hexadecimal 17 | * @elem: the integer to print 18 | * 19 | * Return: Nothing. 20 | */ 21 | void print_elem_hex(int elem) 22 | { 23 | printf("0x%x\n", elem); 24 | } 25 | 26 | /** 27 | * main - check the code for Holberton School students. 28 | * 29 | * Return: Always 0. 30 | */ 31 | int main(void) 32 | { 33 | int array[5] = {0, 98, 402, 1024, 4096}; 34 | 35 | array_iterator(array, 5, &print_elem); 36 | array_iterator(array, 5, &print_elem_hex); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0E-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - entry point for program 5 | * @argc: arg counter, number of args 6 | * @argv: array of pointers to args, in order given 7 | * 8 | * Return: 0 on success, 1 if argc not right, 2 if bytes is negative 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int (*f)(int, char**); 13 | int i = 0; 14 | int hexByte; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | exit(1); 20 | } 21 | else if (atoi(argv[1]) < 0) 22 | { 23 | printf("Error\n"); 24 | exit(2); 25 | } 26 | f = main; 27 | while (i < atoi(argv[1])) 28 | { 29 | hexByte = *(unsigned char *)(f + i); 30 | printf("%.2x", hexByte); 31 | if (i < atoi(argv[1]) - 1) 32 | putchar(' '); 33 | i++; 34 | } 35 | putchar('\n'); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x0E-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * int_index - gets the index of an array element 4 | * @array: array to search through 5 | * @size: size of array (num of elements) 6 | * @cmp: comparison function to determine if element found 7 | * 8 | * Return: integer representing index of element found in array 9 | */ 10 | int int_index(int *array, int size, int (*cmp)(int)) 11 | { 12 | int i = 0; 13 | 14 | if (size <= 0) 15 | return (-1); 16 | if (array == NULL || cmp == NULL) 17 | return (-1); 18 | while (i < size) 19 | { 20 | if (cmp(array[i])) 21 | return (i); 22 | i++; 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H_ 2 | #define _CALC_H_ 3 | /** 4 | * struct op - Struct op 5 | * 6 | * @op: The operator 7 | * @f: The function associated 8 | */ 9 | typedef struct op 10 | { 11 | char *op; 12 | int (*f)(int a, int b); 13 | } op_t; 14 | int op_add(int a, int b); 15 | int op_sub(int a, int b); 16 | int op_mul(int a, int b); 17 | int op_div(int a, int b); 18 | int op_mod(int a, int b); 19 | int (*get_op_func(char *s))(int, int); 20 | #endif /* _CALC_H_ */ 21 | -------------------------------------------------------------------------------- /0x0E-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | /** 4 | * get_op_func - gets the correct function for an operator 5 | * @s: operator to get correct function for 6 | * 7 | * Return: NULL if no match found, otherwise pointer to correct func 8 | */ 9 | int (*get_op_func(char *s))(int, int) 10 | { 11 | op_t ops[] = { 12 | {"+", op_add}, 13 | {"-", op_sub}, 14 | {"*", op_mul}, 15 | {"/", op_div}, 16 | {"%", op_mod}, 17 | {NULL, NULL} 18 | }; 19 | int i = 0; 20 | 21 | while (ops[i].op != NULL) 22 | { 23 | if (*(ops[i].op) == *s) /* found correct operator */ 24 | { 25 | return (ops[i].f); 26 | } 27 | i++; 28 | } 29 | return (NULL); /* no match found */ 30 | } 31 | -------------------------------------------------------------------------------- /0x0E-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # 0x0E. C - Function pointers 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using pointers to functions, and arrays of pointers to functions to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What are function pointers and how to use them 8 | - What does a function pointer exactly hold 9 | - Where does a function pointer point to in the virtual memory 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /0x0E-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNC_POINTERS_ 2 | #define _FUNC_POINTERS_ 3 | void print_name(char *name, void (*f)(char *)); 4 | void array_iterator(int *array, size_t size, void (*action)(int)); 5 | int int_index(int *array, int size, int (*cmp)(int)); 6 | #endif /* _FUNC_POINTERS_ */ 7 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | 13 | sum = sum_them_all(2, 98, 1024); 14 | printf("%d\n", sum); 15 | sum = sum_them_all(4, 98, 1024, 402, -1024); 16 | printf("%d\n", sum); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * sum_them_all - sums all integers given, variadic function 5 | * @n: number of arguments 6 | * @...: rest of arguments, presumed to be integers 7 | * 8 | * Return: sum of rest of arguments 9 | */ 10 | int sum_them_all(const unsigned int n, ...) 11 | { 12 | va_list args; 13 | unsigned int i; 14 | int sum = 0; 15 | 16 | va_start(args, n); 17 | for (i = 0; i < n; i++) 18 | { 19 | sum += va_arg(args, int); 20 | } 21 | va_end(args); 22 | return (sum); 23 | } 24 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/1-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(", ", 4, 0, 98, -100, 402); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/100-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | global main 2 | 3 | section .text 4 | main: mov rax, 1 5 | mov rdi, 1 6 | mov rsi, message 7 | mov rdx, 17 8 | syscall 9 | mov rax, 60 10 | xor rdi, rdi 11 | syscall 12 | 13 | section .data 14 | message:db "Hello, Holberton", 10 15 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/2-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_strings(NULL, 3, "Jay", "Django", NULL); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/3-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_all("bceis", 'H', 0, "lberton"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # 0x0F. C - Variadic functions 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using variadic functions, and their macros to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - What are variadic functions 8 | - How to use `va_start`, `va_arg` and `va_end` macros 9 | - Why and how to use the `const` type qualifier 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /0x0F-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _VAR_FUNCS_ 2 | #define _VAR_FUNCS_ 3 | #include 4 | /** 5 | * struct print_convert_variable - structure to hold function and char key 6 | * @type: abbreviation for type, i=int, c=char, f=float, s=string 7 | * @f: pointer to function for proper print statement 8 | * 9 | * Description: key:value, char:function, e.g: 'c':_print_char 10 | */ 11 | typedef struct print_convert_variable 12 | { 13 | char type; 14 | void (*f)(va_list *); 15 | } conv_t; 16 | int sum_them_all(const unsigned int n, ...); 17 | void print_numbers(const char *seperator, const unsigned int n, ...); 18 | void print_strings(const char *seperator, const unsigned int n, ...); 19 | void print_all(const char * const format, ...); 20 | int _putchar(int); 21 | #endif /* _VAR_FUNCS_ */ 22 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * print_list - prints a linked list 5 | * @h: head of list 6 | * 7 | * Return: number of nodes in list as size_t 8 | */ 9 | size_t print_list(const list_t *h) 10 | { 11 | const list_t *temp; 12 | size_t n_nodes = 0; 13 | 14 | temp = h; 15 | while (temp != NULL) 16 | { 17 | printf("[%u] ", temp->len); 18 | if (!temp->str) 19 | printf("(nil)\n"); 20 | else 21 | printf("%s\n", temp->str); 22 | temp = temp->next; 23 | n_nodes++; 24 | } 25 | return (n_nodes); 26 | } 27 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * list_len - gets the linked lists number of nodes 4 | * @h: head of list 5 | * 6 | * Return: number of nodes as size_t 7 | */ 8 | size_t list_len(const list_t *h) 9 | { 10 | const list_t *temp; 11 | size_t n_nodes = 0; 12 | 13 | temp = h; 14 | while (temp != NULL) 15 | { 16 | temp = temp->next; 17 | n_nodes++; 18 | } 19 | return (n_nodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = list_len(head); 30 | printf("-> %lu elements\n", n); 31 | free(new->str); 32 | free(new); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Using constructors in C! Who knew!? */ 4 | void main_constructor(void) __attribute__((constructor)); 5 | /** 6 | * main_constructor - constructor function that runs before main () 7 | * 8 | * Return: always void 9 | */ 10 | void main_constructor(void) 11 | { 12 | printf("You're beat! and yet, you must allow,\n"); 13 | printf("I bore my house upon my back!\n"); 14 | } 15 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | extern printf 2 | 3 | section .data 4 | msg: db "Hello, Holberton", 0 5 | fmt: db "%s", 10, 0 6 | 7 | section .text 8 | 9 | global main 10 | main: 11 | push rbp 12 | mov rdi,fmt 13 | mov rsi,msg 14 | mov rax,0 15 | call printf 16 | pop rbp 17 | mov rax,0 18 | ret 19 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * add_node - adds a node to the head of a linked list 5 | * @head: pointer to a list_t pointer that points to the head struct 6 | * @str: string to add as node 7 | * 8 | * Return: pointer to new head of list, NULL on failure 9 | */ 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *temp_head; 13 | int length = 0; 14 | 15 | temp_head = *head; 16 | *head = malloc(sizeof(list_t)); 17 | if (!*head) 18 | return (NULL); 19 | while (str[length]) 20 | length++; 21 | (*head)->len = length; 22 | if (str) 23 | { 24 | (*head)->str = strdup(str); 25 | if (!(*head)->str) 26 | { 27 | free(*head); 28 | return (NULL); 29 | } 30 | } 31 | else 32 | (*head)->str = NULL; 33 | (*head)->next = temp_head; 34 | return (*head); 35 | } 36 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_list - frees all elements in a linked list 4 | * @head: pointer to head element of list 5 | * 6 | * Return: always void 7 | */ 8 | void free_list(list_t *head) 9 | { 10 | list_t *temp; 11 | 12 | temp = head; 13 | while (temp) 14 | { 15 | if (temp->str) 16 | free(temp->str); 17 | free(temp); 18 | temp = temp->next; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Jennie"); 17 | add_node_end(&head, "&"); 18 | add_node_end(&head, "Jay"); 19 | add_node_end(&head, "love"); 20 | add_node_end(&head, "asm"); 21 | print_list(head); 22 | free_list(head); 23 | head = NULL; 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x11 C. - Singly linked lists 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using linked lists (allocating memory, freeing, setting up dynamically) to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - When and why using linked lists vs arrays 8 | - How to build and use linked lists 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /0x11-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_H_ 2 | #define _LISTS_H_ 3 | #include 4 | /** 5 | * struct list_s - singly linked list 6 | * @str: string - (malloc'ed string) 7 | * @len: length of the string 8 | * @next: points to the next node 9 | * 10 | * Description: singly linked list node structure 11 | * for Holberton project 12 | */ 13 | typedef struct list_s 14 | { 15 | char *str; 16 | unsigned int len; 17 | struct list_s *next; 18 | } list_t; 19 | size_t print_list(const list_t *h); 20 | size_t list_len(const list_t *h); 21 | int _putchar(char c); 22 | list_t *add_node(list_t **head, const char *str); 23 | list_t *add_node_end(list_t **head, const char *str); 24 | void free_list(list_t *head); 25 | #endif /* _LISTS_H_ */ 26 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = print_listint(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * print_listint - prints the elements in linked list 5 | * @h: head of listint_t type 6 | * 7 | * Return: size_t, number of nodes. 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t n_nodes = 0; 12 | const listint_t *temp_h; 13 | 14 | temp_h = h; 15 | while (temp_h) 16 | { 17 | printf("%d\n", temp_h->n); 18 | temp_h = temp_h->next; 19 | n_nodes++; 20 | } 21 | return (n_nodes); 22 | } 23 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * listint_len - gets number of elements in linked list containing ints 5 | * @h: head of listint_t type 6 | * 7 | * Return: size_t, number of nodes. 8 | */ 9 | size_t listint_len(const listint_t *h) 10 | { 11 | size_t n_nodes = 0; 12 | const listint_t *temp_h; 13 | 14 | temp_h = h; 15 | while (temp_h) 16 | { 17 | temp_h = temp_h->next; 18 | n_nodes++; 19 | } 20 | return (n_nodes); 21 | } 22 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = listint_len(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * reverse_listint - reverses a linked list 4 | * @head: double pointer to head of linked list 5 | * 6 | * Return: pointer to new head of linked list 7 | */ 8 | listint_t *reverse_listint(listint_t **head) 9 | { 10 | listint_t *prev = NULL; 11 | listint_t *next = NULL; 12 | 13 | if (!head || !*head) 14 | return (NULL); 15 | while (*head) 16 | { 17 | next = (*head)->next; 18 | (*head)->next = prev; 19 | prev = *head; 20 | *head = next; 21 | } 22 | *head = prev; 23 | return (*head); 24 | } 25 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * print_listint_safe - prints the elements in linked list 5 | * @h: head of listint_t type 6 | * 7 | * Return: size_t, number of nodes. Exits with 98 on failure 8 | */ 9 | size_t print_listint_safe(const listint_t *h) 10 | { 11 | size_t n_nodes = 0; 12 | const listint_t *temp_h = NULL, *loop_node= NULL; 13 | 14 | temp_h = h; 15 | if (!h) 16 | exit(98); 17 | while (temp_h) 18 | { 19 | if (h->next == temp_h && n_nodes > 2) 20 | { 21 | loop_node = temp_h; 22 | printf("loop node: [%p] %d\n", (void *)loop_node, loop_node->n); 23 | exit(98); 24 | } 25 | printf("[%p] %d\n", (void *)temp_h, temp_h->n); 26 | temp_h = temp_h->next; 27 | n_nodes++; 28 | } 29 | return (n_nodes); 30 | } 31 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | listint_t *find_listint_loop(listint_t *head) 3 | { 4 | listint_t *adv_once = head, *adv_twice = head; 5 | 6 | while (adv_once && adv_twice && adv_twice->next) 7 | { 8 | adv_once = adv_once->next; 9 | adv_twice = adv_twice->next->next; 10 | if (adv_once == adv_twice) 11 | { 12 | return (adv_once); 13 | } 14 | } 15 | return (NULL); 16 | } 17 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * add_nodeint - adds a node of listint_t to the head of a linked list 5 | * @head: pointer to a list_t pointer that points to the head struct 6 | * @n: integer data to put in new node 7 | * 8 | * Return: pointer to new head of list, NULL on failure 9 | */ 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | listint_t *temp_head; 13 | 14 | temp_head = *head; 15 | *head = malloc(sizeof(listint_t)); 16 | if (!*head) 17 | return (NULL); 18 | (*head)->n = n; 19 | (*head)->next = temp_head; 20 | return (*head); 21 | } 22 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint(&head, 0); 17 | add_nodeint(&head, 1); 18 | add_nodeint(&head, 2); 19 | add_nodeint(&head, 3); 20 | add_nodeint(&head, 4); 21 | add_nodeint(&head, 98); 22 | add_nodeint(&head, 402); 23 | add_nodeint(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_listint - frees a linked list 4 | * @head: head of list 5 | * 6 | * Return: always void 7 | */ 8 | void free_listint(listint_t *head) 9 | { 10 | while (head) 11 | { 12 | free(head); 13 | head = head->next; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint(head); 26 | head = NULL; 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_listint2 - frees a linked list and sets head to NULL 4 | * @head: double pointer to head of list 5 | * 6 | * Return: always void. 7 | */ 8 | void free_listint2(listint_t **head) 9 | { 10 | if (head == NULL) 11 | return; 12 | while (*head) 13 | { 14 | free(*head); 15 | *head = (*head)->next; 16 | } 17 | head = NULL; 18 | } 19 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint2(&head); 26 | printf("%p\n", (void *)head); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * pop_listint - pops a list element from the beginning of a list 4 | * @head: double pointer to head of list 5 | * 6 | * Return: value of n for element deleted 7 | */ 8 | int pop_listint(listint_t **head) 9 | { 10 | listint_t *new_head; 11 | int n_of_deleted_head; 12 | 13 | if (!*head) /* HEAD is NULL, empty list */ 14 | return (0); 15 | n_of_deleted_head = (*head)->n; 16 | new_head = (*head)->next; 17 | free(*head); 18 | *head = new_head; 19 | return (n_of_deleted_head); 20 | } 21 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * get_nodeint_at_index - gets a node of linked list at specific index 4 | * @head: head of list 5 | * @index: index to pull node from 6 | * 7 | * Return: pointer to node at specified index, NULL if not found 8 | */ 9 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 10 | { 11 | unsigned int ctr = 0; 12 | listint_t *temp_h; 13 | 14 | if (!head) /* EMPTY list */ 15 | return (NULL); 16 | temp_h = head; 17 | while (temp_h) 18 | { 19 | if (ctr == index) 20 | return (temp_h); 21 | temp_h = temp_h->next; 22 | ctr++; 23 | } 24 | return (NULL); /* didn't find node */ 25 | } 26 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *node; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | print_listint(head); 26 | node = get_nodeint_at_index(head, 5); 27 | printf("%d\n", node->n); 28 | print_listint(head); 29 | free_listint2(&head); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/8-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | sum = sum_listint(head); 26 | printf("sum = %d\n", sum); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * sum_listint - sums the n value of a linked list of listint_t 4 | * @head: head of list 5 | * 6 | * Return: integer represetnting sum of all n values from list 7 | */ 8 | int sum_listint(listint_t *head) 9 | { 10 | int sum = 0; 11 | listint_t *temp_h; 12 | 13 | if (!head) 14 | return (0); 15 | temp_h = head; 16 | while (temp_h) 17 | { 18 | sum += temp_h->n; 19 | temp_h = temp_h->next; 20 | } 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/9-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | printf("-----------------\n"); 26 | insert_nodeint_at_index(&head, 5, 4096); 27 | print_listint(head); 28 | free_listint2(&head); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x12-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x12. C - More singly linked lists 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using linked lists (allocating memory, freeing, setting up dynamically) to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - How to use linked lists 8 | - Start to look for the right source of information without too much help 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * binary_to_uint - converts binary string to an unsigned integer 4 | * @b: pointer to binary string in memory to convert to integer 5 | * 6 | * Return: unsigned integer representing binary value 7 | */ 8 | unsigned int binary_to_uint(const char *b) 9 | { 10 | int base = 1; 11 | unsigned int temp = 0, result = 0; 12 | int len = 0; 13 | 14 | if (!b) 15 | return (0); 16 | while (b[len]) 17 | len++; 18 | while (len > 0) 19 | { 20 | if (b[len - 1] != '0' && b[len - 1] != '1') 21 | return (0); 22 | temp = (b[len - 1] == '1') ? 1 : 0; 23 | result += (temp * base); 24 | base *= 2; 25 | len--; 26 | } 27 | return (result); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = binary_to_uint("1"); 14 | printf("%u\n", n); 15 | n = binary_to_uint("101"); 16 | printf("%u\n", n); 17 | n = binary_to_uint("1e01"); 18 | printf("%u\n", n); 19 | n = binary_to_uint("1100010"); 20 | printf("%u\n", n); 21 | n = binary_to_uint("0000000000000000000110010010"); 22 | printf("%u\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | print_binary(0); 12 | printf("\n"); 13 | print_binary(1); 14 | printf("\n"); 15 | print_binary(98); 16 | printf("\n"); 17 | print_binary(1024); 18 | printf("\n"); 19 | print_binary((1 << 10) + 1); 20 | printf("\n"); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_binary - prints a binary number using bit shifting and masks 4 | * @n: number to print 5 | * 6 | * Return: always void 7 | */ 8 | void print_binary(unsigned long int n) 9 | { 10 | unsigned int mask = 32768; /* 1000 0000 0000 0000 */ 11 | char hit_rel_flag = 0; 12 | 13 | while (mask > 0) 14 | { 15 | if ((n & mask) == 0 && (hit_rel_flag || mask == 0x01)) 16 | _putchar('0'); 17 | else if ((n & mask) != 0) 18 | { 19 | _putchar('1'); 20 | hit_rel_flag = 1; 21 | } 22 | mask = mask >> 1; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | /** 2 | * get_endianness - gets the endianness of the CPU running 3 | * 4 | * Return: 0 if big endian, 1 if little endian 5 | */ 6 | int get_endianness(void) 7 | { 8 | unsigned int i = 1; 9 | char *c = (char *)&i; 10 | 11 | /* c points to integer, will only pull first byte of integer */ 12 | /* if it is 1 then it is little endian (last byte stored first) */ 13 | /* if it is 0 then it is big endian because the data is stored last */ 14 | if (*c) 15 | return (1); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | int main(void) 5 | { 6 | int n; 7 | 8 | n = get_endianness(); 9 | if (n != 0) 10 | { 11 | printf("Little Endian\n"); 12 | } 13 | else 14 | { 15 | printf("Big Endian\n"); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol\4 2 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * get_bit - gets a bit, 1 or 0, at index 4 | * @n: number to get bit from 5 | * @index: index to find bit at 6 | * 7 | * Return: 1 or 0 depending if bit is lit or not 8 | */ 9 | int get_bit(unsigned long int n, unsigned int index) 10 | { 11 | int ret = 0; 12 | unsigned long int mask = 0x01; 13 | 14 | mask = mask << index; 15 | if (mask == 0) 16 | return (-1); 17 | ret = ((n & mask)) ? 1 : 0; 18 | return (ret); 19 | } 20 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = get_bit(1024, 10); 14 | printf("%d\n", n); 15 | n = get_bit(98, 1); 16 | printf("%d\n", n); 17 | n = get_bit(1024, 0); 18 | printf("%d\n", n); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | set_bit(&n, 5); 15 | printf("%lu\n", n); 16 | n = 0; 17 | set_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | set_bit(&n, 0); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * set_bit - sets a bit at given index to 1 4 | * @n: number to set bit in 5 | * @index: index to set bit at 6 | * 7 | * Return: 1 if it worked, or -1 on error 8 | */ 9 | int set_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned long int mask = 0x01; 12 | 13 | mask = mask << index; 14 | if (mask == 0x00) /* SOMETHING WENT WRONG MASK IS ALL 0'S */ 15 | return (-1); 16 | *n |= mask; 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * clear_bit - sets a bit at given index to 0 4 | * @n: number to set bit in 5 | * @index: index to set bit at 6 | * 7 | * Return: 1 if it worked, or -1 on error 8 | */ 9 | int clear_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned long int mask = 0x01; 12 | 13 | mask = ~(mask << index); 14 | if (mask == 0x00) /* something went wrong mask is all 0's */ 15 | return (-1); 16 | *n &= mask; 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | clear_bit(&n, 10); 15 | printf("%lu\n", n); 16 | n = 0; 17 | clear_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | clear_bit(&n, 1); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | /** 2 | * flip_bits - gets number of bits needed to flip for n to equal m 3 | * @n: number to see how many bit flips are needed to equal m for 4 | * @m: number to set other to equal given bit flips 5 | * 6 | * Return: number of bit flips needed for one number to equal the other 7 | */ 8 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 9 | { 10 | unsigned int n_flips = 0; 11 | unsigned long int xor = (n ^ m); 12 | unsigned long int mask = 0x01; 13 | 14 | while (mask <= xor) 15 | { 16 | if (mask & xor) /* bit is lit in xor, will need to flip */ 17 | n_flips++; 18 | mask = mask << 1; 19 | } 20 | return (n_flips); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = flip_bits(1024, 1); 14 | printf("%u\n", n); 15 | n = flip_bits(402, 98); 16 | printf("%u\n", n); 17 | n = flip_bits(1024, 3); 18 | printf("%u\n", n); 19 | n = flip_bits(1024, 1025); 20 | printf("%u\n", n); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # 0x13. C - Bit Manipulation 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using bit manipulation to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - Look for the right source of information without too much help 8 | - How to manipulate bits and use bitwise operators 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x13-bit_manipulation/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef _HOLBERTON_H_ 2 | #define _HOLBERTON_H_ 3 | unsigned int binary_to_uint(const char *b); 4 | void print_binary(unsigned long int n); 5 | void _putchar(char c); 6 | int get_bit(unsigned long int n, unsigned int index); 7 | int set_bit(unsigned long int *n, unsigned int index); 8 | int clear_bit(unsigned long int *n, unsigned int index); 9 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 10 | int get_endianness(void); 11 | #endif /* _HOLBERTON_H_ */ 12 | -------------------------------------------------------------------------------- /0x14-file_io/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | ssize_t n; 13 | 14 | if (ac != 2) 15 | { 16 | dprintf(2, "Usage: %s filename\n", av[0]); 17 | exit(1); 18 | } 19 | n = read_textfile(av[1], 114); 20 | printf("\n(printed chars: %li)\n", n); 21 | n = read_textfile(av[1], 1024); 22 | printf("\n(printed chars: %li)\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /** 8 | * create_file - creates a file and copies text into it 9 | * @filename: the filename to create 10 | * @text_content: text to copy into new file 11 | * 12 | * Return: 1 on success, -1 on failure (file can not be created, or written, 13 | * or write fails, etc). 14 | */ 15 | int create_file(const char *filename, char *text_content) 16 | { 17 | int fd, err, len; 18 | 19 | if (!filename) 20 | return (-1); 21 | fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600); 22 | if (fd < 0) 23 | return (-1); 24 | 25 | while (text_content && *(text_content + len)) 26 | len++; 27 | err = write(fd, text_content, len); 28 | close(fd); 29 | if (err < 0) 30 | return (-1); 31 | return (1); 32 | } 33 | -------------------------------------------------------------------------------- /0x14-file_io/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = create_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-file_io/100-elf_header.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - entry point for program to get header of ELF file 4 | * @argc: count of arguments present in CLI 5 | * @argv: pointer to array of pointers to arguments 6 | * 7 | * Return: 1 on success, error code on failure 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | printf("argc:%d, argv:%p\n", argc, (void *)*argv); 12 | return (1); 13 | } 14 | -------------------------------------------------------------------------------- /0x14-file_io/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = append_text_to_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-file_io/README.md: -------------------------------------------------------------------------------- 1 | # 0x14. C - File I/O 2 | 3 | Welcome to my **low level C programming** experience at Holberton School. In this project we will be using file systemcalls/functions to complete different tasks. Read more about the goals below: 4 | 5 | 6 | ## Goals 7 | - Look for the right source of information online 8 | - How to create, open, close, read and write files 9 | - What are file descriptors 10 | - What are the 3 standard file descriptors, what are their purpose and what are their `POSIX` names 11 | - How to use the I/O system calls `open`, `close`, `read` and `write` 12 | - What are and how to use the flags `O_RDONLY`, `O_WRONLY`, `O_RDWR` 13 | - What are file permissions, and how to set them when creating a file with the `open` system call 14 | - What is a system call 15 | - What is the difference between a function and a system call 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /0x14-file_io/RequiesCat: -------------------------------------------------------------------------------- 1 | Requiescat 2 | by Oscar Wilde 3 | 4 | Tread lightly, she is near 5 | Under the snow, 6 | Speak gently, she can hear 7 | The daisies grow. 8 | 9 | All her bright golden hair 10 | Tarnished with rust, 11 | She that was young and fair 12 | Fallen to dust. 13 | 14 | Lily-like, white as snow, 15 | She hardly knew 16 | She was a woman, so 17 | Sweetly she grew. 18 | 19 | Coffin-board, heavy stone, 20 | Lie on her breast, 21 | I vex my heart alone, 22 | She is at rest. 23 | 24 | Peace, Peace, she cannot hear 25 | Lyre or sonnet, 26 | All my life's buried here, 27 | Heap earth upon it. 28 | -------------------------------------------------------------------------------- /0x14-file_io/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef __HOLBERTON_H__ 2 | #define __HOLBERTON_H__ 3 | ssize_t read_textfile(const char *filename, size_t letters); 4 | int create_file(const char *filename, char *text_content); 5 | int append_text_to_file(const char *filename, char *text_content); 6 | #endif /* __HOLBERTON_H__ */ 7 | -------------------------------------------------------------------------------- /0x14-file_io/incitatous: -------------------------------------------------------------------------------- 1 | Why you should think twice before putting pictures on social media. 2 | (What you always wanted to know about @Incitatous) 3 | #PrivacyAware 4 | http://imgur.com/a/Mq1tc 5 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = NULL; 30 | n = print_dlistint(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_dlistint - prints a doubly linked list 6 | * @h: head of doubly linked list (DLL) 7 | * 8 | * Return: number of nodes in DLL 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | size_t nodes = 0; 13 | const dlistint_t *temp; 14 | 15 | temp = h; 16 | while (temp) 17 | { 18 | printf("%d\n", temp->n); 19 | temp = temp->next; 20 | nodes++; 21 | } 22 | return (nodes); 23 | } 24 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - gets the length of a doubly linked list 5 | * @h: head of doubly linked list (DLL) 6 | * 7 | * Return: number of nodes in DLL 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t nodes = 0; 12 | const dlistint_t *temp; 13 | 14 | temp = h; 15 | while (temp) 16 | { 17 | temp = temp->next; 18 | nodes++; 19 | } 20 | return (nodes); 21 | } 22 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = new; 30 | n = dlistint_len(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | import this -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint(&head, 0); 17 | add_dnodeint(&head, 1); 18 | add_dnodeint(&head, 2); 19 | add_dnodeint(&head, 3); 20 | add_dnodeint(&head, 4); 21 | add_dnodeint(&head, 98); 22 | add_dnodeint(&head, 402); 23 | add_dnodeint(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a doubly linked list 5 | * @head: pointer to head of doubly linked list to free 6 | * 7 | * Return: always void 8 | */ 9 | void free_dlistint(dlistint_t *head) 10 | { 11 | dlistint_t *temp; 12 | 13 | while (head) 14 | { 15 | temp = head->next; 16 | free(head); 17 | head = temp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | free_dlistint(head); 26 | head = NULL; 27 | return (EXIT_SUCCESS); 28 | } 29 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - gets a doubly linked list node at a 5 | * a given index 6 | * @head: pointer to head of DLL to look through 7 | * @index: index of node to find 8 | * 9 | * Return: pointer to node if found, otherwise NULL 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | unsigned int i = 0; 14 | 15 | while (head) 16 | { 17 | if (i++ == index) 18 | break; 19 | head = head->next; 20 | } 21 | return (head); 22 | } 23 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *node; 15 | 16 | head = NULL; 17 | add_dnodeint_end(&head, 0); 18 | add_dnodeint_end(&head, 1); 19 | add_dnodeint_end(&head, 2); 20 | add_dnodeint_end(&head, 3); 21 | add_dnodeint_end(&head, 4); 22 | add_dnodeint_end(&head, 98); 23 | add_dnodeint_end(&head, 402); 24 | add_dnodeint_end(&head, 1024); 25 | print_dlistint(head); 26 | node = get_dnodeint_at_index(head, 5); 27 | printf("%d\n", node->n); 28 | free_dlistint(head); 29 | head = NULL; 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_dnodeint_end(&head, 0); 18 | add_dnodeint_end(&head, 1); 19 | add_dnodeint_end(&head, 2); 20 | add_dnodeint_end(&head, 3); 21 | add_dnodeint_end(&head, 4); 22 | add_dnodeint_end(&head, 98); 23 | add_dnodeint_end(&head, 402); 24 | add_dnodeint_end(&head, 1024); 25 | sum = sum_dlistint(head); 26 | printf("sum = %d\n", sum); 27 | free_dlistint(head); 28 | head = NULL; 29 | return (EXIT_SUCCESS); 30 | } 31 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - sums a doubly linked list 5 | * @head: pointer to head of DLL 6 | * 7 | * Return: sum of all elements of the DLL, 0 if DLL is NULL 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | return (sum); 19 | } 20 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | printf("-----------------\n"); 26 | insert_dnodeint_at_index(&head, 99, 4096); 27 | print_dlistint(head); 28 | free_dlistint(head); 29 | head = NULL; 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x16-doubly_linked_lists/a.out -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x16-doubly_linked_lists/b -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x16-doubly_linked_lists/e -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/generate_palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char is_palindrome(unsigned int num); 4 | /** 5 | * main - entry point to find palindrome program 6 | * 7 | * Return: always 0 8 | */ 9 | int main(void) 10 | { 11 | unsigned int d1, d2, temp, max; 12 | 13 | max = 0; 14 | for (d1 = 100; d1 <= 999; d1++) 15 | { 16 | for (d2 = 100; d2 <= 999; d2++) 17 | { 18 | temp = d1 * d2; 19 | if (is_palindrome(temp)) 20 | max = (temp > max) ? temp : max; 21 | } 22 | } 23 | printf("Largest palindrome of 3 digit numbers is: %d\n", max); 24 | } 25 | 26 | char is_palindrome(unsigned int num) 27 | { 28 | unsigned int reverse = 0, rem = 0, n = num; 29 | 30 | while (n != 0) 31 | { 32 | rem = n % 10; 33 | reverse = reverse * 10 + rem; 34 | n /= 10; 35 | } 36 | if (reverse == num) 37 | return (1); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x16-doubly_linked_lists/h -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/j: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x16-doubly_linked_lists/j -------------------------------------------------------------------------------- /0x16-doubly_linked_lists/k: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x16-doubly_linked_lists/k -------------------------------------------------------------------------------- /0x17-dynamic_libraries/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always EXIT_SUCCESS. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", _strlen("Holberton")); 12 | return (EXIT_SUCCESS); 13 | } 14 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | gcc -fPIC -Wall -Wextra -Werror -pedantic *.c -shared -o liball.so 4 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _isdigit - checks if int is digit or not 3 | * 4 | * @c: int c, represents a char 5 | * 6 | * Return: 1 if c is digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= 48 && c <= 57) /* ascii 0..9 values */ 11 | return (1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _memcpy - copys n bytes of memory 3 | * 4 | * @dest: pointer to dest str 5 | * @src: source to be copied from 6 | * @n: num bytes to copy from src 7 | * 8 | * Return: pointer to beginning of memory area dest 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i = 0; 13 | char *start = dest; 14 | 15 | while (i < n) 16 | { 17 | *dest++ = *src++; 18 | i++; 19 | } 20 | return (start); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strncat - concatanates two strings 3 | * 4 | * @dest: destination string 5 | * @src: string to add to end of dest 6 | * @n: num of bytes to take from src 7 | * 8 | * Description: only takes n bytes from src 9 | * 10 | * Return: char pointer to dest 11 | */ 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int counter = 0, srcLength = 0; 15 | char *temp = dest; 16 | 17 | while (*(src + srcLength) != '\0') 18 | srcLength++; 19 | while (*dest) 20 | dest++; 21 | if (n > srcLength) 22 | n = srcLength; 23 | while (counter < n) 24 | { 25 | *dest++ = *src++; 26 | counter++; 27 | } 28 | *dest = '\0'; 29 | return (temp); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _atoi - converts string to integer 4 | * 5 | * @s: string to convert from 6 | * 7 | * Return: integer from conversion 8 | */ 9 | int _atoi(char *s) 10 | { 11 | int sign = 1; 12 | unsigned int total = 0; 13 | char working = 0; 14 | 15 | while (*s) 16 | { 17 | if (*s == '-') 18 | sign = sign * -1; 19 | if (*s >= '0' && *s <= '9') 20 | { 21 | working = 1; 22 | total = total * 10 + *s - '0'; 23 | } 24 | else if (*s < '0' || *s > '9') 25 | { 26 | if (working) 27 | break; 28 | } 29 | s++; 30 | } 31 | if (sign < 0) 32 | total = (-(total)); 33 | return (total); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/100-operations.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) 2 | { 3 | return (a + b); 4 | } 5 | 6 | int sub(int a, int b) 7 | { 8 | return (a - b); 9 | } 10 | 11 | int div(int a, int b) 12 | { 13 | return (a / b); 14 | } 15 | 16 | int mul(int a, int b) 17 | { 18 | return (a * b); 19 | } 20 | 21 | int mod(int a, int b) 22 | { 23 | return (a % b); 24 | } 25 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x17-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x17-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -O /tmp/libinjection.so https://github.com/BennettDixon/holbertonschool-low_level_programming/raw/master/0x17-dynamic_libraries/libinjection.so 3 | export LD_PRELOAD=/tmp/libinjection.so 4 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _strchr - finds if char occurs in string 4 | * 5 | * @s: string to check 6 | * @c: character to find in s 7 | * 8 | * Return: pointer to first instance of c, otherwise NULL 9 | */ 10 | char *_strchr(char *s, char c) 11 | { 12 | int i = 0; 13 | 14 | while (*(s + i)) 15 | { 16 | if (*(s + i) == c) 17 | return (s + i); 18 | i++; 19 | } 20 | if (*(s + i) == c) 21 | return (s + i); 22 | return (NULL); 23 | } 24 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strlen - gets length of string 3 | * 4 | * @s: string to check length of 5 | * 6 | * Return: length of string as int 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int counter; 11 | 12 | counter = 0; 13 | while (*(s + counter) != '\0') 14 | counter++; 15 | return (counter); 16 | } 17 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strncpy - copies string, up to n bytes 3 | * 4 | * @dest: destination str to copy into 5 | * @src: string to copy to dest 6 | * @n: number of bytes to copy from src 7 | * 8 | * Return: char pointer to beginning of dest 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | char *temp = dest; 13 | int counter = 0, srcLength = 0; 14 | 15 | if (n <= 0) 16 | return (dest); 17 | while (src[srcLength] != '\0') 18 | srcLength++; 19 | srcLength++; 20 | if (n > srcLength) 21 | n = srcLength; 22 | 23 | while (counter < n) 24 | { 25 | dest[counter] = src[counter]; 26 | counter++; 27 | } 28 | return (temp); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _islower - print alphabet out 3 | * 4 | * @c: character 5 | * 6 | * Return: 1 if lower, 0 otherwise 7 | */ 8 | int _islower(int c) 9 | { 10 | if (c >= 'a' && c <= 'z') 11 | return (1); 12 | else 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _puts - puts string to stdout 4 | * 5 | * @str: string to print to stdout 6 | * 7 | * Return: always void 8 | */ 9 | void _puts(char *str) 10 | { 11 | while (*str) 12 | _putchar(*str++); 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strspn - finds length of prefix substring 3 | * 4 | * @s: string to check 5 | * @accept: substring of chars 6 | * 7 | * Return: n bytes in intial segment of s which 8 | * consist only of bytes from accept 9 | */ 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | int i; 13 | int consistC = 0; 14 | int prevC; 15 | 16 | while (*s) 17 | { 18 | i = 0; 19 | prevC = consistC; 20 | while (*(accept + i) != '\0') 21 | { 22 | if (*(accept + i) == *s) 23 | consistC++; 24 | i++; 25 | } 26 | if (prevC == consistC) /* didn't equal a char from accept */ 27 | break; 28 | s++; 29 | } 30 | return (consistC); 31 | } 32 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _isalpha - checks if a char is alpha or not 3 | * 4 | * @c: character parameter to check 5 | * 6 | * Return: 1 if letter, 0 otherwise 7 | */ 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 11 | return (1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strpbrk - searches string for set of bytes 3 | * 4 | * @s: string to check 5 | * @accept: substring of chars 6 | * 7 | * Return: pointer to first occurance 8 | */ 9 | char *_strpbrk(char *s, char *accept) 10 | { 11 | int i; 12 | 13 | while (*s) 14 | { 15 | i = 0; 16 | while (*(accept + i) != '\0') 17 | { 18 | if (*(accept + i) == *s) 19 | return (s); 20 | i++; 21 | } 22 | s++; 23 | } 24 | return (0); /* didn't find an occurance */ 25 | } 26 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _abs - calculates absolute value 3 | * 4 | * @n: integer parameter 5 | * 6 | * Return: absolute value of n 7 | */ 8 | int _abs(int n) 9 | { 10 | if (n < 0) 11 | return (-(n)); 12 | else 13 | return (n); 14 | } 15 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strcpy - copies string from src to dest 3 | * 4 | * @dest: pointer to destination of string 5 | * @src: pointer to source string to copy from 6 | * 7 | * Return: pointer to dest 8 | */ 9 | char *_strcpy(char *dest, char *src) 10 | { 11 | char *temp = dest; 12 | 13 | while (*src) 14 | *dest++ = *src++; 15 | return (temp); 16 | } 17 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/custom_check_injection.c: -------------------------------------------------------------------------------- 1 | int iterations_of_rand = 0; 2 | 3 | int rand() 4 | { 5 | iterations_of_rand++; 6 | switch (iterations_of_rand) 7 | { 8 | case 1: 9 | return (9); 10 | case 2: 11 | return (8); 12 | case 3: 13 | return (10); 14 | case 4: 15 | return (24); 16 | case 5: 17 | return (75); 18 | case 6: 19 | return (9); 20 | } 21 | return (22); 22 | } 23 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef __HOLBERTON_H__ 2 | #define __HOLBERTON_H__ 3 | int _putchar(char c); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int _abs(int n); 7 | int _isupper(int c); 8 | int _isdigit(int c); 9 | int _strlen(char *s); 10 | void _puts(char *s); 11 | char *_strcpy(char *dest, char *src); 12 | int _atoi(char *s); 13 | char *_strcat(char *dest, char *src); 14 | char *_strncat(char *dest, char *src, int n); 15 | char *_strncpy(char *dest, char *src, int n); 16 | int _strcmp(char *s1, char *s2); 17 | char *_memset(char *s, char b, unsigned int n); 18 | char *_memcpy(char *dest, char *src, unsigned int n); 19 | char *_strchr(char *s, char c); 20 | unsigned int _strspn(char *s, char *accept); 21 | char *_strpbrk(char *s, char *accept); 22 | char *_strstr(char *haystack, char *needle); 23 | #endif /* __HOLBERTON_H__ */ 24 | -------------------------------------------------------------------------------- /0x17-dynamic_libraries/libholberton.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x17-dynamic_libraries/libholberton.so -------------------------------------------------------------------------------- /0x17-dynamic_libraries/libinjection.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x17-dynamic_libraries/libinjection.so -------------------------------------------------------------------------------- /0x17-dynamic_libraries/testing_ops.py: -------------------------------------------------------------------------------- 1 | import random 2 | import ctypes 3 | 4 | cops = ctypes.CDLL('./100-operations.so') 5 | a = random.randint(-111, 111) 6 | b = random.randint(-111, 111) 7 | print("{} + {} = {}".format(a, b, cops.add(a, b))) 8 | print("{} - {} = {}".format(a, b, cops.sub(a, b))) 9 | print("{} x {} = {}".format(a, b, cops.mul(a, b))) 10 | print("{} / {} = {}".format(a, b, cops.div(a, b))) 11 | print("{} % {} = {}".format(a, b, cops.mod(a, b))) 12 | -------------------------------------------------------------------------------- /0x19-hash_tables/.gitignore: -------------------------------------------------------------------------------- 1 | # all output files 2 | *.out 3 | -------------------------------------------------------------------------------- /0x19-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | #include 3 | 4 | /** 5 | * hash_table_create - creates a hash table 6 | * @size: size of hash table to create 7 | * 8 | * Return: pointer to new hash_table_t in memory (heap) 9 | */ 10 | hash_table_t *hash_table_create(unsigned long int size) 11 | { 12 | unsigned long int i; 13 | hash_table_t *new_table = NULL; 14 | 15 | new_table = malloc(sizeof(hash_table_t)); 16 | if (!new_table) 17 | return (NULL); 18 | new_table->size = size; 19 | new_table->array = malloc(sizeof(hash_node_t) * size); 20 | if (!new_table->array) 21 | { 22 | free(new_table); 23 | return (NULL); 24 | } 25 | for (i = 0; i < size; i++) 26 | (new_table->array)[i] = NULL; 27 | return (new_table); 28 | } 29 | -------------------------------------------------------------------------------- /0x19-hash_tables/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%p\n", (void *)ht); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x19-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | /** 2 | * hash_djb2 - provides hash value for key 3 | * @str: key (string) to use to generate hash value 4 | * 5 | * Return: hash value 6 | */ 7 | unsigned long int hash_djb2(const unsigned char *str) 8 | { 9 | unsigned long int hash; 10 | int c; 11 | 12 | hash = 5381; 13 | while ((c = *str++)) 14 | { 15 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 16 | } 17 | return (hash); 18 | } 19 | -------------------------------------------------------------------------------- /0x19-hash_tables/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | 15 | s = "cisfun"; 16 | printf("%lu\n", hash_djb2((unsigned char *)s)); 17 | s = "Don't forget to tweet today"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | s = "98"; 20 | printf("%lu\n", hash_djb2((unsigned char *)s)); 21 | return (EXIT_SUCCESS); 22 | } 23 | -------------------------------------------------------------------------------- /0x19-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * key_index - gets the index of given key for hashtable 4 | * @key: str containing key 5 | * @size: size of the hash table's array 6 | * 7 | * Return: index of given key in hash_table 8 | */ 9 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 10 | { 11 | unsigned long int hash_key; 12 | 13 | hash_key = hash_djb2((unsigned char *)key); 14 | return (hash_key % size); 15 | } 16 | -------------------------------------------------------------------------------- /0x19-hash_tables/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | unsigned long int hash_table_array_size; 15 | 16 | hash_table_array_size = 1024; 17 | s = "cisfun"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 20 | s = "Don't forget to tweet today"; 21 | printf("%lu\n", hash_djb2((unsigned char *)s)); 22 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 23 | s = "98"; 24 | printf("%lu\n", hash_djb2((unsigned char *)s)); 25 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 26 | return (EXIT_SUCCESS); 27 | } 28 | -------------------------------------------------------------------------------- /0x19-hash_tables/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%d\n", hash_table_set(ht, "betty", "holberton")); 17 | printf("value for added node in main: %s\n", ht->array[173]->value); 18 | return (EXIT_SUCCESS); 19 | } 20 | -------------------------------------------------------------------------------- /0x19-hash_tables/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for Holberton School students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = NULL; 16 | hash_table_print(ht); 17 | ht = hash_table_create(1024); 18 | hash_table_print(ht); 19 | hash_table_set(ht, "c", "fun"); 20 | hash_table_set(ht, "python", "awesome"); 21 | hash_table_set(ht, "Jennie", "and Jay love asm"); 22 | hash_table_set(ht, "N", "queens"); 23 | hash_table_set(ht, "Asterix", "Obelix"); 24 | hash_table_set(ht, "Betty", "Holberton"); 25 | hash_table_set(ht, "98", "Battery Street"); 26 | hash_table_print(ht); 27 | return (EXIT_SUCCESS); 28 | } 29 | -------------------------------------------------------------------------------- /0x19-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | #include 3 | 4 | /** 5 | * hash_table_delete - frees and empties a hash table 6 | * @ht: hash table to delete 7 | */ 8 | void hash_table_delete(hash_table_t *ht) 9 | { 10 | unsigned long int i; 11 | hash_node_t *next_node = NULL, *node = NULL; 12 | 13 | if (!ht) 14 | return; 15 | 16 | if (ht->array) 17 | { 18 | for (i = 0; i < ht->size; i++) 19 | { 20 | if (ht->array[i] != NULL) 21 | { 22 | node = ht->array[i]; 23 | while (node) 24 | { 25 | next_node = node->next; 26 | if (node->value) 27 | free(node->value); 28 | if (node->key) 29 | free(node->key); 30 | free(node); 31 | node = next_node; 32 | } 33 | } 34 | } 35 | free(ht->array); 36 | } 37 | free(ht); 38 | } 39 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/0-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; 13 | size_t n = sizeof(array) / sizeof(array[0]); 14 | 15 | print_array(array, n); 16 | printf("\n"); 17 | bubble_sort(array, n); 18 | printf("\n"); 19 | print_array(array, n); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/1-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n^2) 2 | O(n^2) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; 13 | size_t n = sizeof(array) / sizeof(array[0]); 14 | 15 | print_array(array, n); 16 | printf("\n"); 17 | selection_sort(array, n); 18 | printf("\n"); 19 | print_array(array, n); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(nlog(n)) 2 | O(nlog(n)) 3 | O(n^2) 4 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sort.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int array[] = {19, 48, 99, 71, 13, 52, 96, 73, 86, 7}; 13 | int array2[] = {20, 4}; 14 | size_t n = sizeof(array) / sizeof(array[0]); 15 | 16 | (void)array2; 17 | print_array(array, n); 18 | printf("\n"); 19 | quick_sort(array, n); 20 | printf("\n"); 21 | print_array(array, n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/deck: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BennettDixon/holbertonschool-low_level_programming/3005393bb484c73084767868a394e3254308ff9f/0x1A-sorting_algorithms/deck -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * print_array - Prints an array of integers 6 | * 7 | * @array: The array to be printed 8 | * @size: Number of elements in @array 9 | */ 10 | void print_array(const int *array, size_t size) 11 | { 12 | size_t i; 13 | 14 | i = 0; 15 | while (array && i < size) 16 | { 17 | if (i > 0) 18 | printf(", "); 19 | printf("%d", array[i]); 20 | ++i; 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x1A-sorting_algorithms/print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "sort.h" 3 | 4 | /** 5 | * print_list - Prints a list of integers 6 | * 7 | * @list: The list to be printed 8 | */ 9 | void print_list(const listint_t *list) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (list) 15 | { 16 | if (i > 0) 17 | printf(", "); 18 | printf("%d", list->n); 19 | ++i; 20 | list = list->next; 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x1B-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all : main.c holberton.c m.h 2 | cc main.c holberton.c -o holberton 3 | -------------------------------------------------------------------------------- /0x1B-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | SRC = holberton.c main.c 3 | 4 | all : $(SRC) m.h 5 | $(CC) $(SRC) -o holberton 6 | -------------------------------------------------------------------------------- /0x1B-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | SRC = holberton.c main.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME=holberton 5 | RM=rm 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all : $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | .PHONY : clean 12 | clean : 13 | -$(RM) -f *~ $(NAME) 14 | 15 | .PHONY : oclean 16 | oclean : 17 | -$(RM) -f $(OBJ) 18 | 19 | .PHONY : fclean 20 | fclean : clean oclean 21 | 22 | re : 23 | $(CC) $(CFLAGS) -c $(SRC) 24 | -------------------------------------------------------------------------------- /0x1B-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | SRC= holberton.c main.c 3 | OBJ=$(SRC:.c=.o) 4 | NAME=holberton 5 | 6 | all : $(OBJ) 7 | $(CC) $(SRC) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1B-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | SRC = holberton.c main.c 3 | OBJ =$(SRC:.c=.o) 4 | NAME=holberton 5 | RM=rm 6 | 7 | all : $(OBJ) 8 | $(CC) $(SRC) -o $(NAME) 9 | 10 | .PHONY : clean oclean fclean 11 | 12 | clean : 13 | -$(RM) -f *~ $(NAME) 14 | 15 | oclean : 16 | -$(RM) -f $(OBJ) 17 | 18 | fclean : clean oclean 19 | 20 | re : oclean all 21 | -------------------------------------------------------------------------------- /0x1B-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | SRC = holberton.c main.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME=holberton 5 | RM=rm 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all : $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | .PHONY : clean oclean fclean 12 | 13 | clean : 14 | -$(RM) -f *~ $(NAME) 15 | 16 | oclean : 17 | -$(RM) -f $(OBJ) 18 | 19 | fclean : clean oclean 20 | 21 | re : oclean all 22 | -------------------------------------------------------------------------------- /0x1B-makefiles/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 1, 1, 0], 11 | [0, 1, 0, 1, 1, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 1, 0, 1, 0, 0], 14 | [0, 0, 0, 0, 0, 0] 15 | ] 16 | print(island_perimeter(grid)) 17 | -------------------------------------------------------------------------------- /0x1B-makefiles/5-off_main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /0x1B-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_holberton(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1B-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_holberton(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-binary_trees/10-binary_tree_depth.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_depth - gets the depth of a node in abinary tree 5 | * @tree: root node to draw height from for tree 6 | * 7 | * Return: size_t representing height, 0 on failure or NULL 8 | */ 9 | size_t binary_tree_depth(const binary_tree_t *tree) 10 | { 11 | size_t depth = 0; 12 | 13 | if (!tree) 14 | return (0); 15 | 16 | depth += (tree->parent) ? 1 + binary_tree_depth(tree->parent) : 0; 17 | return (depth); 18 | } 19 | -------------------------------------------------------------------------------- /0x1C-binary_trees/11-binary_tree_size.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_size - gets the size of a binary tree 5 | * @tree: root node to draw height from for tree 6 | * 7 | * Return: size_t representing height, 0 on failure or NULL 8 | */ 9 | size_t binary_tree_size(const binary_tree_t *tree) 10 | { 11 | size_t size = 0; 12 | 13 | if (!tree) 14 | return (0); 15 | 16 | size += binary_tree_size(tree->left); 17 | size += binary_tree_size(tree->right); 18 | size += 1; 19 | return (size); 20 | } 21 | -------------------------------------------------------------------------------- /0x1C-binary_trees/12-binary_tree_leaves.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_leaves - gets the # of leaves of a binary tree 5 | * @tree: root node to draw height from for tree 6 | * 7 | * Return: size_t representing height, 0 on failure or NULL 8 | */ 9 | size_t binary_tree_leaves(const binary_tree_t *tree) 10 | { 11 | size_t leaves = 0; 12 | 13 | if (!tree) 14 | return (0); 15 | 16 | if (!tree->left && !tree->right) 17 | return (1); 18 | leaves += binary_tree_leaves(tree->left); 19 | leaves += binary_tree_leaves(tree->right); 20 | return (leaves); 21 | } 22 | -------------------------------------------------------------------------------- /0x1C-binary_trees/13-binary_tree_nodes.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_nodes - gets the # of nodes of a binary tree 5 | * @tree: root node to draw height from for tree 6 | * 7 | * Return: size_t representing height, 0 on failure or NULL 8 | */ 9 | size_t binary_tree_nodes(const binary_tree_t *tree) 10 | { 11 | size_t nodes = 0; 12 | 13 | if (!tree) 14 | return (0); 15 | 16 | if (!tree->left && !tree->right) 17 | return (0); 18 | nodes += binary_tree_nodes(tree->left); 19 | nodes += binary_tree_nodes(tree->right); 20 | nodes += (tree->right || tree->left) ? 1 : 0; 21 | return (nodes); 22 | } 23 | -------------------------------------------------------------------------------- /0x1C-binary_trees/17-binary_tree_sibling.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_sibling - finds the sibling of a node in a binary tree 5 | * @node: node to find sibling for 6 | * 7 | * Return: pointer to sibling node 8 | */ 9 | binary_tree_t *binary_tree_sibling(binary_tree_t *node) 10 | { 11 | if (!node || !node->parent) 12 | return (NULL); 13 | if (node->parent->left == node) 14 | return (node->parent->right); 15 | else 16 | return (node->parent->left); 17 | } 18 | -------------------------------------------------------------------------------- /0x1C-binary_trees/18-binary_tree_uncle.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | binary_tree_t *get_sibling(binary_tree_t *node); 4 | 5 | /** 6 | * binary_tree_uncle - finds the uncle of a node in a binary tree 7 | * @node: node to find uncle for 8 | * 9 | * Return: pointer to uncle node 10 | */ 11 | binary_tree_t *binary_tree_uncle(binary_tree_t *node) 12 | { 13 | if (!node) 14 | return (NULL); 15 | return (get_sibling(node->parent)); 16 | } 17 | 18 | 19 | /** 20 | * get_sibling - gets the sibling node to find uncle 21 | * @node: to get sibling from 22 | * 23 | * Return: sibling of node 24 | */ 25 | binary_tree_t *get_sibling(binary_tree_t *node) 26 | { 27 | if (!node || !node->parent) 28 | return (NULL); 29 | if (node->parent->left == node) 30 | return (node->parent->right); 31 | else 32 | return (node->parent->left); 33 | } 34 | -------------------------------------------------------------------------------- /0x1C-binary_trees/3-binary_tree_delete.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | #include 3 | 4 | /** 5 | * binary_tree_delete - frees a binary tree from memory 6 | * @tree: root of tree to delete 7 | */ 8 | void binary_tree_delete(binary_tree_t *tree) 9 | { 10 | /* BASE CASE */ 11 | if (!tree) 12 | return; 13 | 14 | binary_tree_delete(tree->left); 15 | binary_tree_delete(tree->right); 16 | free(tree); 17 | } 18 | -------------------------------------------------------------------------------- /0x1C-binary_trees/4-binary_tree_is_leaf.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_leaf - checks if a tree node is a leaf 5 | * @node: node to check 6 | * 7 | * Return: 1 (is leaf), 0 (otherwise -- e.g null node or non leaf) 8 | */ 9 | int binary_tree_is_leaf(const binary_tree_t *node) 10 | { 11 | return ((node && !node->right && !node->left) ? 1 : 0); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-binary_trees/5-binary_tree_is_root.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_root - checks if a tree node is a root node 5 | * @node: node to check 6 | * 7 | * Return: 1 (is root), 0 (otherwise -- e.g null node or non root) 8 | */ 9 | int binary_tree_is_root(const binary_tree_t *node) 10 | { 11 | return ((node && !node->parent) ? 1 : 0); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-binary_trees/6-binary_tree_preorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_preorder - traverses a tree in pre-order calling func 5 | * @tree: const pointer to root node to begin traversal at 6 | * @func: function to call on tree node's data 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/7-binary_tree_inorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_inorder - traverses a tree in in-order calling func 5 | * @tree: const pointer to root node to begin traversal at 6 | * @func: function to call on tree node's data 7 | */ 8 | void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (!tree || !func) 11 | return; 12 | 13 | binary_tree_inorder(tree->left, func); 14 | func(tree->n); 15 | binary_tree_inorder(tree->right, func); 16 | } 17 | -------------------------------------------------------------------------------- /0x1C-binary_trees/8-binary_tree_postorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_postorder - traverses a tree in post-order calling func 5 | * @tree: const pointer to root node to begin traversal at 6 | * @func: function to call on tree node's data 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 | -------------------------------------------------------------------------------- /0x1C-binary_trees/9-binary_tree_height.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_height - gets the height of a binary tree 5 | * @tree: root node to draw height from for tree 6 | * 7 | * Return: size_t representing height, 0 on failure or NULL 8 | */ 9 | size_t binary_tree_height(const binary_tree_t *tree) 10 | { 11 | size_t l_size = 0; 12 | size_t r_size = 0; 13 | 14 | if (!tree) 15 | return (0); 16 | 17 | l_size += (tree->left) ? 1 + binary_tree_height(tree->left) : 0; 18 | r_size += (tree->right) ? 1 + binary_tree_height(tree->right) : 0; 19 | return ((l_size > r_size) ? l_size : r_size); 20 | } 21 | -------------------------------------------------------------------------------- /0x1C-binary_trees/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../binary_trees.h" 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | binary_tree_t *root; 12 | 13 | root = binary_tree_node(NULL, 98); 14 | 15 | root->left = binary_tree_node(root, 12); 16 | root->left->left = binary_tree_node(root->left, 6); 17 | root->left->right = binary_tree_node(root->left, 16); 18 | 19 | root->right = binary_tree_node(root, 402); 20 | root->right->left = binary_tree_node(root->right, 256); 21 | root->right->right = binary_tree_node(root->right, 512); 22 | 23 | binary_tree_print(root); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x1C-binary_trees/tests/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 12); 16 | root->right = binary_tree_node(root, 402); 17 | binary_tree_print(root); 18 | printf("\n"); 19 | binary_tree_insert_left(root->right, 128); 20 | binary_tree_insert_left(root, 54); 21 | binary_tree_print(root); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x1C-binary_trees/tests/10-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t depth; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | depth = binary_tree_depth(root); 23 | printf("Depth of %d: %lu\n", root->n, depth); 24 | depth = binary_tree_depth(root->right); 25 | printf("Depth of %d: %lu\n", root->right->n, depth); 26 | depth = binary_tree_depth(root->left->right); 27 | printf("Depth of %d: %lu\n", root->left->right->n, depth); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1C-binary_trees/tests/13-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t nodes; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | nodes = binary_tree_nodes(root); 23 | printf("Nodes in %d: %lu\n", root->n, nodes); 24 | nodes = binary_tree_nodes(root->right); 25 | printf("Nodes in %d: %lu\n", root->right->n, nodes); 26 | nodes = binary_tree_nodes(root->left->right); 27 | printf("Nodes in %d: %lu\n", root->left->right->n, nodes); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1C-binary_trees/tests/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 12); 16 | root->right = binary_tree_node(root, 402); 17 | binary_tree_print(root); 18 | printf("\n"); 19 | binary_tree_insert_right(root->left, 54); 20 | binary_tree_insert_right(root, 128); 21 | binary_tree_print(root); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x1C-binary_trees/tests/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 12); 16 | root->right = binary_tree_node(root, 402); 17 | binary_tree_insert_right(root->left, 54); 18 | binary_tree_insert_right(root, 128); 19 | binary_tree_print(root); 20 | binary_tree_delete(root); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x1C-binary_trees/tests/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int ret; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | ret = binary_tree_is_leaf(root); 23 | printf("Is %d a leaf: %d\n", root->n, ret); 24 | ret = binary_tree_is_leaf(root->right); 25 | printf("Is %d a leaf: %d\n", root->right->n, ret); 26 | ret = binary_tree_is_leaf(root->right->right); 27 | printf("Is %d a leaf: %d\n", root->right->right->n, ret); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1C-binary_trees/tests/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int ret; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | ret = binary_tree_is_root(root); 23 | printf("Is %d a root: %d\n", root->n, ret); 24 | ret = binary_tree_is_root(root->right); 25 | printf("Is %d a root: %d\n", root->right->n, ret); 26 | ret = binary_tree_is_root(root->right->right); 27 | printf("Is %d a root: %d\n", root->right->right->n, ret); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * linear_search - linear search implementation for arrays 5 | * @array: point to input array 6 | * @size: size of array as size_t 7 | * @value: value to search for 8 | * 9 | * Return: index in array where value is located 10 | */ 11 | int linear_search(int *array, size_t size, int value) 12 | { 13 | int i; 14 | int _size = (int)size; 15 | 16 | if (!array || size <= 0) 17 | return (-1); 18 | for (i = 0; i < _size; i++) 19 | { 20 | printf("Value checked array[%d] = [%d]\n", i, array[i]); 21 | if (array[i] == value) 22 | return (i); 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * binary_search - binary search on a sorted array 6 | * @array: array to search (sorted) 7 | * @size: size of array 8 | * @value: value to search for 9 | * 10 | * Return: index of value if found, -1 on error or not found 11 | */ 12 | int binary_search(int *array, size_t size, int value) 13 | { 14 | if (!array || size <= 0) 15 | return (-1); 16 | /* do binary search */ 17 | /* TODO */ 18 | 19 | /* failed */ 20 | return (-1); 21 | } 22 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); 18 | printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1D-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef __SEARCH_ALGO_H__ 2 | #define __SEARCH_ALGO_H__ 3 | int linear_search(int *array, size_t size, int value); 4 | int binary_search(int *array, size_t size, int value); 5 | #endif /* __SEARCH_ALGO_H__ */ 6 | --------------------------------------------------------------------------------