├── 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_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── .8-24_hours.c.swp ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md ├── _putchar.c ├── main.c └── main.h ├── 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 ├── _putchar.c └── main.h ├── 0x09-static_libraries ├── .gitignore ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── 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 ├── _putchar.c └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 101-mul.o ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── _putchar.c └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── #function_pointers.h# ├── 0-main.c ├── 0-print_name.c ├── 1-array_iterator.c ├── 1-main.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 2-main.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md ├── a ├── b ├── c ├── calc ├── function_pointers.h └── main ├── 0x10-variadic_functions ├── 0-main.c ├── 0-sum_them_all.c ├── 1-main.c ├── 1-print_numbers.c ├── 100-hello_holberton.asm ├── 2-main.c ├── 2-print_strings.c ├── 3-main.c ├── 3-print_all.c ├── README.md ├── a ├── b ├── b.c ├── c ├── d ├── p_functions.c └── 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 ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 103-find_loop.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 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 ├── 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 ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md ├── keygen5 └── lists.h ├── 0x18-dynamic_libraries ├── 0-isupper.c ├── 0-memset.c ├── 0-strcat.c ├── 1-create_dynamic_lib.sh ├── 1-isdigit.c ├── 1-memcpy.c ├── 1-strncat.c ├── 100-atoi.c ├── 100-operations.so ├── 101-make_me_win.sh ├── 101-md5_gm ├── 2-strchr.c ├── 2-strlen.c ├── 2-strncpy.c ├── 3-islower.c ├── 3-puts.c ├── 3-strcmp.c ├── 3-strspn.c ├── 4-isalpha.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 6-abs.c ├── 9-strcpy.c ├── README.md ├── _putchar.c ├── gm ├── liball.so ├── libdynamic.so ├── libtest.so ├── main.c ├── main.h ├── test.c └── test.o ├── 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 ├── 0x1E-search_algorithms ├── 0-linear.c ├── 0-main.c ├── 1-binary.c ├── 1-main.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── 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 | 3 | /** 4 | * main - prints and that piece of art is useful" - Dora Korpar, 2015-10-19 5 | * 6 | * Return: 1 (Success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | write(1, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 12 | 13 | return (1); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints exactly "Programming is like building a multilingual puzzle. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | puts("\"Programming is like building a multilingual puzzle"); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints with proper grammar, but the outcome is a piece of art, 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | printf("with proper grammar, but the outcome is a piece of art,\n"); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the size of various types. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | printf("Size of a char: %i byte(s)\n", sizeof(char)); 12 | printf("Size of an int: %i byte(s)\n", sizeof(int)); 13 | printf("Size of a long int: %i byte(s)\n", sizeof(long int)); 14 | printf("Size of a long long int: %i byte(s)\n", sizeof(long long int)); 15 | printf("Size of a float: %i byte(s)\n", sizeof(float)); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - entry point 6 | * Description: prints if a number is positive or negative. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | if (n > 0) 16 | { 17 | printf("%d is positive\n", n); 18 | } 19 | else if (n == 0) 20 | { 21 | printf("%d is zero\n", n); 22 | } 23 | else if (n < 0) 24 | { 25 | printf("%d is negative\n", n); 26 | } 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * Description: checks on the last digit of the assigned var 7 | * Return: Always zero (success) 8 | */ 9 | int main(void) 10 | { 11 | int n, m; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | m = n % 10; 16 | if (m > 5) 17 | { 18 | printf("Last digit of %d is %d and is greater than 5\n", n, m); 19 | } 20 | else if (m < 6 && m != 0) 21 | { 22 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, m); 23 | } 24 | else 25 | { 26 | printf("Last digit of %d is %d and is 0\n", n, m); 27 | } 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entyr point 5 | * Description: prints two digits combination 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int c, i; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | for (i = '0'; i <= '9'; i++) 15 | { 16 | if (c < i) 17 | { 18 | putchar(c); 19 | putchar(i); 20 | 21 | if (c != '8' || (c == '8' && i != '9')) 22 | { 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | } 27 | } 28 | } 29 | putchar('\n'); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints 3 combination of numbers 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | int c, i, k; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | for (i = '0'; i <= '9'; i++) 15 | { 16 | for (k = '0'; k <= '9'; k++) 17 | { 18 | if (c < i && i < k) 19 | { 20 | putchar(c); 21 | putchar(i); 22 | putchar(k); 23 | 24 | if (c != '7') 25 | { 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | } 31 | } 32 | } 33 | putchar('\n'); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints all possible combination of 2-digit numbers 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int c, i, k, j; 11 | 12 | for (c = 48; c <= 57; c++) 13 | { 14 | for (i = 48; i <= 57; i++) 15 | { 16 | for (k = 48; k <= 57; k++) 17 | { 18 | for (j = 48; j <= 57; j++) 19 | { 20 | if (((k + j) > (c + i) && k >= c) || c < k) 21 | { 22 | putchar(c); 23 | putchar(i); 24 | putchar(' '); 25 | putchar(k); 26 | putchar(j); 27 | 28 | if (c + i + k + j == 227 && c == 57) 29 | { 30 | break; 31 | } 32 | else 33 | { 34 | putchar(','); 35 | putchar(' '); 36 | } 37 | } 38 | } 39 | } 40 | } 41 | } 42 | putchar('\n'); 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * Description: prints the alphabets in lowercase/n 7 | * Return: Always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int ch; 12 | 13 | for (ch = 'a'; ch <= 'z'; ch++) 14 | { 15 | putchar(ch); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints alphabets in lowercase then uppercase 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | for (ch = 'A'; ch <= 'Z'; ch++) 17 | { 18 | putchar(ch); 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints lowercase alphabets 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | if (ch != 'e' && ch != 'q') 15 | { 16 | putchar(ch); 17 | } 18 | else 19 | { 20 | } 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main -Entry point 5 | * Description: prints allsingle digit numbers of 6 | * base 10 from 0 7 | * Return: Always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int a; 12 | 13 | for (a = 0; a < 10; a++) 14 | { 15 | printf("%d", a); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description:prints char below 10 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | 12 | for (a = 0; a < 10; a++) 13 | { 14 | putchar(a + '0'); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints lowercase alphabets in reverse 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | 12 | for (a = 'z'; a >= 'a'; a--) 13 | { 14 | putchar(a); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: Print base 16 numbers 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | char d = '0'; 12 | 13 | while (d <= '9') 14 | { 15 | putchar(d); 16 | d++; 17 | } 18 | for (c = 'a'; c <= 'f'; c++) 19 | { 20 | putchar(c); 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints numbers 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = '0'; i <= '9' ; i++) 13 | { 14 | putchar(i); 15 | 16 | if (i != '9') 17 | { 18 | putchar(','); 19 | putchar(' '); 20 | } 21 | } 22 | 23 | putchar('\n'); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.8-24_hours.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x02-functions_nested_loops/.8-24_hours.c.swp -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - Prints _putchar as a message. 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int str[] = {95, 112, 117, 116, 99, 104, 97, 114}; 11 | int count, sz; 12 | 13 | sz = sizeof(str) / sizeof(int); 14 | for (count = 0; count < sz; count++) 15 | { 16 | _putchar(str[count]); 17 | } 18 | 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - Entry point 5 | * Description: a function that prints the alphabet, 6 | * in lowecase followed by a new line 7 | * Return: void 8 | */ 9 | void print_alphabet(void) 10 | { 11 | char letter = 'a'; 12 | 13 | while (letter <= 'z') 14 | { 15 | _putchar(letter); 16 | letter++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * add - adds two integers 4 | * @n1: integer 1 5 | * @n2: integer 2 6 | * Description: give two integers, returns sum 7 | * Return: sum 8 | */ 9 | 10 | int add(int n1, int n2) 11 | { 12 | int sum; 13 | 14 | sum = n1 + n2; 15 | 16 | return (sum); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - Prints a multiplication table up to param 5 | * @n: The number to be treated 6 | * 7 | * Return: Number matrix 8 | */ 9 | 10 | void print_times_table(int n) 11 | { 12 | int x, y, z; 13 | 14 | if (n >= 0 && n <= 14) 15 | { 16 | for (x = 0; x <= n; x++) 17 | { 18 | for (y = 0; y <= n; y++) 19 | { 20 | z = x * y; 21 | if (z > 99) 22 | { 23 | _putchar(','); 24 | _putchar(32); 25 | _putchar((z / 100) + '0'); 26 | _putchar(((z / 10) % 10) + '0'); 27 | _putchar((z % 10) + '0'); 28 | } 29 | else if (z > 9) 30 | { 31 | _putchar(','); 32 | _putchar(32); 33 | _putchar(32); 34 | _putchar(((z / 10) % 10) + '0'); 35 | _putchar((z % 10) + '0'); 36 | } 37 | else 38 | { 39 | if (y != 0) 40 | { 41 | _putchar(','); 42 | _putchar(32); 43 | _putchar(32); 44 | _putchar(32); 45 | } 46 | _putchar(z + '0'); 47 | } 48 | } 49 | _putchar('\n'); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the sum of all multiples of 3 or 5 up to 102 5 | * Return: Always (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i, z = 0; 10 | 11 | while (i < 1024) 12 | { 13 | if ((i % 3 == 0) || (i % 5 == 0)) 14 | { 15 | z += i; 16 | } 17 | 18 | i++; 19 | } 20 | 21 | printf("%d\n", z); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the first 52 fibonacci numbers 5 | * 6 | * Return: Nothing! 7 | */ 8 | int main(void) 9 | { 10 | int i = 0; 11 | long j = 1, k = 2; 12 | 13 | while (i < 50) 14 | { 15 | if (i == 0) 16 | printf("%ld", j); 17 | else if (i == 1) 18 | printf(", %ld", k); 19 | else 20 | { 21 | k += j; 22 | j = k - j; 23 | printf(", %ld", k); 24 | } 25 | 26 | ++i; 27 | } 28 | 29 | printf("\n"); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: computes and prints even number < 4,000,000 5 | * 5 below 1024 (excluded), followed by a new line 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int a = 0, b = 1, next = 0; 11 | int sum = 0; 12 | 13 | while (next < 4000000) 14 | { 15 | next = a + b; 16 | a = b; 17 | b = next; 18 | if (next % 2 == 0) 19 | sum += next; 20 | } 21 | printf("%i\n", sum); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | /** 5 | * numLength - returns the lenth of string 6 | * @num : operand number 7 | * Return: number of digits 8 | */ 9 | 10 | int numLength(int num) 11 | { 12 | int length = 0; 13 | 14 | if (!num) 15 | { 16 | return (1); 17 | } 18 | 19 | while (num) 20 | { 21 | num = num / 10; 22 | length += 1; 23 | } 24 | 25 | return (length); 26 | } 27 | /** 28 | * *main - prints the first 98 fibonaci sequences 29 | * Return: 0 30 | */ 31 | 32 | int main(void) 33 | { 34 | unsigned long f1 = 1, f2 = 2, tmp, mx = 100000000, f1o = 0, f2o = 0, tmpo = 0; 35 | short int i = 1, initial0s; 36 | 37 | while (i <= 98) 38 | { 39 | if (f1o > 0) 40 | printf("%lu", f1o); 41 | initial0s = numLength(mx) - 1 - numLength(f1); 42 | while (f1o > 0 && initial0s > 0) 43 | { 44 | printf("%i", 0); 45 | initial0s--; 46 | } 47 | printf("%lu", f1); 48 | 49 | tmp = (f1 + f2) % mx; 50 | tmpo = f1o + f2o + (f1 + f2) / mx; 51 | f1 = f2; 52 | f1o = f2o; 53 | f2 = tmp; 54 | f2o = tmpo; 55 | 56 | if (i != 98) 57 | printf(", "); 58 | else 59 | printf("\n"); 60 | i++; 61 | } 62 | return (0); 63 | } 64 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - prints numbers to 98 6 | * @n: starting integer 7 | * 8 | * Description: prints all natural number from n - 98 9 | * 10 | * Return: void 11 | */ 12 | void print_to_98(int n) 13 | { 14 | if (n < 98) 15 | { 16 | while (n < 98) 17 | { 18 | printf("%d, ", n); 19 | n++; 20 | } 21 | } 22 | else if (n > 98) 23 | { 24 | while (n > 98) 25 | { 26 | printf("%d, ", n); 27 | n--; 28 | } 29 | } 30 | printf("98\n"); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Make alphabet x10 times 5 | * 6 | * Return: void 7 | */ 8 | 9 | void print_alphabet_x10(void) 10 | { 11 | char c; 12 | int i = 0; 13 | 14 | while (i <= 9) 15 | { 16 | for (c = 'a'; c <= 'z'; c++) 17 | { 18 | _putchar(c); 19 | } 20 | _putchar('\n'); 21 | 22 | i++; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Entry point 5 | * Description: checks is a character in lowercase 6 | * @c: the integer value it receives 7 | * Return: 1 if true. 0 if false. 8 | */ 9 | int _islower(int c) 10 | { 11 | int i = 'a'; 12 | 13 | for (i = 'a'; i <= 'z'; i++) 14 | { 15 | /* refer int c*/ 16 | if (c == i) 17 | { 18 | return (1); 19 | } 20 | } 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Entry point 5 | * @c: the integer value it receives 6 | * Description: checks if alphabet 7 | * Return: 1 if true. 0 if false 8 | */ 9 | 10 | int _isalpha(int c) 11 | { 12 | if (c >= 'a' && c <= 'z') 13 | { 14 | return (1); 15 | } 16 | else if (c >= 'A' && c <= 'Z') 17 | { 18 | return (1); 19 | } 20 | else 21 | { 22 | return (0); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - Entr point 5 | * @n: carrier variable 6 | * Description: prints the sign of a number 7 | * Return: 1 if n>0, 0 if n == 0, -1 if n<0 8 | */ 9 | 10 | int print_sign(int n) 11 | { 12 | if (n > 0) 13 | { 14 | _putchar('+'); 15 | return (1); 16 | } 17 | else if (n < 0) 18 | { 19 | _putchar('-'); 20 | return (-1); 21 | } 22 | _putchar('0'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Entry point 5 | * @n: n is an integer 6 | * Description: prints the absolute value of an integer 7 | * Return: int 8 | */ 9 | 10 | int _abs(int n) 11 | { 12 | if (n > 0) 13 | { 14 | return (n); 15 | } 16 | else if (n < 0) 17 | { 18 | return (-n); 19 | } 20 | else 21 | { 22 | return (n); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /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: n is an integer 6 | * Description: prints the last digit of a number 7 | * Return: integer 8 | */ 9 | 10 | int print_last_digit(int n) 11 | { 12 | int last = n % 10; 13 | 14 | if (n < 0) 15 | { 16 | last = last * -1; 17 | } 18 | _putchar(last + '0'); 19 | return (last); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day 5 | * Description: prints every minute of the day 6 | * Return: void 7 | */ 8 | 9 | void jack_bauer(void) 10 | { 11 | int hours_tens, hours_ones, minutes_tens, minutes_ones, hours_max; 12 | 13 | hours_max = 58; 14 | hours_tens = '0'; 15 | while (hours_tens < '3') 16 | { 17 | if (hours_tens == '2') 18 | { 19 | hours_max = '4'; 20 | } 21 | hours_ones = '0'; 22 | while (hours_ones < hours_max) 23 | { 24 | minutes_tens = '0'; 25 | while (minutes_tens < '6') 26 | { 27 | minutes_ones = '0'; 28 | while (minutes_ones < 58) 29 | { 30 | _putchar(hours_tens); 31 | _putchar(hours_ones); 32 | _putchar(':'); 33 | _putchar(minutes_tens); 34 | _putchar(minutes_ones); 35 | _putchar('\n'); 36 | minutes_ones++; 37 | } 38 | minutes_ones = '0'; 39 | minutes_tens++; 40 | } 41 | minutes_tens = '0'; 42 | hours_ones++; 43 | } 44 | hours_ones = '0'; 45 | hours_tens++; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * times_table - prints the 9 times table 4 | * Description: prints the 9 times table 5 | * Return: void 6 | */ 7 | 8 | void times_table(void) 9 | { 10 | int row, column, product, tens, ones; 11 | 12 | for (row = 0; row <= 9; row++) 13 | { 14 | for (column = 0; column <= 9; column++) 15 | { 16 | product = row * column; 17 | tens = product / 10; 18 | ones = product % 10; 19 | 20 | if (column == 0) 21 | { 22 | _putchar('0'); 23 | } 24 | else if (product < 10) 25 | { 26 | _putchar(','); 27 | _putchar(' '); 28 | _putchar(' '); 29 | _putchar(ones + '0'); 30 | } 31 | else 32 | { 33 | _putchar(','); 34 | _putchar(' '); 35 | _putchar(tens + '0'); 36 | _putchar(ones + '0'); 37 | } 38 | 39 | } 40 | _putchar('\n'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | 2 | Functions and nested loops 3 | 4 | Authors 5 | 6 | Amarachi Omereife 7 | 8 | Requirements 9 | 10 | C programming language 11 | 12 | Ubuntu 20.04 LTS 13 | 14 | gcc 4.8.4 using the flags -Wall -Werror -Wextra -pedantic -std=gnu89 15 | 16 | Synopsis 17 | 18 | This project introduces functions and nested loops in C programming. Each .c file contains a different functions as listed below: 19 | 20 | Description of what each file does 21 | 22 | 0. _putchar: Write a program that prints _putchar, followed by a new line. 23 | 24 | 1-alphabet.c: Write a function that prints the alphabet, in lowercase, followed by a new line. 25 | 26 | 2-print_alphabet_x10.c: prints 10 times the alphabet, in lowercase 27 | 28 | 3-islower.c: checks for lowercase character; returns 1 if true and 0 if false 29 | 30 | 4-isalpha.c: checks for alphabetic character; returns 1 if true and 0 if false 31 | 32 | 5-sign.c: prints the sign of a number 33 | 34 | 6-abs.c: computes the absolute value of an integer 35 | 36 | 7-print_last_digit.c: prints the last digit of a number 37 | 38 | 8-24_hours.c: prints every minute starting from 00:00 to 23:59 39 | 40 | 9-times_table.c: prints the 9 times table, starting with 0 41 | 42 | 10-add.c: adds two integers and returns the result 43 | 44 | 11-print_to_98.c: prints all natural numbers from n to 98 45 | 46 | 47 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _main_h_ 2 | #define _main_h_ 3 | 4 | int _putchar(char c); 5 | void print_alphabet(void); 6 | void print_alphabet_x10(void); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int print_sign(int n); 10 | int _abs(int); 11 | int print_last_digit(int); 12 | void jack_bauer(void); 13 | void times_table(void); 14 | int add(int, int); 15 | void print_to_98(int n); 16 | void print_times_table(int n); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /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 > b) 24 | { 25 | largest = c; 26 | } 27 | else 28 | { 29 | largest = b; 30 | } 31 | 32 | return (largest); 33 | } 34 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * print_remaining_days - takes a date and prints how many days are 4 | * left in the year, taking leap years into account 5 | * @month: month in number format 6 | * @day: day of month 7 | * @year: year 8 | * Return: void 9 | */ 10 | 11 | void print_remaining_days(int month, int day, int year) 12 | { 13 | if ((year % 4 == 0 && year % 100 != 0) || 14 | year % 400 == 0) 15 | { 16 | if (month > 2) 17 | { 18 | day++; 19 | } 20 | 21 | printf("Day of the year: %d\n", day); 22 | printf("Remaining days: %d\n", 366 - day); 23 | } 24 | else 25 | { 26 | if (month == 2 && day >= 60) 27 | { 28 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 29 | } 30 | else 31 | { 32 | printf("Day of the year: %d\n", day); 33 | printf("Remaining days: %d\n", 365 - day); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | ALX | 0x03-Debugging| Debugging Task 2 | 3 | 4 | Task 0. Create the following: main.h and main.c files 5 | 6 | Task 1. Comment out (don’t delete it!) the part of the code that is causing the output to go into an infinite loop. 7 | 8 | Task 2. Write a program that prints the largest of three integers. 9 | 10 | Task 3. Write a program that converts a date to the day of year and determines how many days are left in the year, taking leap year into consideration. 11 | 12 | 13 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * _isupper - A function that checks for uppercase character. 5 | * 6 | * @c: is the character to check 7 | * 8 | * Return: 1 if c is uppercase otherwise 0 9 | */ 10 | int _isupper(int c) 11 | { 12 | int a = isupper(c); 13 | 14 | if (a == 0) 15 | return (a); 16 | else 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * _isdigit - A function that checks for a digit. 5 | * 6 | * @c: is the character to check 7 | * 8 | * Return: 1 if c is a digit otherwise 0 9 | */ 10 | int _isdigit(int c) 11 | { 12 | int a = isdigit(c); 13 | 14 | if (a == 0) 15 | return (a); 16 | else 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include"main.h" 2 | /** 3 | * print_triangle - A function who print a triangle 4 | * 5 | * @size: size of a triangle 6 | * 7 | * Return: Does not return anything 8 | */ 9 | void print_triangle(int size) 10 | { 11 | int par1, par2, space, output; 12 | 13 | for (par1 = 1; par1 <= size; par1++) 14 | { 15 | space = size - par1; 16 | output = size - space; 17 | 18 | for (par2 = 1; par2 <= space; par2++) 19 | { 20 | _putchar(' '); 21 | } 22 | for (par2 = 1; par2 <= output; par2++) 23 | { 24 | _putchar('#'); 25 | } 26 | _putchar('\n'); 27 | 28 | } 29 | if (size <= 0) 30 | _putchar('\n'); 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Finds and prints the largest prime factor 5 | * of the number 612852475143 6 | * 7 | * Return: 0 if success 8 | */ 9 | int main(void) 10 | { 11 | unsigned long num = 612852475143; 12 | unsigned long factor = 2; 13 | 14 | while (num) 15 | { 16 | if (num / factor > 0 && num % factor == 0) 17 | num /= factor; 18 | else if (num / factor > 0 && num % factor != 0) 19 | factor++; 20 | else 21 | break; 22 | } 23 | printf("%ld\n", factor); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_number - prints an integer 4 | * @na: integer to print 5 | * Return: nothing 6 | */ 7 | void print_number(int na) 8 | { 9 | unsigned int pc, i, z, nmodul, n; 10 | 11 | if (na == 0) 12 | _putchar('0'); 13 | /* if n is negative we make it positive*/ 14 | if (na < 0) 15 | { 16 | n = -na; 17 | _putchar('-'); 18 | } 19 | else 20 | n = na; 21 | if (n < 10 && n > 0) 22 | _putchar(n + '0'); 23 | while (n >= 10) 24 | { 25 | pc = n; 26 | z = 0; 27 | nmodul = 0; 28 | for (i = 0; pc >= 10; i++) 29 | { 30 | if (pc / 10 > 0 && pc % 10 == 0) 31 | z++; 32 | else 33 | z = 0; 34 | pc /= 10; 35 | nmodul = (nmodul) ? nmodul * 10 : 10; 36 | } 37 | _putchar(pc + '0'); 38 | while (z-- > 0) 39 | _putchar('0'); 40 | n %= nmodul; 41 | if (n > 0 && n < 10) 42 | _putchar(n + '0'); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * mul - multiplies two integers and returns the result 4 | * 5 | * @a: is the first number 6 | * @b: is the second number 7 | * Return: the product value of @a and @b 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_numbers - A function who print the numbers, from 0 to 9 4 | * 5 | * Description - Does not receive any parameters 6 | * 7 | * Return: Does not return anything 8 | */ 9 | void print_numbers(void) 10 | { 11 | int i; 12 | 13 | for (i = 48; i < 58; i++) 14 | { 15 | _putchar(i); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_most_numbers - A function who print the numbers, from 0 to 9 4 | * except 2 and 4. 5 | * Description - Does not receive any parameters 6 | * 7 | * Return: Does not return anything 8 | */ 9 | void print_most_numbers(void) 10 | { 11 | int i; 12 | 13 | for (i = 48; i < 58; i++) 14 | { 15 | if (!(i == 50 || i == 52)) 16 | _putchar(i); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * more_numbers - A function who print 10 times the numbers, from 0 to 14 4 | * 5 | * Description - Does not receive any parameters 6 | * 7 | * Return: Does not return anything 8 | */ 9 | void more_numbers(void) 10 | { 11 | int i, j, k, l; 12 | 13 | for (j = 0; j < 10; j++) 14 | { 15 | k = 0; 16 | l = 0; 17 | for (i = 48; i < 63; i++) 18 | { 19 | if (!(i > 57)) 20 | l = i; 21 | else 22 | { 23 | l = i - 10; 24 | k = 49; 25 | _putchar(k); 26 | } 27 | _putchar(l); 28 | } 29 | _putchar('\n'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_line - A function who print lines n times 4 | * 5 | * @n: number of times to print line 6 | * 7 | * Return: Does not return anything 8 | */ 9 | void print_line(int n) 10 | { 11 | int i; 12 | 13 | if (n > 0) 14 | { 15 | for (i = 0; i < n; i++) 16 | { 17 | _putchar('_'); 18 | } 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_diagonal - A function who print diagonal line n times 4 | * 5 | * @n: number of times to print diagonal line 6 | * 7 | * Return: Does not return anything 8 | */ 9 | void print_diagonal(int n) 10 | { 11 | int i, j; 12 | 13 | if (n > 0) 14 | { 15 | for (j = 0; j < n; j++) 16 | { 17 | for (i = 0; i < j; i++) 18 | { 19 | _putchar(' '); 20 | } 21 | _putchar(92); 22 | _putchar('\n'); 23 | } 24 | } 25 | else 26 | _putchar('\n'); 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_square - A function who print a square 4 | * 5 | * @size: size of square 6 | * 7 | * Return: Does not return anything 8 | */ 9 | void print_square(int size) 10 | { 11 | int i, j; 12 | int n = size; 13 | 14 | if (n > 0) 15 | { 16 | for (j = 0; j < n; j++) 17 | { 18 | for (i = 0; i < n; i++) 19 | { 20 | _putchar(35); 21 | } 22 | _putchar('\n'); 23 | } 24 | } 25 | else 26 | _putchar('\n'); 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - the entry point 4 | * 5 | * Return: 0 if the program run with success 6 | */ 7 | int main(void) 8 | { 9 | int a = 0, b = 0, c = 0; 10 | 11 | for (a = 1; a < 101; a++) 12 | { 13 | b = a % 3; 14 | c = a % 5; 15 | if (b == 0 && c == 0) 16 | printf("FizzBuzz"); 17 | else if (c == 0 && b != 0) 18 | printf("Buzz"); 19 | else if (b == 0 && c != 0) 20 | printf("Fizz"); 21 | else 22 | printf("%d", a); 23 | if (a < 100) 24 | putchar(' '); 25 | } 26 | putchar('\n'); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | ALL TASKS 2 | 3 | Task 0: Writes a function that checks for uppercase characters. 4 | 5 | 6 | 7 | Task 1: Write a function that checks for a digit (0 through 9). 8 | 9 | 10 | 11 | Task2: Write a function that multiplies two integers. 12 | 13 | 14 | 15 | Task 3: Write a function that prints the numbers, from 0 to 9, followed by a new line. 16 | 17 | 18 | 19 | Task 4: Write a function that prints the numbers, from 0 to 9, followed by a new line. 20 | 21 | 22 | 23 | Task 5: Write a function that prints 10 times the numbers, from 0 to 14, followed by a new line. 24 | 25 | 26 | 27 | Task 6: Write a function that draws a straight line in the terminal. 28 | 29 | 30 | 31 | Task 7: Write a function that draws a diagonal line on the terminal. 32 | 33 | 34 | 35 | Task 8: Write a function that prints a square, followed by a new line 36 | 37 | 38 | 39 | Task 9: Write a program that prints the numbers from 1 to 100, followed by a new line.. 40 | 41 | 42 | 43 | Task 10: Write a function that prints a triangle, followed by a new line. 44 | 45 | 46 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /** 5 | * File: main.h 6 | * 7 | * Desc: header file 8 | */ 9 | 10 | int _putchar(char c); 11 | int _isupper(int c); 12 | int _isdigit(int c); 13 | int mul(int a, int b); 14 | void print_numbers(void); 15 | void print_most_numbers(void); 16 | void more_numbers(void); 17 | void print_line(int n); 18 | void print_diagonal(int n); 19 | void print_square(int size); 20 | void print_triangle(int size); 21 | void print_number(int n); 22 | 23 | #endif /* MAIN_H */ 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value 5 | * @n: a pointer to an int 6 | * Return: nothing 7 | */ 8 | 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | } 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swaps the values of a and b 5 | * @a: pointer 1 6 | * @b: pointer 2 7 | * Return: 0 8 | */ 9 | 10 | void swap_int(int *a, int *b) 11 | { 12 | *a += *b; 13 | *b = *a - *b; 14 | *a = *a - *b; 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _atoi - convert a string to an integer. 4 | * @s: string to convert 5 | * Return: the converted value in int 6 | */ 7 | 8 | int _atoi(char *s) 9 | { 10 | int a = 0, digit = 0, num = 0, signe = 0, count = 0, length = 0; 11 | 12 | while (s[length] != '\0') 13 | length++; 14 | while (count < length && a == 0) 15 | { 16 | if (s[count] == '-') 17 | ++signe; 18 | if (s[count] >= '0' && s[count] <= '9') 19 | { 20 | digit = s[count] - '0'; 21 | if (signe % 2) 22 | digit = -digit; 23 | num = num * 10 + digit; 24 | a = 1; 25 | if (s[count + 1] < '0' || s[count + 1] > '9') 26 | break; 27 | a = 0; 28 | } 29 | count++; 30 | } 31 | if (a == 0) 32 | return (0); 33 | return (num); 34 | } 35 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define G_MIN 33 6 | #define G_MAX 126 7 | /** 8 | * main - generate valid passwords for 101-crackme 9 | * Return: 0 is run with success 10 | */ 11 | 12 | int main(void) 13 | { 14 | int sum = 2772; 15 | char c; 16 | 17 | srand(time(NULL)); 18 | while (sum > G_MAX) 19 | { 20 | c = rand() % (G_MAX - G_MIN) + G_MIN; 21 | sum -= c; 22 | if (sum < G_MIN) 23 | { 24 | c -= (G_MIN - sum); 25 | sum = G_MIN; 26 | } 27 | putchar(c); 28 | } 29 | putchar(sum); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns length of string 5 | * @s: string to check 6 | * 7 | * Return: int length of the string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int length = 0; 13 | 14 | while (s[length]) 15 | length++; 16 | return (length); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string and a new line 5 | * @str: pointer to a string to print 6 | * 7 | * Return: 0 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i = 0; 13 | 14 | while (str[i]) 15 | { 16 | _putchar(str[i]); 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints string in reverse 5 | * @s: the used string reference pointer 6 | * Return: 0 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int i = 0; 12 | 13 | while (s[i]) 14 | i++; 15 | while (i--) 16 | _putchar(s[i]); 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - prints a string in reverse 5 | * @s: the string to be reversed 6 | * Return: 0 7 | */ 8 | 9 | void rev_string(char *s) 10 | { 11 | int len, i, half; 12 | char temp; 13 | 14 | for (len = 0; s[len] != '\0'; len++) 15 | ; 16 | i = 0; 17 | half = len / 2; 18 | 19 | while (half--) 20 | { 21 | temp = s[len - i - 1]; 22 | s[len - i - 1] = s[i]; 23 | s[i] = temp; 24 | i++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints every other character of a 5 | * string, starting with the first character 6 | * 7 | * @str: the used string reference 8 | * Return: 0 9 | */ 10 | 11 | void puts2(char *str) 12 | { 13 | int i = 0; 14 | 15 | while (str[i] != '\0') 16 | { 17 | if (i % 2 == 0) 18 | { 19 | _putchar(str[i]); 20 | } 21 | i++; 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints the second half of the string 5 | * @str: the string reference 6 | * Return: 0 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | ; 15 | i++; 16 | for (i /= 2; str[i] != '\0'; i++) 17 | { 18 | _putchar(str[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - print `n` elements of an array of integers 6 | * @a: int array pointer 7 | * @n: int 8 | * Description: Numbers must be separated by comma and space. 9 | * Numbers should be displayed in the same order they are stored in array. 10 | * You can only use _putchar to print. 11 | */ 12 | 13 | void print_array(int *a, int n) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | for (n--; n >= 0; n--, i++) 19 | { 20 | printf("%d", a[i]); 21 | if (n > 0) 22 | { 23 | printf(", "); 24 | } 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - Copy the string pointed to by pointer `src` to 5 | * the buffer pointed to by `dest` 6 | * @dest: char pointer 7 | * @src: char 8 | * Return: Pointer to `dest 9 | */ 10 | 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int i = -1; 14 | 15 | do { 16 | i++; 17 | dest[i] = src[i]; 18 | } while (src[i] != '\0'); 19 | 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | ALX Task | Pointers and Arrays 2 | 3 | Task 0: Write a function that takes a pointer to an int as parameter and updates the value it points to to 98. 4 | 5 | 6 | 7 | Task 1: Write a function that swaps the values of two integers. 8 | 9 | 10 | 11 | Task 2: Write a function that returns the length of a string. 12 | 13 | 14 | 15 | Task 3: Write a function that prints a string, followed by a new line, to stdout. 16 | 17 | 18 | 19 | Task 4: Write a function that prints a string, in reverse, followed by a new line. 20 | 21 | 22 | 23 | Task 5: Write a function that reverses a string. 24 | 25 | 26 | 27 | Task 6: Write a function that prints every other character of a string, starting with the first character, followed by a new line. 28 | 29 | 30 | 31 | Task 7: Write a function that prints half of a string, followed by a new line. 32 | 33 | 34 | 35 | Task 8: Write a function that prints n elements of an array of integers, followed by a new line. 36 | 37 | 38 | 39 | Task 9: Write a function that copies the string pointed to by src, including the terminating null byte (\0), to the buffer pointed to by dest. 40 | 41 | 42 | 43 | Task 10: Write a function that converts a string to an integer. 44 | 45 | 46 | 47 | Task 11: Create a program that generates random valid passwords for the program 101-crackme. 48 | 49 | 50 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void reset_to_98(int *n); 3 | void swap_int(int *a, int *b); 4 | int _strlen(char *s); 5 | void _puts(char *str); 6 | void print_rev(char *s); 7 | void rev_string(char *s); 8 | void puts2(char *str); 9 | void puts_half(char *str); 10 | void print_array(int *a, int n); 11 | char *_strcpy(char *dest, char *src); 12 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concatenates two strings 5 | * @dest: the resulting string 6 | * @src: string to add to @dest 7 | * Return: @dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i, len; 13 | 14 | for (len = 0; dest[len] != '\0'; len++) 15 | ; 16 | for (i = 0; src[i] != '\0'; i++) 17 | dest[len + i] = src[i]; 18 | dest[len + i] = '\0'; 19 | return (dest); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings 4 | * use at most @n bytes from @src 5 | * @dest: the resulting string 6 | * @src: string to add to @dest 7 | * @n: number of bytes 8 | * Return: @dest 9 | */ 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int i, len; 13 | 14 | for (len = 0; dest[len] != '\0'; len++) 15 | ; 16 | for (i = 0; src[i] != '\0' && i < n; i++) 17 | dest[len + i] = src[i]; 18 | dest[len + i] = '\0'; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string in rot13 5 | * @s: string to be encoded 6 | * 7 | * Return: the resulting string 8 | */ 9 | 10 | char *rot13(char *s) 11 | { 12 | int i, j; 13 | 14 | char a[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 15 | char b[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 16 | 17 | for (i = 0; s[i] != '\0'; i++) 18 | { 19 | for (j = 0; a[j] != '\0'; j++) 20 | { 21 | if (s[i] == a[j]) 22 | { 23 | s[i] = b[j]; 24 | break; 25 | } 26 | } 27 | } 28 | 29 | return (s); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer; 5 | * @n: integer to be printed; 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int n1; 11 | 12 | if (n < 0) 13 | { 14 | n1 = -n; 15 | _putchar('-'); 16 | } else 17 | { 18 | n1 = n; 19 | } 20 | 21 | if (n1 / 10) 22 | print_number(n1 / 10); 23 | 24 | _putchar((n1 % 10) + '0'); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * infinite_add - adds two numbers 5 | * @n1: first number 6 | * @n2: second number 7 | * @r: buffer for result 8 | * @size_r: buffer size 9 | * 10 | * Return: address of r or 0 11 | */ 12 | 13 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 14 | { 15 | int i, j, k, l, m, n; 16 | 17 | for (i = 0; n1[i]; i++) 18 | ; 19 | for (j = 0; n2[j]; j++) 20 | ; 21 | if (i > size_r || j > size_r) 22 | return (0); 23 | m = 0; 24 | for (i -= 1, j -= 1, k = 0; k < size_r - 1; i--, j--, k++) 25 | { 26 | n = m; 27 | if (i >= 0) 28 | n += n1[i] - '0'; 29 | if (j >= 0) 30 | n += n2[j] - '0'; 31 | if (i < 0 && j < 0 && n == 0) 32 | { 33 | break; 34 | } 35 | m = n / 10; 36 | r[k] = n % 10 + '0'; 37 | } 38 | r[k] = '\0'; 39 | if (i >= 0 || j >= 0 || m) 40 | return (0); 41 | for (k -= 1, l = 0; l < k; k--, l++) 42 | { 43 | m = r[k]; 44 | r[k] = r[l]; 45 | r[l] = m; 46 | } 47 | return (r); 48 | } 49 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copies a string 5 | * @dest: point to destinatin 6 | * @src: string to copy 7 | * @n: number of bytes to copy 8 | * Return: @dest 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n && src[i] != '\0'; i++) 16 | dest[i] = src[i]; 17 | for (; i < n; i++) 18 | dest[i] = '\0'; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare 2 strings 5 | * @s1: first string 6 | * @s2: second string 7 | * Return: an integer as difference of last character 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, diff = 0; 13 | 14 | for (; s1[i] == s2[i] && s1[i] != '\0'; i++) 15 | ; 16 | diff = s1[i] - s2[i]; 17 | return (diff); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - reverses the content of an array of integers 5 | * @a: array to reverse 6 | * @n: number of elements of the array 7 | * Return: nothing 8 | */ 9 | 10 | void reverse_array(int *a, int n) 11 | { 12 | int i, tmp, 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 | j--; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - changes all lowercase letters of a string to uppercase 5 | * @str: string to change 6 | * Return: string changed 7 | */ 8 | 9 | char *string_toupper(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i] != '\0') 14 | { 15 | if (str[i] > 96 && str[i] < 123) 16 | str[i] -= 32; 17 | i++; 18 | } 19 | 20 | return (str); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes all words of a string 5 | * @str: string to change 6 | * Return: string changed 7 | */ 8 | 9 | char *cap_string(char *str) 10 | { 11 | int i = 0, j = 0, tmp = 0; 12 | char sep[] = {'\n', ' ', '\t', ',', ';', '"', 13 | '(', ')', '{', '}', '?', '!', '.'}; 14 | 15 | while (str[i] != '\0') 16 | { 17 | tmp = 0; 18 | for (j = 0; sep[j] != '\0' && i != 0; j++) 19 | { 20 | if (str[i - 1] == sep[j]) 21 | { 22 | tmp = 1; 23 | break; 24 | } 25 | } 26 | if (i == 0 || tmp == 1) 27 | { 28 | if (str[i] > 96 && str[i] < 123) 29 | str[i] -= 32; 30 | } 31 | i++; 32 | } 33 | 34 | return (str); 35 | } 36 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - encodes a string into 1337 replaced: 5 | * a,A to 4 e,E to 3 o,O to 0 t,T to 7 l,L to 1 6 | * @str: string to encode 7 | * Return: a pointer to the encoded string 8 | */ 9 | 10 | char *leet(char *str) 11 | { 12 | int i = 0, j = 0; 13 | char s1[] = {'a', 'e', 'o', 't', 'l', 'A', 'E', 'O', 'T', 'L'}; 14 | int s2[] = {4, 3, 0, 7, 1, 4, 3, 0, 7, 1}; 15 | 16 | while (str[i] != '\0') 17 | { 18 | for (j = 0; s1[j] != '\0'; j++) 19 | { 20 | if (str[i] == s1[j]) 21 | str[i] = s2[j] + 48; 22 | } 23 | i++; 24 | } 25 | return (str); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | ALX | More pointers, arrays and strings 2 | 3 | 4 | 5 | Task 0: Write a function that concatenates two strings. 6 | 7 | 8 | 9 | Task 1: Write a function that concatenates two strings. 10 | 11 | 12 | 13 | Task 2: Write a function that copies a string. 14 | 15 | 16 | 17 | Task 3: Write a function that compares two strings. 18 | 19 | 20 | 21 | Task 4: Write a function that reverses the content of an array of integers.. 22 | 23 | 24 | 25 | Task 5: Write a function that changes all lowercase letters of a string to uppercase.. 26 | 27 | 28 | 29 | Task 6: Write a function that capitalizes all words of a string. 30 | 31 | 32 | 33 | Task 7: Write a function that encodes a string into 1337. 34 | 35 | 36 | 37 | Task 8: Write a function that encodes a string using rot13.. 38 | 39 | 40 | 41 | Task 9: Write a function that prints an integer. 42 | 43 | 44 | 45 | Task 10: Add one line to this code, so that the program prints a[2] = 98, followed by a new line. 46 | 47 | 48 | 49 | Task 11: Write a function that adds two numbers.. 50 | 51 | 52 | 53 | Task 12: Write a function that prints a buffer. 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char); 2 | char *_strcat(char *dest, char *src); 3 | char *_strncat(char *dest, char *src, int n); 4 | char *_strncpy(char *dest, char *src, int n); 5 | int _strcmp(char *s1, char *s2); 6 | void reverse_array(int *a, int n); 7 | char *string_toupper(char *); 8 | char *cap_string(char *); 9 | char *leet(char *); 10 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 11 | void print_number(int n); 12 | char *rot13(char *); 13 | void print_buffer(char *b, int size); 14 | int main(void); 15 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memset - fills memory with a constant byte. 5 | * @s: pointer to put the constant 6 | * @b: constant 7 | * @n: max bytes to use 8 | * Return: s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; n > 0; i++, n--) 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 n bytes from memory area 5 | * src to memomy area dest 6 | * @dest: memory area 7 | * @src: source 8 | * @n: length of src 9 | * 10 | * Return: pointer to dest. 11 | */ 12 | char *_memcpy(char *dest, char *src, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | dest[i] = src[i]; 19 | } 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - Sets the value of a pointer to a char. 5 | * @s: The pointer. 6 | * @to: The char. 7 | */ 8 | void set_string(char **s, char *to) 9 | { 10 | *s = to; 11 | } 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - Locates a character in a string. 5 | * @s: The string to be searched. 6 | * @c: The character to be located. 7 | * 8 | * Return: If c is found - a pointer to the first occurence. 9 | * If c is not found - NULL. 10 | */ 11 | 12 | char *_strchr(char *s, char c) 13 | { 14 | int index; 15 | 16 | for (index = 0; s[index] >= '\0'; index++) 17 | { 18 | if (s[index] == c) 19 | return (s + index); 20 | } 21 | 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - Gets the length of a prefix substring. 5 | * @s: The string to be searched. 6 | * @accept: The prefix to be measured. 7 | * 8 | * Return: The number of bytes in s which 9 | * consist only of bytes from accept. 10 | */ 11 | 12 | unsigned int _strspn(char *s, char *accept) 13 | { 14 | unsigned int bytes = 0; 15 | int index; 16 | 17 | while (*s) 18 | { 19 | for (index = 0; accept[index]; index++) 20 | { 21 | if (*s == accept[index]) 22 | { 23 | bytes++; 24 | break; 25 | } 26 | 27 | else if (accept[index + 1] == '\0') 28 | return (bytes); 29 | } 30 | 31 | s++; 32 | } 33 | 34 | return (bytes); 35 | } 36 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - Searches a string for any of a set of bytes. 5 | * @s: The string to be searched. 6 | * @accept: The set of bytes to be searched for. 7 | * 8 | * Return: If a set is matched - a pointer to the matched byte. 9 | * If no set is matched - NULL. 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int index; 14 | 15 | while (*s) 16 | { 17 | for (index = 0; accept[index]; index++) 18 | { 19 | if (*s == accept[index]) 20 | return (s); 21 | } 22 | 23 | s++; 24 | } 25 | return ('\0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - Locates a substring. 5 | * @haystack: The string to be searched. 6 | * @needle: The substring to be located. 7 | * 8 | * Return: If the substring is located - a pointer to the beginning 9 | * of the located substring. 10 | * If the substring is not located - NULL. 11 | */ 12 | 13 | char *_strstr(char *haystack, char *needle) 14 | { 15 | int index; 16 | 17 | if (*needle == 0) 18 | return (haystack); 19 | 20 | while (*haystack) 21 | { 22 | index = 0; 23 | 24 | if (haystack[index] == needle[index]) 25 | { 26 | do { 27 | if (needle[index + 1] == '\0') 28 | return (haystack); 29 | 30 | index++; 31 | 32 | } while (haystack[index] == needle[index]); 33 | } 34 | 35 | haystack++; 36 | } 37 | 38 | return ('\0'); 39 | } 40 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - Prints a chessboard. 5 | * @a: The chessboard to be printed. 6 | */ 7 | void print_chessboard(char (*a)[8]) 8 | { 9 | int indx1, indx2; 10 | 11 | for (indx1 = 0; a[indx1][7]; indx1++) 12 | { 13 | for (indx2 = 0; indx2 < 8; indx2++) 14 | _putchar(a[indx1][indx2]); 15 | 16 | _putchar('\n'); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - Prints the sum of the two diagonals 6 | * of a square matrix of integers. 7 | * @a: The matrix of integers. 8 | * @size: The size of the matrix. 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int index, sum1 = 0, sum2 = 0; 13 | 14 | for (index = 0; index < size; index++) 15 | { 16 | sum1 += a[index]; 17 | a += size; 18 | } 19 | 20 | a -= size; 21 | 22 | for (index = 0; index < size; index++) 23 | { 24 | sum2 += a[index]; 25 | a -= size; 26 | } 27 | 28 | printf("%d, %d\n", sum1, sum2); 29 | } 30 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x07. C - Even more pointers, arrays and strings 2 | 3 | Task 0: Write a function that fills memory with a constant byte. 4 | 5 | Task 1: Write a function that copies memory area. 6 | 7 | Task 2: Write a function that locates a character in a string. 8 | 9 | Task 3: Write a function that gets the length of a prefix substring. 10 | 11 | Task 4: Write a function that searches a string for any of a set of bytes. 12 | 13 | Task 5: Write a function that locates a substring. 14 | 15 | Task 6: Write a function that prints the chessboard. 16 | 17 | Task 7: Write a function that prints the sum of the two diagonals of a square matrix of integers. 18 | 19 | Task 8: Write a function that sets the value of a pointer to a char. 20 | 21 | Task 9: Create a file that contains the password for the crackme2 executable 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * simple_print_buffer - prints buffer in hexa 6 | * @buffer: the address of memory to print 7 | * @size: the size of the memory to print 8 | * 9 | * Return: Nothing. 10 | */ 11 | void simple_print_buffer(char *buffer, unsigned int size) 12 | { 13 | unsigned int i; 14 | 15 | i = 0; 16 | while (i < size) 17 | { 18 | if (i % 10) 19 | { 20 | printf(" "); 21 | } 22 | if (!(i % 10) && i) 23 | { 24 | printf("\n"); 25 | } 26 | printf("0x%02x", buffer[i]); 27 | i++; 28 | } 29 | printf("\n"); 30 | } 31 | 32 | /** 33 | * main - check the code for ALX School students. 34 | * 35 | * Return: Always 0. 36 | */ 37 | int main(void) 38 | { 39 | char buffer[98] = {0x00}; 40 | 41 | simple_print_buffer(buffer, 98); 42 | _memset(buffer, 0x01, 95); 43 | printf("-------------------------------------------------\n"); 44 | simple_print_buffer(buffer, 98); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | int _putchar(char c); 7 | char *_memset(char *s, char b, unsigned int n); 8 | char *_memcpy(char *dest, char *src, unsigned int n); 9 | char *_strchr(char *s, char c); 10 | unsigned int _strspn(char *s, char *accept); 11 | char *_strpbrk(char *s, char *accept); 12 | char *_strstr(char *haystack, char *needle); 13 | void print_chessboard(char (*a)[8]); 14 | void print_diagsums(int *a, int size); 15 | void set_string(char **s, char *to); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - function that print a string recursively 5 | * @s: string to print 6 | * Return: void 7 | */ 8 | 9 | void _puts_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | { 13 | _putchar('\n'); 14 | return; 15 | } 16 | else 17 | { 18 | _putchar(*s); 19 | _puts_recursion(s + 1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - function that print a string in reverse 5 | * @s: string to print 6 | * Return: void 7 | */ 8 | 9 | void _print_rev_recursion(char *s) 10 | { 11 | if (*s != '\0') 12 | { 13 | _print_rev_recursion(s + 1); 14 | _putchar(*s); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - function that return the length of a string 5 | * @s: the string 6 | * Return: length of s 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | return (0); 13 | else 14 | return (1 + _strlen_recursion(s + 1)); 15 | } 16 | /** 17 | * palcheck - checker 18 | * @x: iterration 19 | * @y: length of s 20 | * @s: string 21 | * Return: 1 or 0 22 | */ 23 | int palcheck(int x, int y, char *s) 24 | { 25 | if (x >= y) 26 | return (1); 27 | if (*(s + x) != *(s + y - x)) 28 | return (0); 29 | return (palcheck(x + 1, y, s)); 30 | } 31 | /** 32 | * is_palindrome - check if a string is a palindrome 33 | * @s: string 34 | * Return: 1 if s is a palindrome otherwise 0 35 | */ 36 | int is_palindrome(char *s) 37 | { 38 | int i = _strlen_recursion(s); 39 | 40 | return (palcheck(0, i - 1, s)); 41 | } 42 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - function that compares two strings 5 | * @s1: first string 6 | * @s2: second string that can cointain * which replace any string 7 | * Return: 1 if matching 0 ortherwise 8 | */ 9 | int wildcmp(char *s1, char *s2) 10 | { 11 | if (*s1 == '\0' && *s2 == '\0') 12 | return (1); 13 | if (*s1 == *s2) 14 | return (wildcmp(s1 + 1, s2 + 1)); 15 | if (*s2 == '*') 16 | { 17 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 18 | return (0); 19 | if (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)) 20 | return (1); 21 | } 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - function that return the length of a string 5 | * @s: the string 6 | * Return: length of s 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | return (0); 13 | else 14 | return (1 + _strlen_recursion(s + 1)); 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - function that return the factorial of a number 5 | * @n: the number 6 | * Return: factorial of n 7 | */ 8 | 9 | int factorial(int n) 10 | { 11 | if (n < 0) 12 | return (-1); 13 | else if (n == 0) 14 | return (1); 15 | else 16 | return (n * factorial(n - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - function that return the value of x raised 5 | * to the power of y 6 | * @x: first parameter 7 | * @y: second parameter 8 | * Return: x power y 9 | */ 10 | 11 | int _pow_recursion(int x, int y) 12 | { 13 | if (y < 0) 14 | return (-1); 15 | else if (y == 0) 16 | return (1); 17 | else 18 | return (x * _pow_recursion(x, y - 1)); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * sqrt_check - verify if s is the square root of c 5 | * @s: parameter 6 | * @c: number to find sqrt of 7 | * Return: -1 or sqrt of c 8 | */ 9 | 10 | int sqrt_check(int s, int c) 11 | { 12 | if (s * s == c) 13 | return (s); 14 | if (s * s > c) 15 | return (-1); 16 | return (sqrt_check(s + 1, c)); 17 | } 18 | /** 19 | * _sqrt_recursion - function that returns the natural root of a number 20 | * @n: parameter 21 | * Return: natural root of n 22 | */ 23 | 24 | int _sqrt_recursion(int n) 25 | { 26 | if (n < 0) 27 | return (-1); 28 | if (n == 0 || n == 1) 29 | return (n); 30 | return (sqrt_check(1, n)); 31 | } 32 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prime_check - verify if s is the square root of c 5 | * @s: parameter 6 | * @c: number to find if is a prime numberr 7 | * Return: 1 or 0 8 | */ 9 | 10 | int prime_check(int s, int c) 11 | { 12 | if (s * s > c) 13 | return (1); 14 | if (c % s == 0) 15 | return (0); 16 | return (prime_check(s + 1, c)); 17 | } 18 | /** 19 | * is_prime_number - function that checks if n is a prime number 20 | * @n: parameter 21 | * Return: 1 if n is prime number or 0 otherwise 22 | */ 23 | 24 | int is_prime_number(int n) 25 | { 26 | if (n <= 1) 27 | return (0); 28 | return (prime_check(2, n)); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | ALX | 0x08. C - Recursion 2 | 3 | Task 0: Write a function that prints a string, followed by a new line. 4 | 5 | Task 1: Write a function that prints a string in reverse. 6 | 7 | Task 2: Write a function that returns the length of a string. 8 | 9 | Task 3: Write a function that returns the factorial of a given number. 10 | 11 | Task 4: Write a function that returns the value of x raised to the power of y. 12 | 13 | Task 5: Write a function that returns the natural square root of a number. 14 | 15 | Task 6: Write a function that returns 1 if the input integer is a prime number, otherwise return 0. 16 | 17 | Task 7: Write a function that returns 1 if a string is a palindrome and 0 if not. 18 | 19 | Task 8: Write a function that compares two strings and returns 1 if the strings can be considered identical, otherwise return 0. 20 | 21 | -------------------------------------------------------------------------------- /0x08-recursion/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H 2 | #define _MAIN_H 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 | -------------------------------------------------------------------------------- /0x09-static_libraries/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.c -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_isupper - a function that checks for uppercase character 5 | *@c: single letter input 6 | *Return: 1 if int c is lowercase, 0 if otherwise 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - fills memory with a constant byte 5 | * @s: memory space to be filled 6 | * @b: byte to fill with 7 | * @n: number of spaces to fill 8 | * 9 | * Return: pointer to 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 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concatenates two strings 5 | * @dest: pointer to the destination string 6 | * @src: pointer to the source string 7 | * Return: pointer to the resulting string dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int j, k, l = 0; 13 | 14 | for (k = 0; dest[k] != '\0'; k++) 15 | { 16 | l++; 17 | 18 | } 19 | 20 | for (k = l, j = 0; src[j] != '\0'; j++, k++) 21 | { 22 | dest[k] = src[j]; 23 | } 24 | dest[k] = '\0'; 25 | 26 | return (dest); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_isdigit - a function that checks for digit 5 | *@c: a character 6 | *Return: 1 if int c is a digit, 0 otherwise 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - copies memory area 5 | * @dest: destination of copy 6 | * @src: memory area to copy 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 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates two strings using n bytes 5 | * @dest: pointer to the destination string 6 | * @src: pointer to the source string 7 | *@n: integer 8 | * Return: pointer to the resulting string dest 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int j, k, l = 0; 14 | 15 | for (k = 0; dest[k] != '\0'; k++) 16 | { 17 | l++; 18 | 19 | } 20 | 21 | for (k = l, j = 0; src[j] != '\0' && j < n; j++, k++) 22 | { 23 | dest[k] = src[j]; 24 | } 25 | dest[k] = '\0'; 26 | 27 | return (dest); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string to an integer. 5 | * @s: the string to be comverted. 6 | * 7 | *Return: The integer value of the comverted string. 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int sign = 1; 13 | unsigned int num = 0; 14 | 15 | do { 16 | 17 | if (*s == '-') 18 | sign *= -1; 19 | 20 | else if (*s >= '0' && *s <= '9') 21 | num = (num * 10) + (*s - '0'); 22 | 23 | else if (num > 0) 24 | break; 25 | } while (*s++); 26 | 27 | return (num * sign); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - locates a character in a string 5 | * @s: string to check 6 | * @c: character to check for 7 | * 8 | * Return: pointer to spot in s with c or null 9 | */ 10 | char *_strchr(char *s, char c) 11 | { 12 | int i; 13 | 14 | for (i = 0; *(s + i); i++) 15 | { 16 | if (*(s + i) == c) 17 | return (s + i); 18 | } 19 | if (*(s + i) == c) 20 | return (s + i); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen - return the length of the string 4 | *@s: char type pointer 5 | *Return: length of string 6 | */ 7 | int _strlen(char *s) 8 | { 9 | int i, compt = 0; 10 | 11 | for (i = 0; s[i] != '\0'; i++) 12 | { 13 | compt++; 14 | } 15 | 16 | return (compt); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string up to n 4 | * @dest: Destination of the pointer to the string 5 | * @src: Source of the pointer to the string to copy 6 | * @n: Number of bytes to copy. 7 | * Return: Pointer to the destination string. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; src[i] != '\0' && i < n; i++) 15 | dest[i] = src[i]; 16 | while (i < n) 17 | dest[i++] = '\0'; 18 | return (dest); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_islower - a function that checks for lowercase character 5 | *@c: single letter input 6 | *Return: 1 if int c is lowercase, 0 if otherwise 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - a function that prints a string 5 | * @str: string input 6 | * Return: string 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | while (*str) 12 | { 13 | _putchar(*str++); 14 | } 15 | 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Compare two strings 5 | * @s1: string 6 | * @s2: string 7 | * Return: negative int if s1 < s2, 0 if matching, and positive int if s1 > s2 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i; 13 | 14 | for (i = 0; s1[i] != '\0' || s2[i] != '\0'; i++) 15 | { 16 | if (s1[i] != s2[i]) 17 | { 18 | return (s1[i] - s2[i]); 19 | } 20 | } 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets lengthof a prefix substring 5 | * @s: string to check 6 | * @accept: string to check against 7 | * 8 | * Return: number of bytes of s in accept 9 | */ 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; s[i]; i++) 15 | { 16 | for (j = 0; accept[j]; j++) 17 | { 18 | if (s[i] == accept[j]) 19 | break; 20 | } 21 | if (!accept[j]) 22 | break; 23 | } 24 | return (i); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_isalpha - Check if character is a alphabet character. 5 | *@c: type int character 6 | *Return: 1 if letter, lowercase or uppercase, and 0 otherwise 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | { 13 | return (1); 14 | } 15 | 16 | else 17 | { 18 | return (0); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes 5 | * @s: string to check 6 | * @accept: string to check against 7 | * 8 | * Return: pointer to byte in s that matches or NULL if no match 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; s[i]; i++) 15 | { 16 | for (j = 0; accept[j]; j++) 17 | { 18 | if (s[i] == accept[j]) 19 | break; 20 | } 21 | if (accept[j]) 22 | return (s + i); 23 | } 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - locates a substring 5 | * @haystack: string in which to check for needle 6 | * @needle: substring to find in haystack 7 | * 8 | * Return: pointer to beginning of needle in haystack or NULL if no match 9 | */ 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | unsigned int i = 0, j = 0; 13 | 14 | while (haystack[i]) 15 | { 16 | while (needle[j] && (haystack[i] == needle[0])) 17 | { 18 | if (haystack[i + j] == needle[j]) 19 | j++; 20 | else 21 | break; 22 | } 23 | if (needle[j]) 24 | { 25 | i++; 26 | j = 0; 27 | } 28 | else 29 | { 30 | return (haystack + i); 31 | } 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_abs - compute the absolute value of an integer 5 | *@n: int type number 6 | *Return: absolute value of @n 7 | */ 8 | 9 | int _abs(int n) 10 | { 11 | if (n < 0) 12 | { 13 | return (n * -1); 14 | } 15 | else 16 | { 17 | return (n); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcpy - Copies string 5 | *@dest: destination 6 | *@src: source 7 | *Return: dest 8 | */ 9 | 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i = 0; 13 | 14 | while (*(src + i) != '\0') 15 | { 16 | *(dest + i) = *(src + i); 17 | i++; 18 | } 19 | *(dest + i) = '\0'; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | 0x09. C - Static libraries 2 | 3 | Task 0: Create the static library libmy.a containing all the functions listed: 4 | 5 | 0-isupper.o 6 | 0-memset.o 7 | 0-strcat.o 8 | 100-atoi.o 9 | 1-isdigit.o 10 | 1-memcpy.o 11 | 1-strncat.o 12 | 2-strchr.o 13 | 2-strlen.o 14 | 2-strncpy.o 15 | 3-islower.o 16 | 3-puts.o 17 | 3-strcmp.o 18 | 3-strspn.o 19 | 4-isalpha.o 20 | 4-strpbrk.o 21 | 5-strstr.o 22 | 6-abs.o 23 | 9-strcpy.o 24 | 25 | Task 1: Create a script called create_static_lib.sh that creates a static library called liball.a from all the .c files that are in the current directory 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar rc liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif /* MAIN_H */ 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the program 5 | * @argc: number of arguments passed to the function 6 | * @argv: argument vector of pointers to strings 7 | * 8 | * Return: always 0 9 | */ 10 | int main(int argc __attribute__((unused)), char *argv[]) 11 | { 12 | printf("%s\n", argv[0]); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name number of arguments passed to it 5 | * @argc: number of arguments passed to the function 6 | * @argv: argument vector of pointers to strings 7 | * 8 | * Return: always 0 9 | */ 10 | int main(int argc, char *argv[] __attribute__((unused))) 11 | { 12 | printf("%d\n", argc - 1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints minimum number of coins to make change for an amount of money. 6 | * @argc: number of arguments passed to the function 7 | * @argv: argument vector of pointers to strings 8 | * 9 | * Return: 0 if no errors, else 1 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int a, n = 0, i, t; 14 | int c[5] = {25, 10, 5, 2, 1}; 15 | 16 | if (argc != 2) 17 | { 18 | puts("Error"); 19 | return (1); 20 | } 21 | a = atoi(argv[1]); 22 | if (a <= 0) 23 | { 24 | puts("0"); 25 | return (1); 26 | } 27 | else 28 | { 29 | for (i = 0; i < 5; i++) 30 | { 31 | t = a / c[i]; 32 | a -= t * c[i]; 33 | n += t; 34 | if (a == 0) 35 | break; 36 | } 37 | } 38 | printf("%d\n", n); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all arguments it recieves 5 | * @argc: number of arguments passed to the function 6 | * @argv: argument vector of pointers to strings 7 | * 8 | * Return: always 0 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < argc; i++) 15 | { 16 | printf("%s\n", argv[i]); 17 | } 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - multiplies two numbers 6 | * @argc: number of arguments passed to the function 7 | * @argv: argument vector of pointers to strings 8 | * 9 | * Return: 0 if no errors, else 1 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int a, b, c; 14 | 15 | if (argc != 3) 16 | { 17 | puts("Error"); 18 | return (1); 19 | } 20 | a = atoi(argv[1]); 21 | b = atoi(argv[2]); 22 | c = a * b; 23 | printf("%d\n", c); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - adds positive numbers 6 | *@argc: number of arguments 7 | *@argv: array of arguments 8 | *Return: 0 on success, 1 on failure 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, j, sum = 0; 14 | 15 | for (i = 1; i < argc; i++) 16 | { 17 | for (j = 0; argv[i][j] != '\0'; j++) 18 | { 19 | if (argv[i][j] < '0' || argv[i][j] > '9') 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | } 25 | 26 | sum += atoi(argv[i]); 27 | } 28 | 29 | printf("%d\n", sum); 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | 0x0A. C - argc, argv 2 | 3 | Task 0: Write a program that prints its name, followed by a new line. 4 | 5 | Task 1: Write a program that prints the number of arguments passed into it. 6 | 7 | Task 2: Write a program that prints all arguments it receives. 8 | 9 | Task 3: Write a program that multiplies two numbers 10 | 11 | Task 4: Write a program that adds positive numbers. 12 | 13 | Task 5: Write a program that prints the minimum number of coins to make change for an amount of money. 14 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * create_array - Creates an array of chars and 6 | * initializes it with a specific char. 7 | * @size: The size of the array to be initialized. 8 | * @c: The specific char to intialize the array with. 9 | * 10 | * Return: If size == 0 or the function fails - NULL. 11 | * Otherwise - a pointer to the array. 12 | */ 13 | char *create_array(unsigned int size, char c) 14 | { 15 | char *array; 16 | unsigned int index; 17 | 18 | if (size == 0) 19 | return (NULL); 20 | 21 | array = malloc(sizeof(char) * size); 22 | 23 | if (array == NULL) 24 | return (NULL); 25 | 26 | for (index = 0; index < size; index++) 27 | array[index] = c; 28 | 29 | return (array); 30 | } 31 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strdup - Returns a pointer to a newly-allocated space in memory 6 | * containing a copy of the string given as parameter. 7 | * @str: The string to be copied. 8 | * 9 | * Return: If str == NULL or insufficient memory is available - NULL. 10 | * Otherwise - a pointer to the duplicated string. 11 | */ 12 | char *_strdup(char *str) 13 | { 14 | char *duplicate; 15 | int index, len = 0; 16 | 17 | if (str == NULL) 18 | return (NULL); 19 | 20 | for (index = 0; str[index]; index++) 21 | len++; 22 | 23 | duplicate = malloc(sizeof(char) * (len + 1)); 24 | 25 | if (duplicate == NULL) 26 | return (NULL); 27 | 28 | for (index = 0; str[index]; index++) 29 | duplicate[index] = str[index]; 30 | 31 | duplicate[len] = '\0'; 32 | 33 | return (duplicate); 34 | } 35 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - Concatenates all arguments of the program into a string; 6 | * arguments are separated by a new line in the string. 7 | * @ac: The number of arguments passed to the program. 8 | * @av: An array of pointers to the arguments. 9 | * 10 | * Return: If ac == 0, av == NULL, or the function fails - NULL. 11 | * Otherwise - a pointer to the new string. 12 | */ 13 | char *argstostr(int ac, char **av) 14 | { 15 | char *str; 16 | int arg, byte, index, size = ac; 17 | 18 | if (ac == 0 || av == NULL) 19 | return (NULL); 20 | 21 | for (arg = 0; arg < ac; arg++) 22 | { 23 | for (byte = 0; av[arg][byte]; byte++) 24 | size++; 25 | } 26 | 27 | str = malloc(sizeof(char) * size + 1); 28 | 29 | if (str == NULL) 30 | return (NULL); 31 | 32 | index = 0; 33 | 34 | for (arg = 0; arg < ac; arg++) 35 | { 36 | for (byte = 0; av[arg][byte]; byte++) 37 | str[index++] = av[arg][byte]; 38 | 39 | str[index++] = '\n'; 40 | } 41 | 42 | str[size] = '\0'; 43 | 44 | return (str); 45 | } 46 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - Concatenates two strings. 6 | * @s1: The string to be concatenated upon. 7 | * @s2: The string to be concatenated to s1. 8 | * 9 | * Return: If concatenation fails - NULL. 10 | * Otherwise - a pointer the newly-allocated space in memory 11 | * containing the concatenated strings. 12 | */ 13 | char *str_concat(char *s1, char *s2) 14 | { 15 | char *concat_str; 16 | int index, concat_index = 0, len = 0; 17 | 18 | if (s1 == NULL) 19 | s1 = ""; 20 | 21 | if (s2 == NULL) 22 | s2 = ""; 23 | 24 | for (index = 0; s1[index] || s2[index]; index++) 25 | len++; 26 | 27 | concat_str = malloc(sizeof(char) * len); 28 | 29 | if (concat_str == NULL) 30 | return (NULL); 31 | 32 | for (index = 0; s1[index]; index++) 33 | concat_str[concat_index++] = s1[index]; 34 | 35 | for (index = 0; s2[index]; index++) 36 | concat_str[concat_index++] = s2[index]; 37 | 38 | return (concat_str); 39 | } 40 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - Returns a pointer to a 2-dimensional array of 6 | * integers with each element initalized to 0. 7 | * @width: The width of the 2-dimensional array. 8 | * @height: The height of the 2-dimensional array. 9 | * 10 | * Return: If width <= 0, height <= 0, or the function fails - NULL. 11 | * Otherwise - a pointer to the 2-dimensional array of integers. 12 | */ 13 | int **alloc_grid(int width, int height) 14 | { 15 | int **twoD; 16 | int hgt_index, wid_index; 17 | 18 | if (width <= 0 || height <= 0) 19 | return (NULL); 20 | 21 | twoD = malloc(sizeof(int *) * height); 22 | 23 | if (twoD == NULL) 24 | return (NULL); 25 | 26 | for (hgt_index = 0; hgt_index < height; hgt_index++) 27 | { 28 | twoD[hgt_index] = malloc(sizeof(int) * width); 29 | 30 | if (twoD[hgt_index] == NULL) 31 | { 32 | for (; hgt_index >= 0; hgt_index--) 33 | free(twoD[hgt_index]); 34 | 35 | free(twoD); 36 | return (NULL); 37 | } 38 | } 39 | 40 | for (hgt_index = 0; hgt_index < height; hgt_index++) 41 | { 42 | for (wid_index = 0; wid_index < width; wid_index++) 43 | twoD[hgt_index][wid_index] = 0; 44 | } 45 | 46 | return (twoD); 47 | } 48 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - Frees a 2-dimensional array of integers. 6 | * @grid: The 2-dimensional array of integers to be freed. 7 | * @height: The height of grid. 8 | */ 9 | void free_grid(int **grid, int height) 10 | { 11 | int index; 12 | 13 | for (index = 0; index < height; index++) 14 | free(grid[index]); 15 | 16 | free(grid); 17 | } 18 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0B. C - malloc, free 2 | 3 | Task 0: Write a function that creates an array of chars, and initializes it with a specific char. 4 | 5 | Task 1: Write a function that returns a pointer to a newly allocated space in memory, which contains a copy of the string given as a parameter. 6 | 7 | Task 2: Write a function that concatenates two strings. 8 | 9 | Task 3: Write a function that returns a pointer to a 2 dimensional array of integers. 10 | 11 | Task 4: Write a function that frees a 2 dimensional grid previously created by your alloc_grid function.. 12 | 13 | Task 5: Write a function that concatenates all the arguments of your program. 14 | 15 | Task 6: Write a function that splits a string into words. 16 | 17 | -------------------------------------------------------------------------------- /0x0B-malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | char **strtow(char *str); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * malloc_checked - Allocates memory using malloc 6 | * @b: The number of bytes to be allocated 7 | * 8 | * Return: A pointer to the allocated memory. 9 | */ 10 | 11 | void *malloc_checked(unsigned int b) 12 | { 13 | void *p = malloc(b); 14 | 15 | if (p == NULL) 16 | exit(98); 17 | 18 | return (p); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - Concatenates two strings using at 6 | * most an inputted number of bytes. 7 | * @s1: The first string. 8 | * @s2: The second string. 9 | * @n: The maximum number of bytes of s2 to concatenate to s1. 10 | * 11 | * Return: If the function fails - NULL. 12 | * Otherwise - a pointer to the concatenated space in memory. 13 | */ 14 | char *string_nconcat(char *s1, char *s2, unsigned int n) 15 | { 16 | char *concat; 17 | unsigned int len = n, index; 18 | 19 | if (s1 == NULL) 20 | s1 = ""; 21 | 22 | if (s2 == NULL) 23 | s2 = ""; 24 | 25 | for (index = 0; s1[index]; index++) 26 | len++; 27 | 28 | concat = malloc(sizeof(char) * (len + 1)); 29 | 30 | if (concat == NULL) 31 | return (NULL); 32 | 33 | len = 0; 34 | 35 | for (index = 0; s1[index]; index++) 36 | concat[len++] = s1[index]; 37 | 38 | for (index = 0; s2[index] && index < n; index++) 39 | concat[len++] = s2[index]; 40 | 41 | concat[len] = '\0'; 42 | 43 | return (concat); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _realloc - Reallocates a memory block using malloc and free. 6 | * @ptr: A pointer to the memory previously allocated. 7 | * @old_size: The size in bytes of the allocated space for ptr. 8 | * @new_size: The size in bytes for the new memory block. 9 | * 10 | * Return: If new_size == old_size - ptr. 11 | * If new_size == 0 and ptr is not NULL - NULL. 12 | * Otherwise - a pointer to the reallocated memory block. 13 | */ 14 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 15 | { 16 | void *mem; 17 | char *ptr_copy, *filler; 18 | unsigned int index; 19 | 20 | if (new_size == old_size) 21 | return (ptr); 22 | 23 | if (ptr == NULL) 24 | { 25 | mem = malloc(new_size); 26 | 27 | if (mem == NULL) 28 | return (NULL); 29 | 30 | return (mem); 31 | } 32 | 33 | if (new_size == 0 && ptr != NULL) 34 | { 35 | free(ptr); 36 | return (NULL); 37 | } 38 | 39 | ptr_copy = ptr; 40 | mem = malloc(sizeof(*ptr_copy) * new_size); 41 | 42 | if (mem == NULL) 43 | { 44 | free(ptr); 45 | return (NULL); 46 | } 47 | 48 | filler = mem; 49 | 50 | for (index = 0; index < old_size && index < new_size; index++) 51 | filler[index] = *ptr_copy++; 52 | 53 | free(ptr); 54 | return (mem); 55 | } 56 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x0C-more_malloc_free/101-mul.o -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _calloc - Allocates memory for an array of a certain number 6 | * of elements each of an inputted byte size. 7 | * @nmemb: The number of elements. 8 | * @size: The byte size of each array element. 9 | * 10 | * Return: If nmemb = 0, size = 0, or the function fails - NULL. 11 | * Otherwise - a pointer to the allocated memory. 12 | */ 13 | 14 | void *_calloc(unsigned int nmemb, unsigned int size) 15 | { 16 | void *mem; 17 | char *filler; 18 | unsigned int index; 19 | 20 | if (nmemb == 0 || size == 0) 21 | return (NULL); 22 | 23 | mem = malloc(size * nmemb); 24 | 25 | if (mem == NULL) 26 | return (NULL); 27 | 28 | filler = mem; 29 | 30 | for (index = 0; index < (size * nmemb); index++) 31 | filler[index] = '\0'; 32 | 33 | return (mem); 34 | } 35 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * array_range - Creates an array of integers ordered 6 | * from min to max, inclusive. 7 | * @min: The first value of the array. 8 | * @max: The last value of the array. 9 | * 10 | * Return: If min > max or the function fails - NULL. 11 | * Otherwise - a pointer to the newly created array. 12 | */ 13 | 14 | int *array_range(int min, int max) 15 | { 16 | int *array, index, size; 17 | 18 | if (min > max) 19 | return (NULL); 20 | 21 | size = max - min + 1; 22 | 23 | array = malloc(sizeof(int) * size); 24 | 25 | if (array == NULL) 26 | return (NULL); 27 | 28 | for (index = 0; index < size; index++) 29 | array[index] = min++; 30 | 31 | return (array); 32 | } 33 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0C. C - More malloc, free 2 | 3 | 4 | 5 | Task 0: Write a function that allocates memory using malloc 6 | 7 | 8 | 9 | Task 1: Write a function that concatenates two strings. 10 | 11 | 12 | 13 | Task 2: Write a function that allocates memory for an array, using malloc. 14 | 15 | 16 | 17 | Task 3: Write a function that creates an array of integers. 18 | 19 | 20 | 21 | Task 4: Write a function that reallocates a memory block using malloc and free 22 | 23 | 24 | 25 | Task 5: Write a program that multiplies two positive numbers. 26 | 27 | 28 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void _puts(char *s); 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 | int _putchar(char c); 11 | int _atoi(const char *s); 12 | void print_int(unsigned long int n); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | #define SIZE 1024 5 | 6 | #endif /* OBJECT_LIKE_MACRO_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif /* PI_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the name of the file it was 4 | * compiled from, followes by anew line. 5 | * Return: 0 if success 6 | */ 7 | int main(void) 8 | { 9 | printf("%s\n", __FILE__); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x < 0) ? -(x) : x) 5 | 6 | #endif /* FUNCTION_LIKE_MACRO_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) (x + y) 5 | 6 | #endif /* SUM_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | 0x0D. C - Preprocessor 2 | 3 | 4 | Task 0: Create a header file that defines a macro named SIZE as an abbreviation for the token 1024. 5 | 6 | Task 1: Create a header file that defines a macro named PI as an abbreviation for the token 3.14159265359. 7 | 8 | Task 2: Write a program that prints the name of the file it was compiled from, followed by a new line. 9 | 10 | Task 3: Write a function-like macro ABS(x) that computes the absolute value of a number x. 11 | 12 | Task 4: Write a function-like macro SUM(x, y) that computes the sum of the numbers x and y. 13 | 14 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | /** 3 | * init_dog - initialize a variable of type struct dog 4 | * @d: a pointer to a dog structure 5 | * @name: the name to set 6 | * @age: age to set 7 | * @owner: owner to set 8 | * 9 | * Return: Nothing after setting 10 | */ 11 | void init_dog(struct dog *d, char *name, float age, char *owner) 12 | { 13 | /* I verifie if @d is not null */ 14 | if (!d) 15 | return; 16 | /*since d is not NULL i initialize */ 17 | d->name = name; 18 | d->age = age; 19 | d->owner = owner; 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * print_dog - prints a struct dog 5 | * @d: a pointer to a dog to print 6 | * 7 | * Return: nothing 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | /*declare nil variable for NULL value */ 12 | char *nil = "(nil)"; 13 | 14 | /*check if d is not NULL */ 15 | if (d == NULL) 16 | return; 17 | /* printing value if they exist otherwise nil */ 18 | printf("Name: %s\n", (d->name) ? d->name : nil); 19 | printf("Age: %f\n", d->age); 20 | printf("Owner: %s\n", (d->owner) ? d->owner : nil); 21 | } 22 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | * free_dog - frees a structure of type dog 5 | * @d: a pointer to the the dog struct 6 | * Return: nothing 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d == NULL) 11 | return; 12 | free(d->name); 13 | free(d->owner); 14 | free(d); 15 | } 16 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | 0x0E. C - Structures, typedef 2 | 3 | 4 | Task 0: Define a new type struct dog with the given elements 5 | 6 | Task 1: Write a function that initialize a variable of type struct dog 7 | 8 | Task 2: Write a function that prints a struct dog 9 | 10 | Task 3: Define a new type dog_t as a new name for the type struct dog. 11 | 12 | Task 4: Write a function that creates a new dog. 13 | 14 | Task 5: Write a function that frees dogs. 15 | 16 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * struct dog - a data type with poppy structure 6 | * @name: name of the dog 7 | * @age: age of the dog 8 | * @owner: owner of the dog 9 | * 10 | * Description: represente a dog, his name, age and his owner 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 | 29 | #endif /* DOG_H */ 30 | -------------------------------------------------------------------------------- /0x0F-function_pointers/#function_pointers.h#: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | 4 | gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-print_name.c -o a 5 | void print_name(char *name, void (*f)(char *)); 6 | int _putchar(int c); 7 | void array_iterator(int *array, size_t size, void (*action)(int)); 8 | int int_index(int *array, int size, int (*cmp)(int)); 9 | int (*get_op_func(char *s))(int, int); 10 | int op_add(int a, int b); 11 | int op_sub(int a, int b); 12 | int op_mul(int a, int b); 13 | int op_div(int a, int b); 14 | int op_mod(int a, int b); 15 | 16 | #endif /* FUNCTION_POINTERS_H */ 17 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_name_as_is - prints a name as is 6 | * @name: name of the person 7 | * 8 | * Return: Nothing. 9 | */ 10 | void print_name_as_is(char *name) 11 | { 12 | printf("Hello, my name is %s\n", name); 13 | } 14 | 15 | /** 16 | * print_name_uppercase - print a name in uppercase 17 | * @name: name of the person 18 | * 19 | * Return: Nothing. 20 | */ 21 | void print_name_uppercase(char *name) 22 | { 23 | unsigned int i; 24 | 25 | printf("Hello, my uppercase name is "); 26 | i = 0; 27 | while (name[i]) 28 | { 29 | if (name[i] >= 'a' && name[i] <= 'z') 30 | { 31 | putchar(name[i] + 'A' - 'a'); 32 | } 33 | else 34 | { 35 | putchar(name[i]); 36 | } 37 | i++; 38 | } 39 | } 40 | 41 | /** 42 | * main - check the code for ALX School students. 43 | * 44 | * Return: Always 0. 45 | */ 46 | int main(void) 47 | { 48 | print_name("Bob", print_name_as_is); 49 | print_name("Bob Dylan", print_name_uppercase); 50 | printf("\n"); 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | /** 3 | * print_name - prints a name 4 | * @name: string as a name 5 | * @f: pointer to a function 6 | * Return: nothing 7 | */ 8 | void print_name(char *name, void (*f)(char *)) 9 | { 10 | if (name && f) 11 | f(name); 12 | } 13 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | /** 3 | * array_iterator - execute a function on array's element 4 | * @array: array 5 | * @size: size of the array 6 | * @action: function to execute 7 | * Return: nothing 8 | */ 9 | void array_iterator(int *array, size_t size, void (*action)(int)) 10 | { 11 | size_t i = 0; 12 | 13 | if (array && action) 14 | { 15 | for (; i < size; i++) 16 | action(array[i]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_elem - prints an integer 6 | * @elem: the integer to print 7 | * 8 | * Return: Nothing. 9 | */ 10 | void print_elem(int elem) 11 | { 12 | printf("%d\n", elem); 13 | } 14 | 15 | /** 16 | * print_elem_hex - prints an integer, in hexadecimal 17 | * @elem: the integer to print 18 | * 19 | * Return: Nothing. 20 | */ 21 | void print_elem_hex(int elem) 22 | { 23 | printf("0x%x\n", elem); 24 | } 25 | 26 | /** 27 | * main - check the code for ALX School students. 28 | * 29 | * Return: Always 0. 30 | */ 31 | int main(void) 32 | { 33 | int array[5] = {0, 98, 402, 1024, 4096}; 34 | 35 | array_iterator(array, 5, &print_elem); 36 | array_iterator(array, 5, &print_elem_hex); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - number of bytes 6 | *@argc: argument counter 7 | *@argv: argument vector 8 | *Return: 0 9 | **/ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int byte, i; 14 | int (*code_addr)(int, char **) = main; 15 | unsigned char code; 16 | 17 | if (argc < 2) 18 | { 19 | printf("Error\n"); 20 | exit(1); 21 | } 22 | 23 | byte = atoi(argv[1]); 24 | 25 | if (byte < 0) 26 | { 27 | printf("Error\n"); 28 | exit(2); 29 | } 30 | for (i = 0; i < byte; i++) 31 | { 32 | code = *(unsigned char *)code_addr; 33 | printf("%.2x", code); 34 | code_addr++; 35 | if (i != byte - 1) 36 | putchar(' '); 37 | } 38 | putchar('\n'); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | /** 3 | * int_index - searches for an integer within an array 4 | * @array: pointer to an array 5 | * @size: size of the array 6 | * @cmp: pointer to the function to use to compare values 7 | * Return: the index of the first element for which the cmp 8 | * function does not return 0 9 | */ 10 | int int_index(int *array, int size, int (*cmp)(int)) 11 | { 12 | int loc, flag = 0; 13 | 14 | if (array == NULL || cmp == NULL) 15 | return (-1); 16 | if (size <= 0) 17 | return (-1); 18 | for (loc = 0; loc < size; loc++) 19 | { 20 | flag = cmp(array[loc]); 21 | if (flag != 0) 22 | return (loc); 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * is_98 - check if a number is equal to 98 6 | * @elem: the integer to check 7 | * 8 | * Return: 0 if false, something else otherwise. 9 | */ 10 | int is_98(int elem) 11 | { 12 | return (98 == elem); 13 | } 14 | 15 | /** 16 | * is_strictly_positive - check if a number is greater than 0 17 | * @elem: the integer to check 18 | * 19 | * Return: 0 if false, something else otherwise. 20 | */ 21 | int is_strictly_positive(int elem) 22 | { 23 | return (elem > 0); 24 | } 25 | 26 | 27 | /** 28 | * abs_is_98 - check if the absolute value of a number is 98 29 | * @elem: the integer to check 30 | * 31 | * Return: 0 if false, something else otherwise. 32 | */ 33 | int abs_is_98(int elem) 34 | { 35 | return (elem == 98 || -elem == 98); 36 | } 37 | 38 | /** 39 | * main - check the code for ALX School students. 40 | * 41 | * Return: Always 0. 42 | */ 43 | int main(void) 44 | { 45 | int array[20] = {0, -98, 98, 402, 1024, 4096, -1024, -98, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 98}; 46 | int index; 47 | 48 | index = int_index(array, 20, is_98); 49 | printf("%d\n", index); 50 | index = int_index(array, 20, abs_is_98); 51 | printf("%d\n", index); 52 | index = int_index(array, 20, is_strictly_positive); 53 | printf("%d\n", index); 54 | return (0); 55 | } 56 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct op - Struct op 9 | * 10 | * @op: The operator 11 | * @f: The function associated 12 | */ 13 | typedef struct op 14 | { 15 | char *op; 16 | int (*f)(int a, int b); 17 | } op_t; 18 | 19 | int (*get_op_func(char *s))(int, int); 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 | 26 | #endif /* CALC_H */ 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | /** 3 | * get_op_func - selects the correct function to perform the operation 4 | * asked by the user. 5 | * @s: String containing the given pamareter 6 | * Return: a pointer to the function that corresponds 7 | * to the operator given as a parameter 8 | */ 9 | int (*get_op_func(char *s))(int a, int b) 10 | { 11 | op_t ops[] = { 12 | {"+", op_add}, 13 | {"-", op_sub}, 14 | {"*", op_mul}, 15 | {"/", op_div}, 16 | {"%", op_mod}, 17 | {NULL, NULL} 18 | }; 19 | int i = 0; 20 | 21 | while ((i < 5 && s != NULL) && (s[1] == '\0')) 22 | { 23 | if (ops[i].op[0] == s[0]) 24 | return (ops[i].f); 25 | i++; 26 | } 27 | return (NULL); 28 | } 29 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | /** 3 | * main - program that performs simple operations. 4 | * @argc: number of arguments 5 | * @argv: array of arguments 6 | * Return: 0 if success otherwise exit with error number 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | int a, b; 11 | /* @p is a function pointer that point to the given operation */ 12 | int (*p)(int, int); 13 | 14 | if (argc != 4) 15 | { 16 | printf("Error\n"); 17 | exit(98); 18 | } 19 | a = atoi(argv[1]); 20 | b = atoi(argv[3]); 21 | p = get_op_func(argv[2]); 22 | if (p == NULL) 23 | { 24 | printf("Error\n"); 25 | exit(99); 26 | } 27 | if ((p == op_div || p == op_mod) && b == 0) 28 | { 29 | printf("Error\n"); 30 | exit(100); 31 | } 32 | printf("%d\n", p(a, b)); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | /** 3 | * op_add - returns the sum of a and b 4 | * @a: first operand 5 | * @b: the second operand 6 | * Return: the sum 7 | */ 8 | int op_add(int a, int b) 9 | { 10 | return (a + b); 11 | } 12 | /** 13 | * op_sub - returns the difference of a and b 14 | * @a: first operand 15 | * @b: second operand 16 | * Return: the difference 17 | */ 18 | int op_sub(int a, int b) 19 | { 20 | return (a - b); 21 | } 22 | /** 23 | * op_mul - returns the product of a and b 24 | * @a: first operand 25 | * @b: second operand 26 | * Return: the product 27 | */ 28 | int op_mul(int a, int b) 29 | { 30 | return (a * b); 31 | } 32 | /** 33 | * op_div - returns the result of the division of a by b 34 | * @a: first operand 35 | * @b: second operand 36 | * Return: the quotient 37 | */ 38 | int op_div(int a, int b) 39 | { 40 | return (a / b); 41 | } 42 | /** 43 | * op_mod - returns the remainder of the division of a by b 44 | * @a: first operand 45 | * @b: second operand 46 | * Return: the remainder as integer 47 | */ 48 | int op_mod(int a, int b) 49 | { 50 | return (a % b); 51 | } 52 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | ALX | 0x0F - Function pointers 2 | 3 | Task 0: Write a function that prints a name. 4 | 5 | Task 1: Write a function that executes a function given as a parameter on each element of an array. 6 | 7 | Task 2: Write a function that searches for an integer. 8 | 9 | Task 3: Write a program that performs simple operations. 10 | 11 | Task 4: Write a program that prints the opcodes of its own main function. 12 | 13 | -------------------------------------------------------------------------------- /0x0F-function_pointers/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x0F-function_pointers/a -------------------------------------------------------------------------------- /0x0F-function_pointers/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x0F-function_pointers/b -------------------------------------------------------------------------------- /0x0F-function_pointers/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x0F-function_pointers/c -------------------------------------------------------------------------------- /0x0F-function_pointers/calc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x0F-function_pointers/calc -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | 4 | #include 5 | 6 | void print_name(char *name, void (*f)(char *)); 7 | int _putchar(int c); 8 | void array_iterator(int *array, size_t size, void (*action)(int)); 9 | int int_index(int *array, int size, int (*cmp)(int)); 10 | int (*get_op_func(char *s))(int, int); 11 | int op_add(int a, int b); 12 | int op_sub(int a, int b); 13 | int op_mul(int a, int b); 14 | int op_div(int a, int b); 15 | int op_mod(int a, int b); 16 | 17 | #endif /* FUNCTION_POINTERS_H */ 18 | -------------------------------------------------------------------------------- /0x0F-function_pointers/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x0F-function_pointers/main -------------------------------------------------------------------------------- /0x10-variadic_functions/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | 13 | sum = sum_them_all(2, 98, 1024); 14 | printf("%d\n", sum); 15 | sum = sum_them_all(4, 98, 1024, 402, -1024); 16 | printf("%d\n", sum); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | /** 3 | * sum_them_all - a function that returns the sum of all its parameters. 4 | * @n: the number of parameters 5 | * 6 | * Return: the sum or 0 if @n is 0 7 | */ 8 | int sum_them_all(const unsigned int n, ...) 9 | { 10 | va_list args_ptr; 11 | int nplus, sum = 0; 12 | unsigned int args = 0; 13 | 14 | va_start(args_ptr, n); 15 | if (n == 0) 16 | return (0); 17 | while (args < n) 18 | { 19 | nplus = va_arg(args_ptr, int); 20 | sum = sum + nplus; 21 | args++; 22 | } 23 | va_end(args_ptr); 24 | return (sum); 25 | } 26 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for ALX School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(", ", 4, 0, 98, -1024, 402); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | /** 4 | * print_numbers - prints numbers, followed by a new line. 5 | * @separator: string to print between numbers 6 | * @n: number of integers passed to the function 7 | * Return: nothing 8 | */ 9 | void print_numbers(const char *separator, const unsigned int n, ...) 10 | { 11 | unsigned int i = 0; 12 | va_list arg_ptr; 13 | 14 | if (n == 0) 15 | { 16 | printf("\n"); 17 | return; 18 | } 19 | va_start(arg_ptr, n); 20 | while (i < n) 21 | { 22 | if (i != 0 && separator) 23 | printf("%s", separator); 24 | printf("%d", (int)va_arg(arg_ptr, int)); 25 | i++; 26 | } 27 | va_end(arg_ptr); 28 | printf("\n"); 29 | } 30 | -------------------------------------------------------------------------------- /0x10-variadic_functions/100-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | section .data 2 | msg db "Hello, Holberton", `\n` 3 | 4 | section .text 5 | global main 6 | main: 7 | mov rax, 1 8 | mov rdi, 1 9 | mov rsi, msg 10 | mov rdx, 17 11 | syscall 12 | mov rax, 60 13 | mov rdi, 0 14 | syscall 15 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for ALX School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_strings(", ", 2, "Jay", "Django"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | /** 3 | * print_strings - prints strings, followed by a new line. 4 | * @separator: string to print between strings 5 | * @n: number of integers passed to the function 6 | * Return: nothing 7 | */ 8 | void print_strings(const char *separator, const unsigned int n, ...) 9 | { 10 | unsigned int i = 0; 11 | va_list arg_ptr; 12 | char *s_arg; 13 | 14 | if (n == 0) 15 | { 16 | printf("\n"); 17 | return; 18 | } 19 | va_start(arg_ptr, n); 20 | while (i < n) 21 | { 22 | s_arg = va_arg(arg_ptr, char *); 23 | if (i != 0 && separator != NULL) 24 | printf("%s", separator); 25 | if (s_arg != NULL) 26 | printf("%s", s_arg); 27 | else 28 | printf("(nil)"); 29 | i++; 30 | } 31 | va_end(arg_ptr); 32 | printf("\n"); 33 | } 34 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | /** 3 | * main - check the code for ALX School students. 4 | * 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | print_all("ceis", 'H', 0, "lberton"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | ALX | Variadic functions 2 | 3 | 4 | Task 0: Write a function that returns the sum of all its parameters. 5 | 6 | Task 1: Write a function that prints numbers, followed by a new line. 7 | 8 | Task 2: Write a function that prints strings, followed by a new line. 9 | 10 | Task 3: Write a function that prints anything. 11 | 12 | -------------------------------------------------------------------------------- /0x10-variadic_functions/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x10-variadic_functions/a -------------------------------------------------------------------------------- /0x10-variadic_functions/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x10-variadic_functions/b -------------------------------------------------------------------------------- /0x10-variadic_functions/b.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | /** 4 | * print_all - prints anything 5 | * @format: list of types per arguments passed 6 | * Return: nothing 7 | */ 8 | void print_all(const char * const format, ...) 9 | { 10 | int i = 0, len, flag; 11 | char *print; 12 | va_list p_arg; 13 | 14 | if (format == NULL) 15 | return; 16 | len = strlen(format); 17 | va_start(p_arg, format); 18 | while (i < len) 19 | { 20 | flag = 1; 21 | while (flag && i < len) 22 | { 23 | switch (format[i]) 24 | { 25 | case 'c': 26 | printf("%c", (char)va_arg(p_arg, int)); 27 | flag = 0; 28 | break; 29 | case 'i': 30 | printf("%d", (int)va_arg(p_arg, int)); 31 | flag = 0; 32 | break; 33 | case 'f': 34 | printf("%f", (double)va_arg(p_arg, double)); 35 | flag = 0; 36 | break; 37 | case 's': 38 | print = (char *)va_arg(p_arg, char*); 39 | if (print == NULL) 40 | { 41 | printf("(nil)"); 42 | break; 43 | } 44 | printf("%s", print); 45 | flag = 0; 46 | break; 47 | default: 48 | flag = 1; 49 | } 50 | if (i < len - 1 && flag == 0) 51 | printf(", "); 52 | i++; 53 | } 54 | } 55 | printf("\n"); 56 | va_end(p_arg); 57 | } 58 | -------------------------------------------------------------------------------- /0x10-variadic_functions/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x10-variadic_functions/c -------------------------------------------------------------------------------- /0x10-variadic_functions/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x10-variadic_functions/d -------------------------------------------------------------------------------- /0x10-variadic_functions/p_functions.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | /** 3 | * p_char - print a character 4 | * @arg: argument to print 5 | * Return: Nothing 6 | */ 7 | void p_char(va_list arg) 8 | { 9 | printf("%c", (char)va_arg(arg, int)); 10 | } 11 | /** 12 | * p_int - print an integer 13 | * @arg: argument to print 14 | * Return: Nothing 15 | */ 16 | void p_int(va_list arg) 17 | { 18 | printf("%d", va_arg(arg, int)); 19 | } 20 | /** 21 | * p_float - print float 22 | * @arg: argument to print 23 | * Return: Nothing 24 | */ 25 | void p_float(va_list arg) 26 | { 27 | printf("%f", (double)va_arg(arg, double)); 28 | } 29 | /** 30 | * p_strings - print strings 31 | * @arg: argument to print 32 | * Return: Nothing 33 | */ 34 | void p_strings(va_list arg) 35 | { 36 | char *s = (char *)va_arg(arg, char *); 37 | 38 | printf("%s", (s) ? s : "(nil)"); 39 | } 40 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct pt - Struct of printer type 9 | * 10 | * @c: Type to print 11 | * @pf: The function associated 12 | */ 13 | typedef struct pt 14 | { 15 | char c; 16 | void (*pf)(va_list); 17 | } f_print; 18 | 19 | void (*get_printer(char f))(va_list); 20 | void p_strings(va_list arg); 21 | void p_float(va_list arg); 22 | void p_int(va_list arg); 23 | void p_char(va_list arg); 24 | int sum_them_all(const unsigned int n, ...); 25 | void print_numbers(const char *separator, const unsigned int n, ...); 26 | void print_strings(const char *separator, const unsigned int n, ...); 27 | void print_all(const char * const format, ...); 28 | 29 | #endif /* VARIADIC_FUNCTIONS_H */ 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_list - prints all the elements of a list_t list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t print_list(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | if (h->str == NULL) 16 | printf("[%d] %s\n", 0, "(nil)"); 17 | else 18 | printf("[%d] %s\n", h->len, h->str); 19 | h = h->next; 20 | nelem++; 21 | } 22 | return (nelem); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * list_len - returns then number of elements in a list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t list_len(const list_t *h) 9 | { 10 | size_t number_of_nodes; 11 | 12 | number_of_nodes = 0; 13 | while (h != NULL) 14 | { 15 | h = h->next; 16 | number_of_nodes++; 17 | } 18 | return (number_of_nodes); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * first - function executed before main 5 | * Return: no return. 6 | */ 7 | 8 | void __attribute__ ((constructor)) first() 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node - adds a new node at the beginning 4 | * of a list_t list. 5 | * @head: head of the linked list. 6 | * @str: string to store in the list. 7 | * Return: address of the head. 8 | */ 9 | 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *new_node; 13 | size_t n; 14 | 15 | new_node = malloc(sizeof(list_t)); 16 | if (new_node == NULL) 17 | return (NULL); 18 | 19 | new_node->str = strdup(str); 20 | 21 | for (n = 0; str[n]; n++) 22 | ; 23 | 24 | new_node->len = n; 25 | new_node->next = *head; 26 | *head = new_node; 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - adds a new node at the end 5 | * of a list_t list. 6 | * @head: head of the linked list. 7 | * @str: string to store in the list. 8 | * Return: address of the head. 9 | */ 10 | 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *new_node, *current_node; 14 | size_t n; 15 | 16 | new_node = malloc(sizeof(list_t)); 17 | if (new_node == NULL) 18 | return (NULL); 19 | 20 | new_node->str = strdup(str); 21 | 22 | for (n = 0; str[n]; n++) 23 | ; 24 | 25 | new_node->len = n; 26 | new_node->next = NULL; 27 | current_node = *head; 28 | 29 | if (current_node == NULL) 30 | { 31 | *head = new_node; 32 | } 33 | else 34 | { 35 | while (current_node->next != NULL) 36 | current_node = current_node->next; 37 | current_node->next = new_node; 38 | } 39 | 40 | return (*head); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list 5 | * @head: head of the linked list. 6 | * Return: no return. 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *current_node; 12 | 13 | while ((current_node = head) != NULL) 14 | { 15 | head = head->next; 16 | free(current_node->str); 17 | free(current_node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | ALX || 0x12. C - Singly linked lists 2 | 3 | Task 0 : Write a function that prints all the elements of a list_t list. 4 | 5 | Task 1: Write a function that returns the number of elements in a linked list_t list. 6 | 7 | Task 2: Write a function that adds a new node at the beginning of a list_t list. 8 | 9 | Task 3: Write a function that adds a new node at the end of a list_t list. 10 | 11 | Task 4: Write a function that frees a list_t list. 12 | 13 | Task 5: Write a function that prints You're beat! and yet, you must allow,\nI bore my house upon my back!\n before the main function is executed. 14 | 15 | Task 6: Write a 64-bit program in assembly that prints Hello, Holberton, followed by a new line. 16 | 17 | 18 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | */ 16 | typedef struct list_s 17 | { 18 | char *str; 19 | unsigned int len; 20 | struct list_s *next; 21 | } list_t; 22 | 23 | size_t print_list(const list_t *h); 24 | size_t list_len(const list_t *h); 25 | list_t *add_node(list_t **head, const char *str); 26 | list_t *add_node_end(list_t **head, const char *str); 27 | void free_list(list_t *head); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /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: head of the list 6 | * 7 | * Return: the number of nodes 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | const listint_t *cursor = h; 12 | size_t count = 0; 13 | 14 | while (cursor != NULL) 15 | { 16 | printf("%d\n", cursor->n); 17 | count += 1; 18 | cursor = cursor->next; 19 | } 20 | return (count); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - counts the number of nodes in a linked list 5 | * @h: head of the list 6 | * 7 | * Return: the number of elements 8 | */ 9 | size_t listint_len(const listint_t *h) 10 | { 11 | const listint_t *cursor = h; 12 | size_t count = 0; 13 | 14 | while (cursor != NULL) 15 | { 16 | count += 1; 17 | cursor = cursor->next; 18 | } 19 | return (count); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * delete_nodeint_at_index - deletes the node by a given 4 | * index of a linked list 5 | * @head : pointer to the head of the list 6 | * @index: index of the node to be deleted 7 | * (indices starting at 0) 8 | * Return: 1 - if function succeeds, 9 | * -1 - if function fails 10 | */ 11 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 12 | { 13 | listint_t *tmp, *copy = *head; 14 | unsigned int node; 15 | 16 | if (copy == NULL) 17 | return (-1); 18 | 19 | if (index == 0) 20 | { 21 | *head = (*head)->next; 22 | free(copy); 23 | return (1); 24 | } 25 | 26 | for (node = 0; node < (index - 1); node++) 27 | { 28 | if (copy->next == NULL) 29 | return (-1); 30 | 31 | copy = copy->next; 32 | } 33 | 34 | tmp = copy->next; 35 | copy->next = tmp->next; 36 | free(tmp); 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: pointer to the first node. 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *p; 12 | listint_t *n; 13 | 14 | p = NULL; 15 | n = NULL; 16 | 17 | while (*head != NULL) 18 | { 19 | n = (*head)->next; 20 | (*head)->next = p; 21 | p = *head; 22 | *head = n; 23 | } 24 | 25 | *head = p; 26 | return (*head); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * print_listint_safe - prints a linked list. 28 | * @head: head of a list. 29 | * 30 | * Return: number of nodes in the list. 31 | */ 32 | size_t print_listint_safe(const listint_t *head) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | 37 | hptr = NULL; 38 | while (head != NULL) 39 | { 40 | new = malloc(sizeof(listp_t)); 41 | 42 | if (new == NULL) 43 | exit(98); 44 | 45 | new->p = (void *)head; 46 | new->next = hptr; 47 | hptr = new; 48 | 49 | add = hptr; 50 | 51 | while (add->next != NULL) 52 | { 53 | add = add->next; 54 | if (head == add->p) 55 | { 56 | printf("-> [%p] %d\n", (void *)head, head->n); 57 | free_listp(&hptr); 58 | return (nnodes); 59 | } 60 | } 61 | 62 | printf("[%p] %d\n", (void *)head, head->n); 63 | head = head->next; 64 | nnodes++; 65 | } 66 | 67 | free_listp(&hptr); 68 | return (nnodes); 69 | } 70 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp2(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * free_listint_safe - frees a linked list. 28 | * @h: head of a list. 29 | * 30 | * Return: size of the list that was freed. 31 | */ 32 | size_t free_listint_safe(listint_t **h) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | listint_t *curr; 37 | 38 | hptr = NULL; 39 | while (*h != NULL) 40 | { 41 | new = malloc(sizeof(listp_t)); 42 | 43 | if (new == NULL) 44 | exit(98); 45 | 46 | new->p = (void *)*h; 47 | new->next = hptr; 48 | hptr = new; 49 | 50 | add = hptr; 51 | 52 | while (add->next != NULL) 53 | { 54 | add = add->next; 55 | if (*h == add->p) 56 | { 57 | *h = NULL; 58 | free_listp2(&hptr); 59 | return (nnodes); 60 | } 61 | } 62 | 63 | curr = *h; 64 | *h = (*h)->next; 65 | free(curr); 66 | nnodes++; 67 | } 68 | 69 | *h = NULL; 70 | free_listp2(&hptr); 71 | return (nnodes); 72 | } 73 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * find_listint_loop - finds the loop contained 4 | * in a linked list 5 | * @head : pointer to the head of the linked list 6 | * Return: pointer the node where the loop starts, 7 | * or NULL - if there is no loop 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *node1, *node2; 12 | 13 | if (head == NULL || head->next == NULL) 14 | return (NULL); 15 | 16 | node1 = head->next; 17 | node2 = (head->next)->next; 18 | 19 | while (node2) 20 | { 21 | if (node1 == node2) 22 | { 23 | node1 = head; 24 | 25 | while (node1 != node2) 26 | { 27 | node1 = node1->next; 28 | node2 = node2->next; 29 | } 30 | 31 | return (node1); 32 | } 33 | 34 | node1 = node1->next; 35 | node2 = (node2->next)->next; 36 | } 37 | 38 | return (NULL); 39 | } 40 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - adds a node to the beginning of a linked list 5 | * @head: pointer to the head of the list 6 | * @n: integer to be used as content 7 | * 8 | * Return: address of the newly added node 9 | */ 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | listint_t *new_node; 13 | 14 | new_node = malloc(sizeof(listint_t)); 15 | if (new_node != NULL) 16 | { 17 | new_node->n = n; 18 | new_node->next = *head; 19 | } 20 | else 21 | return (NULL); 22 | if (*head != NULL) 23 | new_node->next = *head; 24 | *head = new_node; 25 | return (new_node); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - adds a node to the end of a linked list 5 | * @head: pointer to the head of the list 6 | * @n: number to be used as content 7 | * 8 | * Return: address of the newly added node 9 | */ 10 | listint_t *add_nodeint_end(listint_t **head, const int n) 11 | { 12 | listint_t *new_node; 13 | listint_t *cursor = *head; 14 | 15 | new_node = malloc(sizeof(listint_t)); 16 | if (new_node != NULL) 17 | { 18 | new_node->n = n; 19 | new_node->next = NULL; 20 | } 21 | else 22 | return (NULL); 23 | if (cursor != NULL) 24 | { 25 | while (cursor->next != NULL) 26 | cursor = cursor->next; 27 | cursor->next = new_node; 28 | } 29 | else 30 | *head = new_node; 31 | return (new_node); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a linked list 5 | * @head: head of the list 6 | * 7 | * Return: void 8 | */ 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *temp; 12 | 13 | while (head != NULL) 14 | { 15 | temp = head; 16 | head = head->next; 17 | free(temp); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * free_listint2 - free the linked list 4 | * @head : pointer to list 5 | * Return: void 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; 17 | *head = (*head)->next; 18 | free(temp); 19 | } 20 | head = NULL; 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * pop_listint - deletes the head node 4 | * @head : pointer to the head of the linked list 5 | * Return: head node's data (n) 6 | * or 0 - if the linked list is empty 7 | */ 8 | int pop_listint(listint_t **head) 9 | { 10 | listint_t *temp; 11 | int n; 12 | 13 | temp = *head; 14 | 15 | if (temp == NULL) 16 | return (0); 17 | 18 | *head = temp->next; 19 | n = temp->n; 20 | free(temp); 21 | return (n); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * get_nodeint_at_index - finds a given node in a linked list 4 | * @head : pointer to the head of the linked list 5 | * @index: index of the node to find (indices starting at 0) 6 | * Return: pointer to the desired node, 7 | * or NULL - if the node does not exist 8 | */ 9 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 10 | { 11 | size_t n; 12 | 13 | for (n = 0; (n < index) && (head->next); n++) 14 | head = head->next; 15 | 16 | if (n < index) 17 | return (NULL); 18 | 19 | return (head); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | /** 4 | * sum_listint - calculates the sum of all the data (n) of a linked list 5 | * @head : pointer to the head of the list 6 | * Return: sum of all the data (n), 7 | * or 0 - if the list is empty 8 | */ 9 | int sum_listint(listint_t *head) 10 | { 11 | int add; 12 | 13 | add = 0; 14 | 15 | while (head) 16 | { 17 | add += head->n; 18 | head = head->next; 19 | } 20 | return (add); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * insert_nodeint_at_index - adds a new node to a linked list 4 | * at a given position 5 | * @head : pointer to the head of the list 6 | * @idx : index of the list where the new node will be added 7 | * (indices start at 0) 8 | * @n : value for the new node to be added 9 | * Return: pointer to the new node, 10 | * or NULL - if function fails 11 | */ 12 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 13 | { 14 | listint_t *new, *temp = *head; 15 | unsigned int node; 16 | 17 | new = malloc(sizeof(listint_t)); 18 | 19 | if (new == NULL) 20 | return (NULL); 21 | 22 | new->n = n; 23 | 24 | if (idx == 0) 25 | { 26 | new->next = temp; 27 | *head = new; 28 | return (new); 29 | } 30 | 31 | for (node = 0; node < (idx - 1); node++) 32 | { 33 | if (temp == NULL || temp->next == NULL) 34 | return (NULL); 35 | 36 | temp = temp->next; 37 | } 38 | 39 | new->next = temp->next; 40 | temp->next = new; 41 | 42 | return (new); 43 | } 44 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x13. C - More singly linked lists 2 | 3 | Task 0 : Write a function that prints all the elements of a listint_t list. 4 | 5 | Task 1: Write a function that returns the number of elements in a linked listint_t list. 6 | 7 | Task 2: Write a function that adds a new node at the beginning of a listint_t list. 8 | 9 | Task 3: Write a function that adds a new node at the end of a listint_t list.. 10 | 11 | Task 4: Write a function that frees a listint_t list. 12 | 13 | Task 5: Write a function that frees a listint_t list. 14 | 15 | Task 6: Write a function that deletes the head node of a listint_t linked list, and returns the head node’s data (n). 16 | 17 | Task 7: Write a function that returns the nth node of a listint_t linked list. 18 | 19 | Task 8: Write a function that returns the sum of all the data (n) of a listint_t linked list. 20 | 21 | Task 9: Write a function that inserts a new node at a given position. 22 | 23 | Task 10: Write a function that deletes the node at index index of a listint_t linked list. 24 | 25 | Task 11: Write a function that reverses a listint_t linked list. 26 | 27 | Task 12: Write a function that prints a listint_t linked list. 28 | 29 | Task 13: Write a function that frees a listint_t list. 30 | 31 | Task 14: Write a function that finds the loop in a linked list. 32 | 33 | 34 | All Tasks done By: 35 | 36 | Amarachi Crystal Omereife 37 | Email: mirrorprojects@gmail.com 38 | Telegram: Amarachi Omereife (@marameref) 39 | Facebook/Instagram: @marameref 40 | 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 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 | /** 22 | * struct listp_s - singly linked list 23 | * @p: pointers of nodes 24 | * @next: points to the next node 25 | * 26 | * Description: singly linked list of pointers 27 | */ 28 | typedef struct listp_s 29 | { 30 | void *p; 31 | struct listp_s *next; 32 | } listp_t; 33 | 34 | size_t print_listint(const listint_t *h); 35 | size_t listint_len(const listint_t *h); 36 | listint_t *add_nodeint(listint_t **head, const int n); 37 | listint_t *add_nodeint_end(listint_t **head, const int n); 38 | void free_listint(listint_t *head); 39 | void free_listint2(listint_t **head); 40 | int pop_listint(listint_t **head); 41 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 42 | int sum_listint(listint_t *head); 43 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 44 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 45 | listint_t *reverse_listint(listint_t **head); 46 | size_t print_listint_safe(const listint_t *head); 47 | size_t free_listint_safe(listint_t **h); 48 | listint_t *find_listint_loop(listint_t *head); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to an 5 | * unsigned int. 6 | * @b: binary. 7 | * 8 | * Return: unsigned int. 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int ui; 13 | int len, base_two; 14 | 15 | if (!b) 16 | return (0); 17 | 18 | ui = 0; 19 | 20 | for (len = 0; b[len] != '\0'; len++) 21 | ; 22 | 23 | for (len--, base_two = 1; len >= 0; len--, base_two *= 2) 24 | { 25 | if (b[len] != '0' && b[len] != '1') 26 | { 27 | return (0); 28 | } 29 | 30 | if (b[len] & 1) 31 | { 32 | ui += base_two; 33 | } 34 | } 35 | 36 | return (ui); 37 | } 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary representation 5 | * of a number. 6 | * @n: unsigned long int. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_binary(unsigned long int n) 11 | { 12 | if (n >> 0) 13 | { 14 | if (n >> 1) 15 | print_binary(n >> 1); 16 | _putchar((n & 1) + '0'); 17 | } 18 | else 19 | { 20 | _putchar('0'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * 6 | * Return: 0 if big endian, 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | unsigned int x; 11 | char *c; 12 | 13 | x = 1; 14 | c = (char *) &x; 15 | 16 | return ((int)*c); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given 5 | * index. 6 | * @n: unsigned long int input. 7 | * @index: index of the bit. 8 | * 9 | * Return: value of the bit. 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (n == 0 && index < 64) 16 | return (0); 17 | 18 | for (i = 0; i <= 63; n >>= 1, i++) 19 | { 20 | if (index == i) 21 | { 22 | return (n & 1); 23 | } 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets the value of a bit to 1. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | *n = (*n | m); 20 | 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a bit to 0. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | 20 | if (*n & m) 21 | *n ^= m; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - returns the number of bits you would 5 | * need to flip to get from one number to another 6 | * @n: number one. 7 | * @m: number two. 8 | * 9 | * Return: number of bits. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned int nbits; 14 | 15 | for (nbits = 0; n || m; n >>= 1, m >>= 1) 16 | { 17 | if ((n & 1) != (m & 1)) 18 | nbits++; 19 | } 20 | 21 | return (nbits); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | ALX | 0x14. C - Bit manipulation 2 | 3 | Task 0: Write a function that converts a binary number to an unsigned int. 4 | 5 | Task 1: Write a function that prints the binary representation of a number. 6 | 7 | Task 2: Write a function that returns the value of a bit at a given index. 8 | 9 | Task 3: Write a function that sets the value of a bit to 1 at a given index. 10 | 11 | Task 4: Write a function that sets the value of a bit to 0 at a given index. 12 | 13 | Task 5: Write a function that returns the number of bits you would need to flip to get from one number to another. 14 | 15 | Task 6: Write a function that checks the endianness. 16 | 17 | Task 7: Find the password for this program. 18 | 19 | All task done by: 20 | 21 | Amarachi Crystal Omereife 22 | @marameref {instagram, twitter, telegram} 23 | 24 | -------------------------------------------------------------------------------- /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 _putchar(char c); 7 | int get_bit(unsigned long int n, unsigned int index); 8 | int set_bit(unsigned long int *n, unsigned int index); 9 | int clear_bit(unsigned long int *n, unsigned int index); 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 11 | int get_endianness(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * read_textfile - reads a text file and prints it to the POSIX standard output 10 | * @filename: name of the file to read 11 | * @letters: number of letters it should read and print 12 | * 13 | * Return: actual number of letters it could read and print 14 | */ 15 | ssize_t read_textfile(const char *filename, size_t letters) 16 | { 17 | int fd; 18 | ssize_t lenr, lenw; 19 | char *buffer; 20 | 21 | if (filename == NULL) 22 | return (0); 23 | fd = open(filename, O_RDONLY); 24 | if (fd == -1) 25 | return (0); 26 | buffer = malloc(sizeof(char) * letters); 27 | if (buffer == NULL) 28 | { 29 | close(fd); 30 | return (0); 31 | } 32 | lenr = read(fd, buffer, letters); 33 | close(fd); 34 | if (lenr == -1) 35 | { 36 | free(buffer); 37 | return (0); 38 | } 39 | lenw = write(STDOUT_FILENO, buffer, lenr); 40 | free(buffer); 41 | if (lenr != lenw) 42 | return (0); 43 | return (lenw); 44 | } 45 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file 5 | * @filename: filename. 6 | * @text_content: content writed in the file. 7 | * 8 | * Return: 1 if it success. -1 if it fails. 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | int nletters; 14 | int rwr; 15 | 16 | if (!filename) 17 | return (-1); 18 | 19 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 20 | 21 | if (fd == -1) 22 | return (-1); 23 | 24 | if (!text_content) 25 | text_content = ""; 26 | 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | 35 | close(fd); 36 | 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /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: filename. 6 | * @text_content: added content. 7 | * 8 | * Return: 1 if the file exists. -1 if the fails does not exist 9 | * or if it fails. 10 | */ 11 | int append_text_to_file(const char *filename, char *text_content) 12 | { 13 | int fd; 14 | int nletters; 15 | int rwr; 16 | 17 | if (!filename) 18 | return (-1); 19 | 20 | fd = open(filename, O_WRONLY | O_APPEND); 21 | 22 | if (fd == -1) 23 | return (-1); 24 | 25 | if (text_content) 26 | { 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | } 35 | 36 | close(fd); 37 | 38 | return (1); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | 0x15. C - File I/O 2 | 3 | 4 | Task 0: Write a function that reads a text file and prints it to the POSIX standard output. 5 | 6 | Task 1: Create a function that creates a file. 7 | 8 | Task 2: Write a function that appends text at the end of a file. 9 | 10 | Task 3: Write a program that copies the content of a file to another file. 11 | 12 | Task 4: Write a program that displays the information contained in the ELF header at the start of an ELF file. 13 | 14 | 15 | All Tasks done by: 16 | Amarachi Udo-Obia omereife 17 | @marameref (Twitter, telegram) 18 | 19 | -------------------------------------------------------------------------------- /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 | int _putchar(char c); 12 | ssize_t read_textfile(const char *filename, size_t letters); 13 | int create_file(const char *filename, char *text_content); 14 | int _strlen(char *str); 15 | int append_text_to_file(const char *filename, char *text_content); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - Prints all the elements of a dlistint_t list. 5 | * @h: The head of the dlistint_t list. 6 | * 7 | * Return: The number of nodes in the list. 8 | */ 9 | size_t print_dlistint(const dlistint_t *h) 10 | { 11 | size_t nodes = 0; 12 | 13 | while (h) 14 | { 15 | nodes++; 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | } 19 | 20 | return (nodes); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - Counts the number of elements in a linked dlistint_t list. 5 | * @h: The head of the dlistint_t list. 6 | * 7 | * Return: The number of elements in the dlistint_t list. 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t nodes = 0; 12 | 13 | while (h) 14 | { 15 | nodes++; 16 | h = h->next; 17 | } 18 | 19 | return (nodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Generates and prints passwords for the crackme5 executable. 7 | * @argc: The number of arguments supplied to the program. 8 | * @argv: An array of pointers to the arguments. 9 | * 10 | * Return: Always 0. 11 | */ 12 | int main(int __attribute__((__unused__)) argc, char *argv[]) 13 | { 14 | char password[7], *codex; 15 | int len = strlen(argv[1]), i, tmp; 16 | 17 | codex = "A-CHRDw87lNS0E9B2TibgpnMVys5XzvtOGJcYLU+4mjW6fxqZeF3Qa1rPhdKIouk"; 18 | 19 | tmp = (len ^ 59) & 63; 20 | password[0] = codex[tmp]; 21 | 22 | tmp = 0; 23 | for (i = 0; i < len; i++) 24 | tmp += argv[1][i]; 25 | password[1] = codex[(tmp ^ 79) & 63]; 26 | 27 | tmp = 1; 28 | for (i = 0; i < len; i++) 29 | tmp *= argv[1][i]; 30 | password[2] = codex[(tmp ^ 85) & 63]; 31 | 32 | tmp = 0; 33 | for (i = 0; i < len; i++) 34 | { 35 | if (argv[1][i] > tmp) 36 | tmp = argv[1][i]; 37 | } 38 | srand(tmp ^ 14); 39 | password[3] = codex[rand() & 63]; 40 | 41 | tmp = 0; 42 | for (i = 0; i < len; i++) 43 | tmp += (argv[1][i] * argv[1][i]); 44 | password[4] = codex[(tmp ^ 239) & 63]; 45 | 46 | for (i = 0; i < argv[1][0]; i++) 47 | tmp = rand(); 48 | password[5] = codex[(tmp ^ 229) & 63]; 49 | 50 | password[6] = '\0'; 51 | printf("%s", password); 52 | return (0); 53 | } 54 | -------------------------------------------------------------------------------- /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 dlistint_t list. 5 | * @head: A pointer to the head of the dlistint_t list. 6 | * @n: The integer for the new node to contain. 7 | * 8 | * Return: If the function fails - NULL. 9 | * Otherwise - the address of the new node. 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | 15 | new = malloc(sizeof(dlistint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->prev = NULL; 21 | new->next = *head; 22 | if (*head != NULL) 23 | (*head)->prev = new; 24 | *head = new; 25 | 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /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 dlistint_t list. 5 | * @head: A pointer to the head of the dlistint_t list. 6 | * @n: The integer for the new node to contain. 7 | * 8 | * Return: If the function fails - NULL. 9 | * Otherwise - the address of the new node. 10 | */ 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new, *last; 14 | 15 | new = malloc(sizeof(dlistint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->next = NULL; 21 | 22 | if (*head == NULL) 23 | { 24 | new->prev = NULL; 25 | *head = new; 26 | return (new); 27 | } 28 | 29 | last = *head; 30 | while (last->next != NULL) 31 | last = last->next; 32 | last->next = new; 33 | new->prev = last; 34 | 35 | return (new); 36 | } 37 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - Frees a linked dlistint_t list. 5 | * @head: The head of the dlistint_t list. 6 | */ 7 | void free_dlistint(dlistint_t *head) 8 | { 9 | dlistint_t *tmp; 10 | 11 | while (head) 12 | { 13 | tmp = head->next; 14 | free(head); 15 | head = tmp; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - Locates a node in a dlistint_t list. 5 | * @head: The head of the dlistint_t list. 6 | * @index: The node to locate. 7 | * 8 | * Return: If the node does not exist - NULL. 9 | * Otherwise - the address of the located node. 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | for (; index != 0; index--) 14 | { 15 | if (head == NULL) 16 | return (NULL); 17 | head = head->next; 18 | } 19 | 20 | return (head); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - Sums all the data of a dlistint_t list. 5 | * @head: The head of the dlistint_t list. 6 | * 7 | * Return: The sum of all 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 new node in a dlistint_t 5 | * list at a given position. 6 | * @h: A pointer to the head of the dlistint_t list. 7 | * @idx: The position to insert the new node. 8 | * @n: The integer for the new node to contain. 9 | * 10 | * Return: If the function fails - NULL. 11 | * Otherwise - the address of the new node. 12 | */ 13 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 14 | { 15 | dlistint_t *tmp = *h, *new; 16 | 17 | if (idx == 0) 18 | return (add_dnodeint(h, n)); 19 | 20 | for (; idx != 1; idx--) 21 | { 22 | tmp = tmp->next; 23 | if (tmp == NULL) 24 | return (NULL); 25 | } 26 | 27 | if (tmp->next == NULL) 28 | return (add_dnodeint_end(h, n)); 29 | 30 | new = malloc(sizeof(dlistint_t)); 31 | if (new == NULL) 32 | return (NULL); 33 | 34 | new->n = n; 35 | new->prev = tmp; 36 | new->next = tmp->next; 37 | tmp->next->prev = new; 38 | tmp->next = new; 39 | 40 | return (new); 41 | } 42 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - Deletes a node from a dlistint_t 5 | * at a given index. 6 | * @head: A pointer to the head of the dlistint_t. 7 | * @index: The index of the node to delete. 8 | * 9 | * Return: Upon success - 1. 10 | * Otherwise - -1. 11 | */ 12 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 13 | { 14 | dlistint_t *tmp = *head; 15 | 16 | if (*head == NULL) 17 | return (-1); 18 | 19 | for (; index != 0; index--) 20 | { 21 | if (tmp == NULL) 22 | return (-1); 23 | tmp = tmp->next; 24 | } 25 | 26 | if (tmp == *head) 27 | { 28 | *head = tmp->next; 29 | if (*head != NULL) 30 | (*head)->prev = NULL; 31 | } 32 | 33 | else 34 | { 35 | tmp->prev->next = tmp->next; 36 | if (tmp->next != NULL) 37 | tmp->next->prev = tmp->prev; 38 | } 39 | 40 | free(tmp); 41 | return (1); 42 | } 43 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # ALX 0x17. C - Doubly linked lists 2 | 3 | Task 0: Write a function that prints all the elements of a dlistint_t list.. (File name: 0-print_dlistint.c) 4 | 5 | Task 1: Write a function that returns the number of elements in a linked dlistint_t list.. (File Name: 1-dlistint_len.c) 6 | 7 | Task 2: Write a function that adds a new node at the beginning of a dlistint_t list.. (File Name: 2-add_dnodeint.c) 8 | 9 | Task 3: Write a function that adds a new node at the end of a dlistint_t list.. (File Name: 3-add_dnodeint_end.c) 10 | 11 | Task 4: Write a function that frees a dlistint_t list...:(File Name: 4-free_dlistint.c) 12 | 13 | Task 5: Write a function that returns the nth node of a dlistint_t linked list. (File Name: 5-get_dnodeint.c) 14 | 15 | Task 6:Write a function that returns the sum of all the data (n) of a dlistint_t linked list. (File Name: 6-sum_dlistint.c) 16 | 17 | Task 7: Write a function that inserts a new node at a given position. (File Names: 7-insert_dnodeint.c, 2-add_dnodeint.c, 3-add_dnodeint_end.c) 18 | 19 | # Advanced Task 20 | 21 | Task 8: Write a function that deletes the node at index index of a dlistint_t linked list.. (File Name: 8-delete_dnodeint.c) 22 | 23 | Task 9: Find the password for crackme4. (File Name: 100-password) 24 | 25 | Task 10: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. (file Name: 102-result) 26 | 27 | Task 11: Write a keygen for crackme5.. (File Name: 103-keygen.c) 28 | 29 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/keygen5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x17-doubly_linked_lists/keygen5 -------------------------------------------------------------------------------- /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 | */ 15 | typedef struct dlistint_s 16 | { 17 | int n; 18 | struct dlistint_s *prev; 19 | struct dlistint_s *next; 20 | } dlistint_t; 21 | 22 | size_t print_dlistint(const dlistint_t *h); 23 | size_t dlistint_len(const dlistint_t *h); 24 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 25 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 26 | void free_dlistint(dlistint_t *head); 27 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 28 | int sum_dlistint(dlistint_t *head); 29 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 30 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 31 | 32 | #endif /* LISTS_H */ 33 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - fills memory with a constant byte, 5 | * @s: memory area. 6 | * @b: constant byte. 7 | * @n: bytes filled. 8 | * Return: the pointer to dest. 9 | */ 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < n; i++) 15 | *(s + i) = b; 16 | 17 | return (s); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - function commute srtings 5 | * @dest: param pointer to a char 6 | * @src: param pointer to a char 7 | * Return: return value of dest 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int i; 12 | int j; 13 | 14 | i = 0; 15 | j = 0; 16 | 17 | while (dest[i] != '\0') 18 | { 19 | i++; 20 | } 21 | while (src[j] != '\0') 22 | { 23 | dest[i] = src[j]; 24 | j++; 25 | i++; 26 | } 27 | 28 | dest[i] = '\0'; 29 | return (dest); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c -Wall -Werror -fPIC *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/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 | return (c >= '0' && c <= '9'); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - copies memory area, 5 | * @dest: destination memory area. 6 | * @src: source memory area. 7 | * @n: bytes filled. 8 | * Return: the pointer to dest. 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < n; i++) 15 | *(dest + i) = *(src + i); 16 | 17 | return (dest); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strncat - concatenates n bytes of two strings 5 | * @dest: pointer destination 6 | * @src: pointer source 7 | * @n: number of bytes 8 | * Return: void 9 | */ 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int dest_len, i; 13 | for (dest_len = 0; dest[dest_len] != '\0'; dest_len++) 14 | ; 15 | for (i = 0; i < n && src[i] != '\0'; i++) 16 | dest[dest_len + i] = src[i]; 17 | 18 | /*should end with a end of string char*/ 19 | dest[dest_len + i] = '\0'; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer. 5 | * @s: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | if (*(s + count) == '-') 17 | pn *= -1; 18 | 19 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 20 | { 21 | if (size > 0) 22 | m *= 10; 23 | size++; 24 | } 25 | count++; 26 | } 27 | 28 | for (i = count - size; i < count; i++) 29 | { 30 | oi = oi + ((*(s + i) - 48) * m); 31 | m /= 10; 32 | } 33 | return (oi * pn); 34 | } 35 | 36 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/sidneyriffic/holbertonschool-low_level_programming/raw/master/0x17-dynamic_libraries/putshack.so 3 | export LD_PRELOAD=/tmp/putshack.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-md5_gm: -------------------------------------------------------------------------------- 1 | d52e6c18e0723f5b025a75dea19ef365 gm 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - locates a character in a string, 5 | * @s: string. 6 | * @c: character. 7 | * Return: the pointer to the first occurrence of the character c. 8 | */ 9 | char *_strchr(char *s, char c) 10 | { 11 | unsigned int i = 0; 12 | 13 | for (; *(s + i) != '\0'; i++) 14 | if (*(s + i) == c) 15 | return (s + i); 16 | if (*(s + i) == c) 17 | return (s + i); 18 | return ('\0'); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input stringeturn. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copies a string 5 | * @dest: destination. 6 | * @src: source. 7 | * @n: amount of bytes from src. 8 | * Return: the pointer to dest. 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 | 22 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - prints 1 or 0 depending on input 5 | * @c: first parameter 6 | * 7 | * Description: prints all lowercase letters 8 | * Return: Always(0). 9 | */ 10 | int _islower(int c) 11 | { 12 | return (c >= 'a' && c <= 'z'); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, 5 | * @str: pointer to the string to print 6 | * Return: void 7 | */ 8 | void _puts(char *str) 9 | { 10 | int i = 0; 11 | 12 | while (str[i]) 13 | { 14 | _putchar(str[i]); 15 | i++; 16 | } 17 | _putchar('\n'); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compares two strings 5 | * @s1: first string. 6 | * @s2: second string. 7 | * Return: 0 if s1 and s2 are equals, 8 | * another number if not. 9 | */ 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Check if character is a alphabet character. 5 | * @c: type int character 6 | * Return: 1 if letter, lowercase or uppercase, and 0 otherwise 7 | */ 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 11 | return (1); 12 | 13 | else 14 | return (0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - function that computes the absolute value of an integer 5 | * @n: int type number 6 | * Return: absolute value of @n 7 | */ 8 | int _abs(int n) 9 | { 10 | if (n < 0) 11 | { 12 | return (n * -1); 13 | } 14 | else 15 | { 16 | return (n); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strcpy - copies the string pointed to by src, 6 | * including the terminating null byte, to the 7 | * buffer pointed to by dest. 8 | * @dest: destination. 9 | * @src: source. 10 | * Return: the pointer to dest. 11 | */ 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int count = 0; 15 | 16 | while (count >= 0) 17 | { 18 | *(dest + count) = *(src + count); 19 | if (*(src + count) == '\0') 20 | break; 21 | count++; 22 | } 23 | return (dest); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x18-dynamic_libraries 2 | In this project, I learnt how to create dynamic libraries from modules and use them 3 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/gm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x18-dynamic_libraries/gm -------------------------------------------------------------------------------- /0x18-dynamic_libraries/liball.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x18-dynamic_libraries/liball.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libtest.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x18-dynamic_libraries/libtest.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int main(void) 4 | { 5 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 6 | return (0); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int printf(const char *format, ...) 6 | { 7 | /* Our code goes here 8 | * we can use write for printing text 9 | * */ 10 | 11 | write(1, "9 8 10 24 75 - 9\n", 17); 12 | write(1, "Congratulations, you win the Jackpot!\n", 38); 13 | exit(EXIT_SUCCESS); 14 | } 15 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/test.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marameref/alx-low_level_programming/9342d94354cccc0b9e71cd26122fea1c56888055/0x18-dynamic_libraries/test.o -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - function that creates a hash table with a 5 | * specified size 6 | * @size: Unsigned long int denoting size of hash table to be created 7 | * Return: pointer to the newly created hash table of struct type 8 | * hash_table_t, returns NULL if an error occurs 9 | */ 10 | 11 | hash_table_t *hash_table_create(unsigned long int size) 12 | { 13 | hash_table_t *table; 14 | 15 | if (size <= 0) 16 | return (NULL); 17 | 18 | table = malloc(sizeof(hash_table_t)); 19 | 20 | if (table == NULL) 21 | return (NULL); 22 | 23 | table->array = malloc(sizeof(hash_node_t *) * size); 24 | 25 | if (table->array == NULL) 26 | { 27 | free(table); 28 | return (NULL); 29 | } 30 | 31 | table->size = size; 32 | 33 | return (table); 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - the djb2 algorithm 5 | * @str: Char pointer of data to be hashed 6 | * Source: http://www.cse.yorku.ca/~oz/hash.html 7 | * Return: Unsigned long int 8 | */ 9 | 10 | unsigned long int hash_djb2(const unsigned char *str) 11 | { 12 | unsigned long int hash; 13 | int c; 14 | 15 | hash = 5381; 16 | while ((c = *str++)) 17 | { 18 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 19 | } 20 | return (hash); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | 4 | /** 5 | * key_index - function that gives the index of a key. 6 | * @key: Const unsigned char - unique value passed 7 | * @size: Unsigned long int - size of array 8 | * Return: Unsigned long int of index where the key is stored 9 | */ 10 | 11 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 12 | { 13 | 14 | unsigned long int index; 15 | 16 | index = hash_djb2(key) % size; 17 | 18 | return (index); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - function that retrieves a value associated with a key 5 | * @ht: Double pointer of type hash_table_t for hash table 6 | * @key: Const char pointer for unique value to be searched for 7 | * Return: The value associated with the element found, NULL if key couldn't be 8 | * found 9 | */ 10 | 11 | char *hash_table_get(const hash_table_t *ht, const char *key) 12 | { 13 | hash_node_t *temp; 14 | unsigned long int index; 15 | 16 | if (ht == NULL || key == NULL) 17 | return (NULL); 18 | 19 | index = key_index((const unsigned char *)key, ht->size); 20 | 21 | if (ht->array[index] == NULL) 22 | return (NULL); 23 | 24 | temp = ht->array[index]; 25 | 26 | while (temp != NULL) 27 | { 28 | if (strcmp(temp->key, key) == 0) 29 | return (temp->value); 30 | temp = temp->next; 31 | } 32 | 33 | return (NULL); 34 | } 35 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - function that prints a hash table. 5 | * @ht: Double pointer of type hash_table_t for hash table 6 | * Return: Void 7 | */ 8 | 9 | void hash_table_print(const hash_table_t *ht) 10 | { 11 | hash_node_t *temp; 12 | unsigned long int i, first_flag = 0; 13 | 14 | if (ht == NULL) 15 | return; 16 | 17 | putchar('{'); 18 | 19 | for (i = 0; i < ht->size; i++) 20 | { 21 | if (ht->array[i] != NULL) 22 | { 23 | if (first_flag == 1) 24 | printf(", "); 25 | 26 | temp = ht->array[i]; 27 | 28 | while (temp != NULL) 29 | { 30 | if (temp->next == NULL) 31 | { 32 | printf("'%s': ", temp->key); 33 | printf("'%s'", temp->value); 34 | break; 35 | } 36 | printf("'%s': '%s', ", temp->key, temp->value); 37 | temp = temp->next; 38 | } 39 | first_flag = 1; 40 | } 41 | 42 | if (ht->array[i] == NULL) 43 | continue; 44 | } 45 | printf("}\n"); 46 | } 47 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - function that deletes a hash table 5 | * @ht: Pointer of type hash_table_t for hash table 6 | * Return: Void 7 | */ 8 | 9 | void hash_table_delete(hash_table_t *ht) 10 | { 11 | unsigned long int i = 0; 12 | hash_node_t *temp, *temp2; 13 | 14 | for (i = 0; i < ht->size; i++) 15 | { 16 | if (ht->array[i] != NULL) 17 | { 18 | temp = ht->array[i]; 19 | 20 | while (temp != NULL) 21 | { 22 | temp2 = temp->next; 23 | free(temp->key); 24 | free(temp->value); 25 | free(temp); 26 | temp = temp2; 27 | } 28 | } 29 | } 30 | 31 | free(ht->array); 32 | free(ht); 33 | } 34 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all : main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | 2 | CC = gcc 3 | SRC = main.c school.c 4 | all : $(SRC) 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | .PHONY : all clean oclean fclean re 7 | 8 | all : m.h $(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 : oclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | all : $(OBJ) 6 | $(CC) $(OBJ) -o $(NAME) 7 | -------------------------------------------------------------------------------- /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 : oclean 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 : oclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Module that calculates the perimeter of an island in a grid.""" 3 | 4 | 5 | def num_water_neighbors(grid, i, j): 6 | """Returns the number of water neighbors a cell has in a grid.""" 7 | 8 | num = 0 9 | 10 | if i <= 0 or not grid[i - 1][j]: 11 | num += 1 12 | if j <= 0 or not grid[i][j - 1]: 13 | num += 1 14 | if j >= len(grid[i]) - 1 or not grid[i][j + 1]: 15 | num += 1 16 | if i >= len(grid) - 1 or not grid[i + 1][j]: 17 | num += 1 18 | 19 | return num 20 | 21 | 22 | def island_perimeter(grid): 23 | """Returns the perimeter of the island in grid.""" 24 | 25 | perimeter = 0 26 | for i in range(len(grid)): 27 | for j in range(len(grid[i])): 28 | if grid[i][j]: 29 | perimeter += num_water_neighbors(grid, i, j) 30 | 31 | return perimeter 32 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # Solutions to tasks on 0x1C. C - Makefiles 2 | 3 | Author: Amarachi Crystal Omereife 4 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 5 | * integers using the Linear search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: Always EXIT_SUCCESS 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < (int)size; i++) 20 | { 21 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 22 | if (value == array[i]) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - searches for a value in an integer array using a binary 5 | * search algorithm, not guaranteed to return lowest index if `value` appears 6 | * twice in `array` 7 | * @array: pointer to first element of array to seach 8 | * @size: number of elements in array 9 | * @value: value to search for 10 | * 11 | * Return: index containing `value`, or -1 if `value` not found or 12 | * `array` is NULL 13 | */ 14 | 15 | int binary_search(int *array, size_t size, int value) 16 | { 17 | int low, mid, high; 18 | int x; 19 | 20 | if (array == NULL) 21 | { 22 | return (-1); 23 | } 24 | 25 | low = 0; 26 | high = size - 1; 27 | 28 | while (low <= high) 29 | { 30 | mid = (low + high) / 2; 31 | 32 | printf("Searching in array: "); 33 | for (x = low; x <= high; x++) 34 | printf("%i%s", array[x], x == high ? "\n" : ", "); 35 | 36 | if (array[mid] < value) 37 | low = mid + 1; 38 | else if (array[mid] > value) 39 | high = mid - 1; 40 | else 41 | return (mid); 42 | } 43 | 44 | return (-1); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); 18 | printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /* remember compiling math.h with gcc requires `-lm` */ 5 | 6 | size_t min(size_t a, size_t b); 7 | 8 | /** 9 | * min - returns the minimum of two size_t values 10 | * @a: first value 11 | * @b: second value 12 | * 13 | * Return: `a` if lower or equal to `b`, `b` otherwise 14 | */ 15 | size_t min(size_t a, size_t b) 16 | { 17 | return (a <= b ? a : b); 18 | } 19 | 20 | /** 21 | * jump_search - searches for a value in a sorted array of integers using 22 | * a jump search algorithm 23 | * @array: pointer to first element of array to search 24 | * @size: number of elements in array 25 | * @value: value to search for 26 | * 27 | * Return: first index containing `value`, or -1 if `value` not found or 28 | * `array` is NULL 29 | */ 30 | 31 | int jump_search(int *array, size_t size, int value) 32 | { 33 | size_t low, high, jump; 34 | 35 | if (!array || size == 0) 36 | return (-1); 37 | 38 | jump = sqrt(size); 39 | 40 | for (high = 0; high < size && array[high] < value; 41 | low = high, high += jump) 42 | { 43 | printf("Value checked array[%lu] = [%d]\n", 44 | high, array[high]); 45 | } 46 | 47 | /* causes 'found' msg even when value not in array */ 48 | printf("Value found between indexes [%lu] and [%lu]\n", low, high); 49 | 50 | for (; low <= min(high, size - 1); low++) 51 | { 52 | printf("Value checked array[%lu] = [%d]\n", low, array[low]); 53 | if (array[low] == value) 54 | return (low); 55 | } 56 | 57 | return (-1); 58 | } 59 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in a sorted array of integers 5 | * using an interpolation search algorithm 6 | * @array: pointer to first element of array to search 7 | * @size: number of elements in array 8 | * @value: value to search for 9 | * 10 | * Return: first index containing `value`, or -1 if `value` not found or 11 | * `array` is NULL 12 | */ 13 | 14 | int interpolation_search(int *array, size_t size, int value) 15 | { 16 | size_t low = 0; 17 | size_t high = size - 1; 18 | size_t pos; 19 | 20 | if (!array) 21 | return (-1); 22 | 23 | while ((array[high] != array[low]) && 24 | (value >= array[low]) && (value <= array[high])) 25 | { 26 | pos = low + (((double)(high - low) / (array[high] - array[low])) 27 | * (value - array[low])); 28 | printf("Value checked array[%lu] = [%d]\n", pos, array[pos]); 29 | if (array[pos] < value) 30 | low = pos + 1; 31 | else if (value < array[pos]) 32 | high = pos - 1; 33 | else 34 | return (pos); 35 | } 36 | if (value == array[low]) 37 | { 38 | printf("Value checked array[%lu] = [%d]\n", low, array[low]); 39 | return (low); 40 | } 41 | pos = low + (((double)(high - low) / (array[high] - array[low])) 42 | * (value - array[low])); 43 | printf("Value checked array[%lu] is out of range\n", pos); 44 | return (-1); 45 | } 46 | -------------------------------------------------------------------------------- /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/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SORTING_ALGOS_H_ 2 | #define _SORTING_ALGOS_H_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * 10 | * @n: Integer 11 | * @index: Index of the node in the list 12 | * @next: Pointer to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct listint_s 18 | { 19 | int n; 20 | size_t index; 21 | struct listint_s *next; 22 | } listint_t; 23 | 24 | /** 25 | * struct skiplist_s - Singly linked list with an express lane 26 | * 27 | * @n: Integer 28 | * @index: Index of the node in the list 29 | * @next: Pointer to the next node 30 | * @express: Pointer to the next node in the express lane 31 | * 32 | * Description: singly linked list node structure with an express lane 33 | * for Holberton project 34 | */ 35 | typedef struct skiplist_s 36 | { 37 | int n; 38 | size_t index; 39 | struct skiplist_s *next; 40 | struct skiplist_s *express; 41 | } skiplist_t; 42 | 43 | int linear_search(int *array, size_t size, int value); 44 | int binary_search(int *array, size_t size, int value); 45 | int jump_search(int *array, size_t size, int value); 46 | int interpolation_search(int *array, size_t size, int value); 47 | int exponential_search(int *array, size_t size, int value); 48 | int advanced_binary(int *array, size_t size, int value); 49 | listint_t *jump_list(listint_t *list, size_t size, int value); 50 | skiplist_t *linear_skip(skiplist_t *list, int value); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 0x1E. C - Search Algorithms 2 | C 3 | Algorithm 4 | --------------------------------------------------------------------------------