├── 0x00-hello_world ├── .3-name.swp ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── .main.h.swp ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── main.h ├── 0x08-recursion ├── .0-puts_recursion.c.swp ├── .2-strlen_recursion.c.swp ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md └── main.h ├── 0x09-static_libraries ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── liball.a ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── add ├── args ├── change ├── mul ├── mynewnameis └── nargs ├── 0x0B-malloc_free ├── 0-create_array.c ├── 0-main.c ├── 1-main.c ├── 1-strdup.c ├── 100-argstostr.c ├── 100-main.c ├── 101-main.c ├── 101-strtow.c ├── 2-main.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 3-main.c ├── 4-free_grid.c ├── 4-main.c ├── README.md ├── a ├── args ├── c ├── f ├── g ├── main.h ├── s └── strtow ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 0x0C-more_malloc_free - Shortcut.lnk ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── _putchar.c └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md ├── lists.h └── master ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 103-find_loop.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 00-get_endianness.c ├── 1-print_binary.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md └── main.h ├── 0x15-file_io ├── 0-main.c ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md ├── Requiescat ├── a └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md ├── lists.h └── palindrome.py ├── 0x18-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 100-test.py ├── 101-make_me_win.sh ├── README.md ├── TEST │ ├── _putchar.c │ ├── _putchar.o │ ├── abs.c │ ├── abs.o │ ├── atoi.c │ ├── atoi.o │ ├── isalpha.c │ ├── isalpha.o │ ├── isdigit.c │ ├── isdigit.o │ ├── islower.c │ ├── islower.o │ ├── isupper.c │ ├── isupper.o │ ├── libfilename.so │ ├── main.h │ ├── memcpy.c │ ├── memcpy.o │ ├── memset.c │ ├── memset.o │ ├── puts.c │ ├── puts.o │ ├── strcat.c │ ├── strcat.o │ ├── strchr.c │ ├── strchr.o │ ├── strcmp.c │ ├── strcmp.o │ ├── strcpy.c │ ├── strcpy.o │ ├── strlen.c │ ├── strlen.o │ ├── strncat.c │ ├── strncat.o │ ├── strncpy.c │ ├── strncpy.o │ ├── strpbrk.c │ ├── strpbrk.o │ ├── strspn.c │ ├── strspn.o │ ├── strstr.c │ └── strstr.o ├── iwin.c ├── iwin.so ├── libdynamic.so ├── main.h └── test.c ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── m.h ├── main.c └── school.c ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential ├── 103-exponential.c ├── 104-advanced_binary ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md ├── listint │ ├── create_list.c │ ├── free_list.c │ └── print_list.c ├── search_algos.h ├── skiplist │ ├── create_skiplist.c │ ├── free_skiplist.c │ └── print_skiplist.c └── test │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 102-main.c │ ├── 103-main.c │ ├── 104-main.c │ ├── 105-main.c │ └── 106-main.c └── README.md /0x00-hello_world/.3-name.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x00-hello_world/.3-name.swp -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -E -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 101-quote.c 3 | * Auth: Brennan D Baraban 4 | */ 5 | 6 | #include 7 | 8 | /** 9 | * main - Prints "and that piece of art is useful" - Dora Korpar, 2015-10-19", 10 | * followed by a new line, to standard error. 11 | * 12 | * Return: Always 1. 13 | */ 14 | int main(void) 15 | { 16 | write(2, 17 | "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 18 | 59); 19 | 20 | return (1); 21 | } 22 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main -Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | puts("\"Programming is like building a multilingual puzzle"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /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 | printf("with proper grammar, but the outcome is a piece of art,\n"); 10 | return (0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | * 8 | */ 9 | int main(void) 10 | { 11 | int a; 12 | long int b; 13 | long long int c; 14 | char d; 15 | float f; 16 | 17 | printf("Size of a char: %lu byte(s)\n", (unsigned long)sizeof(d)); 18 | printf("Size of an int: %lu byte(s)\n", (unsigned long)sizeof(a)); 19 | printf("Size of a long int: %lu byte(s)\n", (unsigned long)sizeof(b)); 20 | printf("Size of a long long int: %lu byte(s)\n", (unsigned long)sizeof(c)); 21 | printf("Size of a float: %lu byte(s)\n", (unsigned long)sizeof(f)); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints a random number and states whether 7 | * it is positive, negative, or zero. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | int n; 14 | 15 | srand(time(0)); 16 | n = rand() - RAND_MAX / 2; 17 | 18 | if (n > 0) 19 | printf("%d is positive\n", n); 20 | else if (n < 0) 21 | printf("%d is negative\n", n); 22 | else 23 | printf("%d is zero\n", n); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | *main -> assign a random number to the variable n each time it is executed 7 | *and print the last digit of the number stored in the variable n 8 | *Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | int x; 14 | 15 | srand(time(0)); 16 | n = rand() - RAND_MAX / 2; 17 | 18 | x = n % 10; 19 | printf("Last digit of %d is %d ", n, x); 20 | if (x > 5) 21 | { 22 | printf("and is greater than 5"); 23 | } 24 | if (x == 0) 25 | { 26 | printf("and is 0"); 27 | } 28 | if (x < 6 && x != 0) 29 | { 30 | printf("and is less than 6 and not 0"); 31 | } 32 | 33 | printf("\n"); 34 | 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints all possible combinations of two different digits, 5 | * in ascending order, separated by a comma followed by a space. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int digit1, digit2; 12 | 13 | for (digit1 = 0; digit1 < 9; digit1++) 14 | { 15 | for (digit2 = digit1 + 1; digit2 < 10; digit2++) 16 | { 17 | putchar((digit1 % 10) + '0'); 18 | putchar((digit2 % 10) + '0'); 19 | 20 | if (digit1 == 8 && digit2 == 9) 21 | continue; 22 | putchar(','); 23 | putchar(' '); 24 | } 25 | } 26 | 27 | putchar('\n'); 28 | 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints all possible combinations of three different digits, 5 | * in ascending order, separated by a comma followed by a space. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int digit1, digit2, digit3; 12 | 13 | for (digit1 = 0; digit1 < 8; digit1++) 14 | { 15 | for (digit2 = digit1 + 1; digit2 < 9; digit2++) 16 | { 17 | for (digit3 = digit2 + 1; digit3 < 10; digit3++) 18 | { 19 | putchar((digit1 % 10) + '0'); 20 | putchar((digit2 % 10) + '0'); 21 | putchar((digit3 % 10) + '0'); 22 | 23 | if (digit1 == 7 && digit2 == 8 && digit3 == 9) 24 | continue; 25 | putchar(','); 26 | putchar(' '); 27 | } 28 | } 29 | } 30 | 31 | putchar('\n'); 32 | 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - Prints all possible combinations of two two-digit numbers, 5 | * ranging from 0-99, separated by a comma followed by a space. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int i, j; 12 | 13 | for (i = 0; i < 100; i++) 14 | { 15 | for (j = 0; j < 100; j++) 16 | { 17 | if (i < j) 18 | { 19 | putchar((i / 10) + 48); 20 | putchar((i % 10) + 48); 21 | putchar(' '); 22 | putchar((j / 10) + 48); 23 | putchar((j % 10) + 48); 24 | if (i != 98 || j != 99) 25 | { 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | } 31 | } 32 | putchar('\n'); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the alphabet in lowercase. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | putchar(letter); 14 | 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the alphabet in lowercase, and then in uppercase. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | putchar(letter); 14 | 15 | for (letter = 'A'; letter <= 'Z'; letter++) 16 | putchar(letter); 17 | 18 | putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the alphabet in lowercase, except for q and e. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | { 14 | if (letter != 'e' && letter != 'q') 15 | putchar(letter); 16 | } 17 | 18 | putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main -> assign a random number to the variable n each time it is executed 5 | *and print the last digit of the number stored in the variable n 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = 0; n < 10; n++) 13 | { 14 | printf("%d", n); 15 | } 16 | printf("\n"); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints all single digit numbers of base 10 starting from 0, 5 | * only using putchar and without char variables. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int num; 12 | 13 | for (num = 0; num < 10; num++) 14 | putchar((num % 10) + '0'); 15 | 16 | putchar('\n'); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the lowercase alphabet in reverse. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | char letter; 11 | 12 | for (letter = 'z'; letter >= 'a'; letter--) 13 | putchar(letter); 14 | 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints all the numbers of base 16 in lowercase. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int num; 11 | char letter; 12 | 13 | for (num = 0; num < 10; num++) 14 | putchar((num % 10) + '0'); 15 | 16 | for (letter = 'a'; letter <= 'f'; letter++) 17 | putchar(letter); 18 | 19 | putchar('\n'); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints all possible combinations of single-digit numbers. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int num; 11 | 12 | for (num = 0; num <= 9; num++) 13 | { 14 | putchar((num % 10) + '0'); 15 | if (num == 9) 16 | continue; 17 | putchar(','); 18 | putchar(' '); 19 | } 20 | 21 | putchar('\n'); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.main.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x02-functions_nested_loops/.main.h.swp -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Print _putchar 5 | * Return: Always 0 6 | */ 7 | int main(void) 8 | { 9 | _putchar('_'); 10 | _putchar('p'); 11 | _putchar('u'); 12 | _putchar('t'); 13 | _putchar('c'); 14 | _putchar('h'); 15 | _putchar('a'); 16 | _putchar('r'); 17 | _putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - prints the alphabet in lowercase, followed by a new line. 5 | */ 6 | void print_alphabet(void) 7 | { 8 | char letter; 9 | 10 | for (letter = 'a'; letter <= 'z'; letter++) 11 | _putchar(letter); 12 | 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - Adds two integers. 5 | * @num1: The first integer to be added. 6 | * @num2: The second integer to be added. 7 | * 8 | * Return: The result of the addition. 9 | */ 10 | int add(int num1, int num2) 11 | { 12 | return (num1 + num2); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - Prints the times table of the input, 5 | * starting with 0. 6 | * @n: The value of the times table to be printed. 7 | */ 8 | void print_times_table(int n) 9 | { 10 | int num, mult, prod; 11 | 12 | if (n >= 0 && n <= 15) 13 | { 14 | for (num = 0; num <= n; num++) 15 | { 16 | _putchar('0'); 17 | 18 | for (mult = 1; mult <= n; mult++) 19 | { 20 | _putchar(','); 21 | _putchar(' '); 22 | 23 | prod = num * mult; 24 | 25 | if (prod <= 99) 26 | _putchar(' '); 27 | if (prod <= 9) 28 | _putchar(' '); 29 | 30 | if (prod >= 100) 31 | { 32 | _putchar((prod / 100) + '0'); 33 | _putchar(((prod / 10)) % 10 + '0'); 34 | } 35 | else if (prod <= 99 && prod >= 10) 36 | { 37 | _putchar((prod / 10) + '0'); 38 | } 39 | _putchar((prod % 10) + '0'); 40 | } 41 | _putchar('\n'); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Lists all the natural numbers below 1024 (excluded) 5 | * that are multiples of 3 or 5. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int i, sum = 0; 12 | 13 | for (i = 0; i < 1024; i++) 14 | { 15 | if ((i % 3) == 0 || (i % 5) == 0) 16 | sum += i; 17 | } 18 | 19 | printf("%d\n", sum); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints first 50 Fibonacci numbers, starting with 1 and 2, 5 | * separated by a comma followed by a space. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int count; 12 | unsigned long fib1 = 0, fib2 = 1, sum; 13 | 14 | for (count = 0; count < 50; count++) 15 | { 16 | sum = fib1 + fib2; 17 | printf("%lu", sum); 18 | 19 | fib1 = fib2; 20 | fib2 = sum; 21 | 22 | if (count == 49) 23 | printf("\n"); 24 | else 25 | printf(", "); 26 | } 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the sum of even-valued Fibonacci sequence 5 | * terms not exceeding 4000000. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long fib1 = 0, fib2 = 1, fibsum; 12 | float tot_sum; 13 | 14 | while (1) 15 | { 16 | fibsum = fib1 + fib2; 17 | if (fibsum > 4000000) 18 | break; 19 | 20 | if ((fibsum % 2) == 0) 21 | tot_sum += fibsum; 22 | 23 | fib1 = fib2; 24 | fib2 = fibsum; 25 | } 26 | printf("%.0f\n", tot_sum); 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the first 98 Fibonacci numbers, starting with 5 | * 1 and 2, separated by a comma followed by a space. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int count; 12 | unsigned long fib1 = 0, fib2 = 1, sum; 13 | unsigned long fib1_half1, fib1_half2, fib2_half1, fib2_half2; 14 | unsigned long half1, half2; 15 | 16 | for (count = 0; count < 92; count++) 17 | { 18 | sum = fib1 + fib2; 19 | printf("%lu, ", sum); 20 | 21 | fib1 = fib2; 22 | fib2 = sum; 23 | } 24 | 25 | fib1_half1 = fib1 / 10000000000; 26 | fib2_half1 = fib2 / 10000000000; 27 | fib1_half2 = fib1 % 10000000000; 28 | fib2_half2 = fib2 % 10000000000; 29 | 30 | for (count = 93; count < 99; count++) 31 | { 32 | half1 = fib1_half1 + fib2_half1; 33 | half2 = fib1_half2 + fib2_half2; 34 | if (fib1_half2 + fib2_half2 > 9999999999) 35 | { 36 | half1 += 1; 37 | half2 %= 10000000000; 38 | } 39 | 40 | printf("%lu%lu", half1, half2); 41 | if (count != 98) 42 | printf(", "); 43 | 44 | fib1_half1 = fib2_half1; 45 | fib1_half2 = fib2_half2; 46 | fib2_half1 = half1; 47 | fib2_half2 = half2; 48 | } 49 | printf("\n"); 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_to_98 - Prints all natural numbers from input to 98, 5 | * in order separated by a comma followed by a space. 6 | * @n: The number to begin counting at. 7 | */ 8 | void print_to_98(int n) 9 | { 10 | if (n >= 98) 11 | { 12 | while (n > 98) 13 | printf("%d, ", n--); 14 | printf("%d\n", n); 15 | } 16 | 17 | else 18 | { 19 | while (n < 98) 20 | printf("%d, ", n++); 21 | printf("%d\n", n); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Prints 10 times the alphabet, in lowercase. 5 | */ 6 | void print_alphabet_x10(void) 7 | { 8 | int count = 0; 9 | char letter; 10 | 11 | while (count++ <= 9) 12 | { 13 | for (letter = 'a'; letter <= 'z'; letter++) 14 | _putchar(letter); 15 | _putchar('\n'); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Checks if a character is lowercase. 5 | * @c: The character to be checked. 6 | * 7 | * Return: 1 if character is lowercase, 0 otherwise. 8 | */ 9 | int _islower(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Checks if a character is alphabetic. 5 | * @c: The character to be checked. 6 | * 7 | * Return: 1 if character is letter, lowercase or uppercase, 0 otherwise. 8 | */ 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || 12 | (c >= 'A' && c <= 'Z')) 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - Prints the sign of a number. 5 | * @n: The number of which the sign will be printed. 6 | * 7 | * Return: 1 if the number is greater than zero, 8 | * 0 if the number is zero, 9 | * -1 if the number is less than zero. 10 | */ 11 | int print_sign(int n) 12 | { 13 | if (n > 0) 14 | { 15 | _putchar('+'); 16 | return (1); 17 | } 18 | else if (n == 0) 19 | { 20 | _putchar('0'); 21 | return (0); 22 | } 23 | else 24 | { 25 | _putchar('-'); 26 | return (-1); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Computes the absolute value of an integer. 5 | * @n: The integer to be computed. 6 | * 7 | * Return: The absolute value of the integer. 8 | */ 9 | int _abs(int n) 10 | { 11 | if (n >= 0) 12 | return (n); 13 | else 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - Prints the last digit of a number. 5 | * @n: The number in question. 6 | * 7 | * Return: Value of the last digit. 8 | */ 9 | int print_last_digit(int n) 10 | { 11 | int last_digit = n % 10; 12 | 13 | if (last_digit < 0) 14 | last_digit *= -1; 15 | 16 | _putchar(last_digit + '0'); 17 | 18 | return (last_digit); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - Prints every minute of the day of 5 | * Jack Bauer, starting from 00:00 to 23:59. 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int hour, minute; 10 | 11 | for (hour = 0; hour <= 23; hour++) 12 | { 13 | for (minute = 0; minute <= 59; minute++) 14 | { 15 | _putchar((hour / 10) + '0'); 16 | _putchar((hour % 10) + '0'); 17 | _putchar(':'); 18 | _putchar((minute / 10) + '0'); 19 | _putchar((minute % 10) + '0'); 20 | _putchar('\n'); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - Prints the 9 times table, starting with 0. 5 | */ 6 | void times_table(void) 7 | { 8 | int num, mult, prod; 9 | 10 | for (num = 0; num <= 9; num++) 11 | { 12 | _putchar('0'); 13 | 14 | for (mult = 1; mult <= 9; mult++) 15 | { 16 | _putchar(','); 17 | _putchar(' '); 18 | 19 | prod = num * mult; 20 | 21 | if (prod <= 9) 22 | _putchar(' '); 23 | else 24 | _putchar((prod / 10) + '0'); 25 | 26 | _putchar((prod % 10) + '0'); 27 | } 28 | _putchar('\n'); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | 5 | int _putchar(char c); 6 | void print_alphabet(void); 7 | void print_alphabet_x10(void); 8 | int _islower(int c); 9 | int _isalpha(int c); 10 | int print_sign(int n); 11 | int _abs(int); 12 | int print_last_digit(int); 13 | void jack_bauer(void); 14 | void times_table(void); 15 | int add(int, int); 16 | void print_to_98(int n); 17 | void print_times_table(int n); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /* while (i < 10)*/ 17 | /* {*/ 18 | /* putchar(i);*/ 19 | /* }*/ 20 | 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a > b && a > c) 16 | { 17 | largest = a; 18 | } 19 | else if (b > c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | 28 | return (largest); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if (year % 4 == 0 || (year % 400 == 0 && year % 100 == 0)) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - uppercase letters 5 | * @c: character to check 6 | * 7 | * Return: 0 or 1 8 | */ 9 | int _isupper(int c) 10 | 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | return (1); 14 | else 15 | return (0); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - write a function that check for a digit (0 through 9). 5 | * @c: char to check 6 | * Return: 0 or 1 7 | **/ 8 | int _isdigit(int c) 9 | { 10 | if (c >= '0' && c <= '9') 11 | return (1); 12 | else 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - entry point 5 | * owned by Bwave/Bright Daniel 6 | * Description: Prints diagonals 7 | *@size: size of the triangle 8 | * Return: void 9 | */ 10 | 11 | void print_triangle(int size) 12 | { 13 | int row, hashes, spaces; 14 | 15 | if (size <= 0) 16 | { 17 | _putchar('\n'); 18 | } 19 | else 20 | { 21 | for (row = 1; row <= size; row++) 22 | { 23 | for (spaces = size - row; spaces >= 1; spaces--) 24 | { 25 | _putchar(' '); 26 | } 27 | for (hashes = 1; hashes <= row; hashes++) 28 | { 29 | _putchar('#'); 30 | } 31 | _putchar('\n'); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * main - prints the largest prime factor of 612852475143 5 | * Bwave ICT / Bright Daniel 6 | * Return: always 0 7 | **/ 8 | 9 | int main(void) 10 | { 11 | unsigned long int i, n = 612852475143; 12 | 13 | for (i = 3; i < 782849; i = i + 2) 14 | { 15 | while ((n % i == 0) && (n != i)) 16 | n = n / i; 17 | } 18 | printf("%lu\n", n); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_number - prints an integer 4 | * @n:.input integer parameter 5 | * 6 | *Owned by Bwave ICT/ Bright Daniel 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int i = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar(45); 15 | i = -i; 16 | } 17 | if (i / 10) 18 | { 19 | print_number(i / 10); 20 | } 21 | _putchar(i % 10 + '0'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * mul - multiplies two integers 5 | * @a: first integer 6 | * @b: second integer 7 | * Return: addition of a and b 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *print_numbers - print 0123456789 5 | * owned by Bwave ICT 6 | *Return: void 7 | */ 8 | 9 | void print_numbers(void) 10 | { 11 | char c; 12 | 13 | for (c = '0'; c <= '9'; c++) 14 | { 15 | _putchar(c); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *print_most_numbers - prints 5 | * owned by Bwave ICT 6 | *Return: void 7 | */ 8 | 9 | void print_most_numbers(void) 10 | { 11 | char c; 12 | 13 | for (c = '0'; c <= '9'; c++) 14 | { 15 | if (!(c == '2' || c == '4')) 16 | _putchar(c); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - nmbr fct 5 | * Bwave ICT file 6 | * Return: no return 7 | */ 8 | 9 | void more_numbers(void) 10 | { 11 | 12 | int i, j; 13 | for (i = 1; i <= 10; i++) 14 | { 15 | for (j = 0; j <= 14; j++) 16 | { 17 | if (j >= 10) 18 | _putchar('1'); 19 | _putchar(j % 10 + '0'); 20 | } 21 | _putchar('\n'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - printing lines 5 | * @n: number of times 6 | * 7 | * Return: void 8 | */ 9 | 10 | 11 | void print_line(int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | _putchar('_'); 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - printing lines diagonal 5 | * @n: input variable 6 | * 7 | * Return: void 8 | */ 9 | 10 | 11 | void print_diagonal(int n) 12 | { 13 | int i, j; 14 | 15 | if (n <= 0) 16 | _putchar('\n'); 17 | for (i = 0; i < n; i++) 18 | { 19 | for (j = 0; j < i; j++) 20 | { 21 | _putchar(' '); 22 | } 23 | _putchar('\\'); 24 | _putchar('\n'); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints a square of size size 5 | * @size: size of the square 6 | * 7 | * Return: void 8 | */ 9 | 10 | 11 | void print_square(int size) 12 | { 13 | int i, j; 14 | 15 | if (size <= 0) 16 | _putchar('\n'); 17 | 18 | for (i = 0; i < size; i++) 19 | { 20 | for (j = 0; j < (size); j++) 21 | { 22 | _putchar('#'); 23 | } 24 | _putchar('\n'); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - entry point 6 | * 7 | * Description: prints the numbers 1 - 100 w/ 8 | * Fizz for multiples of 3, Buzz for multiples of 5, 9 | * and FizzBuzz for multiples of both 10 | * 11 | * Return: always 0 12 | */ 13 | int main(void) 14 | { 15 | int i; 16 | 17 | for (i = 1; i <= 100; i++) 18 | { 19 | if (i % 15 == 0) 20 | printf("FizzBuzz"); 21 | else if (i % 3 == 0) 22 | printf("Fizz"); 23 | else if (i % 5 == 0) 24 | printf("Buzz"); 25 | else 26 | printf("%i", i); 27 | if (i < 100) 28 | printf(" "); 29 | } 30 | printf("\n"); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | who de check 2 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | 5 | int _putchar(char c); 6 | int _isupper(int c); 7 | int _isdigit(int c); 8 | int mul(int a, int b); 9 | void print_numbers(void); 10 | void print_most_numbers(void); 11 | void more_numbers(void); 12 | void print_line(int n); 13 | void print_diagonal(int n); 14 | void print_square(int size); 15 | void print_triangle(int size); 16 | void print_number(int n); 17 | 18 | 19 | #endif 20 | /* 21 | Bwave ICT 22 | */ 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reset_to_98 - takes the pointer and updates the value 4 | * @n: int to check 5 | * Owned By Bwave/Bright 6 | * Return: 0 is success 7 | */ 8 | 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | } 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * swap_int - swaps the value of int a and int b 4 | * Owned by Bwave/Bright 5 | * @a: first int 6 | * @b: second int 7 | * Return 0 8 | */ 9 | void swap_int(int *a, int *b) 10 | { 11 | int s; 12 | 13 | s = *a; 14 | *a = *b; 15 | *b = s; 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - Convert a string to an integer. 5 | * @s: The pointer to convert 6 | * 7 | * Return: A integer 8 | */ 9 | int _atoi(char *s) 10 | { 11 | int c = 0; 12 | unsigned int ni = 0; 13 | int min = 1; 14 | int isi = 0; 15 | 16 | while (s[c]) 17 | { 18 | if (s[c] == 45) 19 | { 20 | min *= -1; 21 | } 22 | 23 | while (s[c] >= 48 && s[c] <= 57) 24 | { 25 | isi = 1; 26 | ni = (ni * 10) + (s[c] - '0'); 27 | c++; 28 | } 29 | 30 | if (isi == 1) 31 | { 32 | break; 33 | } 34 | 35 | c++; 36 | } 37 | 38 | ni *= min; 39 | return (ni); 40 | } 41 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Generates random valid passwords for the 7 | * program 101-crackme. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | char password[84]; 14 | int index = 0, sum = 0, diff_half1, diff_half2; 15 | 16 | srand(time(0)); 17 | 18 | while (sum < 2772) 19 | { 20 | password[index] = 33 + rand() % 94; 21 | sum += password[index++]; 22 | } 23 | 24 | password[index] = '\0'; 25 | 26 | if (sum != 2772) 27 | { 28 | diff_half1 = (sum - 2772) / 2; 29 | diff_half2 = (sum - 2772) / 2; 30 | if ((sum - 2772) % 2 != 0) 31 | diff_half1++; 32 | 33 | for (index = 0; password[index]; index++) 34 | { 35 | if (password[index] >= (33 + diff_half1)) 36 | { 37 | password[index] -= diff_half1; 38 | break; 39 | } 40 | } 41 | for (index = 0; password[index]; index++) 42 | { 43 | if (password[index] >= (33 + diff_half2)) 44 | { 45 | password[index] -= diff_half2; 46 | break; 47 | } 48 | } 49 | } 50 | 51 | printf("%s", password); 52 | 53 | return (0); 54 | } 55 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen - return the length of a string 4 | * Owned By Bwave 5 | * @s: char to check 6 | * Description: this will return the length of a string 7 | * Return: 0 is success 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int a = 0; 12 | 13 | for (; *s++;) 14 | a++; 15 | return (a); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string 5 | * @str: the string 6 | * 7 | * Return: the length of the string 8 | */ 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i] != '\0') 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev -> printing a string in reverse 5 | * @s: the string to be printed in rev 6 | */ 7 | 8 | void print_rev(char *s) 9 | { 10 | int i, n; 11 | 12 | n = 0; 13 | while (s[n] != '\0') 14 | n++; 15 | 16 | for (i = n - 1; i >= 0; i--) 17 | { 18 | _putchar(s[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rev_string - reverses a string 4 | * _putchar - print each character 5 | * @s: char to check 6 | * 7 | * Description: This will reverse a string 8 | * Return: 0 is success 9 | */ 10 | void rev_string(char *s) 11 | { 12 | int a = 0, b, c; 13 | char d; 14 | 15 | while (s[a] != '\0') 16 | { 17 | a++; 18 | } 19 | c = a - 1; 20 | for (b = 0; c >= 0 && b < c; c--, b++) 21 | { 22 | d = s[b]; 23 | s[b] = s[c]; 24 | s[c] = d; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * puts2 - prints every other character of a string 4 | * 5 | * @str: char to check 6 | * 7 | * Return: 0 is success 8 | */ 9 | void puts2(char *str) 10 | { 11 | int string; 12 | 13 | for (string = 0; str[string] != '\0'; string++) 14 | if (string % 2 == 0) 15 | _putchar(str[string]); 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * puts_half - prints half of a string 4 | * @str: char to check 5 | * 6 | * Return: 0 is success 7 | */ 8 | void puts_half(char *str) 9 | { 10 | int string = 0, n; 11 | 12 | while (str[string] != '\0') 13 | string++; 14 | if (string + 1 % 2 != '0') 15 | n = (string - 1) / 2; 16 | else 17 | n = (string / 2); 18 | n++; 19 | 20 | for (string = n; str[string] != '\0'; string++) 21 | _putchar(str[string]); 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_array - prints n element of an array of integers 5 | * Owned by Bwave 6 | * @a: int to check 7 | * @n: int to check 8 | * Return: 0 is success 9 | */ 10 | void print_array(int *a, int n) 11 | { 12 | int x; 13 | 14 | for (x = 0; x < n; x++) 15 | if (x != n - 1) 16 | printf("%d, ", a[x]); 17 | else 18 | printf("%d", a[x]); 19 | printf("\n"); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - copy the string pointed to by src to dest 4 | * @dest: char to check 5 | * @src: char to check 6 | * Return: 0 is success 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int a; 11 | 12 | for (a = 0; src[a] != '\0'; a++) 13 | dest[a] = src[a]; 14 | dest[a] = '\0'; 15 | return (dest); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | Readme file 2 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void reset_to_98(int *n); 5 | void swap_int(int *a, int *b); 6 | int _strlen(char *s); 7 | void _puts(char *str); 8 | void print_rev(char *s); 9 | void rev_string(char *s); 10 | void puts2(char *str); 11 | void puts_half(char *str); 12 | void print_array(int *a, int n); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | int _putchar(char c); 16 | 17 | 18 | #endif /*Bwave Bright*/ 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concat 2 string 5 | * @dest:char 6 | * @src:char 7 | * Return:char 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | char *s = dest; 12 | 13 | while (*dest != '\0') 14 | { 15 | dest++; 16 | } 17 | 18 | while (*src != '\0') 19 | { 20 | *dest = *src; 21 | dest++; 22 | src++; 23 | } 24 | *dest = '\0'; 25 | return (s); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates two strings 5 | * 6 | * @src: The source of strings 7 | * @dest: The destination of the string 8 | * @n: The length of int 9 | * 10 | * Return: pointer to the resulting string dest 11 | */ 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int i, j; 15 | 16 | for (i = 0; dest[i] != '\0'; i++) 17 | { 18 | continue; 19 | } 20 | for (j = 0; src[j] != '\0' && j < n; j++) 21 | { 22 | dest[i + j] = src[j]; 23 | } 24 | dest[i + j] = '\0'; 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string into rot13 5 | * @s: string to encode 6 | * 7 | * Return: address of s 8 | */ 9 | char *rot13(char *s) 10 | { 11 | int i, j; 12 | char a[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 13 | char b[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; 14 | 15 | for (i = 0; *(s + i); i++) 16 | { 17 | for (j = 0; j < 52; j++) 18 | { 19 | if (a[j] == *(s + i)) 20 | { 21 | *(s + i) = b[j]; 22 | break; 23 | } 24 | } 25 | } 26 | return (s); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - Prints any integer with putchar 5 | * @n: Number to prints 6 | * 7 | * Return: Nothing 8 | */ 9 | void print_number(int n) 10 | { 11 | unsigned int x; 12 | 13 | if (n < 0) 14 | { 15 | _putchar('-'); 16 | n *= -1; 17 | } 18 | 19 | x = n; 20 | 21 | if (x / 10) 22 | print_number(x / 10); 23 | 24 | _putchar(x % 10 + '0'); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * infinite_add - adds two numbers 5 | * @n1: first number 6 | * @n2: second number 7 | * @r: buffer for result 8 | * @size_r: buffer size 9 | * ahhh! Crazy task! Expand your knowledge 10 | * Return: address of r or 0 11 | */ 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 13 | { 14 | int i, j, k, l, m, n; 15 | 16 | for (i = 0; n1[i]; i++) 17 | ; 18 | for (j = 0; n2[j]; j++) 19 | ; 20 | if (i > size_r || j > size_r) 21 | return (0); 22 | m = 0; 23 | for (i -= 1, j -= 1, k = 0; k < size_r - 1; i--, j--, k++) 24 | { 25 | n = m; 26 | if (i >= 0) 27 | n += n1[i] - '0'; 28 | if (j >= 0) 29 | n += n2[j] - '0'; 30 | if (i < 0 && j < 0 && n == 0) 31 | { 32 | break; 33 | } 34 | m = n / 10; 35 | r[k] = n % 10 + '0'; 36 | } 37 | r[k] = '\0'; 38 | if (i >= 0 || j >= 0 || m) 39 | return (0); 40 | for (k -= 1, l = 0; l < k; k--, l++) 41 | { 42 | m = r[k]; 43 | r[k] = r[l]; 44 | r[l] = m; 45 | } 46 | return (r); 47 | } 48 | /*Bwave */ 49 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_line - prints a s bytes of a buffer 6 | * @c: buffer to print 7 | * @s: bytes of buffer to print 8 | * @l: line of buffer to print 9 | * 10 | * Return: void 11 | */ 12 | 13 | void print_line(char *c, int s, int l) 14 | { 15 | int j, k; 16 | 17 | for (j = 0; j <= 9; j++) 18 | { 19 | if (j <= s) 20 | printf("%02x", c[l * 10 + j]); 21 | else 22 | printf(" "); 23 | if (j % 2) 24 | putchar(' '); 25 | } 26 | for (k = 0; k <= s; k++) 27 | { 28 | if (c[l * 10 + k] > 31 && c[l * 10 + k] < 127) 29 | putchar(c[l * 10 + k]); 30 | else 31 | putchar('.'); 32 | } 33 | } 34 | 35 | /** 36 | * print_buffer - prints a buffer 37 | * @b: buffer to print 38 | * @size: size of buffer 39 | * 40 | * Return: void 41 | */ 42 | void print_buffer(char *b, int size) 43 | { 44 | int i; 45 | 46 | for (i = 0; i <= (size - 1) / 10 && size; i++) 47 | { 48 | printf("%08x: ", i * 10); 49 | if (i < size / 10) 50 | { 51 | print_line(b, 9, i); 52 | } 53 | else 54 | { 55 | print_line(b, size % 10 - 1, i); 56 | } 57 | putchar('\n'); 58 | } 59 | if (size == 0) 60 | putchar('\n'); 61 | } 62 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strncpy - function that copies a string 5 | * 6 | * @src: The source of strings 7 | * @dest: The destination of the string 8 | * @n: The length of int 9 | * 10 | * Return: pointer to the resulting string dest 11 | */ 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | int i; 15 | 16 | for (i = 0; i < n && *(src + i); i++) 17 | { 18 | *(dest + i) = *(src + i); 19 | } 20 | for (; i < n; i++) 21 | { 22 | *(dest + i) = '\0'; 23 | } 24 | return (dest); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compare 2 string 4 | * @s1:string 5 | * @s2:strmp 6 | * Return:int 7 | * Bwave/ Bright 8 | */ 9 | int _strcmp(char *s1, char *s2) 10 | { 11 | while (((*s1 != '\0') && (*s2 != '\0')) && (*s1 == *s2)) 12 | { 13 | s1++; 14 | s2++; 15 | } 16 | 17 | if (*s1 == *s2) 18 | { 19 | return (0); 20 | } 21 | 22 | else 23 | { 24 | return (*s1 - *s2); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reverse_array - reverse array 4 | * @a:array 5 | * @n:integer 6 | * Return:void 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i, c; 11 | 12 | for (i = 0; (i < (n - 1) / 2); i++) 13 | { 14 | c = a[i]; 15 | a[i] = a[n - 1 - i]; 16 | a[n - 1 - i] = c; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * string_toupper - change lowercase to uppercase 4 | * @s:string 5 | * Return:char 6 | */ 7 | char *string_toupper(char *s) 8 | { 9 | 10 | int i; 11 | 12 | i = 0; 13 | while (*(s + i)) 14 | { 15 | if (*(s + i) >= 'a' && *(s + i) <= 'z') 16 | *(s + i) -= 'a' - 'A'; 17 | i++; 18 | } 19 | return (s); 20 | } 21 | /*Bwave ICT */ 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes all words in a string 5 | * @s: string 6 | * Return: address of s 7 | */ 8 | char *cap_string(char *s) 9 | { 10 | int i = 0, j; 11 | char a[] = " \t\n,;.!?\"(){}"; 12 | 13 | while (*(s + i)) 14 | { 15 | if (*(s + i) >= 'a' && *(s + i) <= 'z') 16 | { 17 | if (i == 0) 18 | *(s + i) -= 'a' - 'A'; 19 | else 20 | { 21 | for (j = 0; j <= 12; j++) 22 | { 23 | if (a[j] == *(s + i - 1)) 24 | *(s + i) -= 'a' - 'A'; 25 | } 26 | } 27 | } 28 | i++; 29 | } 30 | return (s); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - encodes a string into 1337 5 | * @s: string to encode 6 | * 7 | * Return: address of s 8 | */ 9 | char *leet(char *s) 10 | { 11 | int i, j; 12 | char a[] = "aAeEoOtTlL"; 13 | char b[] = "4433007711"; 14 | 15 | for (i = 0; *(s + i); i++) 16 | { 17 | for (j = 0; j <= 9; j++) 18 | { 19 | if (a[j] == s[i]) 20 | s[i] = b[j]; 21 | } 22 | } 23 | return (s); 24 | } 25 | /* Doing hard things is a must */ 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | Readme for this Projects. 2 | Pointers array and strings. 3 | Various task completed 4 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /*bwave*/ 5 | 6 | int _putchar(char c); 7 | char *_strcat(char *dest, char *src); 8 | char *_strncat(char *dest, char *src, int n); 9 | char *_strncpy(char *dest, char *src, int n); 10 | int _strcmp(char *s1, char *s2); 11 | void reverse_array(int *a, int n); 12 | char *string_toupper(char *); 13 | char *cap_string(char *); 14 | char *leet(char *); 15 | char *rot13(char *); 16 | void print_number(int n); 17 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 18 | void print_buffer(char *b, int size); 19 | 20 | #endif /*Bwave*/ 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - function fill the first @n bytes of the memory area pointed 5 | * to by @s with the constant byte @b 6 | * 7 | * @n: bytes of the memory area pointed to by @s 8 | * 9 | * @s: with the constant byte @b 10 | * 11 | * @b: memory area pointer 12 | * 13 | * Return: a pointer to the memory area @s 14 | */ 15 | char *_memset(char *s, char b, unsigned int n) 16 | { 17 | unsigned int i = 0; 18 | 19 | while (i < n) 20 | { 21 | s[i] = b; 22 | i++; 23 | } 24 | 25 | return (s); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - function copies @n bytes from memory area @src 5 | * to memory area @dest 6 | * 7 | * @n: function copies 8 | * 9 | * @src: bytes from memory area 10 | * 11 | * @dest: to memory area 12 | * 13 | * Return: a pointer to @dest 14 | */ 15 | char *_memcpy(char *dest, char *src, unsigned int n) 16 | { 17 | unsigned int i = 0; 18 | 19 | while (i < n) 20 | { 21 | dest[i] = src[i]; 22 | i++; 23 | } 24 | 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * set_string - sets the value of a pointer to a char 4 | * @s: pointer to change 5 | * @to: string to change pointer to 6 | * 7 | * Return: void 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - locates a character in a string 5 | * 6 | * @c: occurrence of the character 7 | * 8 | * @s: in the string 9 | * 10 | * Return: a pointer to the first occurrence of the character 11 | */ 12 | 13 | char *_strchr(char *s, char c) 14 | { 15 | while (*s) 16 | { 17 | if (*s != c) 18 | s++; 19 | else 20 | return (s); 21 | } 22 | if (c == '\0') 23 | return (s); 24 | 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - a function that gets the length of a prefix substring 5 | * 6 | * @s: the initial segment of 7 | * 8 | * @accept: which consist only of bytes from 9 | * 10 | * Return: the number of bytes 11 | */ 12 | 13 | unsigned int _strspn(char *s, char *accept) 14 | { 15 | int z = 0, x, y; 16 | 17 | for (x = 0; s[x] != '\0'; x++) 18 | { 19 | if (s[x] != 32) 20 | { 21 | for (y = 0; accept[y] != '\0'; y++) 22 | { 23 | if (s[x] == accept[y]) 24 | z++; 25 | } 26 | } 27 | else 28 | return (z); 29 | } 30 | return (z); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - function that searches a string for any of a set of bytes 5 | * 6 | * @s:first occurrence in the string 7 | * 8 | * @accept: matches one of the bytes, or @NULL if no such byte 9 | * 10 | * Return: a pointer to the byte 11 | */ 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | int i; 15 | while (*s) 16 | { 17 | for (i = 0; accept[i]; i++) 18 | { 19 | if (*s == accept[i]) 20 | return (s); 21 | } 22 | s++; 23 | } 24 | return (NULL); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - locates a substring 5 | * 6 | * @haystack: the longer string to search 7 | * @needle: the first occurrence of the substring 8 | * 9 | * Return: a pointer beg of substring or @Null if it not foound. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | unsigned int i = 0, j = 0; 14 | 15 | while (haystack[i]) 16 | { 17 | while (needle[j] && (haystack[i] == needle[0])) 18 | { 19 | if (haystack[i + j] == needle[j]) 20 | j++; 21 | else 22 | break; 23 | } 24 | if (needle[j]) 25 | { 26 | i++; 27 | j = 0; 28 | } 29 | else 30 | return (haystack + i); 31 | } 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chessboard 5 | * @a: pointer to pieces to print 6 | * 7 | * Return: void 8 | */ 9 | void print_chessboard(char (*a)[8]) 10 | { 11 | int i, j; 12 | 13 | for (i = 0; i < 8; i++) 14 | { 15 | for (j = 0; j < 8; j++) 16 | { 17 | _putchar(a[i][j]); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_diagsums - prints the sums of the two diagonals of a square matrix 4 | * @a: pointer to start of matrix 5 | * @size: width of matrix column 6 | * 7 | * Return: void 8 | */ 9 | void print_diagsums(int *a, int size) 10 | { 11 | int i, j, p, l = 0, r = 0; 12 | 13 | for (i = 0; i < size; i++) 14 | { 15 | p = (i * size) + i; 16 | l += *(a + p); 17 | } 18 | for (j = 0; j < size; j++) 19 | { 20 | p = (j * size) + (size - 1 - j); 21 | r += *(a + p); 22 | } 23 | printf("%i, %i\n", l, r); 24 | } 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x07. C - Even more pointers, arrays and strings 2 | Tasks 3 | 0. memset 4 | Write a function that fills memory with a constant byte. 5 | 1. memcpy 6 | Write a function that copies memory area. 7 | 2. strchr 8 | Write a function that locates a character in a string. 9 | 3. strspn 10 | Write a function that gets the length of a prefix substring. 11 | 4. strpbrk 12 | Write a function that searches a string for any of a set of bytes. 13 | 5. strstr 14 | Write a function that locates a substring. 15 | 6. Chess is mental torture 16 | Write a function that prints the chessboard. 17 | 7. The line of life is a ragged diagonal between duty and desire 18 | Write a function that prints the sum of the two diagonals of a square matrix of integers. 19 | 8. Double pointer, double fun 20 | Write a function that sets the value of a pointer to a char. 21 | 9. My primary goal of hacking was the intellectual curiosity, the seduction of adventure 22 | Create a file that contains the password for the crackme2 executable. 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | 5 | #include 6 | #include 7 | int _putchar(char c); 8 | char *_memset(char *s, char b, unsigned int n); 9 | char *_memcpy(char *dest, char *src, unsigned int n); 10 | char *_strchr(char *s, char c); 11 | unsigned int _strspn(char *s, char *accept); 12 | char *_strpbrk(char *s, char *accept); 13 | char *_strstr(char *haystack, char *needle); 14 | void print_chessboard(char (*a)[8]); 15 | void print_diagsums(int *a, int size); 16 | void set_string(char **s, char *to); 17 | #endif/*MAIN_H*/ 18 | -------------------------------------------------------------------------------- /0x08-recursion/.0-puts_recursion.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x08-recursion/.0-puts_recursion.c.swp -------------------------------------------------------------------------------- /0x08-recursion/.2-strlen_recursion.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x08-recursion/.2-strlen_recursion.c.swp -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts_recursion - print a string 4 | * @s:string 5 | * Return:void 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s) 10 | { 11 | _putchar(*s); 12 | _puts_recursion(s + 1); 13 | } 14 | else 15 | { 16 | _putchar('\n'); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _print_rev_recursion - print a string in reverse 4 | * @s: string 5 | * Return 0; 6 | */ 7 | 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s) 11 | { 12 | _print_rev_recursion(s + 1); 13 | _putchar(*s); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen_recursion - Prints the length of a string. 4 | * @s: the string to be printed 5 | * Return: the length of string 6 | */ 7 | int _strlen_recursion(char *s) 8 | { 9 | if (s[0] != '\0') 10 | return (1 + _strlen_recursion(s + 1)); 11 | return (0); 12 | } 13 | /** 14 | * pal_checker - check if s is palindrome. 15 | * @s: string base address. 16 | * @i: left index. 17 | * @j: rigth index. 18 | * Return: 1 if s is palindrome, 0 otherwise. 19 | */ 20 | int pal_checker(char *s, int i, int j) 21 | { 22 | if (s[i] == s[j]) 23 | if (i > j / 2) 24 | return (1); 25 | else 26 | return (pal_checker(s, i + 1, j - 1)); 27 | else 28 | return (0); 29 | } 30 | /** 31 | * is_palindrome - check if s is palindrome 32 | * @s: base address for string. 33 | * 34 | * Return: 1 if n is prime, 0 otherwise. 35 | */ 36 | int is_palindrome(char *s) 37 | { 38 | return (pal_checker(s, 0, _strlen_recursion(s) - 1)); 39 | } 40 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * str_checker - check if two strings are identical. 5 | * @s1: string_1 base address. 6 | * @s2: string_2 base address. 7 | * @i: left index. 8 | * @j: special index. (joker) 9 | * Return: 1 if s is palindrome, 0 otherwise. 10 | */ 11 | int str_checker(char *s1, char *s2, int i, int j) 12 | { 13 | if (s1[i] == '\0' && s2[j] == '\0') 14 | return (1); 15 | if (s1[i] == s2[j]) 16 | return (str_checker(s1, s2, i + 1, j + 1)); 17 | if (s1[i] == '\0' && s2[j] == '*') 18 | return (str_checker(s1, s2, i, j + 1)); 19 | if (s2[j] == '*') 20 | return (str_checker(s1, s2, i + 1, j) || str_checker(s1, s2, i, j + 1)); 21 | return (0); 22 | } 23 | /** 24 | * wildcmp - check if strings could be considered identical 25 | * @s1: base address for string. 26 | * @s2: base address for string. 27 | * 28 | * Return: 1 if are considered identical. 29 | */ 30 | int wildcmp(char *s1, char *s2) 31 | { 32 | return (str_checker(s1, s2, 0, 0)); 33 | } 34 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen_recursion - length of string 4 | * @s:string 5 | * Return:int 6 | */ 7 | int _strlen_recursion(char *s) 8 | { 9 | int i = 0; 10 | 11 | if (*s) 12 | { 13 | i++; 14 | i += _strlen_recursion(s + 1); 15 | 16 | } 17 | return (i); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * factorial - factorial of int 4 | * @n:int 5 | * Return:int 6 | */ 7 | int factorial(int n) 8 | { 9 | int x; 10 | 11 | if (n == 0) 12 | { 13 | return (1); 14 | } 15 | else if (n < 0) 16 | { 17 | return (-1); 18 | } 19 | else 20 | { 21 | x = n * factorial(n - 1); 22 | } 23 | return (x); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | 2 | #include "main.h" 3 | /** 4 | * _pow_recursion - power 5 | * @x:int 6 | * @y:int 7 | * Return:int 8 | */ 9 | 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | { 14 | return (-1); 15 | } 16 | else if (y != 0) 17 | return (x * _pow_recursion(x, y - 1)); 18 | 19 | else 20 | { 21 | return (1); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | 2 | #include "main.h" 3 | 4 | /** 5 | * check - checks for the square root 6 | * @a:int 7 | * @b:int 8 | * 9 | * Return: int 10 | */ 11 | int check(int a, int b) 12 | { 13 | if (a * a == b) 14 | return (a); 15 | if (a * a > b) 16 | return (-1); 17 | return (check(a + 1, b)); 18 | } 19 | 20 | /** 21 | * _sqrt_recursion - returns the natural square root of a number 22 | * @n: integer to find sqrt of 23 | * Return: natural square root or -1 24 | */ 25 | int _sqrt_recursion(int n) 26 | { 27 | if (n == 0) 28 | return (0); 29 | return (check(1, n)); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | 2 | #include "main.h" 3 | 4 | /** 5 | * check - checks to see if number is prime 6 | * @a:int 7 | * @b:int 8 | * Return:int 9 | */ 10 | int check(int a, int b) 11 | { 12 | if (b < 2 || b % a == 0) 13 | return (0); 14 | else if (a > b / 2) 15 | return (1); 16 | else 17 | return (check(a + 1, b)); 18 | } 19 | 20 | /** 21 | * is_prime_number - states if number is prime 22 | * @n:int 23 | * Return:int 24 | */ 25 | int is_prime_number(int n) 26 | { 27 | if (n == 2) 28 | return (1); 29 | return (check(2, n)); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | All the task of recursion. 2 | Still on the issue of doing hard things 3 | Following alx framework 4 | By Bwave / Bright Oghor 5 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void _puts_recursion(char *s); 3 | void _print_rev_recursion(char *s); 4 | int _strlen_recursion(char *s); 5 | int factorial(int n); 6 | int _pow_recursion(int x, int y); 7 | int _sqrt_recursion(int n); 8 | int is_prime_number(int n); 9 | int is_palindrome(char *s); 10 | int wildcmp(char *s1, char *s2); 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | Static Libaries 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | /** 6 | * 7 | * * _putchar - writes the character c to stdout 8 | * 9 | * * @c: The character to print 10 | * 11 | * * 12 | * 13 | * * Return: On success 1. 14 | * 15 | * * On error, -1 is returned, and errno is set appropriately*/ 16 | 17 | int _putchar(char c) 18 | 19 | { 20 | 21 | return (write(1, &c, 1)); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c *.c 3 | ar rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | 5 | int _putchar(char c); 6 | int _islower(int c); 7 | int _isalpha(int c); 8 | int _abs(int n); 9 | int _isupper(int c); 10 | int _isdigit(int c); 11 | int _strlen(char *s); 12 | void _puts(char *s); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | char *_strcat(char *dest, char *src); 16 | char *_strncat(char *dest, char *src, int n); 17 | char *_strncpy(char *dest, char *src, int n); 18 | int _strcmp(char *s1, char *s2); 19 | char *_memset(char *s, char b, unsigned int n); 20 | char *_memcpy(char *dest, char *src, unsigned int n); 21 | char *_strchr(char *s, char c); 22 | unsigned int _strspn(char *s, char *accept); 23 | char *_strpbrk(char *s, char *accept); 24 | char *_strstr(char *haystack, char *needle); 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints its name, followed by a new line. 5 | * @argc: argument count 6 | * @argv: arguments 7 | * 8 | * Return: 0 9 | */ 10 | int main(int argc, char **argv) 11 | { 12 | (void)argc; 13 | 14 | printf("%s\n", argv[0]); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the number of arguments passed into it. 5 | * @argc: argument count 6 | * @argv: arguments 7 | * 8 | * Return: 0 9 | */ 10 | int main(int argc, char **argv) 11 | { 12 | (void)argv; 13 | 14 | printf("%d\n", argc - 1); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints the min number of coins to make change 6 | * for an amount of money 7 | * @argc: argument count 8 | * @argv: arguments 9 | * Return: 0 10 | */ 11 | int main(int argc, char **argv) 12 | { 13 | int total, count; 14 | unsigned int i; 15 | char *p; 16 | int cents[] = {25, 10, 5, 2}; 17 | 18 | if (argc != 2) 19 | { 20 | printf("Error\n"); 21 | return (1); 22 | } 23 | 24 | total = strtol(argv[1], &p, 10); 25 | count = 0; 26 | 27 | if (!*p) 28 | { 29 | while (total > 1) 30 | { 31 | for (i = 0; i < sizeof(cents[i]); i++) 32 | { 33 | if (total >= cents[i]) 34 | { 35 | count += total / cents[i]; 36 | total = total % cents[i]; 37 | } 38 | } 39 | } 40 | if (total == 1) 41 | count++; 42 | } 43 | else 44 | { 45 | printf("Error\n"); 46 | return (1); 47 | } 48 | 49 | printf("%d\n", count); 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all arguments it receives. 5 | * @argc: argument count 6 | * @argv: arguments 7 | * 8 | * Return: 0 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 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - multiplies two numbers. 6 | * @argc: argument count 7 | * @argv: arguments 8 | * 9 | * Return: 0 10 | */ 11 | int main(int argc, char **argv) 12 | { 13 | int x, y; 14 | 15 | if (argc < 3) 16 | { 17 | printf("Error\n"); 18 | return (1); 19 | } 20 | 21 | x = atoi(argv[1]); 22 | y = atoi(argv[2]); 23 | printf("%d\n", x * y); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - adds positive numbers. 6 | * @argc: argument count 7 | * @argv: arguments 8 | * 9 | * Return: 0 10 | */ 11 | int main(int argc, char **argv) 12 | { 13 | int i, n, sum = 0; 14 | char *flag; 15 | 16 | if (argc < 2) 17 | { 18 | printf("0\n"); 19 | return (0); 20 | } 21 | 22 | for (i = 1; argv[i]; i++) 23 | { 24 | n = strtol(argv[i], &flag, 10); 25 | if (*flag) 26 | { 27 | printf("Error\n"); 28 | return (1); 29 | } 30 | else 31 | { 32 | sum += n; 33 | } 34 | } 35 | printf("%d\n", sum); 36 | 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # 0x0A-argc_argv 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/add: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0A-argc_argv/add -------------------------------------------------------------------------------- /0x0A-argc_argv/args: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0A-argc_argv/args -------------------------------------------------------------------------------- /0x0A-argc_argv/change: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0A-argc_argv/change -------------------------------------------------------------------------------- /0x0A-argc_argv/mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0A-argc_argv/mul -------------------------------------------------------------------------------- /0x0A-argc_argv/mynewnameis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0A-argc_argv/mynewnameis -------------------------------------------------------------------------------- /0x0A-argc_argv/nargs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0A-argc_argv/nargs -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * create_array - creates an array of chars, 6 | * and initializes it with a specific char. 7 | * @size: the size of the array 8 | * @c: initial value 9 | * 10 | * Return: a pointer to the array, or NULL if it fails 11 | */ 12 | char *create_array(unsigned int size, char c) 13 | { 14 | char *array; 15 | unsigned int i; 16 | 17 | if (size == 0) 18 | return (NULL); 19 | 20 | array = (char *)malloc(sizeof(char) * size); 21 | if (array == NULL) 22 | return (NULL); 23 | 24 | for (i = 0; i < size; i++) 25 | array[i] = c; 26 | 27 | return (array); 28 | } 29 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * simple_print_buffer - prints buffer in hexa 7 | * @buffer: the address of memory to print 8 | * @size: the size of the memory to print 9 | * 10 | * Return: Nothing. 11 | */ 12 | void simple_print_buffer(char *buffer, unsigned int size) 13 | { 14 | unsigned int i; 15 | 16 | i = 0; 17 | while (i < size) 18 | { 19 | if (i % 10) 20 | { 21 | printf(" "); 22 | } 23 | if (!(i % 10) && i) 24 | { 25 | printf("\n"); 26 | } 27 | printf("0x%02x", buffer[i]); 28 | i++; 29 | } 30 | printf("\n"); 31 | } 32 | 33 | /** 34 | * main - check the code for ALX School students. 35 | * 36 | * Return: Always 0. 37 | */ 38 | int main(void) 39 | { 40 | char *buffer; 41 | 42 | buffer = create_array(98, 'H'); 43 | if (buffer == NULL) 44 | { 45 | printf("failed to allocate memory\n"); 46 | return (1); 47 | } 48 | simple_print_buffer(buffer, 98); 49 | free(buffer); 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = _strdup(""); 15 | if (s == NULL) 16 | { 17 | printf("failed to allocate memory\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strdup - returns a pointer to a newly allocated space in memory, 6 | * which contains a copy of the string given as a parameter. 7 | * @str: the source string 8 | * 9 | * Return: returns a pointer to the duplicated string. 10 | * It returns NULL if insufficient memory was available 11 | */ 12 | char *_strdup(char *str) 13 | { 14 | char *copy; 15 | int i, len = 0; 16 | 17 | if (str == NULL) 18 | return (NULL); 19 | 20 | while (str[len] != '\0') 21 | len++; 22 | 23 | copy = (char *)malloc((sizeof(char) * len) + 1); 24 | if (copy == NULL) 25 | return (NULL); 26 | 27 | for (i = 0; i < len; i++) 28 | copy[i] = str[i]; 29 | copy[len] = '\0'; 30 | 31 | return (copy); 32 | } 33 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments of your program. 6 | * @ac: arguments count 7 | * @av: arguments vector 8 | * 9 | * Return: a pointer to a new string, or NULL if it fails 10 | */ 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *str, *s; 14 | int i, j, k, len = 0; 15 | 16 | if (ac == 0 || av == NULL) 17 | return (NULL); 18 | 19 | for (i = 0; i < ac; i++) 20 | { 21 | s = av[i]; 22 | j = 0; 23 | 24 | while (s[j++]) 25 | len++; 26 | len++; 27 | } 28 | 29 | str = (char *)malloc(sizeof(char) * (len + 1)); 30 | if (str == NULL) 31 | return (NULL); 32 | 33 | for (i = 0, j = 0; i < ac && j < len; i++) 34 | { 35 | s = av[i]; 36 | k = 0; 37 | 38 | while (s[k]) 39 | { 40 | str[j] = s[k]; 41 | k++; 42 | j++; 43 | } 44 | str[j++] = '\n'; 45 | } 46 | str[j] = '\0'; 47 | 48 | return (str); 49 | } 50 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char *av[]) 11 | { 12 | char *s; 13 | 14 | s = argstostr(ac, av); 15 | if (s == NULL) 16 | { 17 | return (1); 18 | } 19 | printf("%s", s); 20 | free(s); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_tab - Prints an array of string 7 | * @tab: The array to print 8 | * 9 | * Return: nothing 10 | */ 11 | void print_tab(char **tab) 12 | { 13 | int i; 14 | 15 | for (i = 0; tab[i] != NULL; ++i) 16 | { 17 | printf("%s\n", tab[i]); 18 | } 19 | } 20 | 21 | /** 22 | * main - check the code for ALX School students. 23 | * 24 | * Return: 1 if an error occurred, 0 otherwise 25 | */ 26 | int main(void) 27 | { 28 | char **tab; 29 | 30 | tab = strtow(" "); 31 | if (tab == NULL) 32 | { 33 | printf("Failed\n"); 34 | return (1); 35 | } 36 | print_tab(tab); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = str_concat("Betty ", "Holberton"); 15 | if (s == NULL) 16 | { 17 | printf("failed\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * str_concat - concatenates two strings. 7 | * @s1: first string 8 | * @s2: second string 9 | * 10 | * Return: a pointer to a newly allocated space in memory which 11 | * contains the contents of s1, followed by the contents of s2, 12 | * and null terminated. NULL on failure 13 | */ 14 | char *str_concat(char *s1, char *s2) 15 | { 16 | int i, j, len1, len2, len; 17 | char *result; 18 | 19 | len1 = len2 = 0; 20 | 21 | if (s1 != NULL) 22 | { 23 | i = 0; 24 | while (s1[i++] != '\0') 25 | len1++; 26 | } 27 | 28 | if (s2 != NULL) 29 | { 30 | i = 0; 31 | while (s2[i++] != '\0') 32 | len2++; 33 | } 34 | 35 | len = len1 + len2; 36 | result = (char *)malloc(sizeof(char) * (len + 1)); 37 | if (result == NULL) 38 | return (NULL); 39 | 40 | for (i = 0; i < len1; i++) 41 | result[i] = s1[i]; 42 | for (j = 0; j < len2; j++, i++) 43 | result[i] = s2[j]; 44 | result[len] = '\0'; 45 | 46 | return (result); 47 | } 48 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - returns a pointer to a 2 dimensional array of integers. 6 | * @width: width of array 7 | * @height: height of array 8 | * 9 | * Return: a pointer to a 2 dimensional array of integers. 10 | */ 11 | int **alloc_grid(int width, int height) 12 | { 13 | int **array, i, j; 14 | int len = width * height; 15 | 16 | if (len <= 0) 17 | return (NULL); 18 | 19 | array = (int **)malloc(sizeof(int *) * height); 20 | if (array == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < height; i++) 24 | { 25 | array[i] = (int *)malloc(sizeof(int) * width); 26 | if (array[i] == NULL) 27 | { 28 | for (i--; i >= 0; i--) 29 | free(array[i]); 30 | free(array); 31 | return (NULL); 32 | } 33 | } 34 | 35 | for (i = 0; i < height; i++) 36 | for (j = 0; j < width; j++) 37 | array[i][j] = 0; 38 | 39 | return (array); 40 | } 41 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_grid - prints a grid of integers 7 | * @grid: the address of the two dimensional grid 8 | * @width: width of the grid 9 | * @height: height of the grid 10 | * 11 | * Return: Nothing. 12 | */ 13 | void print_grid(int **grid, int width, int height) 14 | { 15 | int w; 16 | int h; 17 | 18 | h = 0; 19 | while (h < height) 20 | { 21 | w = 0; 22 | while (w < width) 23 | { 24 | printf("%d ", grid[h][w]); 25 | w++; 26 | } 27 | printf("\n"); 28 | h++; 29 | } 30 | } 31 | 32 | /** 33 | * main - check the code for ALX School students. 34 | * 35 | * Return: Always 0. 36 | */ 37 | int main(void) 38 | { 39 | int **grid; 40 | 41 | grid = alloc_grid(48, 32); 42 | if (grid == NULL) 43 | { 44 | return (1); 45 | } 46 | print_grid(grid, 48, 32); 47 | printf("\n"); 48 | grid[0][3] = 98; 49 | grid[3][4] = 402; 50 | print_grid(grid, 6, 4); 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - frees a 2 dimensional grid 6 | * @grid: the grid 7 | * @height: height of the grid 8 | */ 9 | void free_grid(int **grid, int height) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < height; i++) 14 | free(grid[i]); 15 | free(grid); 16 | } 17 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_grid - prints a grid of integers 7 | * @grid: the address of the two dimensional grid 8 | * @width: width of the grid 9 | * @height: height of the grid 10 | * 11 | * Return: Nothing. 12 | */ 13 | void print_grid(int **grid, int width, int height) 14 | { 15 | int w; 16 | int h; 17 | 18 | h = 0; 19 | while (h < height) 20 | { 21 | w = 0; 22 | while (w < width) 23 | { 24 | printf("%d ", grid[h][w]); 25 | w++; 26 | } 27 | printf("\n"); 28 | h++; 29 | } 30 | } 31 | 32 | /** 33 | * main - check the code for ALX School students. 34 | * 35 | * Return: Always 0. 36 | */ 37 | int main(void) 38 | { 39 | int **grid; 40 | 41 | grid = alloc_grid(6, 4); 42 | if (grid == NULL) 43 | { 44 | return (1); 45 | } 46 | print_grid(grid, 6, 4); 47 | printf("\n"); 48 | grid[0][3] = 98; 49 | grid[3][4] = 402; 50 | print_grid(grid, 6, 4); 51 | free_grid(grid, 4); 52 | return (0); 53 | } 54 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0B-malloc_free 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0B-malloc_free/a -------------------------------------------------------------------------------- /0x0B-malloc_free/args: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0B-malloc_free/args -------------------------------------------------------------------------------- /0x0B-malloc_free/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0B-malloc_free/c -------------------------------------------------------------------------------- /0x0B-malloc_free/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0B-malloc_free/f -------------------------------------------------------------------------------- /0x0B-malloc_free/g: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0B-malloc_free/g -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *create_array(unsigned int size, char c); 3 | char *_strdup(char *str); 4 | char *str_concat(char *s1, char *s2); 5 | int **alloc_grid(int width, int height); 6 | void free_grid(int **grid, int height); 7 | char *argstostr(int ac, char **av); 8 | char **strtow(char *str); 9 | -------------------------------------------------------------------------------- /0x0B-malloc_free/s: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0B-malloc_free/s -------------------------------------------------------------------------------- /0x0B-malloc_free/strtow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0B-malloc_free/strtow -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * malloc_checked - cause normal process termination with a status value of 98 5 | * 6 | * @b: allocated memory 7 | * 8 | * Return: a pointer to the allocated memory 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | void *i; 13 | 14 | i = malloc(b); 15 | if (i == NULL) 16 | exit(98); 17 | return (i); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0x0C-more_malloc_free - Shortcut.lnk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x0C-more_malloc_free/0x0C-more_malloc_free - Shortcut.lnk -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_nconcat - a function that concatenates two strings. 5 | * 6 | * @s1: first char 7 | * @s2: secound char 8 | * @n: unsigned int 9 | * 10 | * Return: If the function fails, it should return NULL 11 | */ 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | unsigned int x, y, z; 15 | char *s; 16 | 17 | if (s1 == NULL) 18 | { 19 | x = 0; 20 | } 21 | else 22 | { 23 | for (x = 0; s1[x]; ++x) 24 | ; 25 | } 26 | if (s2 == NULL) 27 | { 28 | y = 0; 29 | } 30 | else 31 | { 32 | for (y = 0; s2[y]; ++y) 33 | ; 34 | } 35 | if (y > n) 36 | y = n; 37 | s = malloc(sizeof(char) * (x + y + 1)); 38 | if (s == NULL) 39 | return (NULL); 40 | for (z = 0; z < x; z++) 41 | s[z] = s1[z]; 42 | for (z = 0; z < y; z++) 43 | s[z + x] = s2[z]; 44 | s[x + y] = '\0'; 45 | return (s); 46 | } 47 | 48 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _realloc - reallocates a memory block using malloc and free 5 | * 6 | * @ptr: pointer to the memory previously allocated 7 | * @old_size:is the size, in bytes, of the allocated space for ptr 8 | * @new_size: the new size, in bytes of the new memory block 9 | * 10 | * Return: pointer allocate new size memory, or NULL 11 | */ 12 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 13 | { 14 | char *p; 15 | unsigned int i, n = new_size; 16 | char *oldp = ptr; 17 | 18 | if (ptr == NULL) 19 | { 20 | p = malloc(new_size); 21 | return (p); 22 | } 23 | else if (new_size == 0) 24 | { 25 | free(ptr); 26 | return (NULL); 27 | } 28 | else if (new_size == old_size) 29 | return (ptr); 30 | p = malloc(new_size); 31 | if (p == NULL) 32 | return (NULL); 33 | if (new_size > old_size) 34 | n = old_size; 35 | for (i = 0; i < n; i++) 36 | p[i] = oldp[i]; 37 | free(ptr); 38 | return (p); 39 | } 40 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _calloc - allocates memory for an array of @nmemb elements of 5 | * @size bytes each and returns a pointer to the allocated memory. 6 | * 7 | * @nmemb: allocate memory for array 8 | * @size: allocate element of size bytes 9 | * 10 | * Return: pointer to the allocated memory. 11 | */ 12 | void *_calloc(unsigned int nmemb, unsigned int size) 13 | { 14 | char *a; 15 | unsigned int b; 16 | 17 | if (nmemb == 0 || size == 0) 18 | return (NULL); 19 | a = malloc(nmemb * size); 20 | if (a == NULL) 21 | return (NULL); 22 | for (b = 0; b < (nmemb * size); b++) 23 | a[b] = 0; 24 | return (a); 25 | } 26 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *array_range - creates an array of integers 6 | * @min: minimum range of values stored 7 | * @max: maximum range of values stored and number of elements 8 | * 9 | * Return: pointer to the new array 10 | */ 11 | int *array_range(int min, int max) 12 | { 13 | int *ptr; 14 | int i, size; 15 | 16 | if (min > max) 17 | return (NULL); 18 | 19 | size = max - min + 1; 20 | 21 | ptr = malloc(sizeof(int) * size); 22 | 23 | if (ptr == NULL) 24 | return (NULL); 25 | 26 | for (i = 0; min <= max; i++) 27 | ptr[i] = min++; 28 | 29 | return (ptr); 30 | } 31 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void _print(char *str, int l); 5 | char *mul(char n, char *num, int num_index, char *dest, int dest_index); 6 | int check_for_digits(char **av); 7 | void init(char *str, int l); 8 | #include 9 | #include 10 | int _putchar(char c); 11 | void *malloc_checked(unsigned int b); 12 | char *string_nconcat(char *s1, char *s2, unsigned int n); 13 | void *_calloc(unsigned int nmemb, unsigned int size); 14 | int *array_range(int min, int max); 15 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 16 | 17 | 18 | #endif/*MAIN_H*/ 19 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _0_OBJECT_LIKE_MARCO_H_ 2 | #define _0_OBJECT_LIKE_MARCO_H_ 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef _PI_H_ 2 | #define _PI_H_ 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - prints the name of the file 4 | * 5 | *Return: always 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _3_FUNCTION_LIKE_MACRO_H_ 2 | #define _3_FUNCTION_LIKE_MACRO_H_ 3 | 4 | #define ABS(x) ((x) < 0 ? (-(x)) : (x)) 5 | 6 | #endif /* _3_FUNCTION_LIKE_MACRO_H_ */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUM_H_ 2 | #define _SUM_H_ 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | Learning Objectives 2 | At the end of this project, you are expected to be able to explain to anyone, without the help of Google: 3 | 4 | General 5 | What are macros and how to use them 6 | What are the most common predefined macros 7 | How to include guard your header files 8 | Requirements 9 | General 10 | Allowed editors: vi, vim, emacs 11 | All your files will be compiled on Ubuntu 20.04 LTS using gcc, using the options -Wall -Werror -Wextra -pedantic -std=gnu89 12 | All your files should end with a new line 13 | A README.md file, at the root of the folder of the project is mandatory 14 | Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl 15 | You are not allowed to use global variables 16 | No more than 5 functions per file 17 | The only C standard library functions allowed are malloc, free and exit. Any use of functions like printf, puts, calloc, realloc etc… is forbidden 18 | You are allowed to use _putchar 19 | You don’t have to push _putchar.c, we will use our file. If you do it won’t be taken into account 20 | In the following examples, the main.c files are shown as examples. You can use them to test your functions, but you don’t have to push them to your repo (if you do we won’t take them into account). We will use our own main.c files at compilation. Our main.c files might be different from the one shown in the examples 21 | Don’t forget to push your header file 22 | All your header files should be include guarded 23 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - initializes a structure of type dog 5 | * @d: pointer to structure 6 | * @name: pointer to the name of the dog 7 | * @age: age of dog 8 | * @owner: pointer to owner of dog 9 | * 10 | * Return: void 11 | */ 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d) 15 | { 16 | d->name = name; 17 | d->age = age; 18 | d->owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * print_dog - prints a struct dog 6 | * @d: pointer to structure 7 | * 8 | * Return: void 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (d != NULL) 13 | { 14 | if (d->name != NULL) 15 | printf("Name: %s\n", d->name); 16 | else 17 | printf("Name: (nil)\n"); 18 | printf("Age: %.6f\n", d->age); 19 | if (d->owner != NULL) 20 | printf("Owner: %s\n", d->owner); 21 | else 22 | printf("Owner: (nil)\n"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * new_dog - creates a new dog 6 | * @name: name of dog 7 | * @age: age of dog 8 | * @owner: owner of dog 9 | * 10 | * Return: pointer to new dog 11 | */ 12 | dog_t *new_dog(char *name, float age, char *owner) 13 | { 14 | unsigned int nl, ol, i; 15 | dog_t *dog; 16 | 17 | if (name == NULL || owner == NULL) 18 | return (NULL); 19 | dog = malloc(sizeof(dog_t)); 20 | if (dog == NULL) 21 | return (NULL); 22 | for (nl = 0; name[nl]; nl++) 23 | ; 24 | nl++; 25 | dog->name = malloc(nl * sizeof(char)); 26 | if (dog->name == NULL) 27 | { 28 | free(dog); 29 | return (NULL); 30 | } 31 | for (i = 0; i < nl; i++) 32 | dog->name[i] = name[i]; 33 | dog->age = age; 34 | for (ol = 0; owner[ol]; ol++) 35 | ; 36 | ol++; 37 | dog->owner = malloc(ol * sizeof(char)); 38 | if (dog->owner == NULL) 39 | { 40 | free(dog->name); 41 | free(dog); 42 | return (NULL); 43 | } 44 | for (i = 0; i < ol; i++) 45 | dog->owner[i] = owner[i]; 46 | return (dog); 47 | } 48 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * free_dog - frees dogs 6 | * @d: pointer to dog to free 7 | * 8 | * Return: void 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d) 13 | { 14 | free(d->name); 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | 0x0E. C - Structures, typedef 2 | Tasks 3 | 0. Poppy 4 | Define a new type struct dog with the following elements: 5 | 6 | name, type = char * 7 | age, type = float 8 | owner, type = char * 9 | 1. A dog is the only thing on earth that loves you more than you love yourself 10 | Write a function that initialize a variable of type struct dog 11 | 2. A dog will teach you unconditional love. If you can have that in your life, things won't be too bad 12 | Write a function that prints a struct dog 13 | 14 | Prototype: void print_dog(struct dog *d); 15 | Format: see example bellow 16 | You are allowed to use the standard library 17 | If an element of d is NULL, print (nil) instead of this element. (if name is NULL, print Name: (nil)) 18 | If d is NULL print nothing. 19 | 3. Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to read 20 | Define a new type dog_t as a new name for the type struct dog. 21 | 4. A door is what a dog is perpetually on the wrong side of 22 | Write a function that creates a new dog. 23 | 24 | Prototype: dog_t *new_dog(char *name, float age, char *owner); 25 | You have to store a copy of name and owner 26 | Return NULL if the function fails 27 | 5. How many legs does a dog have if you call his tail a leg? Four. Saying that a tail is a leg doesn't make it a leg 28 | Write a function that frees dogs. 29 | 30 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG 2 | #define DOG 3 | 4 | /** 5 | * struct dog - dog class 6 | * 7 | * @name: Name of the dog 8 | * 9 | * @age: Age of the dog 10 | * 11 | * @owner: Owner of the dog 12 | */ 13 | 14 | struct dog 15 | { 16 | char *name; 17 | float age; 18 | char *owner; 19 | }; 20 | 21 | void init_dog(struct dog *d, char *name, float age, char *owner); 22 | void print_dog(struct dog *d); 23 | 24 | typedef struct dog dog_t; 25 | 26 | dog_t *new_dog(char *name, float age, char *owner); 27 | 28 | void free_dog(dog_t *d); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - a function that prints a name. 5 | * @name: the name given 6 | * @f: function of name 7 | * 8 | * Rteurn: void 9 | */ 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (name != NULL && f != NULL) 13 | 14 | f(name); 15 | } 16 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - a function given as a 5 | * parameter on each element of an array. 6 | * 7 | * @array: array to execute func on 8 | * @size: is the size of the array 9 | * @action: is a pointer to the function you need to use 10 | * 11 | * Return: void 12 | */ 13 | void array_iterator(int *array, size_t size, void (*action)(int)) 14 | { 15 | size_t i; 16 | 17 | if (array && action) 18 | { 19 | i = 0; 20 | while (i < size) 21 | { 22 | action(array[i]); 23 | i++; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * print_opcodes - print the opcodes of this program 6 | * @a: address of the main function 7 | * @n: number of bytes to print 8 | * 9 | * Return: void 10 | */ 11 | void print_opcodes(char *a, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | printf("%.2hhx", a[i]); 18 | if (i < n - 1) 19 | printf(" "); 20 | } 21 | printf("\n"); 22 | 23 | } 24 | 25 | /** 26 | * main - prints the opcodes of its own main function 27 | * @argc: number of arguments passed to the function 28 | * @argv: array of pointers to arguments 29 | * 30 | * Return: always O 31 | */ 32 | int main(int argc, char **argv) 33 | { 34 | int n; 35 | 36 | if (argc != 2) 37 | { 38 | printf("Error\n"); 39 | exit(1); 40 | } 41 | n = atoi(argv[1]); 42 | if (n < 0) 43 | { 44 | printf("Error\n"); 45 | exit(2); 46 | } 47 | print_opcodes((char *)&main, n); 48 | return (0); 49 | } 50 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - ... 5 | * @array: ... 6 | * @size: ... 7 | * @cmp: ... 8 | * 9 | * Return: ... 10 | */ 11 | int int_index(int *array, int size, int (*cmp)(int)) 12 | { 13 | int i = 0; 14 | 15 | if (size > 0) 16 | { 17 | if (array != NULL && cmp != NULL) 18 | { 19 | while (i < size) 20 | { 21 | if (cmp(array[i])) 22 | return (i); 23 | 24 | i++; 25 | } 26 | } 27 | } 28 | 29 | return (-1); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _HEADER_ 2 | #define _HEADER_ 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 | 15 | int op_add(int a, int b); 16 | int op_sub(int a, int b); 17 | int op_mul(int a, int b); 18 | int op_div(int a, int b); 19 | int op_mod(int a, int b); 20 | int (*get_op_func(char *s))(int, int); 21 | #endif 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * get_op_func - ... 7 | * @s: ... 8 | * 9 | * Return: ... 10 | */ 11 | int (*get_op_func(char *s))(int, int) 12 | { 13 | op_t ops[] = { 14 | { "+", op_add }, 15 | { "-", op_sub }, 16 | { "*", op_mul }, 17 | { "/", op_div }, 18 | { "%", op_mod }, 19 | { NULL, NULL } 20 | }; 21 | int i = 0; 22 | 23 | while (i < 5) 24 | { 25 | if (strcmp(s, ops[i].op) == 0) 26 | return (ops[i].f); 27 | 28 | i++; 29 | } 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - ... 7 | * @argc: ... 8 | * @argv: ... 9 | * 10 | * Return: ... 11 | */ 12 | int main(int argc, char *argv[]) 13 | { 14 | int (*oprt)(int, int); 15 | 16 | if (argc != 4) 17 | { 18 | printf("Error\n"); 19 | exit(98); 20 | } 21 | 22 | oprt = get_op_func(argv[2]); 23 | 24 | if (!oprt) 25 | { 26 | printf("Error\n"); 27 | exit(99); 28 | } 29 | 30 | printf("%d\n", oprt(atoi(argv[1]), atoi(argv[3]))); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * op_add - ... 7 | * @a: ... 8 | * @b: ... 9 | * 10 | * Return: ... 11 | */ 12 | int op_add(int a, int b) 13 | { 14 | return (a + b); 15 | } 16 | 17 | /** 18 | * op_sub - ... 19 | * @a: ... 20 | * @b: ... 21 | * 22 | * Return: ... 23 | */ 24 | int op_sub(int a, int b) 25 | { 26 | return (a - b); 27 | } 28 | 29 | /** 30 | * op_mul - ... 31 | * @a: ... 32 | * @b: ... 33 | * 34 | * Return: ... 35 | */ 36 | int op_mul(int a, int b) 37 | { 38 | return (a * b); 39 | } 40 | 41 | /** 42 | * op_div - ... 43 | * @a: ... 44 | * @b: ... 45 | * 46 | * Return: ... 47 | */ 48 | int op_div(int a, int b) 49 | { 50 | if (b == 0) 51 | { 52 | printf("Error\n"); 53 | exit(100); 54 | } 55 | 56 | return (a / b); 57 | } 58 | 59 | /** 60 | * op_mod - ... 61 | * @a: ... 62 | * @b: ... 63 | * 64 | * Return: ... 65 | */ 66 | int op_mod(int a, int b) 67 | { 68 | if (b == 0) 69 | { 70 | printf("Error\n"); 71 | exit(100); 72 | } 73 | 74 | return (a % b); 75 | } 76 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | 0x0F. C - Function pointers 2 | Mandatory 3 | 4 | function_pointers.h - header file for 0-print_name.c, 1-array_iterator.c, and 2-int_index.c. 5 | 6 | 0-print_name.c - function that prints a name using a function given as a parameter. 7 | 8 | 1-array_iterator.c - function that executes a function given as a parameter on each element of an array. 9 | 10 | 2-int_index.c - function that searches for an integer using a comparative function given as a parameter. 11 | 12 | 3-calc.h - header file for 3-main.c, 3-op_functions.c, and 3-get_op_func.c. 13 | 14 | 3-main.c - performs simple operations passed in as parameters. 15 | 16 | 3-op_functions.c - contains the following 5 functions: 17 | 18 | int op_add(int a, int b) - returns the sum of a and b. 19 | int op_sub(int a, int b) - returns the difference of a and b. 20 | int op_mul(int a, int b) - returns the product of a and b. 21 | int op_div(int a, int b) - returns the result of the division of a by b. 22 | int op_mod(int a, int b) - returns the remainder of the division of a by b. 23 | 24 | 3-get_op_func.c - selects the correct function from 3-op_functions.c to perform the operation asked by the user. 25 | 26 | Advanced 27 | 28 | 100-main_opcodes.c - prints the opcodes of its own main function up to a specified number of bytes. 29 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_ 2 | #define FUNCTION_POINTERS_ 3 | 4 | #include 5 | 6 | int _putchar(char c); 7 | void print_name(char *name, void (*f)(char *)); 8 | void array_iterator(int *array, size_t size, void (*action)(int)); 9 | int int_index(int *array, int size, int (*cmp)(int)); 10 | 11 | #endif/* FUNCTION_POINTERS_H*/ 12 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - adds all the numbers 5 | * @n: the number of parameters passed 6 | * Return: Always 0 7 | */ 8 | 9 | int sum_them_all(const unsigned int n, ...) 10 | { 11 | int sum; 12 | unsigned int i; 13 | va_list ap; 14 | 15 | va_start(ap, n); 16 | 17 | if (n == 0) 18 | return (0); 19 | 20 | for (i = 0; i < n; i++) 21 | sum += va_arg(ap, int); 22 | 23 | va_end(ap); 24 | 25 | return (sum); 26 | } 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_numbers - prints numbers followed by a new line 5 | * @n: number of parameter 6 | * @separator: separator between two numbers 7 | * Return: Always 0 8 | */ 9 | 10 | void print_numbers(const char *separator, const unsigned int n, ...) 11 | { 12 | unsigned int i; 13 | va_list ap; 14 | 15 | va_start(ap, n); 16 | 17 | if (separator == NULL) 18 | separator = ""; 19 | 20 | for (i = 0; i < n; i++) 21 | { 22 | printf("%d", va_arg(ap, int)); 23 | if (i < n - 1) 24 | printf("%s", separator); 25 | } 26 | printf("\n"); 27 | va_end(ap); 28 | } 29 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - Prints strings passed to the function separed by separators 5 | * @separator: A string that is used to separate strings 6 | * @n: number of unnamed parameters 7 | * Return: Always 0 8 | */ 9 | 10 | void print_strings(const char *separator, const unsigned int n, ...) 11 | { 12 | char *str; 13 | unsigned int i; 14 | va_list ap; 15 | 16 | va_start(ap, n); 17 | if (separator == NULL) 18 | separator = ""; 19 | 20 | for (i = 0; i < n; i++) 21 | { 22 | str = va_arg(ap, char*); 23 | if (str == NULL) 24 | str = "(nil)"; 25 | printf("%s", str); 26 | if (i < n - 1) 27 | printf("%s", separator); 28 | } 29 | printf("\n"); 30 | va_end(ap); 31 | } 32 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_all - Prints all of the arguments when specified 5 | * @format: specifies the necessary operations 6 | * Return: void 7 | */ 8 | 9 | void print_all(const char * const format, ...) 10 | { 11 | int i; 12 | int flag; 13 | char *str; 14 | va_list a_list; 15 | 16 | va_start(a_list, format); 17 | i = 0; 18 | while (format != NULL && format[i] != '\0') 19 | { 20 | switch (format[i]) 21 | { 22 | case 'c': 23 | printf("%c", va_arg(a_list, int)); 24 | flag = 0; 25 | break; 26 | case 'i': 27 | printf("%i", va_arg(a_list, int)); 28 | flag = 0; 29 | break; 30 | case 'f': 31 | printf("%f", va_arg(a_list, double)); 32 | flag = 0; 33 | break; 34 | case 's': 35 | str = va_arg(a_list, char*); 36 | if (str == NULL) 37 | str = "(nil)"; 38 | printf("%s", str); 39 | flag = 0; 40 | break; 41 | default: 42 | flag = 1; 43 | break; 44 | } 45 | if (format[i + 1] != '\0' && flag == 0) 46 | printf(", "); 47 | i++; 48 | } 49 | printf("\n"); 50 | va_end(a_list); 51 | } 52 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # Variaidc function project README file 2 | 3 | Task 0: A function that returns the sum of all its parameters. 4 | 5 | Task 1: A function that prints numbers, followed by a new line. 6 | 7 | Task 2: A function that prints strings, followed by a new line. 8 | 9 | Task 3: A function that prints anything. 10 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS 2 | #define VARIADIC_FUNCTIONS 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | int sum_them_all(const unsigned int n, ...); 9 | void print_numbers(const char *separator, const unsigned int n, ...); 10 | void print_strings(const char *separator, const unsigned int n, ...); 11 | void print_all(const char * const format, ...); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * print_list - check the code for Holberton School students. 8 | * @h: name of the list 9 | * Return: the number of nodes. 10 | */ 11 | size_t print_list(const list_t *h) 12 | { 13 | int count = 0; 14 | 15 | while (h) 16 | { 17 | if (h->str == NULL) 18 | { 19 | printf("[0] (nil)\n"); 20 | } 21 | else 22 | { 23 | printf("[%d] %s\n", h->len, h->str); 24 | } 25 | count++; 26 | h = h->next; 27 | } 28 | return (count); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * list_len - check the code for Holberton School students. 8 | * @h: name of the list 9 | * Return: the number of nodes. 10 | */ 11 | size_t list_len(const list_t *h) 12 | { 13 | int count = 0; 14 | 15 | while (h) 16 | { 17 | count++; 18 | h = h->next; 19 | } 20 | return (count); 21 | } 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * myStartupFun - Apply the constructor attribute to myStartupFun() so that it 5 | * is executed before main() 6 | */ 7 | void myStartupFun(void) __attribute__ ((constructor)); 8 | 9 | /** 10 | * myStartupFun - implementation of myStartupFun 11 | */ 12 | void myStartupFun(void) 13 | { 14 | printf("You're beat! and yet, you must allow,\n"); 15 | printf("I bore my house upon my back!\n"); 16 | } 17 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | extern printf ; the C function, to be called 2 | 3 | section .data ; Data section, initialized variables 4 | msg: db "Hello, Holberton", 0 ; C string needs 0 5 | fmt: db "%s", 10, 0 ; The printf format, "\n",'0' 6 | 7 | section .text ; Code section. 8 | 9 | global main ; the standard gcc entry point 10 | main: ; the program label for the entry point 11 | push rbp ; set up stack frame, must be alligned 12 | 13 | mov rdi,fmt 14 | mov rsi,msg 15 | mov rax,0 ; or can be xor rax,rax 16 | call printf ; Call C function 17 | 18 | pop rbp ; restore stack 19 | 20 | mov rax,0 ; normal, no error, return value 21 | ret ; return 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * _strlen - function that returns the length of a string. 8 | * @s : s is a character 9 | * Return: value is i 10 | */ 11 | int _strlen(const char *s) 12 | { 13 | int i = 0; 14 | 15 | while (s[i] != '\0') 16 | { 17 | i++; 18 | } 19 | return (i); 20 | } 21 | 22 | /** 23 | * add_node - add a new node at beginning of a list_t list. 24 | * @head: head of a list_t list. 25 | * @str: value to insert into element. 26 | * Return: the number of nodes. 27 | */ 28 | list_t *add_node(list_t **head, const char *str) 29 | { 30 | list_t *add; 31 | 32 | add = malloc(sizeof(list_t)); 33 | if (add == NULL) 34 | return (NULL); 35 | add->str = strdup(str); 36 | 37 | add->len = _strlen(str); 38 | add->next = *head; 39 | *head = add; 40 | 41 | return (add); 42 | } 43 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * _strlen - finds the length of a string 7 | * @str: string to find the length of 8 | * 9 | * Return: length of string 10 | */ 11 | unsigned int _strlen(char *str) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; str[i]; i++) 16 | ; 17 | return (i); 18 | } 19 | 20 | /** 21 | * add_node_end - adds a new node to the end of linked list 22 | * @head: double pointer to a linked list 23 | * @str: string to add to the new node 24 | * 25 | * Return: pointer to the new node 26 | */ 27 | list_t *add_node_end(list_t **head, const char *str) 28 | { 29 | list_t *new, *tmp; 30 | 31 | if (str == NULL) 32 | return (NULL); 33 | new = malloc(sizeof(list_t)); 34 | if (new == NULL) 35 | return (NULL); 36 | new->str = strdup(str); 37 | if (new->str == NULL) 38 | { 39 | free(new); 40 | return (NULL); 41 | } 42 | new->len = _strlen(new->str); 43 | new->next = NULL; 44 | if (*head == NULL) 45 | { 46 | *head = new; 47 | return (new); 48 | } 49 | tmp = *head; 50 | while (tmp->next) 51 | tmp = tmp->next; 52 | tmp->next = new; 53 | return (new); 54 | } 55 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * free_list - Realease the memory allocated for a list 7 | * 8 | * @head: A pointer to the first node of the list to free 9 | */ 10 | void free_list(list_t *head) 11 | { 12 | if (head) 13 | { 14 | free_list(head->next); 15 | if (head->str) 16 | free(head->str); 17 | free(head); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x12. C - Singly linked lists 2 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | 5 | /** 6 | * struct list_s - singly linked list 7 | * @str: string - (malloc'ed string) 8 | * @len: length of the string 9 | * @next: points to the next node 10 | * 11 | * Description: singly linked list node structure 12 | * for Holberton project 13 | */ 14 | typedef struct list_s 15 | { 16 | char *str; 17 | unsigned int len; 18 | struct list_s *next; 19 | } list_t; 20 | 21 | size_t print_list(const list_t *h); 22 | size_t list_len(const list_t *h); 23 | list_t *add_node(list_t **head, const char *str); 24 | list_t *add_node_end(list_t **head, const char *str); 25 | void free_list(list_t *head); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/master: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x12-singly_linked_lists/master -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_listint - prints all the elements of a listint_t list. 6 | * 7 | * @h: head of linklist node 8 | * 9 | * Return: the number of nodes 10 | * 11 | */ 12 | size_t print_listint(const listint_t *h) 13 | { 14 | size_t count = 0; 15 | 16 | while (h != NULL) 17 | { 18 | printf("%d\n", h->n); 19 | h = h->next; 20 | count++; 21 | } 22 | return (count); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - the number of elements in a linked listint_t list. 5 | * 6 | * @h: head pointer 7 | * 8 | * Return: the number of elements 9 | * 10 | */ 11 | 12 | size_t listint_len(const listint_t *h) 13 | { 14 | size_t count = 0; 15 | 16 | while (h != NULL) 17 | { 18 | h = h->next; 19 | count++; 20 | } 21 | return (count); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * delete_nodeint_at_index - deletes the node at index 6 | * @head: double pointer 7 | * @index: index of node 8 | * 9 | * Return: pointer to the index node 10 | */ 11 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 12 | { 13 | unsigned int i; 14 | listint_t *temp, *next; 15 | 16 | if (head == NULL || *head == NULL) 17 | return (-1); 18 | if (index == 0) 19 | { 20 | next = (*head)->next; 21 | free(*head); 22 | *head = next; 23 | return (1); 24 | } 25 | temp = *head; 26 | for (i = 0; i < index - 1; i++) 27 | { 28 | if (temp->next == NULL) 29 | return (-1); 30 | temp = temp->next; 31 | } 32 | next = temp->next; 33 | temp->next = next->next; 34 | free(next); 35 | return (1); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - the function that reverses a listint_t 5 | * @head: double pointer 6 | * 7 | * Return: a pointer to the first node of the reversed list 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *previous, *next; 12 | 13 | if (head == NULL || *head == NULL) 14 | return (NULL); 15 | if ((*head)->next == NULL) 16 | return (*head); 17 | previous = NULL; 18 | while (*head != NULL) 19 | { 20 | next = (*head)->next; 21 | (*head)->next = previous; 22 | previous = *head; 23 | *head = next; 24 | } 25 | *head = previous; 26 | return (*head); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * _r - reallocates memory for an array of pointers 7 | * to the nodes in a linked list 8 | * @list: the old list to append 9 | * @size: size of the new list (always one more than the old list) 10 | * @new: new node to add to the list 11 | * 12 | * Return: pointer to the new list 13 | */ 14 | const listint_t **_r(const listint_t **list, size_t size, const listint_t *new) 15 | { 16 | const listint_t **newlist; 17 | size_t i; 18 | 19 | newlist = malloc(size * sizeof(listint_t *)); 20 | if (newlist == NULL) 21 | { 22 | free(list); 23 | exit(98); 24 | } 25 | for (i = 0; i < size - 1; i++) 26 | newlist[i] = list[i]; 27 | newlist[i] = new; 28 | free(list); 29 | return (newlist); 30 | } 31 | 32 | /** 33 | * print_listint_safe - prints a listint_t linked list. 34 | * @head: pointer to the start of the list 35 | * 36 | * Return: the number of nodes in the list 37 | */ 38 | size_t print_listint_safe(const listint_t *head) 39 | { 40 | size_t i, num = 0; 41 | const listint_t **list = NULL; 42 | 43 | while (head != NULL) 44 | { 45 | for (i = 0; i < num; i++) 46 | { 47 | if (head == list[i]) 48 | { 49 | printf("-> [%p] %d\n", (void *)head, head->n); 50 | free(list); 51 | return (num); 52 | } 53 | } 54 | num++; 55 | list = _r(list, num, head); 56 | printf("[%p] %d\n", (void *)head, head->n); 57 | head = head->next; 58 | } 59 | free(list); 60 | return (num); 61 | } 62 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * _ra - reallocates memory for an array of pointers 7 | * to the nodes in a linked list 8 | * @list: the old list to append 9 | * @size: size of the new list (always one more than the old list) 10 | * @new: new node to add to the list 11 | * 12 | * Return: pointer to the new list 13 | */ 14 | listint_t **_ra(listint_t **list, size_t size, listint_t *new) 15 | { 16 | listint_t **newlist; 17 | size_t i; 18 | 19 | newlist = malloc(size * sizeof(listint_t *)); 20 | if (newlist == NULL) 21 | { 22 | free(list); 23 | exit(98); 24 | } 25 | for (i = 0; i < size - 1; i++) 26 | newlist[i] = list[i]; 27 | newlist[i] = new; 28 | free(list); 29 | return (newlist); 30 | } 31 | 32 | /** 33 | * free_listint_safe - frees a listint_t linked list. 34 | * @head: double pointer to the start of the list 35 | * 36 | * Return: the number of nodes in the list 37 | */ 38 | size_t free_listint_safe(listint_t **head) 39 | { 40 | size_t i, num = 0; 41 | listint_t **list = NULL; 42 | listint_t *next; 43 | 44 | if (head == NULL || *head == NULL) 45 | return (num); 46 | while (*head != NULL) 47 | { 48 | for (i = 0; i < num; i++) 49 | { 50 | if (*head == list[i]) 51 | { 52 | *head = NULL; 53 | free(list); 54 | return (num); 55 | } 56 | } 57 | num++; 58 | list = _ra(list, num, *head); 59 | next = (*head)->next; 60 | free(*head); 61 | *head = next; 62 | } 63 | free(list); 64 | return (num); 65 | } 66 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - finds the loop in a linked list. 5 | * @head: pointer to the beginning of the list 6 | * 7 | * Return: address of the node where the loop starts or NULL if there's no loop 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *tortoise, *hare; 12 | 13 | tortoise = hare = head; 14 | while (tortoise && hare && hare->next) 15 | { 16 | tortoise = tortoise->next; 17 | hare = hare->next->next; 18 | if (tortoise == hare) 19 | { 20 | tortoise = head; 21 | break; 22 | } 23 | } 24 | if (!tortoise || !hare || !hare->next) 25 | return (NULL); 26 | while (tortoise != hare) 27 | { 28 | tortoise = tortoise->next; 29 | hare = hare->next; 30 | } 31 | return (hare); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * add_nodeint - add node at beginning of a listint_t list. 5 | * 6 | * @head: head of double pointer 7 | * @n: int add the list 8 | * Return: NULL if it failed 9 | */ 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | listint_t *ptr; 13 | 14 | if (head == NULL) 15 | return (NULL); 16 | ptr = malloc(sizeof(listint_t)); 17 | if (ptr == NULL) 18 | return (NULL); 19 | ptr->n = n; 20 | ptr->next = *head; 21 | *head = ptr; 22 | return (ptr); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * add_nodeint_end - add node at end of a listint_t list. 5 | * 6 | * @head: head of double pointer 7 | * @n: int add the list 8 | * Return: NULL if it failed 9 | */ 10 | listint_t *add_nodeint_end(listint_t **head, const int n) 11 | { 12 | listint_t *new; 13 | listint_t *temp; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | new = malloc(sizeof(listint_t)); 18 | if (new == NULL) 19 | return (NULL); 20 | new->n = n; 21 | new->next = NULL; 22 | if (*head == NULL) 23 | { 24 | *head = new; 25 | return (new); 26 | } 27 | temp = *head; 28 | while (temp->next != NULL) 29 | { 30 | temp = temp->next; 31 | } 32 | temp->next = new; 33 | return (new); 34 | } 35 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_listint - frees a listint_t list 6 | * 7 | * @head: the head of the list 8 | * 9 | * Return: NULL if error 10 | */ 11 | 12 | void free_listint(listint_t *head) 13 | { 14 | listint_t *next; 15 | 16 | while (head != NULL) 17 | { 18 | next = head->next; 19 | free(head); 20 | head = next; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_listint2 - function that frees a listint_t list 6 | * @head: a double pointer of lists 7 | * 8 | * Return: void 9 | */ 10 | 11 | void free_listint2(listint_t **head) 12 | { 13 | listint_t *next; 14 | 15 | if (head == NULL) 16 | return; 17 | while (*head != NULL) 18 | { 19 | next = (*head)->next; 20 | free(*head); 21 | *head = next; 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * pop_listint - deletes the head node 6 | * @head: a double pointer 7 | * 8 | *Return: empty 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | listint_t *start; 13 | int n; 14 | 15 | if (head == NULL || *head == NULL) 16 | return (0); 17 | start = *head; 18 | *head = start->next; 19 | n = start->n; 20 | free(start); 21 | return (n); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - the nth node of a listint_t linked list. 5 | * @head: pointer of the first node 6 | * @index: index is the index of the node 7 | * 8 | * Return: pointer of Index 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | if (head == NULL) 15 | return (NULL); 16 | for (i = 0; i < index; i++) 17 | { 18 | head = head->next; 19 | if (head == NULL) 20 | return (NULL); 21 | } 22 | return (head); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - sum of all the data (n) of a listint_t linked list. 5 | * @head: pointer to the first node 6 | * 7 | * Return: sum all the data 8 | */ 9 | int sum_listint(listint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head != NULL) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | return (sum); 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * insert_nodeint_at_index - inserting a new node at agiven postion 6 | * @head: double pointer 7 | * @idx: index of the node 8 | * @n: new node value 9 | * 10 | * Return: the address of new node 11 | */ 12 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 13 | { 14 | unsigned int i; 15 | listint_t *temp, *new; 16 | 17 | if (head == NULL) 18 | return (NULL); 19 | if (idx != 0) 20 | { 21 | temp = *head; 22 | for (i = 0; i < idx - 1 && temp != NULL; i++) 23 | { 24 | temp = temp->next; 25 | } 26 | if (temp == NULL) 27 | return (NULL); 28 | } 29 | new = malloc(sizeof(listint_t)); 30 | if (new == NULL) 31 | return (NULL); 32 | new->n = n; 33 | if (idx == 0) 34 | { 35 | new->next = *head; 36 | *head = new; 37 | return (new); 38 | } 39 | new->next = temp->next; 40 | temp->next = new; 41 | return (new); 42 | } 43 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | /** 6 | * struct listint_s - singly linked list 7 | * @n: integer 8 | * @next: points to the next node 9 | * 10 | * Description: singly linked list node structure 11 | * 12 | */ 13 | typedef struct listint_s 14 | { 15 | int n; 16 | struct listint_s *next; 17 | } listint_t; 18 | 19 | size_t print_listint(const listint_t *h); 20 | size_t listint_len(const listint_t *h); 21 | listint_t *add_nodeint(listint_t **head, const int n); 22 | listint_t *add_nodeint_end(listint_t **head, const int n); 23 | void free_listint(listint_t *head); 24 | void free_listint2(listint_t **head); 25 | int pop_listint(listint_t **head); 26 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 27 | int sum_listint(listint_t *head); 28 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 29 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 30 | listint_t *reverse_listint(listint_t **head); 31 | size_t print_listint_safe(const listint_t *head); 32 | size_t free_listint_safe(listint_t **h); 33 | listint_t *find_listint_loop(listint_t *head); 34 | int _putchar(char c); 35 | 36 | #endif /*LISTS_H*/ 37 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint -converts a binary number to an unsigned int. 5 | * @b: pointing to a string of 0 and 1 chars 6 | * 7 | * Return:converted number, or 0 8 | */ 9 | unsigned int binary_to_uint(const char *b) 10 | { 11 | int i; 12 | unsigned int j; 13 | 14 | j = 0; 15 | if (!b) 16 | return (0); 17 | for (i = 0; b[i] != '\0'; i++) 18 | { 19 | if (b[i] != '0' && b[i] != '1') 20 | return (0); 21 | } 22 | for (i = 0; b[i] != '\0'; i++) 23 | { 24 | j <<= 1; 25 | if (b[i] == '1') 26 | j += 1; 27 | } 28 | return (j); 29 | } 30 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/00-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * get_endianness - checks the endianness 4 | * 5 | * Return: 0 if big endian, 1 if little endian 6 | */ 7 | int get_endianness(void) 8 | { 9 | int i; 10 | char *c; 11 | 12 | i = 1; 13 | c = (char *)&i; 14 | 15 | return (*c); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _power - calculate (base and power) 4 | * @base: base of the exponet 5 | * @pow: power of the exponet 6 | * Return: value of base and power 7 | */ 8 | unsigned long int _power(unsigned int base, unsigned int pow) 9 | { 10 | unsigned long int num; 11 | unsigned int i; 12 | 13 | num = 1; 14 | for (i = 1; i <= pow; i++) 15 | num *= base; 16 | return (num); 17 | } 18 | /** 19 | * print_binary - prints the binary representation of a number 20 | * @n: num of prented 21 | * Return: void 22 | */ 23 | void print_binary(unsigned long int n) 24 | { 25 | unsigned long int dev, result; 26 | char flag; 27 | 28 | flag = 0; 29 | dev = _power(2, sizeof(unsigned long int) * 8 - 1); 30 | 31 | while (dev != 0) 32 | { 33 | result = n & dev; 34 | if (result == dev) 35 | { 36 | flag = 1; 37 | _putchar('1'); 38 | 39 | } 40 | else if (flag == 1 || dev == 1) 41 | { 42 | _putchar('0'); 43 | } 44 | dev >>= 1; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given index. 5 | * @n: checking bits 6 | * @index: which to check bit 7 | * 8 | * Return: the value of the bit at index 9 | */ 10 | int get_bit(unsigned long int n, unsigned int index) 11 | { 12 | unsigned long int div, res; 13 | 14 | if (index > (sizeof(unsigned long int) * 8 - 1)) 15 | return (-1); 16 | div = 1 << index; 17 | res = n & div; 18 | if (res == div) 19 | return (1); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - the value of a bit to 1 at a given index. 5 | * @index: starting from 0 of the bit you want to set 6 | * @n: pointer number of i 7 | * Return: 1 if it worked, or -1 if an error occurred 8 | */ 9 | int set_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned long int i; 12 | 13 | if (index > (sizeof(unsigned long int) * 8 - 1)) 14 | return (-1); 15 | i = 1 << index; 16 | *n = *n | i; 17 | 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a bit to 0 at a given index. 5 | * @n: num of i 6 | * @index: starting from 0 of the bit you want to set 7 | * Return: 1 if it worked, or -1 if an error occurred 8 | */ 9 | 10 | int clear_bit(unsigned long int *n, unsigned int index) 11 | { 12 | unsigned long int i; 13 | 14 | if (index > (sizeof(unsigned long int) * 8 - 1)) 15 | return (-1); 16 | i = ~(1 << index); 17 | *n = *n & i; 18 | 19 | return (1); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * flip_bits - flip to get from one number to another. 4 | * @n: the first number 5 | * @m: the second number 6 | * Return: the number of bits you would need to flip 7 | */ 8 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 9 | { 10 | unsigned long int differnce, result; 11 | unsigned int j, i; 12 | 13 | j = 0; 14 | result = 1; 15 | differnce = n ^ m; 16 | for (i = 0; i < (sizeof(unsigned long int) * 8); i++) 17 | { 18 | if (result == (differnce & result)) 19 | j++; 20 | result <<= 1; 21 | } 22 | 23 | return (j); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # 0x14. C - Bit manipulation 2 | ## About 3 | An introductory project on: 4 | - Bit manipulation 5 | - Bitwise operators 6 | 7 | ## File Descriptions 8 | ### Mandatory 9 | **[0-binary_to_uint.c](0-binary_to_uint.c)** - function that converts a binary number to an `unsigned int`. 10 | 11 | **[1-print_binary.c](1-print_binary.c)** - function that prints the binary representation of a number. 12 | 13 | **[2-get_bit.c](2-get_bit.c)** - function that returns the value of a bit at a given index. 14 | 15 | **[3-set_bit.c](3-set_bit.c)** - function that sets the value of a bit to `1` at a given index. 16 | 17 | **[4-clear_bit.c](4-clear_bit.c)** - function that sets the value of a bit to `0` at a given index. 18 | 19 | **[5-flip_bits.c](5-flip_bits.c)** - function that returns the number of bits you would need to flip to get from one number to another. 20 | 21 | ### Advanced 22 | **[100-get_endianness.c](100-get_endianness.c)** - function that checks the endianness. 23 | 24 | **[101-password](101-password)** - finds the password for [this program](https://github.com/eyoul) and saves it in a file. 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | unsigned long int _power(unsigned int base, unsigned int pow); 4 | unsigned int binary_to_uint(const char *b); 5 | void print_binary(unsigned long int n); 6 | int get_bit(unsigned long int n, unsigned int index); 7 | int set_bit(unsigned long int *n, unsigned int index); 8 | int clear_bit(unsigned long int *n, unsigned int index); 9 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 10 | int get_endianness(void); 11 | int _putchar(char c); 12 | 13 | #endif /*MAIN_H*/ 14 | -------------------------------------------------------------------------------- /0x15-file_io/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | ssize_t n; 13 | 14 | if (ac != 2) 15 | { 16 | dprintf(2, "Usage: %s filename\n", av[0]); 17 | exit(1); 18 | } 19 | n = read_textfile(av[1], 114); 20 | printf("\n(printed chars: %li)\n", n); 21 | n = read_textfile(av[1], 1024); 22 | printf("\n(printed chars: %li)\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 0-read_textfile.c 3 | * Auth: Dr Marcus. 4 | */ 5 | #include "main.h" 6 | #include 7 | 8 | /** 9 | * read_textfile - Reads a text file and prints it to POSIX stdout. 10 | * @filename: A pointer to the name of the file. 11 | * @letters: The number of letters the 12 | * function should read and print. 13 | * 14 | * Return: If the function fails or filename is NULL - 0. 15 | * O/w - the actual number of bytes the function can read and print. 16 | */ 17 | ssize_t read_textfile(const char *filename, size_t letters) 18 | { 19 | ssize_t o, r, w; 20 | char *buffer; 21 | 22 | if (filename == NULL) 23 | return (0); 24 | 25 | buffer = malloc(sizeof(char) * letters); 26 | if (buffer == NULL) 27 | return (0); 28 | 29 | o = open(filename, O_RDONLY); 30 | r = read(o, buffer, letters); 31 | w = write(STDOUT_FILENO, buffer, r); 32 | 33 | if (o == -1 || r == -1 || w == -1 || w != r) 34 | { 35 | free(buffer); 36 | return (0); 37 | } 38 | 39 | free(buffer); 40 | close(o); 41 | 42 | return (w); 43 | } 44 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * create_file -creates an array of chars, and initializes 4 | * 5 | * @text_content: is a NULL terminated string to write to the file 6 | * @filename: is the name of the file to create 7 | * 8 | * Return: 1 on success, -1 on failure 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int o, w, len = 0; 13 | 14 | if (filename == NULL) 15 | return (-1); 16 | 17 | if (text_content != NULL) 18 | { 19 | for (len = 0; text_content[len];) 20 | len++; 21 | } 22 | 23 | o = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600); 24 | w = write(o, text_content, len); 25 | 26 | if (o == -1 || w == -1) 27 | return (-1); 28 | 29 | close(o); 30 | 31 | return (1); 32 | } 33 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - finds the length of a string 5 | * @str: pointer to the string 6 | * 7 | * Return: length of the string 8 | */ 9 | size_t _strlen(char *str) 10 | { 11 | size_t i; 12 | 13 | for (i = 0; str[i]; i++) 14 | ; 15 | return (i); 16 | } 17 | 18 | /** 19 | * append_text_to_file - appends a text at the end of a file. 20 | * @filename: name of the file 21 | * @text_content: NULL terminated string to add at the end of the file 22 | * 23 | * Return: 1 on success and -1 on failure 24 | */ 25 | int append_text_to_file(const char *filename, char *text_content) 26 | { 27 | int fd; 28 | ssize_t len; 29 | 30 | if (filename == NULL) 31 | return (-1); 32 | fd = open(filename, O_WRONLY | O_APPEND); 33 | if (fd == -1) 34 | return (-1); 35 | if (text_content != NULL) 36 | len = write(fd, text_content, _strlen(text_content)); 37 | close(fd); 38 | if (len == -1) 39 | return (-1); 40 | return (1); 41 | } 42 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | # 0x15. C - File I/O 2 | -------------------------------------------------------------------------------- /0x15-file_io/Requiescat: -------------------------------------------------------------------------------- 1 | by Oscar Wilde 2 | 3 | Tread lightly, she is near 4 | Under the snow, 5 | Speak gently, she can hear 6 | The daisies grow. 7 | 8 | All her bright golden hair 9 | Tarnished with rust, 10 | She that was young and fair 11 | Fallen to dust. 12 | 13 | Lily-like, white as snow, 14 | She hardly knew 15 | She was a woman, so 16 | Sweetly she grew. 17 | 18 | Coffin-board, heavy stone, 19 | Lie on her breast, 20 | I vex my heart alone, 21 | She is at rest. 22 | 23 | Peace, Peace, she cannot hear 24 | Lyre or sonnet, 25 | All my life's buried here, 26 | Heap earth upon it. 27 | -------------------------------------------------------------------------------- /0x15-file_io/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x15-file_io/a -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int _putchar(char c); 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif /*MAIN_H*/ 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_dlistint - print all the elements of a dlistint_t list 6 | * @h: pointer to the start of the linked list 7 | * 8 | * Return: number of nodes 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | size_t i; 13 | 14 | for (i = 0; h != NULL; i++) 15 | { 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | } 19 | return (i); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - count the number of nodes in the linked list 5 | * @h: pointer to the beginning of a linked list 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t i; 12 | 13 | for (i = 0; h != NULL; i++) 14 | h = h->next; 15 | return (i); 16 | } 17 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - generate a key depending on a username for crackme5 7 | * @argc: number of arguments passed 8 | * @argv: arguments passed to main 9 | * 10 | * Return: 0 on success, 1 on error 11 | */ 12 | int main(int argc, char *argv[]) 13 | { 14 | unsigned int i, b; 15 | size_t len, add; 16 | char *l = "A-CHRDw87lNS0E9B2TibgpnMVys5XzvtOGJcYLU+4mjW6fxqZeF3Qa1rPhdKIouk"; 17 | char p[7] = " "; 18 | 19 | if (argc != 2) 20 | { 21 | printf("Correct usage: ./keygen5 username\n"); 22 | return (1); 23 | } 24 | len = strlen(argv[1]); 25 | p[0] = l[(len ^ 59) & 63]; 26 | for (i = 0, add = 0; i < len; i++) 27 | add += argv[1][i]; 28 | p[1] = l[(add ^ 79) & 63]; 29 | for (i = 0, b = 1; i < len; i++) 30 | b *= argv[1][i]; 31 | p[2] = l[(b ^ 85) & 63]; 32 | for (b = argv[1][0], i = 0; i < len; i++) 33 | if ((char)b <= argv[1][i]) 34 | b = argv[1][i]; 35 | srand(b ^ 14); 36 | p[3] = l[rand() & 63]; 37 | for (b = 0, i = 0; i < len; i++) 38 | b += argv[1][i] * argv[1][i]; 39 | p[4] = l[(b ^ 239) & 63]; 40 | for (b = 0, i = 0; (char)i < argv[1][0]; i++) 41 | b = rand(); 42 | p[5] = l[(b ^ 229) & 63]; 43 | printf("%s\n", p); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_dnodeint - add a new node at the beginning of the linked list 6 | * @head: double pointer to the beginning of the linked list 7 | * @n: value to add to the new node 8 | * 9 | * Return: pointer to the new node, or NULL on failure 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | new = malloc(sizeof(dlistint_t)); 18 | if (new == NULL) 19 | return (NULL); 20 | new->n = n; 21 | new->prev = NULL; 22 | new->next = *head; 23 | *head = new; 24 | if (new->next != NULL) 25 | (new->next)->prev = new; 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_dnodeint_end - adds a node to the end of a linked list 6 | * @head: double pointer to the beginning of the linked list 7 | * @n: value to add to new node 8 | * 9 | * Return: pointer to the new node, or NULL on failure 10 | */ 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new, *tmp; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | new = malloc(sizeof(dlistint_t)); 18 | if (new == NULL) 19 | return (NULL); 20 | new->n = n; 21 | new->next = NULL; 22 | if (*head == NULL) 23 | { 24 | new->prev = NULL; 25 | *head = new; 26 | return (new); 27 | } 28 | tmp = *head; 29 | while (tmp->next != NULL) 30 | tmp = tmp->next; 31 | tmp->next = new; 32 | new->prev = tmp; 33 | return (new); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_dlistint - free a dlistint_t list 6 | * @head: pointer to the beginning of the linked list 7 | * 8 | * Return: void 9 | */ 10 | void free_dlistint(dlistint_t *head) 11 | { 12 | dlistint_t *next; 13 | 14 | while (head != NULL) 15 | { 16 | next = head->next; 17 | free(head); 18 | head = next; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - finds a specific node of a linked list 5 | * @head: pointer to the beginning of the list 6 | * @index: index of the node to retrieve 7 | * 8 | * Return: pointer to the indexed node, or NULL on failure 9 | */ 10 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | if (head == NULL) 15 | return (NULL); 16 | if (index == 0) 17 | return (head); 18 | for (i = 0; i < index; i++) 19 | { 20 | if (head->next == NULL) 21 | return (NULL); 22 | head = head->next; 23 | } 24 | return (head); 25 | } 26 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - sums all of the data of a dlistint_t linked list 5 | * @head: pointer to the beginning of the linked list 6 | * 7 | * Return: sum of all data, or 0 if the list is empty 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head != NULL) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | return (sum); 19 | } 20 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * insert_dnodeint_at_index - inserts a new node at a given position 7 | * @h: double pointer to the beginning of the linked list 8 | * @idx: index at which to insert the new node 9 | * @n: data to enter into new node 10 | * 11 | * Return: pointer to the new node, or NULL on failure 12 | */ 13 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 14 | { 15 | dlistint_t *new, *next, *current; 16 | unsigned int i; 17 | 18 | if (h == NULL) 19 | return (NULL); 20 | if (idx != 0) 21 | { 22 | current = *h; 23 | for (i = 0; i < idx - 1 && current != NULL; i++) 24 | current = current->next; 25 | if (current == NULL) 26 | return (NULL); 27 | } 28 | new = malloc(sizeof(dlistint_t)); 29 | if (new == NULL) 30 | return (NULL); 31 | new->n = n; 32 | if (idx == 0) 33 | { 34 | next = *h; 35 | *h = new; 36 | new->prev = NULL; 37 | } 38 | else 39 | { 40 | new->prev = current; 41 | next = current->next; 42 | current->next = new; 43 | } 44 | new->next = next; 45 | if (new->next != NULL) 46 | new->next->prev = new; 47 | return (new); 48 | } 49 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * delete_dnodeint_at_index - deletes a node at a specific index 6 | * @head: double pointer to the linked list 7 | * @index: index at which to delete node 8 | * 9 | * Return: 1 on success, -1 on failure 10 | */ 11 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 12 | { 13 | dlistint_t *current; 14 | unsigned int i; 15 | 16 | if (head == NULL || *head == NULL) 17 | return (-1); 18 | current = *head; 19 | if (index == 0) 20 | { 21 | *head = current->next; 22 | if (current->next != NULL) 23 | { 24 | current->next->prev = NULL; 25 | } 26 | free(current); 27 | return (1); 28 | } 29 | for (i = 0; i < index; i++) 30 | { 31 | if (current->next == NULL) 32 | return (-1); 33 | current = current->next; 34 | } 35 | current->prev->next = current->next; 36 | if (current->next != NULL) 37 | current->next->prev = current->prev; 38 | free(current); 39 | return (1); 40 | } 41 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x17-doubly_linked_lists 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_H 2 | #define LIST_H 3 | 4 | #include 5 | /** 6 | * struct dlistint_s - doubly linked list 7 | * @n: integer 8 | * @prev: points to the previous node 9 | * @next: points to the next node 10 | * 11 | * Description: doubly linked list node structure 12 | * 13 | */ 14 | typedef struct dlistint_s 15 | { 16 | int n; 17 | struct dlistint_s *prev; 18 | struct dlistint_s *next; 19 | } dlistint_t; 20 | 21 | size_t print_dlistint(const dlistint_t *h); 22 | size_t dlistint_len(const dlistint_t *h); 23 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 24 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 25 | void free_dlistint(dlistint_t *head); 26 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 27 | int sum_dlistint(dlistint_t *head); 28 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 29 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 30 | 31 | #endif/*LIST_H*/ 32 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/palindrome.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | def ispalindrome(n): 3 | string = str(n) 4 | return string == string[::-1] 5 | 6 | array = [] 7 | for i in range(999, 99, -1): 8 | for j in range(i, 99, -1): 9 | if ispalindrome(i * j): 10 | array.append(i * j) 11 | print(max(array)) 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc *.c -c -fPIC 3 | gcc *.o -shared -o liball.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-test.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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/eyoul/alx-low_level_programming/raw/main/0x18-dynamic_libraries/iwin.so 3 | export LD_PRELOAD=/tmp/iwin.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x18. C - Dynamic libraries 2 | 3 | In this project, i learnt about what dynamic library is, how it works, how to create one and how it works. It also helped me understand the differences between static and shared libraries. 4 | 5 | ## Function Prototypes :floppy_disk: 6 | The following files are scripts written for the projects written in C: 7 | 8 | 9 | ## Table of contents 10 | Files | Description 11 | ----- | ----------- 12 | [libdynamic.so](./libdynamic.so) | C dynamic library containing the function definitions 13 | [main.h](./main.h) | Header files containing the function prototypes 14 | [1-create_dynamic_lib.sh](./1-create_dynamic_lib.sh) | Bash script that creates a dynamic library called liball.so from all the .c files that are in the current directory 15 | [100-operations.so](./100-operations.so) | C dynamic library that contains C functions that can be called from Python 16 | [random.so](./random.so) | C dynamic library to inject in a giga million program 17 | [101-make_me_win.sh](./101-make_me_win.sh) | Bash script to inject the libmask.so library, using LD_PRELOAD, in the giga million program 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/_putchar.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_abs - compute the absolute value of an integer 4 | *@n: print character 5 | *Return: 0 6 | */ 7 | int _abs(int n) 8 | { 9 | if (n > 0) 10 | { 11 | return (n); 12 | } 13 | else 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/abs.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _atoi - gets sign and numbers of string 5 | * @s: array 6 | * Return: gets numbers with its sign 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int cont1 = 0, a, b, c, num = 0, tam; 11 | int aux2 = 1; 12 | 13 | while (*(s + cont1) != '\0') 14 | { 15 | cont1++; 16 | } 17 | for (a = 0; a < cont1; a++) 18 | { 19 | if (*(s + a) >= '0' && *(s + a) <= '9') 20 | break; 21 | } 22 | for (b = a; b < cont1; b++) 23 | { 24 | if (!(*(s + b) >= '0' && *(s + b) <= '9')) 25 | break; 26 | } 27 | for (c = 0; c < a; c++) 28 | { 29 | if (*(s + c) == '-') 30 | aux2 = aux2 * (-1); 31 | } 32 | tam = b - a; 33 | while (tam >= 1) 34 | { 35 | num = (num * 10) + (*(s + a) - '0'); 36 | a++; 37 | tam--; 38 | } 39 | num = num * aux2; 40 | return (num); 41 | } 42 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/atoi.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *_isalpha - check for alphabetic character 5 | *@c: verify character 6 | *Return: 0 7 | */ 8 | int _isalpha(int c) 9 | { 10 | if (c >= 65 && c <= 90) 11 | { 12 | return (1); 13 | } 14 | else if (c >= 97 && c <= 122) 15 | { 16 | return (1); 17 | } 18 | else 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/isalpha.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_isdigit - function to check for digit 5 | *@c: integer 6 | *Return: a 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | int a = 0; 12 | 13 | if (c >= 48 && c <= 57) 14 | a = 1; 15 | return (a); 16 | } 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/isdigit.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *_islower - check for lowercase alphabet 5 | *@c: the character to print 6 | *Return: 0 7 | */ 8 | int _islower(int c) 9 | { 10 | if (c >= 97 && c <= 122) 11 | { 12 | return (1); 13 | } 14 | else 15 | return (0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/islower.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - function block 6 | * Description: check for uppercase 7 | * @c: integer 8 | * Return: 0 9 | */ 10 | 11 | int _isupper(int c) 12 | { 13 | int a = 0; 14 | 15 | if (c >= 65 && c <= 90) 16 | a = 1; 17 | return (a); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/isupper.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/libfilename.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/libfilename.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif/*MAIN_H*/ 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_memcpy - copies bytes from memory area src to memory area dest 5 | *@dest: destination area memory 6 | *@src: source memory area 7 | *@n: data 8 | * 9 | *Return: dest 10 | */ 11 | 12 | char *_memcpy(char *dest, char *src, unsigned int n) 13 | { 14 | unsigned int i; 15 | i = 0; 16 | 17 | while (i < n) 18 | { 19 | dest[i] = src[i]; 20 | i++; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/memcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_memset - fill memory with constant 5 | *@s: space of memory 6 | *@b: byte 7 | *@n: memory area 8 | * 9 | *Return: pointer to the memory area 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | unsigned int i; 15 | i = 0; 16 | 17 | while (i < n) 18 | { 19 | *(s + i) = b; 20 | i++; 21 | } 22 | return (s); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/memset.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_puts - prints a string 5 | *@s: string 6 | * 7 | *Return : nothing 8 | */ 9 | 10 | void _puts(char *s) 11 | { 12 | int i = 0; 13 | 14 | while (s[i] != '\0') 15 | { 16 | _putchar(s[i]); 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/puts.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - appends src to the dest string 5 | * @dest: string to append by src 6 | * @src: string to append to dest 7 | * 8 | * Return: address of dest 9 | */ 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i, j; 13 | 14 | i = j = 0; 15 | while (*(dest + i)) 16 | i++; 17 | while ((*(dest + i) = *(src + j))) 18 | { 19 | i++; 20 | j++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strcat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strchr - locates character in a string 5 | *@s: string 6 | *@c: character 7 | * 8 | *Return: 0 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | 14 | do { 15 | if (*s == c) 16 | return ((char *)s); 17 | } 18 | while (*s++) 19 | ; 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strchr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strcmp - compare strings 5 | *@s1: string 6 | *@s2: string 7 | * 8 | *Return: integer j 9 | */ 10 | 11 | int _strcmp(char *s1, char *s2) 12 | { 13 | int i = 0; 14 | int j = 0; 15 | 16 | while (s1[i] == s2[i] && s1[i] != '\0') 17 | { 18 | i++; 19 | } 20 | j += s1[i] - s2[i]; 21 | return (j); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strcmp.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | *_strcpy - copy string including the terminating null byte 6 | *@dest: destination of string 7 | *@src: source of string 8 | * 9 | *Return: the value dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int i = 0; 15 | 16 | for (i = 0; src[i]; i++) 17 | dest[i] = src[i]; 18 | dest[i] = src[i]; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strlen - count the length of a string 5 | *@s: string 6 | * 7 | *Return: the value of the length i 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int i = 0; 13 | 14 | while (s[i]) 15 | i++; 16 | 17 | return (i); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strlen.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strncat - concatenate strings 5 | *@dest: destination 6 | *@src: source 7 | *@n: integer 8 | * 9 | *Return: destination 10 | */ 11 | 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int j = 0; 15 | int i = 0; 16 | 17 | while (dest[i] != '\0') 18 | { 19 | i++; 20 | } 21 | 22 | for (j = 0; j < n && src[j] != '\0'; j++) 23 | 24 | dest[j + 1] = src[i]; 25 | dest[j + i + 1] = '\0'; 26 | 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strncat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strncpy - copy strings 5 | *@dest: destination 6 | *@src: source 7 | *@n: integer 8 | * 9 | *Return: destination 10 | */ 11 | 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | 15 | int j = 0; 16 | 17 | for (j = 0; src[j] != '\0'; j++) 18 | { 19 | if (j < n) 20 | { 21 | dest[j] = src[j]; 22 | } 23 | } 24 | for (; j < n; j++) 25 | { 26 | dest[j] = '\0'; 27 | } 28 | return (dest); 29 | } 30 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strncpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | 5 | /** 6 | *_strpbrk - locate the first occurence in the string 7 | *@s: string 8 | *@accept: string 9 | * 10 | *Return: NULL 11 | */ 12 | 13 | char *_strpbrk(char *s, char *accept) 14 | { 15 | int i = 0; 16 | int j = 0; 17 | 18 | for (i = 0; s[i]; i++) 19 | { 20 | for (j = 0; accept[j]; j++) 21 | if (*(accept + j) == s[i]) 22 | return (&(s[i])); 23 | } 24 | return (NULL); 25 | } 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strpbrk.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strspn - get the length of a prefix substring 5 | *@s: string 6 | *@accept: string 7 | * 8 | *Return: integer 9 | */ 10 | 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int a, b; 14 | 15 | for (a = 0; s[a]; a++) 16 | { 17 | for (b = 0; accept[b]; b++) 18 | { 19 | if (s[a] == accept[b]) 20 | break; 21 | } 22 | if (s[a] != accept[b]) 23 | break; 24 | } 25 | return (a); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strspn.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | *_strstr - find the first occurence 6 | *@haystack: string 7 | *@needle: string 8 | * 9 | *Return: the pointer to the first occurence 10 | */ 11 | #include 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | 15 | int i = 0; 16 | int j = 0; 17 | int k, s; 18 | 19 | for (s = 0; needle[s]; s++) 20 | ; 21 | if (s == 0) 22 | return (haystack); 23 | while (haystack[i]) 24 | { 25 | if (haystack[i] == needle[j]) 26 | { 27 | i++; 28 | j++; 29 | for (k = i; haystack[k]; k++) 30 | { 31 | if (needle[j] == '\0') 32 | return (&(haystack[i - 1])); 33 | if (haystack[k] != needle[j]) 34 | break; 35 | j++; 36 | } 37 | } 38 | i++; 39 | j = 0; 40 | } 41 | return (NULL); 42 | } 43 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/TEST/strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/TEST/strstr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/iwin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int rand() 5 | { 6 | static int ct = -1; 7 | 8 | ct++; 9 | if (ct == 0) 10 | return 8; 11 | if (ct == 1) 12 | return 8; 13 | if (ct == 2) 14 | return 7; 15 | if (ct == 3) 16 | return 9; 17 | if (ct == 4) 18 | return 23; 19 | if (ct == 5) 20 | return 74; 21 | return ct * ct % 30000; 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/iwin.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/iwin.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif/*MAIN_H*/ 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/test.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 mul(int a, int b) 12 | { 13 | return(a * b); 14 | } 15 | 16 | int div(int a, int b) 17 | { 18 | if (b != 0) 19 | return (a / b); 20 | return (0); 21 | } 22 | 23 | int mod(int a, int b) 24 | { 25 | if (b != 0) 26 | return (a % b); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creates a hash table 5 | * @size: size of the array 6 | * 7 | * Return: pointer to the newly created hash table 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *hash_table; 12 | unsigned long int i; 13 | 14 | hash_table = malloc(sizeof(hash_table_t)); 15 | if (hash_table == NULL) 16 | return (NULL); 17 | hash_table->size = size; 18 | hash_table->array = malloc(size * sizeof(hash_node_t *)); 19 | if (hash_table->array == NULL) 20 | { 21 | free(hash_table); 22 | return (NULL); 23 | } 24 | for (i = 0; i < size; i++) 25 | hash_table->array[i] = NULL; 26 | return (hash_table); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - implementation of the djb2 algorithm 5 | * @str: string used to generate hash value 6 | * 7 | * Return: hash value 8 | */ 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hash; 12 | int c; 13 | 14 | hash = 5381; 15 | while ((c = *str++)) 16 | { 17 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 18 | } 19 | return (hash); 20 | } 21 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - gives the index of a key 5 | * @key: key to get index for 6 | * @size: size of the hash table 7 | * 8 | * Return: index for the key 9 | */ 10 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 11 | { 12 | return (hash_djb2(key) % size); 13 | } 14 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * make_hash_node - creates a new hash node 5 | * @key: key for the node 6 | * @value: for the node 7 | * 8 | * Return: the new node, or NULL on failure 9 | */ 10 | hash_node_t *make_hash_node(const char *key, const char *value) 11 | { 12 | hash_node_t *node; 13 | 14 | node = malloc(sizeof(hash_node_t)); 15 | if (node == NULL) 16 | return (NULL); 17 | node->key = strdup(key); 18 | if (node->key == NULL) 19 | { 20 | free(node); 21 | return (NULL); 22 | } 23 | node->value = strdup(value); 24 | if (node->value == NULL) 25 | { 26 | free(node->key); 27 | free(node); 28 | return (NULL); 29 | } 30 | node->next = NULL; 31 | return (node); 32 | } 33 | 34 | 35 | /** 36 | * hash_table_set - sets a key to a value in the hash table 37 | * @ht: hash table to add elemt to 38 | * @key: key for the data 39 | * @value: data to store 40 | * 41 | * Return: 1 if successful, 0 otherwise 42 | */ 43 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 44 | { 45 | unsigned long int index; 46 | hash_node_t *hash_node, *tmp; 47 | char *new_value; 48 | 49 | if (ht == NULL || ht->array == NULL || ht->size == 0 || 50 | key == NULL || strlen(key) == 0 || value == NULL) 51 | return (0); 52 | index = key_index((const unsigned char *)key, ht->size); 53 | tmp = ht->array[index]; 54 | while (tmp != NULL) 55 | { 56 | if (strcmp(tmp->key, key) == 0) 57 | { 58 | new_value = strdup(value); 59 | if (new_value == NULL) 60 | return (0); 61 | free(tmp->value); 62 | tmp->value = new_value; 63 | return (1); 64 | } 65 | tmp = tmp->next; 66 | } 67 | hash_node = make_hash_node(key, value); 68 | if (hash_node == NULL) 69 | return (0); 70 | hash_node->next = ht->array[index]; 71 | ht->array[index] = hash_node; 72 | return (1); 73 | } 74 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - retrieves a value associated with a key 5 | * @ht: table to retrieve value from 6 | * @key: key to find value 7 | * 8 | * Return: value associated with key, or NULL if key cannot be found 9 | */ 10 | char *hash_table_get(const hash_table_t *ht, const char *key) 11 | { 12 | unsigned long int index; 13 | hash_node_t *tmp; 14 | 15 | if (ht == NULL || ht->array == NULL || ht->size == 0 || 16 | key == NULL || strlen(key) == 0) 17 | return (NULL); 18 | index = key_index((const unsigned char *)key, ht->size); 19 | tmp = ht->array[index]; 20 | while (tmp != NULL) 21 | { 22 | if (strcmp(tmp->key, key) == 0) 23 | return (tmp->value); 24 | tmp = tmp->next; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - prints a hash table 5 | * @ht: hash table to print 6 | * 7 | * Return: void 8 | */ 9 | void hash_table_print(const hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *tmp; 13 | char flag = 0; /* 0 while no data has been printed yet */ 14 | 15 | if (ht == NULL || ht->array == NULL) 16 | return; 17 | printf("{"); 18 | for (i = 0; i < ht->size; i++) 19 | { 20 | tmp = ht->array[i]; 21 | while (tmp != NULL) 22 | { 23 | if (flag == 1) 24 | printf(", "); 25 | printf("'%s': '%s'", tmp->key, tmp->value); 26 | flag = 1; 27 | tmp = tmp->next; 28 | } 29 | } 30 | printf("}\n"); 31 | } 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - deletes a hash table 5 | * @ht: hash table to delete 6 | * 7 | * Return: void 8 | */ 9 | void hash_table_delete(hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *next; 13 | 14 | if (ht == NULL || ht->array == NULL || ht->size == 0) 15 | return; 16 | for (i = 0; i < ht->size; i++) 17 | { 18 | while (ht->array[i] != NULL) 19 | { 20 | next = ht->array[i]->next; 21 | free(ht->array[i]->key); 22 | free(ht->array[i]->value); 23 | free(ht->array[i]); 24 | ht->array[i] = next; 25 | } 26 | } 27 | free(ht->array); 28 | ht->array = NULL; 29 | ht->size = 0; 30 | free(ht); 31 | } 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | 0x1A. C - Hash tables 2 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all : main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 4 | all: $(SRC) 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean: 11 | $(RM) *~ $(NAME) 12 | 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: fclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(CLFAGS) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Return the perimiter of an island. 7 | The grid represents water by 0 and land by 1. 8 | Args: 9 | grid (list): A list of list of integers representing an island. 10 | Returns: 11 | The perimeter of the island defined in grid. 12 | """ 13 | width = len(grid[0]) 14 | height = len(grid) 15 | edges = 0 16 | size = 0 17 | 18 | for i in range(height): 19 | for j in range(width): 20 | if grid[i][j] == 1: 21 | size += 1 22 | if (j > 0 and grid[i][j - 1] == 1): 23 | edges += 1 24 | if (i > 0 and grid[i - 1][j] == 1): 25 | edges += 1 26 | return size * 4 - edges * 2 27 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | 0x1C-makefiles 2 | Github Repo - Alx low level 3 | Author - Bright Daniel 4 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | /* school */ 14 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_holberton - print the logo of Holberton 5 | * 6 | * Return: Nothing 7 | * 8 | */ 9 | void print_school(void) 10 | { 11 | printf("j#0000000000000000000000000000000000000\n"); 12 | printf("j#000000000000000000@Q**g00000000000000\n"); 13 | printf("j#0000000000000000*]++]4000000000000000\n"); 14 | printf("j#000000000000000k]++]++*N#000000000000\n"); 15 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 16 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 17 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 18 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 19 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 20 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 21 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 22 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 23 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 24 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 25 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 26 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 27 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 28 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 29 | printf("j#00000000000w]++++]*0##000000000000000\n"); 30 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 31 | printf("j#00000000000000000we]+]Q00000000000000\n"); 32 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 33 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 34 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 35 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 36 | } 37 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * linear_search - searches for a value in an array of 8 | * integers using the Linear search algorithm 9 | * @array: array to search the value in 10 | * @size: size of the array 11 | * @value: value to look for 12 | * 13 | * Return: the index of the found value, 14 | * or -1 if not found 15 | */ 16 | int linear_search(int *array, size_t size, int value) 17 | { 18 | size_t i = 0; 19 | 20 | if (!array || size == 0) 21 | return (-1); 22 | 23 | while (i < size) 24 | { 25 | printf("Value checked array[%lu] = [%d]\n", i, array[i]); 26 | if (array[i] == value) 27 | return (i); 28 | i++; 29 | } 30 | 31 | return (-1); 32 | } 33 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - Searches for a value in a sorted array 5 | * of integers using binary search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @size: The number of elements in the array. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the array is NULL, -1. 11 | * Otherwise, the index where the value is located. 12 | * 13 | * Description: Prints the [sub]array being searched after each change. 14 | */ 15 | int binary_search(int *array, size_t size, int value) 16 | { 17 | size_t i, left, right; 18 | 19 | if (array == NULL) 20 | return (-1); 21 | 22 | for (left = 0, right = size - 1; right >= left;) 23 | { 24 | printf("Searching in array: "); 25 | for (i = left; i < right; i++) 26 | printf("%d, ", array[i]); 27 | printf("%d\n", array[i]); 28 | 29 | i = left + (right - left) / 2; 30 | if (array[i] == value) 31 | return (i); 32 | if (array[i] > value) 33 | right = i - 1; 34 | else 35 | left = i + 1; 36 | } 37 | 38 | return (-1); 39 | } 40 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x1E-search_algorithms/100-jump -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * jump_search - Searches for a value in a sorted array 5 | * of integers using jump search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @size: The number of elements in the array. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the array is NULL, -1. 11 | * Otherwise, the first index where the value is located. 12 | * 13 | * Description: Prints a value every time it is compared in the array. 14 | * Uses the square root of the array size as the jump step. 15 | */ 16 | int jump_search(int *array, size_t size, int value) 17 | { 18 | size_t i, jump, step; 19 | 20 | if (array == NULL || size == 0) 21 | return (-1); 22 | 23 | step = sqrt(size); 24 | for (i = jump = 0; jump < size && array[jump] < value;) 25 | { 26 | printf("Value checked array[%ld] = [%d]\n", jump, array[jump]); 27 | i = jump; 28 | jump += step; 29 | } 30 | 31 | printf("Value found between indexes [%ld] and [%ld]\n", i, jump); 32 | 33 | jump = jump < size - 1 ? jump : size - 1; 34 | for (; i < jump && array[i] < value; i++) 35 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 36 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 37 | 38 | return (array[i] == value ? (int)i : -1); 39 | } 40 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - Searches for a value in a sorted array 5 | * of integers using interpolation search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @size: The number of elements in the array. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the array is NULL, -1. 11 | * Otherwise, the first index where the value is located. 12 | * 13 | * Description: Prints a value every time it is compared in the array.. 14 | */ 15 | int interpolation_search(int *array, size_t size, int value) 16 | { 17 | size_t i, l, r; 18 | 19 | if (array == NULL) 20 | return (-1); 21 | 22 | for (l = 0, r = size - 1; r >= l;) 23 | { 24 | i = l + (((double)(r - l) / (array[r] - array[l])) * (value - array[l])); 25 | if (i < size) 26 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 27 | else 28 | { 29 | printf("Value checked array[%ld] is out of range\n", i); 30 | break; 31 | } 32 | 33 | if (array[i] == value) 34 | return (i); 35 | if (array[i] > value) 36 | r = i - 1; 37 | else 38 | l = i + 1; 39 | } 40 | 41 | return (-1); 42 | } 43 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x1E-search_algorithms/103-exponential -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BrightDaniel/alx-low_level_programming/fd9e624974a12eb0d81d858485801ef30c629d8e/0x1E-search_algorithms/104-advanced_binary -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * jump_list - Searches for an algorithm in a sorted singly 5 | * linked list of integers using jump search. 6 | * @list: A pointer to the head of the linked list to search. 7 | * @size: The number of nodes in the list. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the head of the list is NULL, NULL. 11 | * Otherwise, a pointer to the first node where the value is located. 12 | * 13 | * Description: Prints a value every time it is compared in the list. 14 | * Uses the square root of the list size as the jump step. 15 | */ 16 | listint_t *jump_list(listint_t *list, size_t size, int value) 17 | { 18 | size_t step, step_size; 19 | listint_t *node, *jump; 20 | 21 | if (list == NULL || size == 0) 22 | return (NULL); 23 | 24 | step = 0; 25 | step_size = sqrt(size); 26 | for (node = jump = list; jump->index + 1 < size && jump->n < value;) 27 | { 28 | node = jump; 29 | for (step += step_size; jump->index < step; jump = jump->next) 30 | { 31 | if (jump->index + 1 == size) 32 | break; 33 | } 34 | printf("Value checked at index [%ld] = [%d]\n", jump->index, jump->n); 35 | } 36 | 37 | printf("Value found between indexes [%ld] and [%ld]\n", 38 | node->index, jump->index); 39 | 40 | for (; node->index < jump->index && node->n < value; node = node->next) 41 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 42 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 43 | 44 | return (node->n == value ? node : NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - Searches for an algorithm in a sorted singly 5 | * linked list of integers using linear skip. 6 | * @list: A pointer to the head of the linked list to search. 7 | * @value: The value to search for. 8 | * 9 | * Return: If the value is not present or the head of the list is NULL, NULL. 10 | * Otherwise, a pointer to the first node where the value is located. 11 | * 12 | * Description: Prints a value every time it is compared in the list. 13 | * Uses the square root of the list size as the jump step. 14 | */ 15 | skiplist_t *linear_skip(skiplist_t *list, int value) 16 | { 17 | skiplist_t *node, *jump; 18 | 19 | if (list == NULL) 20 | return (NULL); 21 | 22 | for (node = jump = list; jump->next != NULL && jump->n < value;) 23 | { 24 | node = jump; 25 | if (jump->express != NULL) 26 | { 27 | jump = jump->express; 28 | printf("Value checked at index [%ld] = [%d]\n", 29 | jump->index, jump->n); 30 | } 31 | else 32 | { 33 | while (jump->next != NULL) 34 | jump = jump->next; 35 | } 36 | } 37 | 38 | printf("Value found between indexes [%ld] and [%ld]\n", 39 | node->index, jump->index); 40 | 41 | for (; node->index < jump->index && node->n < value; node = node->next) 42 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 43 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 44 | 45 | return (node->n == value ? node : NULL); 46 | } 47 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/create_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search_algos.h" 3 | 4 | void free_list(listint_t *list); 5 | 6 | /** 7 | * create_list - Creates a single linked list 8 | * 9 | * @array: Pointer to the array to use to fill the list 10 | * @size: Size of the array 11 | * 12 | * Return: A pointer to the head of the created list (NULL on failure) 13 | */ 14 | listint_t *create_list(int *array, size_t size) 15 | { 16 | listint_t *list; 17 | listint_t *node; 18 | 19 | list = NULL; 20 | while (array && size--) 21 | { 22 | node = malloc(sizeof(*node)); 23 | if (!node) 24 | { 25 | free_list(list); 26 | return (NULL); 27 | } 28 | node->n = array[size]; 29 | node->index = size; 30 | node->next = list; 31 | list = node; 32 | } 33 | return (list); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search_algos.h" 3 | 4 | /** 5 | * free_list - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_list(listint_t *list) 10 | { 11 | listint_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_list(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search_algos.h" 3 | 4 | /** 5 | * print_list - Prints the content of a listint_t 6 | * 7 | * @list: Pointer to the head of the list 8 | */ 9 | void print_list(const listint_t *list) 10 | { 11 | printf("List :\n"); 12 | while (list) 13 | { 14 | printf("Index[%lu] = [%d]\n", list->index, list->n); 15 | list = list->next; 16 | } 17 | printf("\n"); 18 | } 19 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SEARCH_ALGOS_H_ 2 | #define _SEARCH_ALGOS_H_ 3 | 4 | /* LIBRARIES */ 5 | #include 6 | #include 7 | #include 8 | 9 | /* STRUCTURES */ 10 | /** 11 | * struct listint_s - singly linked list 12 | * 13 | * @n: Integer 14 | * @index: Index of the node in the list 15 | * @next: Pointer to the next node 16 | * 17 | * Description: singly linked list node structure 18 | */ 19 | typedef struct listint_s 20 | { 21 | int n; 22 | size_t index; 23 | struct listint_s *next; 24 | } listint_t; 25 | /** 26 | * struct skiplist_s - Singly linked list with an express lane 27 | * 28 | * @n: Integer 29 | * @index: Index of the node in the list 30 | * @next: Pointer to the next node 31 | * @express: Pointer to the next node in the express lane 32 | * 33 | * Description: singly linked list node structure with an express lane 34 | */ 35 | typedef struct skiplist_s 36 | { 37 | int n; 38 | size_t index; 39 | struct skiplist_s *next; 40 | struct skiplist_s *express; 41 | } skiplist_t; 42 | 43 | /* PROTOTYPES */ 44 | int linear_search(int *array, size_t size, int value); 45 | int binary_search(int *array, size_t size, int value); 46 | int jump_search(int *array, size_t size, int value); 47 | int interpolation_search(int *array, size_t size, int value); 48 | int exponential_search(int *array, size_t size, int value); 49 | int advanced_binary(int *array, size_t size, int value); 50 | listint_t *jump_list(listint_t *list, size_t size, int value); 51 | listint_t *create_list(int *array, size_t size); 52 | void free_list(listint_t *list); 53 | void print_list(const listint_t *list); 54 | skiplist_t *linear_skip(skiplist_t *list, int value); 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/create_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | void free_skiplist(skiplist_t *list); 6 | 7 | /** 8 | * init_express - Initializes the express lane of the linked list 9 | * 10 | * @list: Pointer to the head node of the list 11 | * @size: Number of nodes in the list 12 | */ 13 | void init_express(skiplist_t *list, size_t size) 14 | { 15 | const size_t step = sqrt(size); 16 | size_t i; 17 | skiplist_t *save; 18 | 19 | for (save = list, i = 0; i < size; ++i, list = list->next) 20 | { 21 | if (i % step == 0) 22 | { 23 | save->express = list; 24 | save = list; 25 | } 26 | } 27 | } 28 | 29 | /** 30 | * create_skiplist - Create a single linked list 31 | * 32 | * @array: Pointer to the array used to fill the list 33 | * @size: Size of the array 34 | * 35 | * Return: A pointer to the head of the created list (NULL on failure) 36 | */ 37 | skiplist_t *create_skiplist(int *array, size_t size) 38 | { 39 | skiplist_t *list; 40 | skiplist_t *node; 41 | size_t save_size; 42 | 43 | list = NULL; 44 | save_size = size; 45 | while (array && size--) 46 | { 47 | node = malloc(sizeof(*node)); 48 | if (!node) 49 | { 50 | free_skiplist(list); 51 | return (NULL); 52 | } 53 | node->n = array[size]; 54 | node->index = size; 55 | node->express = NULL; 56 | node->next = list; 57 | list = node; 58 | } 59 | init_express(list, save_size); 60 | return (list); 61 | } 62 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/free_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search_algos.h" 3 | 4 | /** 5 | * free_skiplist - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_skiplist(skiplist_t *list) 10 | { 11 | skiplist_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_skiplist(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/print_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * print_skiplist - dump the content of a skiplist_t 7 | * 8 | * @list: Pointer to the head of the list 9 | * 10 | * Return: void 11 | */ 12 | void print_skiplist(const skiplist_t *list) 13 | { 14 | const skiplist_t *node; 15 | 16 | printf("List :\n"); 17 | for (node = list; node; node = node->next) 18 | { 19 | printf("Index[%lu] = [%d]\n", node->index, node->n); 20 | } 21 | printf("\nExpress lane :\n"); 22 | for (node = list; node; node = node->express) 23 | { 24 | printf("Index[%lu] = [%d]\n", node->index, node->n); 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); 18 | printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 6, jump_search(array, size, 6)); 18 | printf("Found %d at index: %d\n\n", 1, jump_search(array, size, 1)); 19 | printf("Found %d at index: %d\n", 999, jump_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, interpolation_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 7, interpolation_search(array, size, 7)); 19 | printf("Found %d at index: %d\n", 999, interpolation_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 54, 61, 62, 76, 99 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 62, exponential_search(array, size, 62)); 18 | printf("Found %d at index: %d\n\n", 3, exponential_search(array, size, 3)); 19 | printf("Found %d at index: %d\n", 999, exponential_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/104-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 8, advanced_binary(array, size, 8)); 18 | printf("Found %d at index: %d\n\n", 5, advanced_binary(array, size, 5)); 19 | printf("Found %d at index: %d\n", 999, advanced_binary(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/105-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | listint_t *create_list(int *array, size_t size); 6 | void print_list(const listint_t *list); 7 | void free_list(listint_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | listint_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_list(array, size); 23 | print_list(list); 24 | 25 | res = jump_list(list, size, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = jump_list(list, size, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = jump_list(list, size, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_list(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/test/106-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | skiplist_t *create_skiplist(int *array, size_t size); 6 | void print_skiplist(const skiplist_t *list); 7 | void free_skiplist(skiplist_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | skiplist_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_skiplist(array, size); 23 | print_skiplist(list); 24 | 25 | res = linear_skip(list, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = linear_skip(list, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = linear_skip(list, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_skiplist(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | General 2 | Why C programming is awesome (don’t forget to tweet today, with the hashtag #cisfun :)) 3 | Who invented C 4 | Who are Dennis Ritchie, Brian Kernighan and Linus Torvalds 5 | What happens when you type gcc main.c 6 | What is an entry point 7 | What is main 8 | How to print text using printf, puts and putchar 9 | How to get the size of a specific type using the unary operator sizeof 10 | How to compile using gcc 11 | What is the default program name when compiling with gcc 12 | What is the official Holberton C coding style and how to check your code with betty-style 13 | How to find the right header to include in your source code when using a standard library function 14 | How does the main function influence the return value of the program 15 | Requirements C 16 | Allowed editors: vi, vim, emacs 17 | All your files will be compiled on Ubuntu 14.04 LTS using gcc 4.8.4 18 | Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl 19 | --------------------------------------------------------------------------------