├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 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_number.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-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 └── make_betty ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md ├── main.h └── make_betty ├── 0x04-more_functions_nested_loops ├── 0-isuper.c ├── 0-isupper.c ├── 0-main.c ├── 1-isdigit.c ├── 10-print_triangle.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 ├── REAME.md ├── main.h └── make_betty ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.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 └── make ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.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 └── make ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md ├── main.h ├── make └── makebetty ├── 0x08-recursion ├── 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 └── make ├── 0x09-static_libraries ├── README.md ├── create_static_lib.sh ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 1 ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── main.h └── make ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md ├── main.h └── make ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── main.h └── make ├── 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 └── make ├── 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 └── make ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md ├── make └── 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 └── make ├── 0x13-more_singly_linked_lists ├── 1 ├── 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 └── make ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md ├── main.h └── make ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md ├── main.h └── makebetty ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_win.sh ├── README.md ├── libdynamic.so ├── libmask.so └── main.h ├── 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 └── tests │ └── 5-main.py ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 103-exponential.c ├── 104-advanced_binary.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 1 (Sucess) 8 | */ 9 | int main(void) 10 | { 11 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 12 | return (1); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -S 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | printf("with proper grammar, but the outcome is a piece of art,\n"); 11 | return (0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /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 | printf("Size of a char: %d byte(s)\n", sizeof(char)); 12 | printf("Size of an int: %d byte(s)\n", sizeof(int)); 13 | printf("Size of a long int: %d byte(s)\n", sizeof(long int)); 14 | printf("Size of a long long int: %d byte(s)\n", sizeof(long long)); 15 | printf("Size of a float: %d byte(s)\n", sizeof(float)); 16 | return (0); 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | # HELLO WORLD PROJECTS 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - assigns a random number to int n everytime 7 | * it executes, and prints it 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | if (n > 0) 17 | printf("%d is positive\n", n); 18 | else if (n == 0) 19 | printf("%d is zero\n", n); 20 | else if (n < 0) 21 | printf("%d is negative\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - prints the last digit of the random 7 | * number stored in the variable n 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | printf("Last digit of %d is %d ", n, n % 10); 17 | if (n % 10 > 5) 18 | printf("and is greater than 5\n"); 19 | else if (n % 10 == 0) 20 | printf("and is 0\n"); 21 | else if (n % 10 < 6 && n % 10 != 0) 22 | printf("and is less than 6 and not 0\n"); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Succes) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int n; 12 | int i; 13 | 14 | for (n = 0 ; n < 9 ; n++) 15 | { 16 | i = n + 1; 17 | do { 18 | putchar('0' + n); 19 | putchar('0' + i); 20 | if (n < 8) 21 | { 22 | putchar(','); 23 | putchar(32); 24 | } 25 | i++; 26 | } while (i < 10); 27 | } 28 | putchar('\n'); 29 | return (0); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Succes) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int n; 12 | int i; 13 | int h; 14 | 15 | for (h = 0 ; h != 8 ; h++) 16 | { 17 | for (n = h + 1 ; n != 9 ; n++) 18 | { 19 | i = n + 1; 20 | do { 21 | putchar('0' + h); 22 | putchar('0' + n); 23 | putchar('0' + i); 24 | if (h != 7) 25 | { 26 | putchar(','); 27 | putchar(32); 28 | } 29 | i++; 30 | } while (i != 10); 31 | } 32 | } 33 | putchar('\n'); 34 | return (0); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /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 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 100; i++) 12 | { 13 | for (j = 0; j < 100; j++) 14 | { 15 | if (i < j) 16 | { 17 | putchar((i / 10) + 48); 18 | putchar((i % 10) + 48); 19 | putchar(' '); 20 | putchar((j / 10) + 48); 21 | putchar((j % 10) + 48); 22 | if (i != 98 || j != 99) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * 7 | * Return: Always 0 (Sucess) 8 | */ 9 | 10 | int main(void) 11 | { 12 | char c; 13 | 14 | for (c = 'a' ; c <= 'z' ; c++) 15 | { 16 | putchar(c); 17 | 18 | } 19 | 20 | for (c = 'A' ; c <= 'Z' ; c++) 21 | { 22 | putchar(c); 23 | } 24 | putchar('\n'); 25 | return (0); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * 7 | * Return: Always 0 (Sucess) 8 | */ 9 | 10 | int main(void) 11 | { 12 | char c; 13 | 14 | for (c = 'a' ; c <= 'z' ; c++) 15 | { 16 | if ((c == 'q' || c == 'e') != 1) 17 | { 18 | putchar(c); 19 | } 20 | 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_number.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hnryjsph/alx-low_level_programming/2fba713144c783d137b1571986a63c7045ec459e/0x01-variables_if_else_while/5-print_number.c -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all single digit numbers of base 5 | * Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = 0; n < 10; n++) 13 | { 14 | printf("%d", n); 15 | } 16 | 17 | printf("\n"); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Succes) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int n; 12 | 13 | for (n = 0 ; n < 10 ; n++) 14 | { 15 | putchar(n + '0'); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * 7 | * Return: Always 0 (Sucess) 8 | */ 9 | 10 | int main(void) 11 | { 12 | char c; 13 | 14 | for (c = 'z' ; c >= 'a' ; c--) 15 | { 16 | putchar(c); 17 | } 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Succes) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int n; 12 | 13 | for (n = 0 ; n < 16 ; n++) 14 | { 15 | if (n < 10) 16 | { 17 | putchar('0' + n); 18 | } 19 | else 20 | { 21 | putchar(87 + n); 22 | } 23 | } 24 | putchar('\n'); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Succes) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int n; 12 | 13 | for (n = 0 ; n < 10 ; n++) 14 | { 15 | putchar(n + '0'); 16 | if (n < 9) 17 | { 18 | putchar(','); 19 | putchar(32); 20 | } 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | # CONDITIONALS AND LOOPS 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - prints _putchar 4 | * Return: Always 0 (Success) 5 | */ 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 lowercas, follwed 5 | * Return: Void 6 | */ 7 | 8 | void print_alphabet(void) 9 | { 10 | char ch; 11 | 12 | ch = 'a'; 13 | 14 | while (ch <= 'z') 15 | { 16 | _putchar(ch); 17 | ch++; 18 | } 19 | _putchar('\n'); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - adds two integers and returns the result 5 | * @a: int to be added to b 6 | * @b: int to be added to a 7 | * Return: sum of a and b 8 | */ 9 | int add(int a, int b) 10 | { 11 | int c; 12 | 13 | c = a + b; 14 | 15 | return (c); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the n times table, starting with 0 5 | * @n: number of the times table 6 | */ 7 | void print_times_table(int n) 8 | { 9 | int i, j, k; 10 | 11 | if (n >= 0 && n <= 15) 12 | { 13 | for (i = 0; i <= n; i++) 14 | { 15 | for (j = 0; j <= n; j++) 16 | { 17 | k = j * i; 18 | if (j == 0) 19 | { 20 | _putchar(k + '0'); 21 | } else if (k < 10 && j != 0) 22 | { 23 | _putchar(','); 24 | _putchar(' '); 25 | _putchar(' '); 26 | _putchar(' '); 27 | _putchar(k + '0'); 28 | } else if (k >= 10 && k < 100) 29 | { 30 | _putchar(','); 31 | _putchar(' '); 32 | _putchar(' '); 33 | _putchar((k / 10) + '0'); 34 | _putchar((k % 10) + '0'); 35 | } else if (k >= 100) 36 | { 37 | _putchar(','); 38 | _putchar(' '); 39 | _putchar((k / 100) + '0'); 40 | _putchar(((k / 10) % 10) + '0'); 41 | _putchar((k % 10) + '0'); 42 | } 43 | } 44 | _putchar('\n'); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - computes and prints the sum of all the multiples 5 | * of 3 or 5 below 1024 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int sum3, sum5, sum; 11 | int i; 12 | 13 | sum3 = 0; 14 | sum5 = 0; 15 | sum = 0; 16 | 17 | for (i = 0; i < 1024; ++i) 18 | { 19 | if ((i % 3) == 0) 20 | { 21 | sum3 = sum3 + i; 22 | } else if ((i % 5) == 0) 23 | { 24 | sum5 = sum5 + i; 25 | } 26 | } 27 | sum = sum3 + sum5; 28 | printf("%lu\n", sum); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the first 50 Fibonacci numbers, starting with 1 and 2 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | long int i, j, k, next; 11 | 12 | j = 1; 13 | 14 | k = 2; 15 | 16 | for (i = 1; i <= 50; ++i) 17 | { 18 | if (j != 20365011074) 19 | { 20 | printf("%ld, ", j); 21 | } else 22 | { 23 | printf("%ld\n", j); 24 | } 25 | next = j + k; 26 | j = k; 27 | k = next; 28 | } 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the sum of the even-valued terms 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | unsigned long int j, k, next, sum; 12 | 13 | j = 1; 14 | k = 2; 15 | sum = 0; 16 | 17 | for (i = 1; i <= 33; ++i) 18 | { 19 | if (j < 4000000 && (j % 2) == 0) 20 | { 21 | sum = sum + j; 22 | } 23 | next = j + k; 24 | j = k; 25 | k = next; 26 | } 27 | 28 | printf("%lu\n", sum); 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the first 98 Fibonacci numbers, 5 | * starting with 1 and 2 6 | * followed by a new line 7 | * Return: ALways 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int i, j, k, j1, j2, k1, k2; 12 | 13 | j = 1; 14 | k = 2; 15 | 16 | printf("%lu", j); 17 | 18 | for (i = 1; i < 91; i++) 19 | { 20 | printf(", %lu", k); 21 | k = k + j; 22 | j = k - j; 23 | } 24 | 25 | j1 = j / 1000000000; 26 | j2 = j % 1000000000; 27 | k1 = k / 1000000000; 28 | k2 = k % 1000000000; 29 | 30 | for (i = 92; i < 99; ++i) 31 | { 32 | printf(", %lu", k1 + (k2 / 1000000000)); 33 | printf("%lu", k2 % 1000000000); 34 | k1 = k1 + j1; 35 | j1 = k1 - j1; 36 | k2 = k2 + j2; 37 | j2 = k2 - j2; 38 | } 39 | 40 | printf("\n"); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - prints all natural numbers from n to 98, 6 | * followed by a new line 7 | * @n: print from this number 8 | */ 9 | void print_to_98(int n) 10 | { 11 | int i, j; 12 | 13 | if (n <= 98) 14 | { 15 | for (i = n; i <= 98; i++) 16 | { 17 | if (i != 98) 18 | printf("%d, ", i); 19 | else if (i == 98) 20 | printf("%d\n", i); 21 | } 22 | } else if (n >= 98) 23 | { 24 | for (j = n; j >= 98; j--) 25 | { 26 | if (j != 98) 27 | printf("%d, ", j); 28 | else if (j == 98) 29 | printf("%d\n", j); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_alphabet_x10 - prints alphatbet 10 time 4 | * Return: void 5 | */ 6 | 7 | void print_alphabet_x10(void) 8 | { 9 | char ch; 10 | int i; 11 | 12 | i = 0; 13 | 14 | while (i < 10) 15 | { 16 | ch = 'a'; 17 | while (ch <= 'z') 18 | { 19 | _putchar(ch); 20 | ch++; 21 | } 22 | _putchar('\n'); 23 | i++; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _islower - check if the given character is lower case. 5 | * @c: character to test. 6 | * 7 | * Return: 1 (True) else 0 (False) 8 | */ 9 | int _islower(char c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | { 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _isalpha - check if the given character is lower case. 5 | * @c: character to test. 6 | * 7 | * Return: 1 (True) else 0 (False) 8 | */ 9 | int _isalpha(char c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | { 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /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 int to check 6 | * Return: 1 and prints + if n is greater than zero 7 | * 0 and prints 0 if n is zero 8 | * -1 and prints - if n is less than zero 9 | */ 10 | int print_sign(int n) 11 | { 12 | if (n > 0) 13 | { 14 | _putchar('+'); 15 | return (1); 16 | } else if (n == 0) 17 | { 18 | _putchar(48); 19 | return (0); 20 | } else if (n < 0) 21 | { 22 | _putchar('-'); 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /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 int to check 6 | * Return: the absolute value of int 7 | */ 8 | int _abs(int n) 9 | { 10 | if (n >= 0) 11 | { 12 | return (n); 13 | } 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 int to extract the last digit from 6 | * Return: value of the last digit 7 | */ 8 | int print_last_digit(int n) 9 | { 10 | int a; 11 | 12 | if (n < 0) 13 | n = -n; 14 | 15 | a = n % 10; 16 | 17 | if (a < 0) 18 | a = -a; 19 | 20 | _putchar(a + '0'); 21 | 22 | return (a); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day of Jack Bauer 5 | * starting from 00:00 to 23:59 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int i, j; 10 | 11 | i = 0; 12 | 13 | while (i < 24) 14 | { 15 | j = 0; 16 | while (j < 60) 17 | { 18 | _putchar((i / 10) + '0'); 19 | _putchar((i % 10) + '0'); 20 | _putchar(':'); 21 | _putchar((j / 10) + '0'); 22 | _putchar((j % 10) + '0'); 23 | _putchar('\n'); 24 | j++; 25 | } 26 | i++; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /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 i, j, k; 9 | 10 | for (i = 0; i < 10; i++) 11 | { 12 | for (j = 0; j < 10; j++) 13 | { 14 | k = j * i; 15 | if (j == 0) 16 | { 17 | _putchar(k + '0'); 18 | } 19 | 20 | if (k < 10 && j != 0) 21 | { 22 | _putchar(','); 23 | _putchar(' '); 24 | _putchar(' '); 25 | _putchar(k + '0'); 26 | } else if (k >= 10) 27 | { 28 | _putchar(','); 29 | _putchar(' '); 30 | _putchar((k / 10) + '0'); 31 | _putchar((k % 10) + '0'); 32 | } 33 | } 34 | _putchar('\n'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | #Functions and nested loops 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | /** 2 | * * print_alphabt - print the lower-case alphabet on one line. 3 | * */ 4 | int _putchar(char c); 5 | void print_alphabet(void); 6 | void print_alphabet_x10(void); 7 | int _islower(char c); 8 | int _isalpha(char c); 9 | int print_sign(int n); 10 | int _abs(int n); 11 | int print_last_digit(int n); 12 | void jack_bauer(void); 13 | void times_table(void); 14 | int add(int a, int b); 15 | void print_to_98(int n); 16 | void print_times_table(int n); 17 | 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/make_betty: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for file in *.c 4 | do 5 | vim $file +'e ++ff=dos | set ff=unix | wq!' 6 | done 7 | -------------------------------------------------------------------------------- /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 >= a && b >= c) 20 | { 21 | largest = b; 22 | } 23 | else if (c >= a && c >= b) 24 | { 25 | largest = c; 26 | } 27 | 28 | 29 | return (largest); 30 | } 31 | -------------------------------------------------------------------------------- /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 % 100 != 0) || (year % 400 == 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/README.md: -------------------------------------------------------------------------------- 1 | # DEBUGGING IN C PROGRAMMING 2 | - The tasks in this folder are for practice of debugging 3 | -------------------------------------------------------------------------------- /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 | void print_remaining_days(int month, int day, int year); 9 | int convert_day(int month, int day); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x03-debugging/make_betty: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for file in *.c 4 | do 5 | vim $file +'e ++ff=dos | set ff=unix | wq!' 6 | done 7 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isuper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_isupper - function that verifies if a character is uppercase or not 5 | *@c: tested character 6 | *Return: returns 1 if it is uppercase , 0 if not 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if ((c >= 'A') && (c <= 'Z')) 12 | { 13 | return (1); 14 | }else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_isupper - function that verifies if a character is uppercase or not 5 | *@c: tested character 6 | *Return: returns 1 if it is uppercase , 0 if not 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if ((c >= 'A') && (c <= 'Z')) 12 | { 13 | return (1); 14 | }else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hnryjsph/alx-low_level_programming/2fba713144c783d137b1571986a63c7045ec459e/0x04-more_functions_nested_loops/0-main.c -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= '0' && c <= '9') 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle, followed by a new line 5 | * @size: size of the triangle 6 | */ 7 | void print_triangle(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 1; i <= size; i++) 17 | { 18 | for (j = i; j < size; j++) 19 | { 20 | _putchar(' '); 21 | } 22 | 23 | for (j = 1; j <= i; j++) 24 | { 25 | _putchar('#'); 26 | } 27 | 28 | _putchar('\n'); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two integers 5 | * @a: int to be multiplied to b 6 | * @b: int to be multiplied to a 7 | * Return: the result of the operation 8 | */ 9 | int mul(int a, int b) 10 | { 11 | int c; 12 | 13 | c = a * b; 14 | 15 | return (c); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints the numbers, from 0 to 9, 5 | * Return: void 6 | */ 7 | void print_numbers(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | _putchar(i + '0'); 14 | } 15 | 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints the numbers, from 0 to 9, 5 | * except 2 and 4, followed by a new line 6 | */ 7 | void print_most_numbers(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | if (i != 2 && i != 4) 14 | { 15 | _putchar(i + '0'); 16 | } 17 | } 18 | 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 10 times the numbers, from 0 to 14 5 | * followed by a new line 6 | */ 7 | void more_numbers(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | for (j = 0; j < 15; j++) 14 | { 15 | if (j >= 10) 16 | _putchar(j / 10 + '0'); 17 | _putchar(j % 10 + '0'); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - draws a straight line in the terminal 5 | * @n: number of times the character _ should be printed 6 | */ 7 | void print_line(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i; 15 | 16 | for (i = 1; i <= n; i++) 17 | { 18 | _putchar('_'); 19 | } 20 | _putchar('\n'); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - draws a diagonal line on the terminal 5 | * @n: number of times the character \ should be printed 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | for (j = 0; j < n; j++) 19 | { 20 | if (j == i) 21 | _putchar('\\'); 22 | else if (j < i) 23 | _putchar(' '); 24 | } 25 | _putchar('\n'); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints a square, followed by a new line; 5 | * @size: size of the square 6 | */ 7 | void print_square(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 0; i < size; i++) 17 | { 18 | for (j = 0; j < size; j++) 19 | { 20 | _putchar('#'); 21 | } 22 | _putchar('\n'); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the numbers from 1 to 100, followed by a new line 6 | * but for multiples of three prints Fizz instead of the number 7 | * and for the multiples of five prints Buzz 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 1; i <= 100; i++) 15 | { 16 | if (i % 3 == 0 && i % 5 != 0) 17 | { 18 | printf(" Fizz"); 19 | } else if (i % 5 == 0 && i % 3 != 0) 20 | { 21 | printf(" Buzz"); 22 | } else if (i % 3 == 0 && i % 5 == 0) 23 | { 24 | printf(" FizzBuzz"); 25 | } else if (i == 1) 26 | { 27 | printf("%d", i); 28 | } else 29 | { 30 | printf(" %d", i); 31 | } 32 | } 33 | printf("\n"); 34 | 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/REAME.md: -------------------------------------------------------------------------------- 1 | #0x04-more_functions_nested_loops 2 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | int _isupper(int c); 6 | int _isdigit(int c); 7 | int mul(int a, int b); 8 | void print_numbers(void); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/make_betty: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for file in *.c 4 | do 5 | vim $file +'e ++ff=dos | set ff=unix | wq!' 6 | done 7 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value of the pointer to 9 5 | * @n: pointer to 98 6 | * Return: void 7 | */ 8 | 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swaps two integers 5 | * @a: first variable to be swaped 6 | * @b: second variable to be swaped 7 | * Return: Void 8 | */ 9 | 10 | void swap_int(int *a, int *b) 11 | { 12 | int c; 13 | 14 | c = *a; 15 | *a = *b; 16 | *b = c; 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string to evaluate 6 | * 7 | * Return: the length of the string 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | 15 | while (s[i] != '\0') 16 | { 17 | i++; 18 | } 19 | 20 | return (i); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string followed buy a new line 5 | * @str: contains the string 6 | * 7 | * Return: Void 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i; 13 | 14 | for (i = 0; str[i] != '\0'; i++) 15 | { 16 | _putchar(str[i]); 17 | } 18 | 19 | _putchar('\n'); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string in reverse, followed by a new line 5 | * @s: string to be printed 6 | */ 7 | void print_rev(char *s) 8 | { 9 | int i, j, len; 10 | 11 | i = 0; 12 | 13 | while (s[i] != '\0') 14 | { 15 | i++; 16 | } 17 | 18 | len = i; 19 | 20 | for (j = len - 1; j >= 0; j--) 21 | { 22 | _putchar(s[j]); 23 | } 24 | 25 | _putchar('\n'); 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverses a string 5 | * @s: string to be reversed 6 | */ 7 | void rev_string(char *s) 8 | { 9 | char tmp; 10 | int i, len, len1; 11 | 12 | len = 0; 13 | len1 = 0; 14 | 15 | while (s[len] != '\0') 16 | { 17 | len++; 18 | } 19 | 20 | len1 = len - 1; 21 | 22 | for (i = 0; i < len / 2; i++) 23 | { 24 | tmp = s[i]; 25 | s[i] = s[len1]; 26 | s[len1--] = tmp; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints one char out of 2 of a string 5 | * followed by a new line 6 | * @str: string to print the chars from 7 | */ 8 | void puts2(char *str) 9 | { 10 | int len, i; 11 | 12 | len = 0; 13 | 14 | while (str[len] != '\0') 15 | { 16 | len++; 17 | } 18 | 19 | for (i = 0; i < len; i += 2) 20 | { 21 | _putchar(str[i]); 22 | } 23 | 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints half of a string 5 | * followed by a new line 6 | * @str: string to be printed 7 | */ 8 | void puts_half(char *str) 9 | { 10 | int len, n, i; 11 | 12 | len = 0; 13 | 14 | while (str[len] != '\0') 15 | { 16 | len++; 17 | } 18 | 19 | if (len % 2 == 0) 20 | { 21 | for (i = len / 2; str[i] != '\0'; i++) 22 | { 23 | _putchar(str[i]); 24 | } 25 | } else if (len % 2) 26 | { 27 | for (n = (len - 1) / 2; n < len - 1; n++) 28 | { 29 | _putchar(str[n + 1]); 30 | } 31 | } 32 | _putchar('\n'); 33 | } 34 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_array - prints n elements of an array of integers 6 | * followed by a new line 7 | * @a: array to be printed 8 | * @n: number of elements to print 9 | */ 10 | void print_array(int *a, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | if (i == 0) 17 | printf("%d", a[i]); 18 | else 19 | printf(", %d", a[i]); 20 | } 21 | printf("\n"); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * 10 | * Return: the pointer to dest 11 | */ 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int len, i; 15 | 16 | len = 0; 17 | 18 | while (src[len] != '\0') 19 | { 20 | len++; 21 | } 22 | 23 | for (i = 0; i < len; i++) 24 | { 25 | dest[i] = src[i]; 26 | } 27 | dest[i] = '\0'; 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # 0x05-pointers_arrays_strings 2 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | void reset_to_98(int *n); 6 | void swap_int(int *a, int *b); 7 | int _strlen(char *s); 8 | void _puts(char *str); 9 | void print_rev(char *s); 10 | void rev_string(char *s); 11 | void puts2(char *str); 12 | void puts_half(char *str); 13 | void print_array(int *a, int n); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | 17 | 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for file in *.c 4 | do 5 | vim $file +'e ++ff=dos | set ff=unix | wq!' 6 | done 7 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcat - concatenates two strings together, 4 | * @dest: destination. 5 | * @src: source. 6 | * Return: the pointer to dest. 7 | */ 8 | char *_strcat(char *dest, char *src) 9 | { 10 | int count = 0, count2 = 0; 11 | 12 | while (*(dest + count) != '\0') 13 | { 14 | count++; 15 | } 16 | 17 | while (count2 >= 0) 18 | { 19 | *(dest + count) = *(src + count2); 20 | if (*(src + count2) == '\0') 21 | break; 22 | count++; 23 | count2++; 24 | } 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two string, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes used from src. 7 | * Return: the pointer to dust. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination, 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compares two strings 5 | * @s1: first string to compare 6 | * @s2: second string to compare 7 | * 8 | * Return: less than 0 if s1 is less than s2, 0 if they are 9 | * equal, more than 0 if s1 is greater than s2 10 | */ 11 | int _strcmp(char *s1, char *s2) 12 | { 13 | while (*s1 == *s2) 14 | { 15 | if (*s1 == '\0') 16 | { 17 | return (0); 18 | } 19 | s1++; 20 | s2++; 21 | } 22 | return (*s1 - *s2); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - reverses an array of integers 5 | * @a: array to be reversed 6 | * @n: number of elements in the array 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i, j, tmp; 11 | 12 | j = n - 1; 13 | 14 | for (i = 0; i < n / 2; i++) 15 | { 16 | tmp = a[i]; 17 | a[i] = a[j]; 18 | a[j--] = tmp; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * string_toupper - changes all lowercase letters of string 4 | * to uppercase 5 | * @s: string to modify 6 | * 7 | * Return: the resulting string 8 | */ 9 | char *string_toupper(char *s) 10 | { 11 | int i; 12 | 13 | for (i = 0; s[i] != '\0'; i++) 14 | { 15 | if (s[i] >= 'a' && s[i] <= 'z') 16 | s[i] = s[i] - 32; 17 | } 18 | 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes every word of a string 5 | * @s: string to modify 6 | * 7 | * Return: the resulting string 8 | */ 9 | char *cap_string(char *s) 10 | { 11 | int i, j; 12 | 13 | char spe[13] = {' ', '\t', '\n', ',', ';', '.', 14 | '!', '?', '"', '(', ')', '{', '}'}; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | if (i == 0 && s[i] >= 'a' && s[i] <= 'z') 19 | s[i] -= 32; 20 | for (j = 0; j < 13; j++) 21 | { 22 | if (s[i] == spe[j]) 23 | { 24 | if (s[i + 1] >= 'a' && s[i + 1] <= 'z') 25 | { 26 | s[i + 1] -= 32; 27 | } 28 | } 29 | } 30 | } 31 | return (s); 32 | } 33 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encodes a string into 1337 4 | * @s: input string. like this 5 | * Return: the pointer to dest. 6 | */ 7 | char *leet(char *s) 8 | { 9 | int count = 0, i; 10 | int low_letters[] = {97, 101, 111, 116, 108}; 11 | int upp_letters[] = {65, 69, 79, 84, 76}; 12 | int numbers[] = {52, 51, 48, 55, 49}; 13 | 14 | while (*(s + count) != '\0') 15 | { 16 | for (i = 0; i < 5; i++) 17 | { 18 | if (*(s + count) == low_letters[i] || *(s + count) == upp_letters[i]) 19 | { 20 | *(s + count) = numbers[i]; 21 | break; 22 | } 23 | } 24 | count++; 25 | } 26 | 27 | return (s); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | #0x06-pointers_arrays_strings 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H__ 3 | 4 | char *_strcat(char *dest, char *src); 5 | char *_strncat(char *dest, char *src, int n); 6 | char *_strncpy(char *dest, char *src, int n); 7 | int _strcmp(char *s1, char *s2); 8 | void reverse_array(int *a, int n); 9 | char *string_toupper(char *); 10 | char *cap_string(char *); 11 | char *leet(char *); 12 | char *rot13(char *); 13 | void print_number(int n); 14 | int _putchar(char); 15 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 16 | void print_buffer(char *b, int size); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for file in *.c 4 | do 5 | vim $file +'e ++ff=dos | set ff=unix | wq!' 6 | done 7 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memset - fills memory with a constant byte 5 | * @s: memory area to be filled 6 | * @b: char to copy 7 | * @n: number of times to copy b 8 | * 9 | * Return: pointer to the memory area s 10 | */ 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | s[i] = b; 18 | } 19 | 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memcpy - copies memory area 5 | * @dest: destination memory area 6 | * @src: memory area to copy from 7 | * @n: number of bytes to copy 8 | * 9 | * Return: pointer to dest 10 | */ 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_strchr - locates a character in a string 6 | * @s: string to search 7 | * @c: char to find 8 | * 9 | * Return: a pointer to the first occurrence of the character 10 | * c in the string s, or NULL if the character is not found 11 | */ 12 | char *_strchr(char *s, char c) 13 | { 14 | int a; 15 | 16 | while (1) 17 | { 18 | a = *s++; 19 | if (a == c) 20 | { 21 | return (s - 1); 22 | } 23 | if (a == 0) 24 | { 25 | return (NULL); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strspn - gets the length of a prefix substring 5 | * @s: string to evaluate 6 | * @accept: string containing the list of characters to match in s 7 | * 8 | * Return: the number of bytes in the initial segment 9 | * of s which consist only of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | int i, j, f, flag; 14 | 15 | f = 0; 16 | 17 | for (i = 0; s[i] != '\0'; i++) 18 | { 19 | flag = 0; 20 | for (j = 0; accept[j] != '\0'; j++) 21 | { 22 | if (s[i] == accept[j]) 23 | { 24 | f++; 25 | flag = 1; 26 | } 27 | } 28 | if (flag == 0) 29 | { 30 | return (f); 31 | } 32 | } 33 | 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_strpbrk - searches a string for any of a set of bytes 6 | * @s: string to search 7 | * @accept: stringcontaining the bytes to look for 8 | * 9 | * Return: pointer to the byte in s that matches one of the bytes in accept 10 | * or NULL if no such byte is found 11 | */ 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | int i, j; 15 | 16 | for (i = 0; *s != '\0'; i++) 17 | { 18 | for (j = 0; accept[j] != '\0'; j++) 19 | { 20 | if (*s == accept[j]) 21 | { 22 | return (s); 23 | } 24 | } 25 | s++; 26 | } 27 | 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_strstr - locates a substring 6 | * @haystack: string to search in 7 | * @needle: substring to look for 8 | * 9 | * Return: pointer to the beginning of the located substring 10 | * or NULL if the substring is not found 11 | */ 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | int i, j; 15 | 16 | for (i = 0; haystack[i] != '\0'; i++) 17 | { 18 | for (j = 0; needle[j] != '\0'; j++) 19 | { 20 | if (haystack[i + j] != needle[j]) 21 | break; 22 | } 23 | if (!needle[j]) 24 | return (&haystack[i]); 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chessboard 5 | * @a: two dimension array to print 6 | */ 7 | void print_chessboard(char (*a)[8]) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 8; i++) 12 | { 13 | for (j = 0; j < 8; j++) 14 | { 15 | _putchar(a[i][j]); 16 | } 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_diagsums - prints the sum of the two diagonals 6 | * of a square matrix of integers 7 | * @a: square matrix of which we print the sum of diagonals 8 | * @size: size of the matrix 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i; 13 | 14 | unsigned int sum, sum1; 15 | 16 | sum = 0; 17 | sum1 = 0; 18 | 19 | for (i = 0; i < size; i++) 20 | { 21 | sum += a[(size * i) + i]; 22 | sum1 += a[(size * (i + 1)) - (i + 1)]; 23 | } 24 | 25 | printf("%d, %d\n", sum, sum1); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | #0x07-pointers_arrays_strings 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | char *_memset(char *s, char b, unsigned int n); 6 | char *_memcpy(char *dest, char *src, unsigned int n); 7 | char *_strchr(char *s, char c); 8 | unsigned int _strspn(char *s, char *accept); 9 | char *_strpbrk(char *s, char *accept); 10 | char *_strstr(char *haystack, char *needle); 11 | void print_chessboard(char (*a)[8]); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *to); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/makebetty: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for file in *.c 4 | do 5 | vim $file +'e ++ff=dos | set ff=unix | wq!' 6 | done 7 | 8 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - prints a string, followed by a new line 5 | * @s: string to print 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | { 11 | _putchar('\n'); 12 | return; 13 | } 14 | _putchar(*s); 15 | _puts_recursion(s + 1); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints a string in reverse 5 | * @s: string to print 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s) 10 | { 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int check_pal(char *s, int i, int len); 4 | int _strlen_recursion(char *s); 5 | 6 | /** 7 | * is_palindrome - checks if a string is a palindrome 8 | * @s: string to reverse 9 | * 10 | * Return: 1 if it is, 0 it's not 11 | */ 12 | int is_palindrome(char *s) 13 | { 14 | if (*s == 0) 15 | return (1); 16 | return (check_pal(s, 0, _strlen_recursion(s))); 17 | } 18 | 19 | /** 20 | * _strlen_recursion - returns the length of a string 21 | * @s: string to calculate the length of 22 | * 23 | * Return: length of the string 24 | */ 25 | int _strlen_recursion(char *s) 26 | { 27 | if (*s == '\0') 28 | return (0); 29 | return (1 + _strlen_recursion(s + 1)); 30 | } 31 | 32 | /** 33 | * check_pal - checks the characters recursively for palindrome 34 | * @s: string to check 35 | * @i: iterator 36 | * @len: length of the string 37 | * 38 | * Return: 1 if palindrome, 0 if not 39 | */ 40 | int check_pal(char *s, int i, int len) 41 | { 42 | if (*(s + i) != *(s + len - 1)) 43 | return (0); 44 | if (i >= len) 45 | return (1); 46 | return (check_pal(s, i + 1, len - 1)); 47 | } 48 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int bandersnatch(char *s1, char *s2); 4 | char *move(char *s2); 5 | 6 | /** 7 | * wildcmp - compares two strings recursively, 8 | * checking for wildcards expansion 9 | * @s1: first string to compare 10 | * @s2: second string to compare 11 | * 12 | * Return: 1 if the strings can be considered identical 13 | * otherwise 0 14 | */ 15 | int wildcmp(char *s1, char *s2) 16 | { 17 | /** 18 | * this is going to be a sum of return values 19 | */ 20 | int sum = 0; 21 | 22 | /** 23 | * if we reach the end of s1 and the char in s2 is a * 24 | * and if the next chars of s2 are *, return 1 25 | */ 26 | if (*s1 == '\0' && *s2 == '*' && !*move(s2)) 27 | return (1); 28 | 29 | /** 30 | * if the chars are equal in both strings, 31 | * if we reached the end of s1, return 1 32 | * else increment s1 and s2 by 1 33 | */ 34 | if (*s1 == *s2) 35 | { 36 | if (*s1 == '\0') 37 | return (1); 38 | return (wildcmp(s1 + 1, s2 + 1)); 39 | } 40 | /** 41 | * if we reached the end of both strings, 42 | * return 0 43 | */ 44 | if (*s1 == '\0' || *s2 == '\0') 45 | return (0); 46 | 47 | /** 48 | * if the char in s2 is a * 49 | * finds the address of the first char after the * 50 | * if we reached the end of s2, return 1 51 | * if the chars are equal, add the return values 52 | * of wildcmp() to sum 53 | * add the return value of bandersnatch() to sum 54 | * convert non-zero to 1, keeps 0 at 0, return 55 | */ 56 | if (*s2 == '*') 57 | { 58 | s2 = move(s2); 59 | if (*s2 == '\0') 60 | return (1); 61 | if (*s1 == *s2) 62 | sum += wildcmp(s1 + 1, s2 + 1); 63 | sum += bandersnatch(s1 + 1, s2); 64 | return (!!sum); 65 | } 66 | return (0); 67 | } 68 | 69 | /** 70 | * bandersnatch - checks recursively for all the paths when the 71 | * characters are equal 72 | * @s1: first string 73 | * @s2: second string 74 | * 75 | * Return: return value of wildcmp() or of itself 76 | */ 77 | int bandersnatch(char *s1, char *s2) 78 | { 79 | /** 80 | * if we reached the end of s1, return 0 81 | * if chars are equal, return the return value of wildcmp() 82 | * increment s1 by 1, not s2 83 | */ 84 | if (*s1 == '\0') 85 | return (0); 86 | if (*s1 == *s2) 87 | return (wildcmp(s1, s2)); 88 | return (bandersnatch(s1 + 1, s2)); 89 | } 90 | 91 | /** 92 | * *move - moves the current char past the * 93 | * @s2: string to iterate over 94 | * 95 | * Return: the address of the character after the * 96 | */ 97 | char *move(char *s2) 98 | { 99 | /** 100 | * if the current char is a * 101 | * increment s2 by 1 102 | * else return the address of 103 | * the first char past all * 104 | */ 105 | if (*s2 == '*') 106 | return (move(s2 + 1)); 107 | else 108 | return (s2); 109 | } 110 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string 5 | * @s: string to calculate the length of 6 | * 7 | * Return: length of the string 8 | */ 9 | int _strlen_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | return (0); 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a number 5 | * @n: number to return the factorial from 6 | * 7 | * Return: factorial of n 8 | */ 9 | int factorial(int n) 10 | { 11 | if (n < 0) 12 | return (-1); 13 | if (n == 0) 14 | return (1); 15 | return (n * factorial(n - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - returns the value of x raised to the power of y 5 | * @x: value to raise 6 | * @y: power 7 | * 8 | * Return: result of the power 9 | */ 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | return (-1); 14 | if (y == 0) 15 | return (1); 16 | return (x * _pow_recursion(x, y - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int actual_sqrt_recursion(int n, int i); 4 | 5 | /** 6 | * _sqrt_recursion - returns the natural square root of a number 7 | * @n: number to calculate the square root of 8 | * 9 | * Return: the resulting square root 10 | */ 11 | int _sqrt_recursion(int n) 12 | { 13 | if (n < 0) 14 | return (-1); 15 | return (actual_sqrt_recursion(n, 0)); 16 | } 17 | 18 | /** 19 | * actual_sqrt_recursion - recurses to find the natural 20 | * square root of a number 21 | * @n: number to calculate the sqaure root of 22 | * @i: iterator 23 | * 24 | * Return: the resulting square root 25 | */ 26 | int actual_sqrt_recursion(int n, int i) 27 | { 28 | if (i * i > n) 29 | return (-1); 30 | if (i * i == n) 31 | return (i); 32 | return (actual_sqrt_recursion(n, i + 1)); 33 | } 34 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int actual_prime(int n, int i); 4 | 5 | /** 6 | * is_prime_number - says if an integer is a prime number or not 7 | * @n: number to evaluate 8 | * 9 | * Return: 1 if n is a prime number, 0 if not 10 | */ 11 | int is_prime_number(int n) 12 | { 13 | if (n <= 1) 14 | return (0); 15 | return (actual_prime(n, n - 1)); 16 | } 17 | 18 | /** 19 | * actual_prime - calculates if a number is prime recursively 20 | * @n: number to evaluate 21 | * @i: iterator 22 | * 23 | * Return: 1 if n is prime, 0 if not 24 | */ 25 | int actual_prime(int n, int i) 26 | { 27 | if (i == 1) 28 | return (1); 29 | if (n % i == 0 && i > 0) 30 | return (0); 31 | return (actual_prime(n, i - 1)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | # 0x08-recursion 2 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | void _puts_recursion(char *s); 6 | void _print_rev_recursion(char *s); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int x, int y); 10 | int _sqrt_recursion(int n); 11 | int is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x09-static_libraries 2 | -------------------------------------------------------------------------------- /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/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hnryjsph/alx-low_level_programming/2fba713144c783d137b1571986a63c7045ec459e/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 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 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the name of the program 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * 9 | * Return: 0 10 | */ 11 | 12 | int main(int argc __attribute__((unused)), char *argv[]) 13 | { 14 | printf("%s\n", argv[0]); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * atoi - prints the multiplle of two numbers 6 | * @s: takes in the string to be converted 7 | * 8 | * Return: n converted string 9 | */ 10 | 11 | int _atoi(char *s) 12 | { 13 | int i, d, n, len, f, digit; 14 | 15 | i = 0; 16 | d = 0; 17 | n = 0; 18 | len = 0; 19 | f = 0; 20 | digit = 0; 21 | 22 | while (s[len] != '\0') 23 | { 24 | len ++; 25 | } 26 | 27 | while (i < len && f == 0) 28 | { 29 | if (s[i] == '-') 30 | { 31 | ++d; 32 | } 33 | 34 | if (s[i] >= '0' && s[i] <= '9') 35 | { 36 | digit = s[i] - '0'; 37 | if (d % 2) 38 | { 39 | digit = -digit; 40 | } 41 | 42 | n = n * 10 + digit; 43 | f = 1; 44 | 45 | if (s[i + 1] < '0' || s[i + 1] > '9') 46 | { 47 | break; 48 | } 49 | f = 0; 50 | } 51 | i++; 52 | } 53 | if (f == 0) 54 | { 55 | return (0); 56 | } 57 | 58 | return (n); 59 | } 60 | 61 | /** 62 | *main - prints the multiplle of two numbers 63 | *@argc: number of arguments passed 64 | *@argv: array of the arguments passed 65 | * 66 | * Return: 0 67 | */ 68 | 69 | int main(int argc, char *argv[]) 70 | { 71 | if (argc > 3 || argc < 3) 72 | { 73 | int a; 74 | int b; 75 | int c; 76 | 77 | a = _atoi(argv[1]); 78 | b = _atoi(argv[2]); 79 | c = a * b; 80 | 81 | printf("%d\n", c); 82 | 83 | return (0); 84 | } 85 | else 86 | { 87 | printf("Error\n"); 88 | 89 | return (1); 90 | } 91 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the number of arguments passed into the file 6 | * @argc: number of arguments passed 7 | * @argv: array of the arguments passed 8 | * 9 | * Return: 0 10 | */ 11 | 12 | int main(int argc, char *argv[] __attribute__((unused))) 13 | { 14 | printf("%d\n", argc - 1); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - prints the minimum number of coins to 7 | * make change for an amount of money 8 | * @argc: number of arguments 9 | * @argv: array of arguments 10 | * 11 | * Return: 0 (Success), 1 (Error) 12 | */ 13 | int main(int argc, char *argv[]) 14 | { 15 | int num, j, result; 16 | int coins[] = {25, 10, 5, 2, 1}; 17 | 18 | if (argc != 2) 19 | { 20 | printf("Error\n"); 21 | return (1); 22 | } 23 | 24 | num = atoi(argv[1]); 25 | result = 0; 26 | 27 | if (num < 0) 28 | { 29 | printf("0\n"); 30 | return (0); 31 | } 32 | 33 | for (j = 0; j < 5 && num >= 0; j++) 34 | { 35 | while (num >= coins[j]) 36 | { 37 | result++; 38 | num -= coins[j]; 39 | } 40 | } 41 | 42 | printf("%d\n", result); 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - Prints all the arguments passed to it 6 | * @argc: the number of arguments passed to it 7 | * @argv: the array of the arguments passed 8 | * 9 | * Return: 0 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int i; 15 | 16 | for (i = 0; i < argc; i++) 17 | { 18 | printf("%s\n", argv[i]); 19 | } 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _atoi - converts a string to an integer 6 | * @s: string to be converted 7 | * 8 | * Return: the int converted from the string 9 | */ 10 | int _atoi(char *s) 11 | { 12 | int i, d, n, len, f, digit; 13 | 14 | i = 0; 15 | d = 0; 16 | n = 0; 17 | len = 0; 18 | f = 0; 19 | digit = 0; 20 | 21 | while (s[len] != '\0') 22 | len++; 23 | 24 | while (i < len && f == 0) 25 | { 26 | if (s[i] == '-') 27 | ++d; 28 | 29 | if (s[i] >= '0' && s[i] <= '9') 30 | { 31 | digit = s[i] - '0'; 32 | if (d % 2) 33 | digit = -digit; 34 | n = n * 10 + digit; 35 | f = 1; 36 | if (s[i + 1] < '0' || s[i + 1] > '9') 37 | break; 38 | f = 0; 39 | } 40 | i++; 41 | } 42 | 43 | if (f == 0) 44 | return (0); 45 | 46 | return (n); 47 | } 48 | 49 | /** 50 | * main - multiplies two numbers 51 | * @argc: number of arguments 52 | * @argv: array of arguments 53 | * 54 | * Return: 0 (Success), 1 (Error) 55 | */ 56 | int main(int argc, char *argv[]) 57 | { 58 | int result, num1, num2; 59 | 60 | if (argc < 3 || argc > 3) 61 | { 62 | printf("Error\n"); 63 | return (1); 64 | } 65 | 66 | num1 = _atoi(argv[1]); 67 | num2 = _atoi(argv[2]); 68 | result = num1 * num2; 69 | 70 | printf("%d\n", result); 71 | 72 | return (0); 73 | } 74 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _atoi - converts a string to an integer 6 | * @s: string to be converted 7 | * 8 | * Return: the int converted from the string 9 | */ 10 | int _atoi(char *s) 11 | { 12 | int i, d, n, len, f, digit; 13 | 14 | i = 0; 15 | d = 0; 16 | n = 0; 17 | len = 0; 18 | f = 0; 19 | digit = 0; 20 | 21 | while (s[len] != '\0') 22 | len++; 23 | 24 | while (i < len && f == 0) 25 | { 26 | if (s[i] == '-') 27 | ++d; 28 | 29 | if (s[i] >= '0' && s[i] <= '9') 30 | { 31 | digit = s[i] - '0'; 32 | if (d % 2) 33 | digit = -digit; 34 | n = n * 10 + digit; 35 | f = 1; 36 | if (s[i + 1] < '0' || s[i + 1] > '9') 37 | break; 38 | f = 0; 39 | } 40 | i++; 41 | } 42 | 43 | if (f == 0) 44 | return (0); 45 | 46 | return (n); 47 | } 48 | 49 | /** 50 | * main - adds two positive number 51 | * @argc: number of arguments 52 | * @argv: array of arguents 53 | * 54 | * Return: 0 (Success), or 1 (Success) 55 | */ 56 | int main(int argc, char *argv[]) 57 | { 58 | int sum, num, i, j, k; 59 | 60 | sum = 0; 61 | 62 | for (i = 1; i < argc; i++) 63 | { 64 | for (j = 0; argv[i][j] != '\0'; j++) 65 | { 66 | if (argv[i][j] > '9' || argv[i][j] < '0') 67 | { 68 | puts("Error"); 69 | return (1); 70 | } 71 | } 72 | } 73 | 74 | for (k = 1; k < argc; k++) 75 | { 76 | num = _atoi(argv[k]); 77 | if (num >= 0) 78 | { 79 | sum += num; 80 | } 81 | } 82 | 83 | printf("%d\n", sum); 84 | return (0); 85 | } 86 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # 0x0A-argc_argv 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | int _atoi(char *s); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /0x0A-argc_argv/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *create_array - creates an array of chars, 6 | * and initializes it with a specific char 7 | * @size: size of the array to create 8 | * @c: char to initialize the array c 9 | * 10 | * Return: pointer to the array (Success), NULL (Error) 11 | */ 12 | char *create_array(unsigned int size, char c) 13 | { 14 | char *p; 15 | unsigned int i = 0; 16 | 17 | if (size == 0) 18 | return (NULL); 19 | 20 | p = (char *) malloc(sizeof(char) * size); 21 | 22 | if (p == NULL) 23 | return (0); 24 | 25 | while (i < size) 26 | { 27 | *(p + i) = c; 28 | i++; 29 | } 30 | 31 | *(p + i) = '\0'; 32 | 33 | return (p); 34 | } 35 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *_strdup - copies the string given as parameter 6 | * @str: string to duplicate 7 | * 8 | * Return: pointer to the copied string (Success), NULL (Error) 9 | */ 10 | char *_strdup(char *str) 11 | { 12 | char *dup; 13 | unsigned int i, len; 14 | 15 | i = 0; 16 | len = 0; 17 | 18 | if (str == NULL) 19 | return (NULL); 20 | 21 | while (str[len]) 22 | len++; 23 | 24 | dup = malloc(sizeof(char) * (len + 1)); 25 | 26 | if (dup == NULL) 27 | return (NULL); 28 | 29 | while ((dup[i] = str[i]) != '\0') 30 | i++; 31 | 32 | return (dup); 33 | } 34 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - this function concatenates all the arguments of your program. 6 | * @ac: args count. 7 | * @av: arguments 8 | * Return: pointer to string. 9 | */ 10 | char *argstostr(int ac, char **av) 11 | { 12 | char *str, *temp; 13 | int i, z, length; 14 | 15 | if (ac == 0 || av == NULL) 16 | return (NULL); 17 | length = 0; 18 | for (i = 0; i < ac; i++) 19 | { 20 | temp = *(av + i); 21 | for (z = 0; temp[z]; z++) 22 | { 23 | length++; 24 | } 25 | length += 1; 26 | } 27 | str = malloc(sizeof(char) * length + 1); 28 | length = 0; 29 | for (i = 0; i < ac; i++) 30 | { 31 | temp = *(av + i); 32 | for (z = 0; temp[z]; ++z) 33 | { 34 | str[length] = temp[z]; 35 | length++; 36 | } 37 | str[length] = '\n'; 38 | length++; 39 | } 40 | str[length] = '\0'; 41 | return (str); 42 | } 43 | 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * count_word - helper function to count the number of words in a string 6 | * @s: string to evaluate 7 | * 8 | * Return: number of words 9 | */ 10 | int count_word(char *s) 11 | { 12 | int flag, c, w; 13 | 14 | flag = 0; 15 | w = 0; 16 | 17 | for (c = 0; s[c] != '\0'; c++) 18 | { 19 | if (s[c] == ' ') 20 | flag = 0; 21 | else if (flag == 0) 22 | { 23 | flag = 1; 24 | w++; 25 | } 26 | } 27 | 28 | return (w); 29 | } 30 | /** 31 | * **strtow - splits a string into words 32 | * @str: string to split 33 | * 34 | * Return: pointer to an array of strings (Success) 35 | * or NULL (Error) 36 | */ 37 | char **strtow(char *str) 38 | { 39 | char **matrix, *tmp; 40 | int i, k = 0, len = 0, words, c = 0, start, end; 41 | 42 | while (*(str + len)) 43 | len++; 44 | words = count_word(str); 45 | if (words == 0) 46 | return (NULL); 47 | 48 | matrix = (char **) malloc(sizeof(char *) * (words + 1)); 49 | if (matrix == NULL) 50 | return (NULL); 51 | 52 | for (i = 0; i <= len; i++) 53 | { 54 | if (str[i] == ' ' || str[i] == '\0') 55 | { 56 | if (c) 57 | { 58 | end = i; 59 | tmp = (char *) malloc(sizeof(char) * (c + 1)); 60 | if (tmp == NULL) 61 | return (NULL); 62 | while (start < end) 63 | *tmp++ = str[start++]; 64 | *tmp = '\0'; 65 | matrix[k] = tmp - c; 66 | k++; 67 | c = 0; 68 | } 69 | } 70 | else if (c++ == 0) 71 | start = i; 72 | } 73 | 74 | matrix[k] = NULL; 75 | 76 | return (matrix); 77 | } 78 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *str_concat - concatenates two strings 6 | * @s1: string to concatenate 7 | * @s2: other string to concatenate 8 | * 9 | * Return: pointer to the new string created (Success), or NULL (Error) 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *s3; 14 | unsigned int i = 0, j = 0, len1 = 0, len2 = 0; 15 | 16 | while (s1 && s1[len1]) 17 | len1++; 18 | while (s2 && s2[len2]) 19 | len2++; 20 | 21 | s3 = malloc(sizeof(char) * (len1 + len2 + 1)); 22 | if (s3 == NULL) 23 | return (NULL); 24 | 25 | i = 0; 26 | j = 0; 27 | 28 | if (s1) 29 | { 30 | while (i < len1) 31 | { 32 | s3[i] = s1[i]; 33 | i++; 34 | } 35 | } 36 | 37 | if (s2) 38 | { 39 | while (i < (len1 + len2)) 40 | { 41 | s3[i] = s2[j]; 42 | i++; 43 | j++; 44 | } 45 | } 46 | s3[i] = '\0'; 47 | 48 | return (s3); 49 | } 50 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * **alloc_grid - creates a two dimensional array of ints 6 | * @width: width of the matrix 7 | * @height: height of the matrix 8 | * 9 | * Return: pointer to the created matrix (Success) 10 | * or NULL (Error) 11 | */ 12 | int **alloc_grid(int width, int height) 13 | { 14 | int **arr; 15 | int i, j; 16 | 17 | if (height <= 0 || width <= 0) 18 | return (NULL); 19 | 20 | arr = (int **) malloc(sizeof(int *) * height); 21 | 22 | if (arr == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; i < height; i++) 26 | { 27 | arr[i] = (int *) malloc(sizeof(int) * width); 28 | if (arr[i] == NULL) 29 | { 30 | free(arr); 31 | for (j = 0; j <= i; j++) 32 | free(arr[j]); 33 | return (NULL); 34 | } 35 | } 36 | 37 | for (i = 0; i < height; i++) 38 | { 39 | for (j = 0; j < width; j++) 40 | { 41 | arr[i][j] = 0; 42 | } 43 | } 44 | return (arr); 45 | } 46 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * free_grid - frees the memory allocate for the grid 6 | * created by alloc_grid() 7 | * @grid: grid to free 8 | * @height: height of the grid 9 | */ 10 | void free_grid(int **grid, int height) 11 | { 12 | int i; 13 | 14 | if (grid == NULL || height == 0) 15 | return; 16 | 17 | for (i = 0; i < height; i++) 18 | free(grid[i]); 19 | 20 | free(grid); 21 | } 22 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0B-malloc_free 2 | 3 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | char *create_array(unsigned int size, char c); 6 | char *_strdup(char *str); 7 | char *str_concat(char *s1, char *s2); 8 | int **alloc_grid(int width, int height); 9 | void free_grid(int **grid, int height); 10 | 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0B-malloc_free/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *malloc_checked - allocates memory using malloc 6 | * @b: number of bytes to allocate 7 | * 8 | * Return: a pointer to the allocated memory 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | void *ptr; 13 | 14 | ptr = malloc(b); 15 | 16 | if (ptr == NULL) 17 | exit(98); 18 | 19 | return (ptr); 20 | } 21 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *string_nconcat - concatenates n bytes of a string to another string 6 | * @s1: string to append to 7 | * @s2: string to concatenate from 8 | * @n: number of bytes from s2 to concatenate to s1 9 | * 10 | * Return: pointer to the resulting string 11 | */ 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | char *s; 15 | unsigned int i = 0, j = 0, len1 = 0, len2 = 0; 16 | 17 | while (s1 && s1[len1]) 18 | len1++; 19 | while (s2 && s2[len2]) 20 | len2++; 21 | 22 | if (n < len2) 23 | s = malloc(sizeof(char) * (len1 + n + 1)); 24 | else 25 | s = malloc(sizeof(char) * (len1 + len2 + 1)); 26 | 27 | if (!s) 28 | return (NULL); 29 | 30 | while (i < len1) 31 | { 32 | s[i] = s1[i]; 33 | i++; 34 | } 35 | 36 | while (n < len2 && i < (len1 + n)) 37 | s[i++] = s2[j++]; 38 | 39 | while (n >= len2 && i < (len1 + len2)) 40 | s[i++] = s2[j++]; 41 | 42 | s[i] = '\0'; 43 | 44 | return (s); 45 | } 46 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *_realloc - reallocates a memory block using malloc and free 6 | * @ptr: pointer to the memory previsouly allocated by malloc 7 | * @old_size: size of the allocated memory for ptr 8 | * @new_size: new size of the new memory block 9 | * 10 | * Return: pointer to the newly allocated memory block 11 | */ 12 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 13 | { 14 | char *ptr1; 15 | char *old_ptr; 16 | unsigned int i; 17 | 18 | if (new_size == old_size) 19 | return (ptr); 20 | 21 | if (new_size == 0 && ptr) 22 | { 23 | free(ptr); 24 | return (NULL); 25 | } 26 | 27 | if (!ptr) 28 | return (malloc(new_size)); 29 | 30 | ptr1 = malloc(new_size); 31 | if (!ptr1) 32 | return (NULL); 33 | 34 | old_ptr = ptr; 35 | 36 | if (new_size < old_size) 37 | { 38 | for (i = 0; i < new_size; i++) 39 | ptr1[i] = old_ptr[i]; 40 | } 41 | 42 | if (new_size > old_size) 43 | { 44 | for (i = 0; i < old_size; i++) 45 | ptr1[i] = old_ptr[i]; 46 | } 47 | 48 | free(ptr); 49 | return (ptr1); 50 | } 51 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * is_digit - checks if a string contains a non-digit char 7 | * @s: string to be evaluated 8 | * 9 | * Return: 0 if a non-digit is found, 1 otherwise 10 | */ 11 | int is_digit(char *s) 12 | { 13 | int i = 0; 14 | 15 | while (s[i]) 16 | { 17 | if (s[i] < '0' || s[i] > '9') 18 | return (0); 19 | i++; 20 | } 21 | return (1); 22 | } 23 | 24 | /** 25 | * _strlen - returns the length of a string 26 | * @s: string to evaluate 27 | * 28 | * Return: the length of the string 29 | */ 30 | int _strlen(char *s) 31 | { 32 | int i = 0; 33 | 34 | while (s[i] != '\0') 35 | { 36 | i++; 37 | } 38 | return (i); 39 | } 40 | 41 | /** 42 | * errors - handles errors for main 43 | */ 44 | void errors(void) 45 | { 46 | printf("Error\n"); 47 | exit(98); 48 | } 49 | 50 | /** 51 | * main - multiplies two positive numbers 52 | * @argc: number of arguments 53 | * @argv: array of arguments 54 | * 55 | * Return: always 0 (Success) 56 | */ 57 | int main(int argc, char *argv[]) 58 | { 59 | char *s1, *s2; 60 | int len1, len2, len, i, carry, digit1, digit2, *result, a = 0; 61 | 62 | s1 = argv[1], s2 = argv[2]; 63 | if (argc != 3 || !is_digit(s1) || !is_digit(s2)) 64 | errors(); 65 | len1 = _strlen(s1); 66 | len2 = _strlen(s2); 67 | len = len1 + len2 + 1; 68 | result = malloc(sizeof(int) * len); 69 | if (!result) 70 | return (1); 71 | for (i = 0; i <= len1 + len2; i++) 72 | result[i] = 0; 73 | for (len1 = len1 - 1; len1 >= 0; len1--) 74 | { 75 | digit1 = s1[len1] - '0'; 76 | carry = 0; 77 | for (len2 = _strlen(s2) - 1; len2 >= 0; len2--) 78 | { 79 | digit2 = s2[len2] - '0'; 80 | carry += result[len1 + len2 + 1] + (digit1 * digit2); 81 | result[len1 + len2 + 1] = carry % 10; 82 | carry /= 10; 83 | } 84 | if (carry > 0) 85 | result[len1 + len2 + 1] += carry; 86 | } 87 | for (i = 0; i < len - 1; i++) 88 | { 89 | if (result[i]) 90 | a = 1; 91 | if (a) 92 | _putchar(result[i] + '0'); 93 | } 94 | if (!a) 95 | _putchar('0'); 96 | _putchar('\n'); 97 | free(result); 98 | return (0); 99 | } 100 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *_memset - fills memory with a constant byte 6 | * @s: memory area to be filled 7 | * @b: char to copy 8 | * @n: number of times to copy b 9 | * 10 | * Return: pointer to the memory area s 11 | */ 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | s[i] = b; 19 | } 20 | 21 | return (s); 22 | } 23 | 24 | /** 25 | * *_calloc - allocates memory for an array 26 | * @nmemb: number of elements in the array 27 | * @size: size of each element 28 | * 29 | * Return: pointer to allocated memory 30 | */ 31 | void *_calloc(unsigned int nmemb, unsigned int size) 32 | { 33 | char *ptr; 34 | 35 | if (nmemb == 0 || size == 0) 36 | return (NULL); 37 | 38 | ptr = malloc(size * nmemb); 39 | 40 | if (ptr == NULL) 41 | return (NULL); 42 | 43 | _memset(ptr, 0, nmemb * size); 44 | 45 | return (ptr); 46 | } 47 | -------------------------------------------------------------------------------- /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/README.md: -------------------------------------------------------------------------------- 1 | # 0x0C-more_malloc_free 2 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | void *malloc_checked(unsigned int b); 6 | char *string_nconcat(char *s1, char *s2, unsigned int n); 7 | void *_calloc(unsigned int nmemb, unsigned int size); 8 | int *array_range(int min, int max); 9 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 10 | void errors(void); 11 | int _strlen(char *s); 12 | int is_digit(char *s); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_MAIN 2 | #define OBJECT_MAIN 3 | 4 | #define SIZE 1024 5 | 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_MAIN 2 | #define PI_MAIN 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file 5 | * 6 | * Return: Always 0 (Success) 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 FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < (0) ? -(x) : (x)) 5 | 6 | #endif 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 | # 0x0D-preprocessor 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - initializes a variable of type struct dog 6 | * @d: pointer to struct dog to initialize 7 | * @name: name to initialize 8 | * @age: age to initialize 9 | * @owner: owner to initialize 10 | */ 11 | 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d == NULL) 15 | { 16 | d = malloc(sizeof(struct dog)); 17 | } 18 | 19 | d->name = name; 20 | d->age = age; 21 | d->owner = owner; 22 | } 23 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * print_dog - prints a struct dog 7 | * @d: struct dog to print 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | if (d == NULL) 12 | return; 13 | 14 | if (d->name == NULL) 15 | d->name = "(nil)"; 16 | if (d->owner == NULL) 17 | d->owner = "(nil)"; 18 | 19 | printf("Name: %s\nAge: %f\nOwner: %s\n", d->name, d->age, d->owner); 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * _strlen - returns the length of a string 6 | * @s: string to evaluate 7 | * 8 | * Return: the length of the string 9 | */ 10 | int _strlen(char *s) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | 16 | while (s[i] != '\0') 17 | { 18 | i++; 19 | } 20 | 21 | return (i); 22 | } 23 | 24 | /** 25 | * *_strcpy - copies the string pointed to by src 26 | * including the terminating null byte (\0) 27 | * to the buffer pointed to by dest 28 | * @dest: pointer to the buffer in which we copy the string 29 | * @src: string to be copied 30 | * 31 | * Return: the pointer to dest 32 | */ 33 | char *_strcpy(char *dest, char *src) 34 | { 35 | int len, i; 36 | 37 | len = 0; 38 | 39 | while (src[len] != '\0') 40 | { 41 | len++; 42 | } 43 | 44 | for (i = 0; i < len; i++) 45 | { 46 | dest[i] = src[i]; 47 | } 48 | dest[i] = '\0'; 49 | 50 | return (dest); 51 | } 52 | 53 | /** 54 | * new_dog - creates a new dog 55 | * @name: name of the dog 56 | * @age: age of the dog 57 | * @owner: owner of the dog 58 | * 59 | * Return: pointer to the new dog (Success), NULL otherwise 60 | */ 61 | dog_t *new_dog(char *name, float age, char *owner) 62 | { 63 | dog_t *dog; 64 | int len1, len2; 65 | 66 | len1 = _strlen(name); 67 | len2 = _strlen(owner); 68 | 69 | dog = malloc(sizeof(dog_t)); 70 | if (dog == NULL) 71 | return (NULL); 72 | 73 | dog->name = malloc(sizeof(char) * (len1 + 1)); 74 | if (dog->name == NULL) 75 | { 76 | free(dog); 77 | return (NULL); 78 | } 79 | dog->owner = malloc(sizeof(char) * (len2 + 1)); 80 | if (dog->owner == NULL) 81 | { 82 | free(dog); 83 | free(dog->name); 84 | return (NULL); 85 | } 86 | _strcpy(dog->name, name); 87 | _strcpy(dog->owner, owner); 88 | dog->age = age; 89 | 90 | return (dog); 91 | } 92 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * free_dog - frees memory allocated for a struct dog 6 | * @d: struct dog to free 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d) 11 | { 12 | free(d->name); 13 | free(d->owner); 14 | free(d); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # 0x0E-structures_typedef 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * struct dog - a dog's basic info 6 | * @name: First member 7 | * @age: Second member 8 | * @owner: Third member 9 | * 10 | * Description: Longer description 11 | */ 12 | struct dog 13 | { 14 | char *name; 15 | float age; 16 | char *owner; 17 | }; 18 | 19 | /** 20 | * dog_t - typedef for struct dog 21 | */ 22 | typedef struct dog dog_t; 23 | 24 | void init_dog(struct dog *d, char *name, float age, char *owner); 25 | void print_dog(struct dog *d); 26 | dog_t *new_dog(char *name, float age, char *owner); 27 | void free_dog(dog_t *d); 28 | char *_strcpy(char *dest, char *src); 29 | int _strlen(char *s); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | /** 2 | * print_name - prints the name of the passed variable 3 | * @name: string of name 4 | * @f: pointer to a function 5 | * 6 | * Return: void 7 | */ 8 | 9 | void print_name(char *name, void(*f)(char *)) 10 | { 11 | if (!name || !f) 12 | { 13 | return; 14 | } 15 | 16 | (*f)(name); 17 | } 18 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * array_iterator - executes a function given as a parameter 6 | * on each element of an array 7 | * @array: array to iterate over 8 | * @size: size of the array 9 | * @action: pointer to function used 10 | */ 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | unsigned int i; 14 | 15 | if (!array || !action) 16 | return; 17 | 18 | for (i = 0; i < size; i++) 19 | action(array[i]); 20 | } 21 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints its own opcodes 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int bytes, i; 14 | char *arr; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | exit(1); 20 | } 21 | 22 | bytes = atoi(argv[1]); 23 | 24 | if (bytes < 0) 25 | { 26 | printf("Error\n"); 27 | exit(2); 28 | } 29 | 30 | arr = (char *)main; 31 | 32 | for (i = 0; i < bytes; i++) 33 | { 34 | if (i == bytes - 1) 35 | { 36 | printf("%02hhx\n", arr[i]); 37 | break; 38 | } 39 | printf("%02hhx ", arr[i]); 40 | } 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - earches for an integer 5 | * @array: array to search in 6 | * @size: size of the array 7 | * @cmp: pointer to the comparing function 8 | * 9 | * Return: index of the first element for which 10 | * the cmp function does not return 0, or -1 if no match is found 11 | * or size is negative 12 | */ 13 | int int_index(int *array, int size, int (*cmp)(int)) 14 | { 15 | int i; 16 | 17 | if (array && cmp) 18 | { 19 | for (i = 0; i < size; i++) 20 | { 21 | if (cmp(array[i]) != 0) 22 | return (i); 23 | } 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct op - Struct op 10 | * 11 | * @op: The operator 12 | * @f: The function associated 13 | */ 14 | typedef struct op 15 | { 16 | char *op; 17 | int (*f)(int a, int b); 18 | } op_t; 19 | 20 | int op_add(int a, int b); 21 | int op_sub(int a, int b); 22 | int op_mul(int a, int b); 23 | int op_div(int a, int b); 24 | int op_mod(int a, int b); 25 | int (*get_op_func(char *s))(int, int); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * get_op_func - function pointer that selects the correct function to perform 5 | * the operation asked by the user 6 | * @s: the operator given by the user 7 | * 8 | * Return: pointer to the function that corresponds to the 9 | * operator given as a parameter 10 | */ 11 | int (*get_op_func(char *s))(int, int) 12 | { 13 | op_t ops[] = { 14 | {"+", op_add}, 15 | {"-", op_sub}, 16 | {"*", op_mul}, 17 | {"/", op_div}, 18 | {"%", op_mod}, 19 | {NULL, NULL} 20 | }; 21 | int i; 22 | 23 | i = 0; 24 | 25 | while (ops[i].op) 26 | { 27 | if (strcmp(ops[i].op, s) == 0) 28 | return (ops[i].f); 29 | i++; 30 | } 31 | 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * main - program that perfroms simple operations 5 | * @argc: number of arguments 6 | * @argv: array of arguments 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int arg1, arg2, result; 13 | char o; 14 | int (*func)(int, int); 15 | 16 | if (argc != 4) 17 | { 18 | printf("Error\n"); 19 | exit(98); 20 | } 21 | 22 | arg1 = atoi(argv[1]); 23 | arg2 = atoi(argv[3]); 24 | 25 | func = get_op_func(argv[2]); 26 | 27 | if (!func) 28 | { 29 | printf("Error\n"); 30 | exit(99); 31 | } 32 | 33 | o = *argv[2]; 34 | 35 | if ((o == '/' || o == '%') && arg2 == 0) 36 | { 37 | printf("Error\n"); 38 | exit(100); 39 | } 40 | 41 | result = func(arg1, arg2); 42 | 43 | printf("%d\n", result); 44 | 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * op_add - calculates the sum of two integers 5 | * @a: first integer 6 | * @b: second integer 7 | * 8 | * Return: sum of a and b 9 | */ 10 | int op_add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | 15 | /** 16 | * op_sub - calculates the difference of two integers 17 | * @a: first integer 18 | * @b: second integer 19 | * 20 | * Return: difference of a and b 21 | */ 22 | int op_sub(int a, int b) 23 | { 24 | return (a - b); 25 | } 26 | 27 | /** 28 | * op_mul - calculates the product of two integers 29 | * @a: first integer 30 | * @b: second integer 31 | * 32 | * Return: product of a and b 33 | */ 34 | int op_mul(int a, int b) 35 | { 36 | return (a * b); 37 | } 38 | 39 | /** 40 | * op_div - calculates the division of two integers 41 | * @a: first integer 42 | * @b: second integer 43 | * 44 | * Return: result of the division of a and b 45 | */ 46 | int op_div(int a, int b) 47 | { 48 | return (a / b); 49 | } 50 | 51 | /** 52 | * op_mod - calculates the remainder of the division of two integers 53 | * @a: first integer 54 | * @b: second integer 55 | * 56 | * Return: remainder of a divided b 57 | */ 58 | int op_mod(int a, int b) 59 | { 60 | return (a % b); 61 | } 62 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # 0x0F-function_pointers 2 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNC_H 2 | #define FUNC_H 3 | #include 4 | 5 | void print_name(char *name, void (*f)(char *)); 6 | void array_iterator(int *array, size_t size, void (*action)(int)); 7 | int int_index(int *array, int size, int (*cmp)(int)); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x0F-function_pointers/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - calculates the sum of all its parameters 5 | * @n: number of arguments passed to the function 6 | * 7 | * Return: the resulting sum 8 | */ 9 | int sum_them_all(const unsigned int n, ...) 10 | { 11 | va_list valist; 12 | unsigned int i; 13 | int sum; 14 | 15 | if (n == 0) 16 | return (0); 17 | va_start(valist, n); 18 | sum = 0; 19 | for (i = 0; i < n; i++) 20 | sum += va_arg(valist, int); 21 | 22 | va_end(valist); 23 | return (sum); 24 | } 25 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_numbers - prints numbers given as parameters 5 | * @separator: string to be printed between numbers 6 | * @n: number of integers passed to the function 7 | */ 8 | void print_numbers(const char *separator, const unsigned int n, ...) 9 | { 10 | unsigned int i; 11 | va_list list; 12 | 13 | va_start(list, n); 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | if (!separator) 18 | printf("%d", va_arg(list, int)); 19 | else if (separator && i == 0) 20 | printf("%d", va_arg(list, int)); 21 | else 22 | printf("%s%d", separator, va_arg(list, int)); 23 | } 24 | 25 | va_end(list); 26 | 27 | printf("\n"); 28 | } 29 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - prints strings 5 | * @separator: separator between strings 6 | * @n: number of arguments 7 | */ 8 | void print_strings(const char *separator, const unsigned int n, ...) 9 | { 10 | unsigned int i; 11 | char *str; 12 | 13 | va_list list; 14 | 15 | va_start(list, n); 16 | 17 | for (i = 0; i < n; i++) 18 | { 19 | str = va_arg(list, char *); 20 | if (!str) 21 | str = "(nil)"; 22 | if (!separator) 23 | printf("%s", str); 24 | else if (separator && i == 0) 25 | printf("%s", str); 26 | else 27 | printf("%s%s", separator, str); 28 | } 29 | 30 | printf("\n"); 31 | 32 | va_end(list); 33 | } 34 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_all - prints anything 5 | * @format: list of types of arguments passed to the function 6 | */ 7 | void print_all(const char * const format, ...) 8 | { 9 | int i = 0; 10 | char *str, *sep = ""; 11 | 12 | va_list list; 13 | 14 | va_start(list, format); 15 | 16 | if (format) 17 | { 18 | while (format[i]) 19 | { 20 | switch (format[i]) 21 | { 22 | case 'c': 23 | printf("%s%c", sep, va_arg(list, int)); 24 | break; 25 | case 'i': 26 | printf("%s%d", sep, va_arg(list, int)); 27 | break; 28 | case 'f': 29 | printf("%s%f", sep, va_arg(list, double)); 30 | break; 31 | case 's': 32 | str = va_arg(list, char *); 33 | if (!str) 34 | str = "(nil)"; 35 | printf("%s%s", sep, str); 36 | break; 37 | default: 38 | i++; 39 | continue; 40 | } 41 | sep = ", "; 42 | i++; 43 | } 44 | } 45 | 46 | printf("\n"); 47 | va_end(list); 48 | } 49 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # 0x10-variadic_functions 2 | -------------------------------------------------------------------------------- /0x10-variadic_functions/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VAR_FUNC_H 2 | #define VAR_FUNC_H 3 | 4 | #include 5 | #include 6 | 7 | int sum_them_all(const unsigned int n, ...); 8 | void print_numbers(const char *separator, const unsigned int n, ...); 9 | void print_strings(const char *separator, const unsigned int n, ...); 10 | void print_all(const char * const format, ...); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * print_list - prints all the elements of a linked list 6 | * @h: pointer to the list_t list to print 7 | * 8 | * Return: the number of nodes printed 9 | */ 10 | size_t print_list(const list_t *h) 11 | { 12 | size_t s = 0; 13 | 14 | while (h) 15 | { 16 | if (!h->str) 17 | printf("[0] (nil)\n"); 18 | else 19 | printf("[%u] %s\n", h->len, h->str); 20 | h = h->next; 21 | s++; 22 | } 23 | 24 | return (s); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * list_len - returns the number of elements in a linked list 6 | * @h: pointer to the list_t list 7 | * 8 | * Return: number of elements in h 9 | */ 10 | size_t list_len(const list_t *h) 11 | { 12 | size_t n = 0; 13 | 14 | while (h) 15 | { 16 | n++; 17 | h = h->next; 18 | } 19 | return (n); 20 | } 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void first(void) __attribute__ ((constructor)); 4 | 5 | /** 6 | * first - prints a sentence before the main 7 | * function is executed 8 | */ 9 | void first(void) 10 | { 11 | printf("You're beat! and yet, you must allow,\n"); 12 | printf("I bore my house upon my back!\n"); 13 | } 14 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | global main 2 | extern printf 3 | main: 4 | mov edi, format 5 | xor eax, eax 6 | call printf 7 | mov eax, 0 8 | ret 9 | format: db `Hello, Holberton\n`,0 10 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * add_node - adds a new node at the beginning of a linked list 7 | * @head: double pointer to the list_t list 8 | * @str: new string to add in the node 9 | * 10 | * Return: the address of the new element, or NULL if it fails 11 | */ 12 | list_t *add_node(list_t **head, const char *str) 13 | { 14 | list_t *new; 15 | unsigned int len = 0; 16 | 17 | while (str[len]) 18 | len++; 19 | 20 | new = malloc(sizeof(list_t)); 21 | if (!new) 22 | return (NULL); 23 | 24 | new->str = strdup(str); 25 | new->len = len; 26 | new->next = (*head); 27 | (*head) = new; 28 | 29 | return (*head); 30 | } 31 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * add_node_end - adds a new node at the end of a linked list 7 | * @head: double pointer to the list_t list 8 | * @str: string to put in the new node 9 | * 10 | * Return: address of the new element, or NULL if it failed 11 | */ 12 | list_t *add_node_end(list_t **head, const char *str) 13 | { 14 | list_t *new; 15 | list_t *temp = *head; 16 | unsigned int len = 0; 17 | 18 | while (str[len]) 19 | len++; 20 | 21 | new = malloc(sizeof(list_t)); 22 | if (!new) 23 | return (NULL); 24 | 25 | new->str = strdup(str); 26 | new->len = len; 27 | new->next = NULL; 28 | 29 | if (*head == NULL) 30 | { 31 | *head = new; 32 | return (new); 33 | } 34 | 35 | while (temp->next) 36 | temp = temp->next; 37 | 38 | temp->next = new; 39 | 40 | return (new); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * free_list - frees a linked list 6 | * @head: list_t list to be freed 7 | */ 8 | void free_list(list_t *head) 9 | { 10 | list_t *temp; 11 | 12 | while (head) 13 | { 14 | temp = head->next; 15 | free(head->str); 16 | free(head); 17 | head = temp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x12-singly_linked_lists 2 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | /** 5 | * struct list_s - singly linked list 6 | * @str: string - (malloc'ed string) 7 | * @len: length of the string 8 | * @next: points to the next node 9 | * 10 | * Description: singly linked list node structure 11 | * for Holberton project 12 | */ 13 | typedef struct list_s 14 | { 15 | char *str; 16 | unsigned int len; 17 | struct list_s *next; 18 | } list_t; 19 | 20 | size_t print_list(const list_t *h); 21 | size_t list_len(const list_t *h); 22 | list_t *add_node(list_t **head, const char *str); 23 | list_t *add_node_end(list_t **head, const char *str); 24 | void free_list(list_t *head); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the elements of a linked list 5 | * @h: linked list of type listint_t to print 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t num = 0; 12 | 13 | while (h) 14 | { 15 | printf("%d\n", h->n); 16 | num++; 17 | h = h->next; 18 | } 19 | 20 | return (num); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1: -------------------------------------------------------------------------------- 1 | # 0x13-more_singly_linked_lists 2 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns the number of elements in a linked lists 5 | * @h: linked list of type listint_t to traverse 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t listint_len(const listint_t *h) 10 | { 11 | size_t num = 0; 12 | 13 | while (h) 14 | { 15 | num++; 16 | h = h->next; 17 | } 18 | 19 | return (num); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes a node in a linked list at a certain index 5 | * @head: pointer to the first element in the list 6 | * @index: index of the node to delete 7 | * 8 | * Return: 1 (Success), or -1 (Fail) 9 | */ 10 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 11 | { 12 | listint_t *temp = *head; 13 | listint_t *current = NULL; 14 | unsigned int i = 0; 15 | 16 | if (*head == NULL) 17 | return (-1); 18 | 19 | if (index == 0) 20 | { 21 | *head = (*head)->next; 22 | free(temp); 23 | return (1); 24 | } 25 | 26 | while (i < index - 1) 27 | { 28 | if (!temp || !(temp->next)) 29 | return (-1); 30 | temp = temp->next; 31 | i++; 32 | } 33 | 34 | 35 | current = temp->next; 36 | temp->next = current->next; 37 | free(current); 38 | 39 | return (1); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list 5 | * @head: pointer to the first node in the list 6 | * 7 | * Return: pointer to the first node in the new list 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *prev = NULL; 12 | listint_t *next = NULL; 13 | 14 | while (*head) 15 | { 16 | next = (*head)->next; 17 | (*head)->next = prev; 18 | prev = *head; 19 | *head = next; 20 | } 21 | 22 | *head = prev; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint_safe - prints a linked list, safely 5 | * @head: list of type listint_t to print 6 | * 7 | * Return: number of nodes in the list 8 | */ 9 | size_t print_listint_safe(const listint_t *head) 10 | { 11 | size_t num = 0; 12 | long int diff; 13 | 14 | while (head) 15 | { 16 | diff = head - head->next; 17 | num++; 18 | printf("[%p] %d\n", (void *)head, head->n); 19 | if (diff > 0) 20 | head = head->next; 21 | else 22 | { 23 | printf("-> [%p] %d\n", (void *)head->next, head->next->n); 24 | break; 25 | } 26 | } 27 | 28 | return (num); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint_safe - frees a linked list 5 | * @h: pointer to the first node in the linked list 6 | * 7 | * Return: number of elements in the freed list 8 | */ 9 | size_t free_listint_safe(listint_t **h) 10 | { 11 | size_t len = 0; 12 | int diff; 13 | listint_t *temp; 14 | 15 | if (!h || !*h) 16 | return (0); 17 | 18 | while (*h) 19 | { 20 | diff = *h - (*h)->next; 21 | if (diff > 0) 22 | { 23 | temp = (*h)->next; 24 | free(*h); 25 | *h = temp; 26 | len++; 27 | } 28 | else 29 | { 30 | free(*h); 31 | *h = NULL; 32 | len++; 33 | break; 34 | } 35 | } 36 | 37 | *h = NULL; 38 | 39 | return (len); 40 | } 41 | -------------------------------------------------------------------------------- /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: linked list to search for 6 | * 7 | * Return: address of the node where the loop starts, or NULL 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *slow = head; 12 | listint_t *fast = head; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | while (slow && fast && fast->next) 18 | { 19 | fast = fast->next->next; 20 | slow = slow->next; 21 | if (fast == slow) 22 | { 23 | slow = head; 24 | while (slow != fast) 25 | { 26 | slow = slow->next; 27 | fast = fast->next; 28 | } 29 | return (fast); 30 | } 31 | } 32 | 33 | return (NULL); 34 | } 35 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - adds a new node at the beginning of a linked list 5 | * @head: pointer to the first node in the list 6 | * @n: data to insert in that new node 7 | * 8 | * Return: pointer to the new node, or NULL if it fails 9 | */ 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | listint_t *new; 13 | 14 | new = malloc(sizeof(listint_t)); 15 | if (!new) 16 | return (NULL); 17 | 18 | new->n = n; 19 | new->next = *head; 20 | *head = new; 21 | 22 | return (new); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - adds a node at the end of a linked list 5 | * @head: pointer to the first element in the list 6 | * @n: data to insert in the new element 7 | * 8 | * Return: pointer to the new node, or NULL if it fails 9 | */ 10 | listint_t *add_nodeint_end(listint_t **head, const int n) 11 | { 12 | listint_t *new; 13 | listint_t *temp = *head; 14 | 15 | new = malloc(sizeof(listint_t)); 16 | if (!new) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->next = NULL; 21 | 22 | if (*head == NULL) 23 | { 24 | *head = new; 25 | return (new); 26 | } 27 | 28 | while (temp->next) 29 | temp = temp->next; 30 | 31 | temp->next = new; 32 | 33 | return (new); 34 | } 35 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a linked list 5 | * @head: listint_t list to be freed 6 | */ 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *temp; 10 | 11 | while (head) 12 | { 13 | temp = head->next; 14 | free(head); 15 | head = temp; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a linked list 5 | * @head: pointer to the listint_t list to be freed 6 | */ 7 | void free_listint2(listint_t **head) 8 | { 9 | listint_t *temp; 10 | 11 | if (head == NULL) 12 | return; 13 | 14 | while (*head) 15 | { 16 | temp = (*head)->next; 17 | free(*head); 18 | *head = temp; 19 | } 20 | 21 | *head = NULL; 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes the head node of a linked list 5 | * @head: pointer to the first element in the linked list 6 | * 7 | * Return: the data inside the elements that was deleted, 8 | * or 0 if the list is empty 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | listint_t *temp; 13 | int num; 14 | 15 | if (!head || !*head) 16 | return (0); 17 | 18 | num = (*head)->n; 19 | temp = (*head)->next; 20 | free(*head); 21 | *head = temp; 22 | 23 | return (num); 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the node at a certain index in a linked list 5 | * @head: first node in the linked list 6 | * @index: index of the node to return 7 | * 8 | * Return: pointer to the node we're looking for, or NULL 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i = 0; 13 | listint_t *temp = head; 14 | 15 | while (temp && i < index) 16 | { 17 | temp = temp->next; 18 | i++; 19 | } 20 | 21 | return (temp ? temp : NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - calculates the sum of all the data in a listint_t list 5 | * @head: first node in the linked list 6 | * 7 | * Return: resulting sum 8 | */ 9 | int sum_listint(listint_t *head) 10 | { 11 | int sum = 0; 12 | listint_t *temp = head; 13 | 14 | while (temp) 15 | { 16 | sum += temp->n; 17 | temp = temp->next; 18 | } 19 | 20 | return (sum); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a new node in a linked list, 5 | * at a given position 6 | * @head: pointer to the first node in the list 7 | * @idx: index where the new node is added 8 | * @n: data to insert in the new node 9 | * 10 | * Return: pointer to the new node, or NULL 11 | */ 12 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 13 | { 14 | unsigned int i; 15 | listint_t *new; 16 | listint_t *temp = *head; 17 | 18 | new = malloc(sizeof(listint_t)); 19 | if (!new || !head) 20 | return (NULL); 21 | 22 | new->n = n; 23 | new->next = NULL; 24 | 25 | if (idx == 0) 26 | { 27 | new->next = *head; 28 | *head = new; 29 | return (new); 30 | } 31 | 32 | for (i = 0; temp && i < idx; i++) 33 | { 34 | if (i == idx - 1) 35 | { 36 | new->next = temp->next; 37 | temp->next = new; 38 | return (new); 39 | } 40 | else 41 | temp = temp->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x13-more_singly_linked_lists 2 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * @n: integer 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | * for Holberton project 14 | */ 15 | typedef struct listint_s 16 | { 17 | int n; 18 | struct listint_s *next; 19 | } listint_t; 20 | 21 | size_t print_listint(const listint_t *h); 22 | size_t listint_len(const listint_t *h); 23 | listint_t *add_nodeint(listint_t **head, const int n); 24 | listint_t *add_nodeint_end(listint_t **head, const int n); 25 | void free_listint(listint_t *head); 26 | void free_listint2(listint_t **head); 27 | int pop_listint(listint_t **head); 28 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 29 | int sum_listint(listint_t *head); 30 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 31 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 32 | listint_t *reverse_listint(listint_t **head); 33 | size_t print_listint_safe(const listint_t *head); 34 | size_t free_listint_safe(listint_t **h); 35 | listint_t *find_listint_loop(listint_t *head); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number 5 | * @b: string 6 | * 7 | * Return: the converted number 8 | */ 9 | unsigned int binary_to_uint(const char *b) 10 | { 11 | int i; 12 | unsigned int deca_val = 0; 13 | 14 | if (!b) 15 | return (0); 16 | 17 | for (i = 0; b[i]; i++) 18 | { 19 | if (b[i] < '0' || b[i] > '1') 20 | return (0); 21 | deca_val = 2 * deca_val + (b[i] - '0'); 22 | } 23 | 24 | return (deca_val); 25 | } 26 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary equivalent of a decimal number 5 | * @n: number to print in binary 6 | */ 7 | void print_binary(unsigned long int n) 8 | { 9 | int i, count = 0; 10 | unsigned long int current; 11 | 12 | for (i = 63; i >= 0; i--) 13 | { 14 | current = n >> i; 15 | 16 | if (current & 1) 17 | { 18 | _putchar('1'); 19 | count++; 20 | } 21 | else if (count) 22 | _putchar('0'); 23 | } 24 | if (!count) 25 | _putchar('0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks if a machine is little or big endian 5 | * Return: 0 for big, 1 for little 6 | */ 7 | int get_endianness(void) 8 | { 9 | unsigned int i = 1; 10 | char *c = (char *) &i; 11 | 12 | return (*c); 13 | } 14 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hnryjsph/alx-low_level_programming/2fba713144c783d137b1571986a63c7045ec459e/0x14-bit_manipulation/101-password -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at an index in a decimal number 5 | * @n: number to search 6 | * @index: index of the bit 7 | * 8 | * Return: value of the bit 9 | */ 10 | int get_bit(unsigned long int n, unsigned int index) 11 | { 12 | int bit_val; 13 | 14 | if (index > 63) 15 | return (-1); 16 | 17 | bit_val = (n >> index) & 1; 18 | 19 | return (bit_val); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets a bit at a given index to 1 5 | * @n: pointer to the number to change 6 | * @index: index of the bit to set to 1 7 | * 8 | * Return: 1 for success, -1 for failure 9 | */ 10 | int set_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (index > 63) 13 | return (-1); 14 | 15 | *n = ((1UL << index) | *n); 16 | return (1); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a given bit to 0 5 | * @n: pointer to the number to change 6 | * @index: index of the bit to clear 7 | * 8 | * Return: 1 for success, -1 for failure 9 | */ 10 | int clear_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (index > 63) 13 | return (-1); 14 | 15 | *n = (~(1UL << index) & *n); 16 | return (1); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - counts the number of bits to change 5 | * to get from one number to another 6 | * @n: first number 7 | * @m: second number 8 | * 9 | * Return: number of bits to change 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | int i, count = 0; 14 | unsigned long int current; 15 | unsigned long int exclusive = n ^ m; 16 | 17 | for (i = 63; i >= 0; i--) 18 | { 19 | current = exclusive >> i; 20 | if (current & 1) 21 | count++; 22 | } 23 | 24 | return (count); 25 | } 26 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # 0x14-bit_manipulation 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 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 _atoi(const char *s); 11 | int _putchar(char c); 12 | int get_endianness(void); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/make: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - reads a text file and prints it to the standard output 5 | * @filename: name of the file to be read 6 | * @letters: number of letters to read and print 7 | * 8 | * Return: the number of letters printed, or 0 if it failed 9 | */ 10 | ssize_t read_textfile(const char *filename, size_t letters) 11 | { 12 | int fd; 13 | int s, t; 14 | char *buf; 15 | 16 | if (!filename) 17 | return (0); 18 | 19 | fd = open(filename, O_RDONLY); 20 | if (fd < 0) 21 | return (0); 22 | 23 | buf = malloc(sizeof(char) * letters); 24 | if (!buf) 25 | return (0); 26 | 27 | s = read(fd, buf, letters); 28 | if (s < 0) 29 | { 30 | free(buf); 31 | return (0); 32 | } 33 | buf[s] = '\0'; 34 | 35 | close(fd); 36 | 37 | t = write(STDOUT_FILENO, buf, s); 38 | if (t < 0) 39 | { 40 | free(buf); 41 | return (0); 42 | } 43 | 44 | free(buf); 45 | return (t); 46 | } 47 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file and fills it with text 5 | * @filename: name of the file to create 6 | * @text_content: text to write in the file 7 | * 8 | * Return: 1 on success, -1 on failure 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd, t, s = 0; 13 | 14 | if (!filename) 15 | return (-1); 16 | 17 | fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600); 18 | if (fd < 0) 19 | return (-1); 20 | 21 | if (text_content) 22 | { 23 | while (text_content[s]) 24 | s++; 25 | t = write(fd, text_content, s); 26 | if (t != s) 27 | return (-1); 28 | } 29 | 30 | close(fd); 31 | 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /0x15-file_io/100-elf_header.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | #include 5 | #include 6 | /** 7 | * printclass - prints the class from an elf header 8 | * 9 | * @head: header information 10 | * 11 | * Return: void 12 | */ 13 | void printclass(char *head) 14 | { 15 | printf(" %-35s", "Class:"); 16 | if (head[4] == 2) 17 | printf("ELF64\n"); 18 | else if (head[4] == 1) 19 | printf("ELF32\n"); 20 | else 21 | printf("", head[4]); 22 | } 23 | 24 | /** 25 | * printdata - prints the information about data organization 26 | * from the elf header 27 | * 28 | * @head: header information 29 | * 30 | * Return: void 31 | */ 32 | void printdata(char *head) 33 | { 34 | printf(" %-35s", "Data:"); 35 | if (head[5] == 1) 36 | printf("2's complement, little endian\n"); 37 | else if (head[5] == 2) 38 | printf("2's complement, big endian\n"); 39 | else 40 | printf("", head[5]); 41 | } 42 | 43 | /** 44 | * printversion - prints version info from elf header 45 | * 46 | * @head: header information 47 | * 48 | * Return: void 49 | */ 50 | void printversion(char *head) 51 | { 52 | printf(" %-35s", "Version:"); 53 | if (head[6] <= EV_CURRENT) 54 | { 55 | printf("%d", head[6]); 56 | if (head[6] == EV_CURRENT) 57 | printf(" (current)\n"); 58 | else 59 | printf("\n"); 60 | } 61 | else 62 | { 63 | printf("49 "); 64 | } 65 | } 66 | 67 | /** 68 | * printabi - prints abi version from header information 69 | * 70 | * @head: header information 71 | * 72 | * Return: void 73 | */ 74 | void printabi(char *head) 75 | { 76 | printf(" %-35s", "OS/ABI:"); 77 | if (head[7] == 0) 78 | printf("UNIX - System V\n"); 79 | else if (head[7] == 1) 80 | printf("UNIX - HP-UX\n"); 81 | else if (head[7] == 2) 82 | printf("UNIX - NetBSD\n"); 83 | else if (head[7] == 3) 84 | printf("UNIX - Linux\n"); 85 | else if (head[7] == 4) 86 | printf("UNIX - GNU Hurd\n"); 87 | else if (head[7] == 6) 88 | printf("UNIX - Solaris\n"); 89 | else if (head[7] == 7) 90 | printf("UNIX - AIX\n"); 91 | else if (head[7] == 8) 92 | printf("UNIX - IRIX\n"); 93 | else if (head[7] == 9) 94 | printf("UNIX - FreeBSD\n"); 95 | else if (head[7] == 10) 96 | printf("UNIX - Tru64\n"); 97 | else if (head[7] == 11) 98 | printf("UNIX - Novell Modesto\n"); 99 | else if (head[7] == 12) 100 | printf("UNIX - OpenBSD\n"); 101 | else if (head[7] == 13) 102 | printf("UNIX - Open VMS\n"); 103 | else if (head[7] == 14) 104 | printf("UNIX - NonStop Kernel\n"); 105 | else if (head[7] == 15) 106 | printf("UNIX - AROS\n"); 107 | else if (head[7] == 16) 108 | printf("UNIX - Fenix OS\n"); 109 | else if (head[7] == 17) 110 | printf("UNIX - CloudABI\n"); 111 | else 112 | printf("\n", head[7]); 113 | printf(" %-35s%d\n", "ABI Version:", head[8]); 114 | } 115 | 116 | /** 117 | * printtype - prints elf filetype from header info 118 | * 119 | * @head: header information 120 | * 121 | * Return: void 122 | */ 123 | void printtype(char *head) 124 | { 125 | int index; 126 | 127 | if (head[5] == 1) 128 | index = 16; 129 | else 130 | index = 17; 131 | printf(" %-35s", "Type:"); 132 | if (head[index] == 1) 133 | printf("REL (Relocatable file)\n"); 134 | else if (head[index] == 2) 135 | printf("EXEC (Executable file)\n"); 136 | else if (head[index] == 3) 137 | printf("DYN (Shared object file)\n"); 138 | else if (head[index] == 4) 139 | printf("CORE (Core file)\n"); 140 | else 141 | printf(": %02x%02x\n", head[16], head[17]); 142 | } 143 | 144 | /** 145 | * printentry - prints entry address of executable from header 146 | * 147 | * @head: header information 148 | * 149 | * Return: void 150 | */ 151 | void printentry(char *head) 152 | { 153 | int i, end; 154 | 155 | printf(" %-35s0x", "Entry point address:"); 156 | if (head[4] == 2) 157 | end = 0x1f; 158 | else 159 | end = 0x1b; 160 | if (head[5] == 1) 161 | { 162 | i = end; 163 | while (head[i] == 0 && i > 0x18) 164 | i--; 165 | printf("%x", head[i--]); 166 | while (i >= 0x18) 167 | printf("%02x", (unsigned char) head[i--]); 168 | printf("\n"); 169 | } 170 | else 171 | { 172 | i = 0x18; 173 | while (head[i] == 0) 174 | i++; 175 | printf("%x", head[i++]); 176 | while (i <= end) 177 | printf("%02x", (unsigned char) head[i++]); 178 | printf("\n"); 179 | } 180 | } 181 | 182 | /** 183 | * main - parses an elf header file 184 | * 185 | * @ac: number of args 186 | * @av: arugment strings 187 | * 188 | * Return: 0 on success 189 | * 1 on incorrect arg number 190 | * 2 on file open failure 191 | * 3 on read failure 192 | * 4 on failure to read enough bytes for a 32 bit file 193 | * 98 if elf magic is not matched 194 | */ 195 | int main(int ac, char *av[]) 196 | { 197 | int ifile, i; 198 | char head[32]; 199 | 200 | if (ac != 2) 201 | return (1); 202 | ifile = open(av[1], O_RDONLY); 203 | if (ifile == -1) 204 | return (1); 205 | i = read(ifile, head, 32); 206 | if (i == -1) 207 | return (1); 208 | if (i < 28) 209 | return (1); 210 | if (head[0] != 0x7f || head[1] != 'E' || head[2] != 'L' || head[3] != 'F') 211 | { 212 | dprintf(STDERR_FILENO, 213 | "readelf: Error: hellofile: Failed to read file header\n"); 214 | return (98); 215 | } 216 | printf("ELF Header:\n Magic: "); 217 | for (i = 0; i < 16; i++) 218 | printf("%02x ", (unsigned int) head[i]); 219 | printf("\n"); 220 | printclass(head); 221 | printdata(head); 222 | printversion(head); 223 | printabi(head); 224 | printtype(head); 225 | printentry(head); 226 | return (0); 227 | } 228 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text at the end of a file 5 | * @filename: file to append the text to 6 | * @text_content: content to append into the file 7 | * 8 | * Return: 1 on success and -1 on failure 9 | */ 10 | int append_text_to_file(const char *filename, char *text_content) 11 | { 12 | int fd, t, s = 0; 13 | 14 | if (!filename) 15 | return (-1); 16 | 17 | fd = open(filename, O_WRONLY | O_APPEND); 18 | if (fd < 0) 19 | return (-1); 20 | 21 | if (text_content) 22 | { 23 | while (text_content[s]) 24 | s++; 25 | t = write(fd, text_content, s); 26 | if (t != s) 27 | return (-1); 28 | } 29 | 30 | close(fd); 31 | 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - copies the content of a file to another file 5 | * @argc: number of arguments passed to the program 6 | * @argv: array of arguments 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int fd_r, fd_w, r, a, b; 13 | char buf[BUFSIZ]; 14 | 15 | if (argc != 3) 16 | { 17 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"); 18 | exit(97); 19 | } 20 | fd_r = open(argv[1], O_RDONLY); 21 | if (fd_r < 0) 22 | { 23 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 24 | exit(98); 25 | } 26 | fd_w = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0664); 27 | while ((r = read(fd_r, buf, BUFSIZ)) > 0) 28 | { 29 | if (fd_w < 0 || write(fd_w, buf, r) != r) 30 | { 31 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]); 32 | close(fd_r); 33 | exit(99); 34 | } 35 | } 36 | if (r < 0) 37 | { 38 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 39 | exit(98); 40 | } 41 | a = close(fd_r); 42 | b = close(fd_w); 43 | if (a < 0 || b < 0) 44 | { 45 | if (a < 0) 46 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd_r); 47 | if (b < 0) 48 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd_w); 49 | exit(100); 50 | } 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | # 0x15-file_io 2 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | ssize_t read_textfile(const char *filename, size_t letters); 12 | int create_file(const char *filename, char *text_content); 13 | int append_text_to_file(const char *filename, char *text_content); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x15-file_io/makebetty: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for file in *.c 3 | do 4 | vim $file +'e ++ff=dos | set ff=unix | wq!' 5 | done 6 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints a doubly linked list 5 | * @h: pointer to the list 6 | * 7 | * Return: number of nodes in the list 8 | */ 9 | size_t print_dlistint(const dlistint_t *h) 10 | { 11 | size_t nodes = 0; 12 | 13 | if (!h) 14 | return (0); 15 | 16 | while (h) 17 | { 18 | printf("%d\n", h->n); 19 | h = h->next; 20 | nodes++; 21 | } 22 | 23 | return (nodes); 24 | } 25 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of nodes in a doubly linked list 5 | * @h: pointer to the list 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t nodes = 0; 12 | 13 | if (!h) 14 | return (0); 15 | 16 | while (h) 17 | { 18 | nodes++; 19 | h = h->next; 20 | } 21 | 22 | return (nodes); 23 | } 24 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds a new node at the beginning of a doubly linked list 5 | * @head: double pointer to the list 6 | * @n: data to insert in the new node 7 | * 8 | * Return: the address of the new element, or NULL if it failed 9 | */ 10 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 11 | { 12 | dlistint_t *new; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | new = malloc(sizeof(dlistint_t)); 18 | if (!new) 19 | return (NULL); 20 | 21 | new->n = n; 22 | 23 | new->next = *head; 24 | new->prev = NULL; 25 | 26 | if (*head) 27 | (*head)->prev = new; 28 | 29 | *head = new; 30 | 31 | return (new); 32 | } 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds a new node at the end of a doubly linked list 5 | * @head: double pointer to the list 6 | * @n: data to insert in the new node 7 | * 8 | * Return: the address of the new element, or NULL if it failed 9 | */ 10 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 11 | { 12 | dlistint_t *new; 13 | dlistint_t *temp = *head; 14 | 15 | if (!head) 16 | return (NULL); 17 | 18 | new = malloc(sizeof(dlistint_t)); 19 | if (!new) 20 | return (NULL); 21 | 22 | new->n = n; 23 | new->next = NULL; 24 | 25 | if (*head == NULL) 26 | { 27 | new->prev = NULL; 28 | *head = new; 29 | return (new); 30 | } 31 | 32 | while (temp->next) 33 | temp = temp->next; 34 | 35 | temp->next = new; 36 | new->prev = temp; 37 | 38 | return (new); 39 | } 40 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a doubly linked list 5 | * @head: pointer to the list to free 6 | */ 7 | void free_dlistint(dlistint_t *head) 8 | { 9 | dlistint_t *temp; 10 | 11 | while (head) 12 | { 13 | temp = head->next; 14 | free(head); 15 | head = temp; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - gets the nth node of a doubly linked list 5 | * @head: pointer to the list 6 | * @index: index of the node to return 7 | * 8 | * Return: address of the node, or if it does not exist, NULL 9 | */ 10 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 11 | { 12 | unsigned int i = 0; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | while (head && i < index) 18 | { 19 | head = head->next; 20 | i++; 21 | } 22 | 23 | return (head ? head : NULL); 24 | } 25 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all the data in a doubly linked list 5 | * @head: pointer to the list 6 | * 7 | * Return: sum of the data 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | 19 | return (sum); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - inserts a node at a given index 5 | * in a doubly linked list 6 | * @h: double pointer to the list 7 | * @idx: index of the node to insert 8 | * @n: data to insert 9 | * 10 | * Return: address of the new node, or NULL if it failed 11 | */ 12 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 13 | { 14 | unsigned int i; 15 | dlistint_t *new; 16 | dlistint_t *temp = *h; 17 | 18 | new = malloc(sizeof(dlistint_t)); 19 | if (!new || !h) 20 | return (NULL); 21 | 22 | new->n = n; 23 | new->next = NULL; 24 | 25 | if (idx == 0) 26 | return (add_dnodeint(h, n)); 27 | 28 | for (i = 0; temp && i < idx; i++) 29 | { 30 | if (i == idx - 1) 31 | { 32 | if (temp->next == NULL) 33 | return (add_dnodeint_end(h, n)); 34 | new->next = temp->next; 35 | new->prev = temp; 36 | temp->next->prev = new; 37 | temp->next = new; 38 | return (new); 39 | } 40 | else 41 | temp = temp->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of nodes in a doubly linked list 5 | * @h: pointer to the list 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t nodes = 0; 12 | 13 | if (!h) 14 | return (0); 15 | 16 | while (h) 17 | { 18 | nodes++; 19 | h = h->next; 20 | } 21 | 22 | return (nodes); 23 | } 24 | 25 | /** 26 | * delete_dnodeint_at_index - deltes a node in a doubly linked list 27 | * at a given index 28 | * @head: double pointer to the list 29 | * @index: index of the node to delete 30 | * 31 | * Return: 1 on success, -1 on failure 32 | */ 33 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 34 | { 35 | dlistint_t *temp = *head; 36 | unsigned int i = 0; 37 | 38 | if (*head == NULL || dlistint_len(temp) < index + 1) 39 | return (-1); 40 | 41 | if (!index) 42 | { 43 | (*head) = temp->next; 44 | if (temp->next) 45 | temp->next->prev = NULL; 46 | temp->next = NULL; 47 | free(temp); 48 | return (1); 49 | } 50 | 51 | while (i < index) 52 | { 53 | temp = temp->next; 54 | i++; 55 | } 56 | 57 | temp->prev->next = temp->next; 58 | if (temp->next) 59 | temp->next->prev = temp->prev; 60 | free(temp); 61 | 62 | return (1); 63 | } 64 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x16. C - Doubly linked lists 2 | 3 | ## Table of contents 4 | Files | Description 5 | ----- | ----------- 6 | [lists.h](./lists.h) | Header file containing all the function prototypes of this project, and the definition of the struct 7 | [0-print_dlistint.c](./0-print_dlistint.c) | C function that prints all the elements of a dlistint_t list 8 | [1-dlistint_len.c](./1-dlistint_len.c) | C function that returns the number of elements in a linked dlistint_t list 9 | [2-add_dnodeint.c](./2-add_dnodeint.c) | C function that adds a new node at the beginning of a dlistint_t list 10 | [3-add_dnodeint_end.c](./) | C function that adds a new node at the end of a dlistint_t list 11 | [4-free_dlistint.c](./4-free_dlistint.c) | C function that free a dlistint_t list 12 | [5-get_dnodeint.c](./5-get_dnodeint.c) | C function that returns the nth node of a dlistint_t linked list 13 | [6-sum_dlistint.c](./6-sum_dlistint.c) | C function that returns the sum of all the data (n) of a dlistint_t linked list 14 | [7-insert_dnodeint.c](./7-insert_dnodeint.c) | C function that inserts a new node at a given position 15 | [8-delete_dnodeint.c](./8-delete_dnodeint.c) | C function that deletes the node at index index of a dlistint_t linked list 16 | [100-password](./100-password) | Password for a compiled python program 17 | [102-result](./102-result) | Biggest palindrome number made from the product of two 3-digit numbers 18 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * for Holberton project 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c -fpic *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hnryjsph/alx-low_level_programming/2fba713144c783d137b1571986a63c7045ec459e/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/lroudge/holbertonschool-low_level_programming/raw/master/0x17-dynamic_libraries/libmask.so 3 | export LD_PRELOAD=/tmp/libmask.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x17. C - Dynamic libraries 2 | 3 | ## Table of contents 4 | Files | Description 5 | ----- | ----------- 6 | [libholberton.so](./libholberton.so) | C dynamic library containing the function definitions 7 | [holberton.h](./holberton.h) | Header files containing the function prototypes 8 | [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 9 | [100-operations.so](./100-operations.so) | C dynamic library that contains C functions that can be called from Python 10 | [libmask.so](./libmask.so) | C dynamic library to inject in a giga million program 11 | [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 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hnryjsph/alx-low_level_programming/2fba713144c783d137b1571986a63c7045ec459e/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libmask.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hnryjsph/alx-low_level_programming/2fba713144c783d137b1571986a63c7045ec459e/0x18-dynamic_libraries/libmask.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_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 26 | -------------------------------------------------------------------------------- /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 of the hash table 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 *ht; 12 | 13 | if (size == 0) 14 | return (NULL); 15 | 16 | ht = malloc(sizeof(hash_table_t)); 17 | if (!ht) 18 | return (NULL); 19 | 20 | ht->size = size; 21 | ht->array = calloc((size_t)ht->size, sizeof(hash_node_t *)); 22 | if (ht->array == NULL) 23 | return (NULL); 24 | 25 | return (ht); 26 | } 27 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - hash function implementing the djb2 algorithm 5 | * @str: string to hash 6 | * 7 | * Return: hash index corresponding to the string 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/100-sorted_hash_table.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * shash_table_create - creates a hash table 5 | * @size: size of the array of linked list in the table 6 | * 7 | * Return: shash_table_t struct 8 | */ 9 | shash_table_t *shash_table_create(unsigned long int size) 10 | { 11 | shash_table_t *sht; 12 | 13 | if (size == 0) 14 | return (NULL); 15 | 16 | sht = calloc(1, sizeof(shash_table_t)); 17 | if (!sht) 18 | return (NULL); 19 | 20 | sht->size = size; 21 | sht->array = calloc((size_t)sht->size, sizeof(shash_node_t *)); 22 | if (sht->array == NULL) 23 | { 24 | free(sht); 25 | return (NULL); 26 | } 27 | 28 | return (sht); 29 | } 30 | 31 | /** 32 | * shash_table_set - adds an element to the hash table 33 | * @ht: hash table to add the element to 34 | * @key: key of the element, will give the index in the array 35 | * @value: value of the element to store in the array 36 | * 37 | * Return: 1 on success, 0 otherwise 38 | */ 39 | int shash_table_set(shash_table_t *ht, const char *key, const char *value) 40 | { 41 | unsigned long int index; 42 | shash_node_t *new; 43 | 44 | if (!ht || !key || !strcmp(key, "") || !value) 45 | return (0); 46 | 47 | index = key_index((unsigned char *)key, ht->size); 48 | 49 | if (check_key_s(ht->array[index], key)) 50 | return (replace_value_s(&ht->array[index], key, value)); 51 | new = add_node_s(&ht->array[index], key, value); 52 | if (!new) 53 | return (0); 54 | 55 | insert_sort(new, ht); 56 | return (1); 57 | } 58 | 59 | /** 60 | * insert_sort - inserts a node in a sorted doubly 61 | * linked list 62 | * @node: shash_node_t to insert 63 | * @ht: pointer to the hash table to insert it into 64 | */ 65 | void insert_sort(shash_node_t *node, shash_table_t *ht) 66 | { 67 | shash_node_t *head = ht->shead; 68 | 69 | if (!head || strcmp(node->key, head->key) < 0) 70 | { 71 | ht->shead = node; 72 | if (!head) 73 | ht->stail = node; 74 | else 75 | { 76 | node->snext = head; 77 | head->sprev = node; 78 | } 79 | return; 80 | } 81 | 82 | while (head->snext && strcmp(node->key, head->snext->key) >= 0) 83 | head = head->snext; 84 | 85 | node->sprev = head; 86 | if (!head->snext) 87 | ht->stail = node; 88 | else 89 | head->snext->sprev = node; 90 | node->snext = head->snext; 91 | head->snext = node; 92 | } 93 | /** 94 | * shash_table_get - retrieves a value associated with a key 95 | * @ht: hash table to look into 96 | * @key: key of the value to retrieve 97 | * 98 | * Return: the value associated with the element, 99 | * or NULL if key couldn’t be found 100 | */ 101 | char *shash_table_get(const shash_table_t *ht, const char *key) 102 | { 103 | unsigned long int index; 104 | shash_node_t *node = NULL; 105 | 106 | if (!ht || !key || !strcmp(key, "")) 107 | return (NULL); 108 | 109 | index = key_index((const unsigned char *)key, ht->size); 110 | node = (ht->array)[index]; 111 | 112 | while (node) 113 | { 114 | if (!strcmp(node->key, key)) 115 | return (node->value); 116 | node = node->next; 117 | } 118 | 119 | return (NULL); 120 | } 121 | 122 | /** 123 | * shash_table_print - prints a hash table 124 | * @ht: hash table to print 125 | */ 126 | void shash_table_print(const shash_table_t *ht) 127 | { 128 | shash_node_t *head = NULL; 129 | 130 | if (!ht) 131 | return; 132 | 133 | head = ht->shead; 134 | 135 | printf("{"); 136 | while (head) 137 | { 138 | printf("'%s': '%s'", head->key, head->value); 139 | if (head->snext) 140 | printf(", "); 141 | head = head->snext; 142 | } 143 | 144 | printf("}\n"); 145 | } 146 | /** 147 | * shash_table_print_rev - prints a hash table in reverse 148 | * @ht: hash table to print 149 | */ 150 | void shash_table_print_rev(const shash_table_t *ht) 151 | { 152 | shash_node_t *tail = NULL; 153 | 154 | if (!ht) 155 | return; 156 | 157 | tail = ht->stail; 158 | 159 | printf("{"); 160 | while (tail) 161 | { 162 | printf("'%s': '%s'", tail->key, tail->value); 163 | if (tail->sprev) 164 | printf(", "); 165 | tail = tail->sprev; 166 | } 167 | 168 | printf("}\n"); 169 | } 170 | 171 | /** 172 | * shash_table_delete - deletes a hash table 173 | * @ht: hash table to be deleted 174 | */ 175 | void shash_table_delete(shash_table_t *ht) 176 | { 177 | if (!ht) 178 | return; 179 | 180 | free_list_s(ht->shead); 181 | free(ht->array); 182 | free(ht); 183 | } 184 | 185 | /** 186 | * replace_value_s - replaces the value at a pre-existing key 187 | * @ht: double pointer to the shash_node_t list 188 | * @key: new key to add in the node 189 | * @value: value to add in the node 190 | * 191 | * Return: 1 on success, 0 on failure 192 | */ 193 | int replace_value_s(shash_node_t **ht, const char *key, const char *value) 194 | { 195 | shash_node_t *temp = *ht; 196 | 197 | while (temp && strcmp(temp->key, key)) 198 | temp = temp->next; 199 | 200 | free(temp->value); 201 | temp->value = strdup(value); 202 | if (!temp->value) 203 | return (0); 204 | return (1); 205 | } 206 | 207 | /** 208 | * check_key_s - checks if a key exists in a hash table 209 | * @ht: pointer to the shash_node_t list 210 | * @key: key to look for 211 | * 212 | * Return: 1 if the key is found, 0 otherwise 213 | */ 214 | int check_key_s(shash_node_t *ht, const char *key) 215 | { 216 | while (ht) 217 | { 218 | if (!strcmp(ht->key, key)) 219 | return (1); 220 | ht = ht->next; 221 | } 222 | 223 | return (0); 224 | } 225 | 226 | /** 227 | * add_node_s - adds a new node at the beginning of a linked list 228 | * @head: double pointer to the shash_node_t list 229 | * @key: new key to add in the node 230 | * @value: value to add in the node 231 | * 232 | * Return: the address of the new element, or NULL if it fails 233 | */ 234 | shash_node_t *add_node_s(shash_node_t **head, const char *key, 235 | const char *value) 236 | { 237 | shash_node_t *new; 238 | 239 | new = calloc(1, sizeof(shash_node_t)); 240 | if (!new) 241 | return (NULL); 242 | 243 | new->key = strdup(key); 244 | if (!new->key) 245 | { 246 | free(new); 247 | return (NULL); 248 | } 249 | new->value = strdup(value); 250 | if (!new->value) 251 | { 252 | free(new); 253 | free(new->key); 254 | return (NULL); 255 | } 256 | 257 | if (*head == NULL) 258 | (*head) = new; 259 | else 260 | { 261 | new->next = (*head); 262 | (*head) = new; 263 | } 264 | 265 | return (*head); 266 | } 267 | 268 | /** 269 | * free_list_s - frees a linked list 270 | * @head: shash_node_t list to be freed 271 | */ 272 | void free_list_s(shash_node_t *head) 273 | { 274 | shash_node_t *temp; 275 | 276 | while (head) 277 | { 278 | temp = head->snext; 279 | free(head->key); 280 | free(head->value); 281 | free(head); 282 | head = temp; 283 | } 284 | } 285 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - gives the index of a key in a hash table 5 | * @key: the key to find the index of 6 | * @size: size of the array of indexes 7 | * 8 | * Return: the index at which the key/value pair 9 | * should be stored in the array of the hash table 10 | */ 11 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 12 | { 13 | unsigned long int hash; 14 | 15 | if (size == 0) 16 | return (0); 17 | 18 | hash = hash_djb2(key); 19 | 20 | return (hash % size); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * replace_value - replaces the value at a pre-existing key 5 | * @ht: double pointer to the hash_node_t list 6 | * @key: new key to add in the node 7 | * @value: value to add in the node 8 | */ 9 | void replace_value(hash_node_t **ht, const char *key, const char *value) 10 | { 11 | hash_node_t *temp = *ht; 12 | 13 | while (temp && strcmp(temp->key, key)) 14 | temp = temp->next; 15 | 16 | free(temp->value); 17 | temp->value = strdup(value); 18 | } 19 | 20 | /** 21 | * check_key - checks if a key exists in a hash table 22 | * @ht: pointer to the hash_node_t list 23 | * @key: key to look for 24 | * 25 | * Return: 1 if the key is found, 0 otherwise 26 | */ 27 | int check_key(hash_node_t *ht, const char *key) 28 | { 29 | while (ht) 30 | { 31 | if (!strcmp(ht->key, key)) 32 | return (1); 33 | ht = ht->next; 34 | } 35 | 36 | return (0); 37 | } 38 | 39 | /** 40 | * add_node - adds a new node at the beginning of a linked list 41 | * @head: double pointer to the hash_node_t list 42 | * @key: new key to add in the node 43 | * @value: value to add in the node 44 | * 45 | * Return: the address of the new element, or NULL if it fails 46 | */ 47 | hash_node_t *add_node(hash_node_t **head, const char *key, const char *value) 48 | { 49 | hash_node_t *new; 50 | 51 | new = malloc(sizeof(hash_node_t)); 52 | if (!new) 53 | return (NULL); 54 | 55 | new->key = strdup(key); 56 | new->value = strdup(value); 57 | 58 | if (*head == NULL) 59 | { 60 | (*head) = new; 61 | new->next = NULL; 62 | } else 63 | { 64 | new->next = (*head); 65 | (*head) = new; 66 | } 67 | 68 | return (*head); 69 | } 70 | 71 | /** 72 | * hash_table_set - adds an element to the hash table 73 | * @ht: hash table to add the element to 74 | * @key: key of the element, will give the index in the array 75 | * @value: value of the element to store in the array 76 | * 77 | * Return: 1 on success, 0 otherwise 78 | */ 79 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 80 | { 81 | unsigned long int index; 82 | 83 | if (!ht || !key || !strcmp(key, "") || !value) 84 | return (0); 85 | 86 | index = key_index((unsigned char *)key, ht->size); 87 | 88 | if (check_key(ht->array[index], key)) 89 | { 90 | replace_value(&ht->array[index], key, value); 91 | return (1); 92 | } 93 | add_node(&ht->array[index], key, value); 94 | if (&ht->array[index] == NULL) 95 | return (0); 96 | return (1); 97 | } 98 | -------------------------------------------------------------------------------- /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: hash table to look into 6 | * @key: key of the value to retrieve 7 | * 8 | * Return: the value associated with the element, 9 | * or NULL if key couldn’t be found 10 | */ 11 | char *hash_table_get(const hash_table_t *ht, const char *key) 12 | { 13 | unsigned long int index; 14 | hash_node_t *node = NULL; 15 | 16 | if (!ht || !key || !strcmp(key, "")) 17 | return (NULL); 18 | 19 | index = key_index((const unsigned char *)key, ht->size); 20 | node = (ht->array)[index]; 21 | 22 | while (node) 23 | { 24 | if (!strcmp(node->key, (char *)key)) 25 | return (node->value); 26 | node = node->next; 27 | } 28 | 29 | return (NULL); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * print_list - prints all the elements of a linked list 5 | * @h: pointer to the hash_node_t list to print 6 | */ 7 | void print_list(hash_node_t *h) 8 | { 9 | while (h) 10 | { 11 | printf("'%s': '%s'", h->key, h->value); 12 | if (h->next) 13 | printf(", "); 14 | h = h->next; 15 | } 16 | } 17 | 18 | /** 19 | * hash_table_print - prints a hash table 20 | * @ht: hash table to print 21 | */ 22 | void hash_table_print(const hash_table_t *ht) 23 | { 24 | unsigned long int i; 25 | hash_node_t *node = NULL; 26 | char *last_key = NULL; 27 | unsigned long int index; 28 | 29 | if (!ht) 30 | return; 31 | 32 | for (i = 0; i < ht->size; i++) 33 | { 34 | if (ht->array[i] != NULL) 35 | node = ht->array[i]; 36 | } 37 | 38 | printf("{"); 39 | 40 | if (node) 41 | { 42 | last_key = node->key; 43 | index = key_index((const unsigned char *)last_key, ht->size); 44 | for (i = 0; i < ht->size; i++) 45 | { 46 | print_list(ht->array[i]); 47 | if (ht->array[i] && i < index) 48 | printf(", "); 49 | } 50 | } 51 | 52 | printf("}\n"); 53 | } 54 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * free_list - frees a linked list 5 | * @head: hash_node_t list to be freed 6 | */ 7 | void free_list(hash_node_t *head) 8 | { 9 | hash_node_t *temp; 10 | 11 | while (head) 12 | { 13 | temp = head->next; 14 | free(head->key); 15 | free(head->value); 16 | free(head); 17 | head = temp; 18 | } 19 | } 20 | 21 | /** 22 | * hash_table_delete - deletes a hash table 23 | * @ht: hash table to be deleted 24 | */ 25 | void hash_table_delete(hash_table_t *ht) 26 | { 27 | unsigned long int i; 28 | 29 | if (!ht) 30 | return; 31 | 32 | for (i = 0; i < ht->size; i++) 33 | free_list(ht->array[i]); 34 | free(ht->array); 35 | free(ht); 36 | } 37 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | # 0x19. C - Hash tables 2 | 3 | ## Description 4 | This Holberton School project is about hash tables in C. How to create and use one, how to deal with collisions. 5 | It also covers what makes a good hash function and what are the most common use for hash tables. 6 | 7 | ## Table of contents 8 | Files | Desciption 9 | ----- | ---------- 10 | [hash_tables.h](./hash_tables.h) | Header file containing the function prototypes and structs for this project 11 | [0-hash_table_create.c](./0-hash_table_create.c) | C function that creates a hash table 12 | [1-djb2.c](./1-djb2.c) | C hash function implementing the djb2 algorithm 13 | [2-key_index.c](./2-key_index.c) | C function that gives you the index of a key 14 | [3-hash_table_set.c](./3-hash_table_set.c) | C function that adds an element to the hash table 15 | [4-hash_table_get.c](./4-hash_table_get.c) | C function that retrieves a value associated with a key 16 | [5-hash_table_print.c](./5-hash_table_print.c) | C function that prints a hash table 17 | [6-hash_table_delete.c](./6-hash_table_delete.c) | C function that deletes a hash table 18 | [100-sorted_hash_table.c](./100-sorted_hash_table.c) | C functions for a sorted hash table 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef HASH_TABLES_H 2 | #define HASH_TABLES_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /** 10 | * struct hash_node_s - Node of a hash table 11 | * 12 | * @key: The key, string 13 | * The key is unique in the HashTable 14 | * @value: The value corresponding to a key 15 | * @next: A pointer to the next node of the List 16 | */ 17 | typedef struct hash_node_s 18 | { 19 | char *key; 20 | char *value; 21 | struct hash_node_s *next; 22 | } hash_node_t; 23 | 24 | /** 25 | * struct hash_table_s - Hash table data structure 26 | * 27 | * @size: The size of the array 28 | * @array: An array of size @size 29 | * Each cell of this array is a pointer to the first node of a linked list, 30 | * because we want our HashTable to use a Chaining collision handling 31 | */ 32 | typedef struct hash_table_s 33 | { 34 | unsigned long int size; 35 | hash_node_t **array; 36 | } hash_table_t; 37 | 38 | hash_table_t *hash_table_create(unsigned long int size); 39 | unsigned long int hash_djb2(const unsigned char *str); 40 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 41 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 42 | char *hash_table_get(const hash_table_t *ht, const char *key); 43 | void hash_table_print(const hash_table_t *ht); 44 | void hash_table_delete(hash_table_t *ht); 45 | 46 | void replace_value(hash_node_t **ht, const char *key, const char *value); 47 | int check_key(hash_node_t *ht, const char *key); 48 | hash_node_t *add_node(hash_node_t **head, const char *key, const char *value); 49 | void print_list(hash_node_t *h); 50 | void free_list(hash_node_t *head); 51 | 52 | /** 53 | * struct shash_node_s - Node of a sorted hash table 54 | * 55 | * @key: The key, string 56 | * The key is unique in the HashTable 57 | * @value: The value corresponding to a key 58 | * @next: A pointer to the next node of the List 59 | * @sprev: A pointer to the previous element of the sorted linked list 60 | * @snext: A pointer to the next element of the sorted linked list 61 | */ 62 | typedef struct shash_node_s 63 | { 64 | char *key; 65 | char *value; 66 | struct shash_node_s *next; 67 | struct shash_node_s *sprev; 68 | struct shash_node_s *snext; 69 | } shash_node_t; 70 | 71 | /** 72 | * struct shash_table_s - Sorted hash table data structure 73 | * 74 | * @size: The size of the array 75 | * @array: An array of size @size 76 | * Each cell of this array is a pointer to the first node of a linked list, 77 | * because we want our HashTable to use a Chaining collision handling 78 | * @shead: A pointer to the first element of the sorted linked list 79 | * @stail: A pointer to the last element of the sorted linked list 80 | */ 81 | typedef struct shash_table_s 82 | { 83 | unsigned long int size; 84 | shash_node_t **array; 85 | shash_node_t *shead; 86 | shash_node_t *stail; 87 | } shash_table_t; 88 | 89 | shash_table_t *shash_table_create(unsigned long int size); 90 | int shash_table_set(shash_table_t *ht, const char *key, const char *value); 91 | char *shash_table_get(const shash_table_t *ht, const char *key); 92 | void shash_table_print(const shash_table_t *ht); 93 | void shash_table_print_rev(const shash_table_t *ht); 94 | void shash_table_delete(shash_table_t *ht); 95 | 96 | int replace_value_s(shash_node_t **ht, const char *key, const char *value); 97 | int check_key_s(shash_node_t *ht, const char *key); 98 | shash_node_t *add_node_s(shash_node_t **head, 99 | const char *key, const char *value); 100 | void insert_sort(shash_node_t *node, shash_table_t *ht); 101 | void free_list_s(shash_node_t *head); 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | all: $(SRC) 4 | $(CC) $(SRC) -o school 5 | -------------------------------------------------------------------------------- /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) $(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 | 8 | The grid represents water by 0 and land by 1. 9 | 10 | Args: 11 | grid (list): A list of list of integers representing an island. 12 | Returns: 13 | The perimeter of the island defined in grid. 14 | """ 15 | width = len(grid[0]) 16 | height = len(grid) 17 | edges = 0 18 | size = 0 19 | 20 | for i in range(height): 21 | for j in range(width): 22 | if grid[i][j] == 1: 23 | size += 1 24 | if (j > 0 and grid[i][j - 1] == 1): 25 | edges += 1 26 | if (i > 0 and grid[i - 1][j] == 1): 27 | edges += 1 28 | return size * 4 - edges * 2 29 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # C - Makefiles 2 | 3 | In this project, I practiced writing Makefiles. 4 | 5 | ## Tests :heavy_check_mark: 6 | 7 | * [tests](./tests): Folder of test files. 8 | 9 | ## Helper Files :raised_hands: 10 | 11 | * [school.c](./school.c): C function that displays a seahorse in text. 12 | Used for Makefile practice purposes throughout project. 13 | 14 | * [main.c](./main.c): Main C function that runs the function defined in 15 | [school.c](./school.c). 16 | 17 | ## Header File :file_folder: 18 | 19 | * [m.h](./m.h): Header file defining the function prototype used in `school.c`. 20 | 21 | ## Tasks :page_with_curl: 22 | 23 | * **0. make -f 0-Makefile** 24 | * [0-Makefile](./0-Makefile): Makefile that creates an executable `school` based on 25 | [school.c](./school.c) and [main.c](./main.c). Includes: 26 | * Rule `all` that builds the executable. 27 | 28 | * **1. make -f 1-Makefile** 29 | * [1-Makefile](./1-Makefile): Makefile that creates an executable `school` based on 30 | [school.c](./school.c) and [main.c](./main.c). Builds on [0-Makefile](./0-Makefile) 31 | with: 32 | * Variable `CC` that defines the compiler to be used. 33 | * Variable `SRC` that defines the `.c` files to compile. 34 | * The `all` rule only recompiles updated source files. 35 | 36 | * **2. make -f 2-Makefile** 37 | * [2-Makefile](./2-Makefile): Makefile that creates an executable `school` based on 38 | [school.c](./school.c) and [main.c](./main.c). Builds on [1-Makefile](./1-Makefile) 39 | with: 40 | * Variable `OBJ` that defines the `.o` files to compile. 41 | * Variable `NAME` that defines the name of the executable. 42 | 43 | * **3. make -f 3-Makefile** 44 | * [3-Makefile](./3-Makefile): Makefile that creates an executable `school` based on 45 | [school.c](./school.c) and [main.c](./main.c). Builds on [2-Makefile](./2-Makefile) 46 | with: 47 | * Rule `clean` that deletes all Emacs/Vim temporary files as well as the 48 | executable. 49 | * Rule `oclean` that deletes the object files. 50 | * Rule `fclean` that deltes all of the temporary files, executable, and 51 | object files. 52 | * Rule `re` that forces recompilation of all source files. 53 | * Variable `RM` that defines the command to delete files. 54 | 55 | * **4. A complete Makefile** 56 | * [4-Makefile](./4-Makefile): Makefile that creates an executable `school` based on 57 | [school.c](./school.c) and [main.c](./main.c). Builds on [3-Makefile](./3-Makefile) 58 | with: 59 | * Variable `CFLAGS` that defines the compiler flags `-Wall -Werror -Wextra 60 | -pedantic`. 61 | 62 | * **5. Island Perimeter** 63 | * [5-island_perimeter.py](./5-island_perimeter.py): Python function that returns the 64 | perimeter of an island defined in a grid. 65 | * Prototype: `def island_perimeter(grid):` 66 | * The parameter `grid` is a list of a list of integers. 67 | * Water is represented by `0`. 68 | * Land is represented by `1`. 69 | * Each element of the lists represents a cell square of side length 1. 70 | * Grid cells are connected horizontally/verticaly (not diagonally). 71 | * The grid is rectangular, with a width and height not exceeding 100. 72 | * The grid is completely surrounded by water, and there is either exactly 73 | one island or nothing. 74 | * The island does not contain lakes (water inside disconnected from 75 | surrounding land). 76 | 77 | * **6. make -f 100-Makefile** 78 | * [100-Makefile](./100-Makefile): Makefile that creates an executable `school` based on 79 | [school.c](./school.c) and [main.c](./main.c). Builds on [4-Makefile](./4-Makefile) 80 | with: 81 | * Does not define the variable `RM`. 82 | * Never uses the string `$(CFLAGS)`. 83 | * Does not compile if the header `m.h` is missing. 84 | * Works even if there are existing files of the same name as any of the 85 | Makefile rules in the current directory. 86 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_school 5 | * 6 | * Return: Nothing 7 | */ 8 | void print_school(void) 9 | { 10 | printf("j#0000000000000000000000000000000000000\n"); 11 | printf("j#000000000000000000@Q**g00000000000000\n"); 12 | printf("j#0000000000000000*]++]4000000000000000\n"); 13 | printf("j#000000000000000k]++]++*N#000000000000\n"); 14 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 15 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 16 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 17 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 18 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 19 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 20 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 21 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 22 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 23 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 24 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 25 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 26 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 27 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 28 | printf("j#00000000000w]++++]*0##000000000000000\n"); 29 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 30 | printf("j#00000000000000000we]+]Q00000000000000\n"); 31 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 32 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 33 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 34 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-makefiles/tests/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /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 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * binary_search - searches for a value in an array of 8 | * integers using the Binary 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 binary_search(int *array, size_t size, int value) 17 | { 18 | if (!array || size == 0) 19 | return (-1); 20 | 21 | return (help_binary(array, value, 0, size - 1)); 22 | } 23 | 24 | /** 25 | * help_binary - searches for a value in an array of 26 | * integers using recursion 27 | * @array: array to search the value in 28 | * @value: value to look for 29 | * @lo: index of the low bound 30 | * @hi: index of the high bound 31 | * 32 | * Return: the index of the found value, 33 | * or -1 if not found 34 | */ 35 | int help_binary(int *array, int value, size_t lo, size_t hi) 36 | { 37 | size_t mid; 38 | 39 | array_print(array, lo, hi); 40 | if (hi == lo && array[lo] != value) 41 | return (-1); 42 | 43 | mid = ((hi - lo) / 2) + lo; 44 | if (array[mid] == value) 45 | return (mid); 46 | if (array[mid] < value) 47 | return (help_binary(array, value, mid + 1, hi)); 48 | if (array[mid] > value) 49 | return (help_binary(array, value, lo, mid - 1)); 50 | return (-1); 51 | } 52 | 53 | /** 54 | * array_print - prints an array 55 | * @array: array to print 56 | * @lo: index of the low bound 57 | * @hi: index of the high bound 58 | */ 59 | void array_print(int *array, size_t lo, size_t hi) 60 | { 61 | size_t i; 62 | 63 | printf("Searching in array: "); 64 | for (i = lo; i <= hi; i++) 65 | { 66 | printf("%d", array[i]); 67 | if (i < hi) 68 | printf(", "); 69 | } 70 | printf("\n"); 71 | } 72 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - searches for a value in a sorted array of integers 6 | * using the Jump search algorithm 7 | * @array: array to look into 8 | * @size: size of the array 9 | * @value: value to look for 10 | * 11 | * Return: index of the value found, or -1 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | size_t start, end, step; 16 | 17 | if (!array || size == 0) 18 | return (-1); 19 | step = sqrt(size); 20 | start = 0, end = step; 21 | 22 | while (start < size) 23 | { 24 | printf("Value checked array[%lu] = [%d]\n", start, array[start]); 25 | if (end < size) 26 | { 27 | if (array[start] <= value && value <= array[end]) 28 | { 29 | printf("Value found between indexes [%lu] and [%lu]\n", start, end); 30 | break; 31 | } 32 | } else 33 | { 34 | if (array[start] <= value) 35 | { 36 | printf("Value found between indexes [%lu] and [%lu]\n", start, end); 37 | break; 38 | } 39 | } 40 | start = end; 41 | end = start + step; 42 | } 43 | 44 | while (start <= end) 45 | { 46 | if (start == size) 47 | return (-1); 48 | printf("Value checked array[%lu] = [%d]\n", start, array[start]); 49 | if (array[start] == value) 50 | return (start); 51 | start++; 52 | } 53 | 54 | return (-1); 55 | } 56 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * exponential_search - searches for a value in an array with 5 | * the exponential search algorithm 6 | * @array: array to search the value in 7 | * @size: size of the array 8 | * @value: value to look for 9 | * 10 | * Return: the index of the found value, 11 | * or -1 if not found 12 | */ 13 | int exponential_search(int *array, size_t size, int value) 14 | { 15 | size_t start, end, x; 16 | 17 | if (!array || size == 0) 18 | return (-1); 19 | 20 | end = 1; 21 | start = 1; 22 | while (end < size) 23 | { 24 | if (value < array[end]) 25 | break; 26 | printf("Value checked array[%lu] = [%d]\n", end, array[end]); 27 | start = end; 28 | end = end * 2; 29 | } 30 | x = end < size - 1 ? end : size - 1; 31 | printf("Value found between indexes [%lu] and [%lu]\n", start, x); 32 | return (help_binary(array, value, end / 2, x)); 33 | } 34 | 35 | /** 36 | * help_binary - searches for a value in an array of 37 | * integers using recursion 38 | * @array: array to search the value in 39 | * @value: value to look for 40 | * @lo: index of the low bound 41 | * @hi: index of the high bound 42 | * 43 | * Return: the index of the found value, 44 | * or -1 if not found 45 | */ 46 | int help_binary(int *array, int value, size_t lo, size_t hi) 47 | { 48 | size_t mid; 49 | 50 | array_print(array, lo, hi); 51 | if (hi == lo && array[lo] != value) 52 | return (-1); 53 | 54 | mid = ((hi - lo) / 2) + lo; 55 | if (array[mid] == value) 56 | return (mid); 57 | if (array[mid] > value) 58 | return (help_binary(array, value, lo, mid - 1)); 59 | if (array[mid] < value) 60 | return (help_binary(array, value, mid + 1, hi)); 61 | return (-1); 62 | } 63 | 64 | /** 65 | * array_print - prints an array 66 | * @array: array to print 67 | * @lo: index of the low bound 68 | * @hi: index of the high bound 69 | */ 70 | void array_print(int *array, size_t lo, size_t hi) 71 | { 72 | size_t i; 73 | 74 | printf("Searching in array: "); 75 | for (i = lo; i <= hi; i++) 76 | { 77 | printf("%d", array[i]); 78 | if (i < hi) 79 | printf(", "); 80 | } 81 | printf("\n"); 82 | } 83 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * advanced_binary - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * @array: array to search the value in 7 | * @size: size of the array 8 | * @value: value to look for 9 | * 10 | * Return: the index of the found value, 11 | * or -1 if not found 12 | */ 13 | int advanced_binary(int *array, size_t size, int value) 14 | { 15 | if (!array || size == 0) 16 | return (-1); 17 | 18 | return (help_binary(array, value, 0, size - 1)); 19 | } 20 | 21 | /** 22 | * help_binary - searches for a value in an array of 23 | * integers using recursion 24 | * @array: array to search the value in 25 | * @value: value to look for 26 | * @lo: index of the low bound 27 | * @hi: index of the high bound 28 | * 29 | * Return: the index of the found value, 30 | * or -1 if not found 31 | */ 32 | int help_binary(int *array, int value, size_t lo, size_t hi) 33 | { 34 | size_t mid; 35 | 36 | array_print(array, lo, hi); 37 | if (hi == lo && array[lo] != value) 38 | return (-1); 39 | 40 | mid = ((hi - lo) / 2) + lo; 41 | if (array[mid] == value) 42 | return (mid); 43 | if (array[mid] < value) 44 | return (help_binary(array, value, mid + 1, hi)); 45 | if (array[mid] > value) 46 | return (help_binary(array, value, lo, mid - 1)); 47 | return (-1); 48 | } 49 | 50 | /** 51 | * array_print - prints an array 52 | * @array: array to print 53 | * @lo: index of the low bound 54 | * @hi: index of the high bound 55 | */ 56 | void array_print(int *array, size_t lo, size_t hi) 57 | { 58 | size_t i; 59 | 60 | printf("Searching in array: "); 61 | for (i = lo; i <= hi; i++) 62 | { 63 | printf("%d", array[i]); 64 | if (i < hi) 65 | printf(", "); 66 | } 67 | printf("\n"); 68 | } 69 | -------------------------------------------------------------------------------- /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/README.md: -------------------------------------------------------------------------------- 1 | # 0x1E. C - Search Algorithms 2 | 3 | ## Table of contents 4 | Files | Description 5 | ----- | ----------- 6 | [0-linear.c](./0-linear.c) | C function that searches for a value in an array of integers using the Linear search algorithm 7 | [1-binary.c](./1-binary.c) | C function that searches for a value in a sorted array of integers using the Binary search algorithm 8 | [2-O](./2-O) | What is the time complexity (worst case) of a linear search in an array of size n? 9 | [3-O](./3-O) | What is the space complexity (worst case) of an iterative linear search algorithm in an array of size n? 10 | [4-O](./4-O) | What is the time complexity (worst case) of a binary search in an array of size n? 11 | [5-O](./5-O) | What is the space complexity (worst case) of a binary search in an array of size n? 12 | [6-O](./6-O) | What is the space complexity of this function / algorithm? 13 | [100-jump.c](./100-jump.c) | C function that searches for a value in a sorted array of integers using the Jump search algorithm 14 | [101-O](./101-O) | What is the time complexity (average case) of a jump search in an array of size n, using step = sqrt(n)? 15 | [107-O](./107-O) | What is the time complexity (average case) of a jump search in a singly linked list of size n, using step = sqrt(n)? 16 | [108-O](./108-O) | What is the time complexity (average case) of a jump search in a skip list of size n, with an express lane using step = sqrt(n)? 17 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS 2 | #define SEARCH_ALGOS 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int linear_search(int *array, size_t size, int value); 9 | int binary_search(int *array, size_t size, int value); 10 | int help_binary(int *array, int value, size_t lo, size_t hi); 11 | void array_print(int *array, size_t lo, size_t hi); 12 | int jump_search(int *array, size_t size, int value); 13 | int advanced_binary(int *array, size_t size, int value); 14 | int exponential_search(int *array, size_t size, int value); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C programming 2 | --------------------------------------------------------------------------------