├── .gitignore ├── .gitmodules ├── 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 ├── 101-print_comb4.c ├── 102-print_comb5 ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16 ├── 8-print_base16.c ├── 9-print_comb ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-holberton.c ├── 1-alphabet.c ├── 10-add.c ├── 100-main.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 ├── 5-sign.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── holberton.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 2-main.c ├── 3-convert_day.c ├── 3-main_a.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-main.c ├── 1-isdigit ├── 1-main.c ├── 10-main.c ├── 10-print_triangle.c ├── 10-triangles ├── 100-prime_factor ├── 100-prime_factor.c ├── 101-main.c ├── 101-print_number.c ├── 101-print_numbers ├── 2-main.c ├── 2-mul ├── 3-main.c ├── 3-print_numbers ├── 3-print_numbers.c ├── 4-main.c ├── 4-print_most_numbers ├── 4-print_most_numbers.c ├── 5-main.c ├── 5-more_numbers ├── 5-more_numbers.c ├── 6-lines ├── 6-main.c ├── 6-print_line.c ├── 7-diagonals ├── 7-main.c ├── 7-print_diagonal.c ├── 8-main.c ├── 8-print_square.c ├── 8-squares ├── 9-fizz_buzz ├── 9-fizz_buzz.c ├── README.md └── holberton.h ├── 0x05-pointers_arrays_strings ├── 0-98 ├── 0-reset_to_98.c ├── 1-swap ├── 1-swap.c ├── 100-atoi ├── 100-main.c ├── 101-crackme ├── 101-keygen ├── 101-keygen.c ├── 2-main.c ├── 2-strlen ├── 3-main.c ├── 3-puts ├── 4-main.c ├── 4-print_rev ├── 4-print_rev.c ├── 5-main.c ├── 5-rev_string ├── 5-rev_string.c ├── 6-main.c ├── 6-puts2 ├── 6-puts2.c ├── 7-main.c ├── 7-puts_half ├── 7-puts_half.c ├── 8-main.c ├── 8-print_array ├── 8-print_array.c ├── 9-main.c ├── 9-strcpy ├── README.md ├── Test │ ├── 0-main.c │ └── 1-main.c └── holberton.h ├── 0x06-pointers_arrays_strings ├── 100-rot13 ├── 100-rot13.c ├── 101-print_number.c ├── 101-print_numbers ├── 102-magic ├── 102-magic.c ├── 103-add ├── 103-infinite_add.c ├── 104-buffer ├── 104-print_buffer.c ├── 2-strncpy ├── 3-strcmp ├── 4-rev_array.c ├── 5-string_toupper ├── 5-string_toupper.c ├── 6-cap ├── 6-cap_string.c ├── 7-1337 ├── 7-leet.c ├── README.md ├── Test │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 101-main.c │ ├── 103-main.c │ ├── 104-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ ├── 6-main.c │ └── 7-main.c └── holberton.h ├── 0x07-pointers_arrays_strings ├── 0-main.c ├── 0-memset ├── 1-main.c ├── 1-memcpy ├── 100-main.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-main.c ├── 2-strchr ├── 3-main.c ├── 3-strspn ├── 4-main.c ├── 4-strpbrk ├── 5-main.c ├── 5-strstr ├── 7-main.c ├── 7-print_chessboard ├── 7-print_chessboard.c ├── 8-main.c ├── 8-print_diagsums.c ├── README.md ├── crackme2 └── holberton.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 100-palindrome ├── 101-wildcmp ├── 101-wildcmp.c ├── 2-strlen_recursion ├── 2-strlen_recursion.c ├── 3-factorial ├── 3-factorial.c ├── 4-po ├── 4-pow_recursion.c ├── 5-sqrt ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 6-prime ├── README.md ├── Test │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 101-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ └── 6-main.c └── holberton.h ├── 0x09-static_libraries ├── README.md ├── create_static_lib.sh ├── holberton.h ├── liball.a └── libholberton.a ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md └── holberton.h ├── 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 └── holberton.h ├── 0x0C-more_malloc_free ├── 0-main.c ├── 0-malloc_checked.c ├── 1-main.c ├── 1-string_nconcat.c ├── 100-main.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 2-main.c ├── 3-array_range.c ├── 3-main.c ├── README.md ├── a ├── b ├── c ├── d ├── e ├── holberton.h └── mul ├── 0x0D-preprocessor ├── 0-main.c ├── 0-object_like_macro.h ├── 1-main.c ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 3-main.c ├── 4-main.c ├── 4-sum.h ├── README.md ├── a ├── b ├── c ├── d └── e ├── 0x0E-structures_typedef ├── 0-main.c ├── 1-init_dog.c ├── 1-main.c ├── 2-main.c ├── 2-print_dog.c ├── 3-main.c ├── 4-main.c ├── 4-new_dog.c ├── 5-free_dog.c ├── 5-main.c ├── README.md ├── a ├── b ├── c ├── d ├── dog.h ├── e └── f ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-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 └── 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 ├── Test_Files │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 2-main.c │ ├── 3-main.c │ └── 4-main.c ├── lists.h └── struct.c ├── 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 ├── Test │ └── 0-main.c └── 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 ├── Test │ └── 0-main.c ├── holberton.h └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── 0-main.c ├── 1-create_dynamic_lib.sh ├── 100-operations.c ├── 100-operations.o ├── 100-operations.so ├── 100-tests.py ├── 101-make_me_win.sh ├── 101-md5_gm ├── README.md ├── liball.so ├── libdynamic.so ├── main.h └── nrandom.so ├── 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 └── tests │ ├── 0-main.c │ ├── 100-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ └── 6-main.c ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── 5-main.py ├── README.md ├── __pycache__ │ └── 5-island_perimeter.cpython-38.pyc ├── m.h ├── main.c ├── main.o ├── school ├── school.c └── school.o ├── 0x1E-search_algorithms ├── 0 ├── 1 ├── 100 ├── 102 ├── 103 ├── 104 ├── 105 ├── 106 ├── 0-linear.c ├── 1-binary.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 ├── listint │ ├── create_list.c │ ├── free_list.c │ └── print_list.c ├── search_algos.h ├── skiplist │ ├── create_skiplist.c │ ├── free_skiplist.c │ └── print_skiplist.c └── tests │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 102-main.c │ ├── 103-main.c │ ├── 104-main.c │ ├── 105-main.c │ └── 106-main.c ├── README.md └── files.zip /.gitignore: -------------------------------------------------------------------------------- 1 | # swap 2 | [._]*.s[a-v][a-z] 3 | [._]*.sw[a-p] 4 | [._]s[a-v][a-z] 5 | [._]sw[a-p] 6 | 7 | # session 8 | Session.vim 9 | 10 | # temporary 11 | .netrwhist 12 | *~ 13 | 14 | # auto-generated tag files 15 | tags 16 | 17 | *.vscode 18 | 0-isupper.o 19 | 0-isupper.c 20 | 0-main.o 21 | 0-memset.c 22 | 0-memset.o 23 | 0-strcat.c 24 | 0-strcat.o 25 | 1-isdigit.c 26 | 1-isdigit.o 27 | 1-memcpy.c 28 | 1-memcpy.o 29 | 1-strncat.c 30 | 1-strncat.o 31 | 100-atoi.c 32 | 100-atoi.o 33 | 2-add.c 34 | 2-add.o 35 | 2-div.c 36 | 2-mod.c 37 | 2-mod.o 38 | 2-mul.c 39 | 2-mul.o 40 | 2-strchr.c 41 | 2-strchr.o 42 | 2-strlen.c 43 | 2-strlen.o 44 | 2-strncpy.c 45 | 2-strncpy.o 46 | 2-sub.c 47 | 2-sub.o 48 | 3-islower.c 49 | 3-islower.o 50 | 3-puts.c 51 | 3-puts.o 52 | 3-strcmp.c 53 | 3-strcmp.o 54 | 3-strspn.c 55 | 3-strspn.o 56 | 4-isalpha.c 57 | 4-isalpha.o 58 | 4-strpbrk.c 59 | 4-strpbrk.o 60 | 5-strstr.c 61 | 5-strstr.o 62 | 6-abs.c 63 | 6-abs.o 64 | 9-strcpy.c 65 | 9-strcpy.o 66 | _putchar.c 67 | _putchar.o 68 | a.out 69 | len 70 | 71 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "sorting_algorithms"] 2 | path = sorting_algorithms 3 | url = https://github.com/Trikcode/sorting_algorithms.git 4 | -------------------------------------------------------------------------------- /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 > $(echo $CFILE|cut -d"." -f1).o 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -masm=intel $CFILE -S > $(echo $CFILE|cut -d"." -f1).s 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints to string 5 | * Description: Prints "and that piece of art is useful.." without puts 6 | * Return: 1 7 | */ 8 | 9 | int main(void) 10 | { 11 | char *s = "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n"; 12 | long l = 59; 13 | long fd = 1; 14 | long syscall = 1; 15 | long ret = 0; 16 | __asm__ ("syscall" 17 | : "=a" (ret) 18 | : "a" (syscall), 19 | "D" (fd), 20 | "S" (s), 21 | "d" (l)); 22 | return (1); 23 | } 24 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -S > $(echo $CFILE|cut -d"." -f1).s 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * Description: prints "\"Programming is like building a multilingual puzzle" 6 | * Return: 0 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 - Print a string 5 | * Description: use printf 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | printf("%s", "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 | * main - Prints sized of characters 4 | * 5 | *Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | printf("Size of a char: %zu byte(s)\n", sizeof(char)); 10 | printf("Size of an int: %zu byte(s)\n", sizeof(int)); 11 | printf("Size of a long int: %zu byte(s)\n", sizeof(long int)); 12 | printf("Size of a long long int: %zu byte(s)\n", sizeof(long long int)); 13 | printf("Size of a float: %zu byte(s)\n", sizeof(float)); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /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 0 (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: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x01-variables_if_else_while/101-print_comb4 -------------------------------------------------------------------------------- /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: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x01-variables_if_else_while/102-print_comb5 -------------------------------------------------------------------------------- /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 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints the alphabet in lowercase\n 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 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints alphabet 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: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x01-variables_if_else_while/6-print_numberz -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x01-variables_if_else_while/8-print_base16 -------------------------------------------------------------------------------- /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: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x01-variables_if_else_while/9-print_comb -------------------------------------------------------------------------------- /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/0-holberton.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * main - Entry point 4 | * Description: prints 'Holberton \n' 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | char str[] = "Holberton\n"; 10 | int i = 0; 11 | 12 | while (str[i] != '\0') 13 | { 14 | char c = str[i]; 15 | 16 | _putchar(c); 17 | i++; 18 | } 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 "holberton.h" 2 | 3 | /** 4 | * add - adds two integers 5 | * @n1: integer 1 6 | * @n2: integer 2 7 | * Description: give two integers, returns sum 8 | * Return: sum 9 | */ 10 | 11 | int add(int n1, int n2) 12 | { 13 | int sum; 14 | 15 | sum = n1 + n2; 16 | 17 | return (sum); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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_times_table(3); 11 | _putchar('\n'); 12 | print_times_table(5); 13 | _putchar('\n'); 14 | print_times_table(98); 15 | _putchar('\n'); 16 | print_times_table(12); 17 | return (0); 18 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | void print_times_table(int n) 10 | { 11 | int x, y, z; 12 | 13 | if (n >= 0 && n <= 14) 14 | { 15 | for (x = 0; x <= n; x++) 16 | { 17 | for (y = 0; y <= n; y++) 18 | { 19 | z = x * y; 20 | if (z > 99) 21 | { 22 | _putchar(','); 23 | _putchar(32); 24 | _putchar((z / 100) + '0'); 25 | _putchar(((z / 10) % 10) + '0'); 26 | _putchar((z % 10) + '0'); 27 | } 28 | else if (z > 9) 29 | { 30 | _putchar(','); 31 | _putchar(32); 32 | _putchar(32); 33 | _putchar(((z / 10) % 10) + '0'); 34 | _putchar((z % 10) + '0'); 35 | } 36 | else 37 | { 38 | if (y != 0) 39 | { 40 | _putchar(','); 41 | _putchar(32); 42 | _putchar(32); 43 | _putchar(32); 44 | } 45 | _putchar(z + '0'); 46 | } 47 | } 48 | _putchar('\n'); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /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/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.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 "holberton.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Make alphabet x10 times 5 | * 6 | * Return: void 7 | */ 8 | void print_alphabet_x10(void) 9 | { 10 | char c; 11 | int i = 0; 12 | 13 | while (i <= 9) 14 | { 15 | for (c = 'a'; c <= 'z'; c++) 16 | { 17 | _putchar(c); 18 | } 19 | 20 | _putchar('\n'); 21 | 22 | i++; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 "holberton.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 | void jack_bauer(void) 9 | { 10 | int hours_tens, hours_ones, minutes_tens, minutes_ones, hours_max; 11 | 12 | hours_max = 58; 13 | hours_tens = '0'; 14 | while (hours_tens < '3') 15 | { 16 | if (hours_tens == '2') 17 | { 18 | hours_max = '4'; 19 | } 20 | hours_ones = '0'; 21 | while (hours_ones < hours_max) 22 | { 23 | minutes_tens = '0'; 24 | while (minutes_tens < '6') 25 | { 26 | minutes_ones = '0'; 27 | while (minutes_ones < 58) 28 | { 29 | _putchar(hours_tens); 30 | _putchar(hours_ones); 31 | _putchar(':'); 32 | _putchar(minutes_tens); 33 | _putchar(minutes_ones); 34 | _putchar('\n'); 35 | minutes_ones++; 36 | } 37 | minutes_ones = '0'; 38 | minutes_tens++; 39 | } 40 | minutes_tens = '0'; 41 | hours_ones++; 42 | } 43 | hours_ones = '0'; 44 | hours_tens++; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * times_table - prints the 9 times table 5 | * Description: prints the 9 times table 6 | * Return: void 7 | */ 8 | 9 | void times_table(void) 10 | { 11 | int row, column, product, tens, ones; 12 | 13 | for (row = 0; row <= 9; row++) 14 | { 15 | for (column = 0; column <= 9; column++) 16 | { 17 | product = row * column; 18 | tens = product / 10; 19 | ones = product % 10; 20 | 21 | if (column == 0) 22 | { 23 | _putchar('0'); 24 | } 25 | else if (product < 10) 26 | { 27 | _putchar(','); 28 | _putchar(' '); 29 | _putchar(' '); 30 | _putchar(ones + '0'); 31 | } 32 | else 33 | { 34 | _putchar(','); 35 | _putchar(' '); 36 | _putchar(tens + '0'); 37 | _putchar(ones + '0'); 38 | } 39 | } 40 | _putchar('\n'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int); 8 | int print_last_digit(int); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int, int); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /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 "holberton.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first int 6 | * @b: second int 7 | * @c: third int 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest = b; 14 | 15 | if (a > b) 16 | { 17 | largest = a; 18 | } 19 | else if (c > b) 20 | { 21 | largest = c; 22 | } 23 | 24 | return (largest); 25 | } 26 | -------------------------------------------------------------------------------- /0x03-debugging/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - prints the largest of 3 integers 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | int a, b, c; 12 | int largest; 13 | 14 | a = 972; 15 | b = -98; 16 | c = 0; 17 | 18 | largest = largest_number(a, b, c); 19 | 20 | printf("%d is the largest number\n", largest); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x03-debugging/3-convert_day.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /* 4 | * convert_day - converts day of month to day of year, without accounting 5 | * for leap year 6 | * @month: month in number format 7 | * @day: day of month 8 | * Return: day of year 9 | */ 10 | 11 | int convert_day(int month, int day) 12 | { 13 | switch (month) 14 | { 15 | case 2: 16 | day = 31 + day; 17 | break; 18 | case 3: 19 | day = 59 + day; 20 | break; 21 | case 4: 22 | day = 90 + day; 23 | break; 24 | case 5: 25 | day = 120 + day; 26 | break; 27 | case 6: 28 | day = 151 + day; 29 | break; 30 | case 7: 31 | day = 181 + day; 32 | break; 33 | case 8: 34 | day = 212 + day; 35 | break; 36 | case 9: 37 | day = 243 + day; 38 | break; 39 | case 10: 40 | day = 273 + day; 41 | break; 42 | case 11: 43 | day = 304 + day; 44 | break; 45 | case 12: 46 | day = 334 + day; 47 | break; 48 | default: 49 | break; 50 | } 51 | return (day); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /0x03-debugging/3-main_a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - takes a date and prints how many days are left in the year, taking 6 | * leap years into account 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int month; 13 | int day; 14 | int year; 15 | 16 | month = 4; 17 | day = 01; 18 | year = 1997; 19 | 20 | printf("Date: %02d/%02d/%04d\n", month, day, year); 21 | 22 | day = convert_day(month, day); 23 | 24 | print_remaining_days(month, day, year); 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * Description: prints the date 8 | * @month: month in number format 9 | * @day: day of month 10 | * @year: year 11 | * Return: void 12 | */ 13 | 14 | void print_remaining_days(int month, int day, int year) 15 | { 16 | if ((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0)) 17 | { 18 | if (month > 2 && day >= 60) 19 | { 20 | day++; 21 | } 22 | 23 | printf("Day of the year: %d\n", day); 24 | printf("Remaining days: %d\n", 366 - day); 25 | } 26 | else 27 | { 28 | if (month == 2 && day == 60) 29 | { 30 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 31 | } 32 | else 33 | { 34 | printf("Day of the year: %d\n", day); 35 | printf("Remaining days: %d\n", 365 - day); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | **I am learning on how to debug code** 2 | -------------------------------------------------------------------------------- /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-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - checks the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = 'A'; 14 | printf("%c: %d\n", c, _isupper(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isupper(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/1-isdigit -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - checks my code 6 | * 7 | * Return: Always 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | char c; 13 | 14 | c = '0'; 15 | printf("%c: %d\n", c, _isdigit(c)); 16 | c = 'a'; 17 | printf("%c: %d\n", c, _isdigit(c)); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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_triangle(2); 11 | print_triangle(10); 12 | print_triangle(1); 13 | print_triangle(0); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_triangle - check for a digit 5 | * @size : integer type 6 | * Return:void 7 | */ 8 | 9 | void print_triangle(int size) 10 | { 11 | int i = 1, ii; 12 | 13 | while (i <= size && size > 0) 14 | { 15 | ii = 0; 16 | while (ii < size - i) 17 | { 18 | _putchar(' '); 19 | ii++; 20 | } 21 | ii = 0; 22 | while (ii < i) 23 | { 24 | _putchar('#'); 25 | ii++; 26 | } 27 | 28 | _putchar('\n'); 29 | i++; 30 | } 31 | if (i == 1) 32 | _putchar('\n'); 33 | } 34 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-triangles: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/10-triangles -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/100-prime_factor -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prime numbers 5 | * 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | long num = 612852475143; 12 | long divisor = 2; 13 | long larg_prim = 0; 14 | 15 | while (num != 1) 16 | { 17 | if (num % divisor == 0) 18 | { 19 | num = num / divisor; 20 | larg_prim = divisor; 21 | } 22 | divisor += 1; 23 | } 24 | printf("%ld\n", larg_prim); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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_number(98); 11 | _putchar('\n'); 12 | print_number(402); 13 | _putchar('\n'); 14 | print_number(1024); 15 | _putchar('\n'); 16 | print_number(0); 17 | _putchar('\n'); 18 | print_number(-98); 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_number - prints numbers 4 | * @n: number to be printed 5 | * Return:void 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int x; 11 | 12 | x = n; 13 | if (n < 0) 14 | { 15 | _putchar(45); 16 | x = -n; 17 | } 18 | if (x / 10) 19 | print_number(x / 10); 20 | _putchar((x % 10) + '0'); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/101-print_numbers -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - checks my code 6 | * 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | printf("%d\n", mul(98, 1024)); 13 | printf("%d\n", mul(-402, 4096)); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/2-mul -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - checks my code 5 | * 6 | * Return: Always 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | print_numbers(); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/3-print_numbers -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_numbers - print 0 - 9 5 | * 6 | * Return: void 7 | */ 8 | 9 | void print_numbers(void) 10 | { 11 | char a = 0; 12 | 13 | while (a <= 9) 14 | { 15 | _putchar(a + '0'); 16 | a++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - checks my code 5 | * 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | print_most_numbers(); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/4-print_most_numbers -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_most_numbers - prints 0 - 9 except 2, 3 5 | * Description: prints 6 | * Return: void 7 | */ 8 | 9 | void print_most_numbers(void) 10 | { 11 | int a = 0; 12 | 13 | while (a <= 9) 14 | { 15 | if (a != 2 && a != 4) 16 | { 17 | _putchar(a + '0'); 18 | } 19 | a++; 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check my code 5 | * 6 | * Return: Always 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | more_numbers(); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/5-more_numbers -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * more_numbers - prints 0 - 14 5 | * 6 | * Return: void 7 | */ 8 | 9 | void more_numbers(void) 10 | { 11 | char n, c; 12 | int i = 0; 13 | 14 | while (i < 10) 15 | { 16 | for (n = 0; n <= 14; n++) 17 | { 18 | c = n; 19 | if (n > 9) 20 | { 21 | _putchar('1'); 22 | c = n % 10; 23 | } 24 | _putchar('0' + c); 25 | } 26 | 27 | _putchar('\n'); 28 | i++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-lines: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/6-lines -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - test my code 5 | * 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | print_line(0); 12 | print_line(2); 13 | print_line(10); 14 | print_line(-4); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_line - prints a line 5 | * @n: variable 6 | * 7 | * Return: 0 8 | */ 9 | 10 | void print_line(int n) 11 | { 12 | int a = 0; 13 | 14 | while (a < n && n > 0) 15 | { 16 | _putchar('_'); 17 | a++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-diagonals: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/7-diagonals -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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_diagonal(0); 11 | print_diagonal(2); 12 | print_diagonal(10); 13 | print_diagonal(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_diagonal - diagonal lines made of backslashes 5 | * @n: number of \ to be printed 6 | * Return: void 7 | */ 8 | 9 | void print_diagonal(int n) 10 | { 11 | int i = 0, ii; 12 | 13 | while (i < n && n > 0) 14 | { 15 | ii = 0; 16 | while (ii < i) 17 | { 18 | _putchar(' '); 19 | ii++; 20 | } 21 | 22 | _putchar('\\'); 23 | _putchar('\n'); 24 | i++; 25 | } 26 | if (i == 0) 27 | _putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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_square(2); 11 | print_square(10); 12 | print_square(0); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_square - check for a digit 5 | * @n: number of _ to be printed 6 | * Return: void 7 | */ 8 | 9 | void print_square(int n) 10 | { 11 | int i = 0, ii; 12 | 13 | while (i < n && n > 0) 14 | { 15 | ii = 0; 16 | while (ii < n) 17 | { 18 | _putchar('#'); 19 | ii++; 20 | } 21 | _putchar('\n'); 22 | i++; 23 | } 24 | if (i == 0) 25 | _putchar('\n'); 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-squares: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/8-squares -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x04-more_functions_nested_loops/9-fizz_buzz -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Fizz Buzz code 5 | * 6 | * Return: void 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i = 1; 12 | 13 | while (i <= 100) 14 | { 15 | if (i % 3 == 0 && i % 5 == 0) 16 | { 17 | printf("FizzBuzz"); 18 | } 19 | else if (i % 3 == 0) 20 | { 21 | printf("Fizz"); 22 | } 23 | else if (i % 5 == 0) 24 | { 25 | printf("Buzz"); 26 | } 27 | else 28 | { 29 | printf("%i", i); 30 | } 31 | if (i != 100) 32 | { 33 | putchar(' '); 34 | } 35 | 36 | i++; 37 | } 38 | putchar('\n'); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | **More functions, more nested loops** 2 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _isupper(int c); 3 | int _isdigit(int c); 4 | int mul(int a, int b); 5 | void print_numbers(void); 6 | void print_most_numbers(void); 7 | void print_most_numbers(void); 8 | void more_numbers(void); 9 | void print_line(int n); 10 | void print_diagonal(int n); 11 | void print_square(int size); 12 | void print_triangle(int size); 13 | void print_number(int n); 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-98: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/0-98 -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value of n to 98 5 | * @n: integer pointer to a variable to be updated 6 | * 7 | * Return: nothing 8 | */ 9 | 10 | void reset_to_98(int *n) 11 | { 12 | *n = 98; 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/1-swap -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/100-atoi -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | int main(void) 4 | 5 | { 6 | int nb; 7 | nb = _atoi("98"); 8 | printf("%d\n", nb); 9 | nb = _atoi("-402"); 10 | printf("%d\n", nb); 11 | nb = _atoi(" ------++++++-----+++++--98"); 12 | printf("%d\n", nb); 13 | nb = _atoi("214748364"); 14 | printf("%d\n", nb); 15 | nb = _atoi("0"); 16 | printf("%d\n", nb); 17 | nb = _atoi("Suite 402"); 18 | printf("%d\n", nb); 19 | nb = _atoi(" + + - -98 Battery Street; San Francisco, CA 94111 - USA "); 20 | printf("%d\n", nb); 21 | nb = _atoi("---++++ -++ Sui - te - 402 #cisfun :)"); 22 | printf("%d\n", nb); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-crackme: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/101-crackme -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/101-keygen -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - generates random passwords for 101-crackme 7 | * Return: zero 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | char c; 13 | 14 | srand(time(NULL)); 15 | while (sum <= 2645) 16 | { 17 | c = rand() % 128; 18 | sum += c; 19 | putchar(c); 20 | } 21 | putchar(2772 - sum); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | int main(void) 5 | { 6 | char *str; 7 | int len; 8 | 9 | str = "Holberton!"; 10 | len = _strlen(str); 11 | printf("%d\n", len); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/2-strlen -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | int main(void) 4 | { 5 | char *str; 6 | 7 | str = "I do not fear computers. i fear the lack of them - isaac Asimov"; 8 | _puts(str); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/3-puts -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | int main(void) 4 | { 5 | char *str; 6 | 7 | str = "I do not fear computers. I fear the lack of them - Isaac Asimov"; 8 | print_rev(str); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/4-print_rev -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_rev - prints a 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-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | int main(void) 5 | { 6 | char s[10] = "Holberton"; 7 | 8 | printf("%s\n", s); 9 | rev_string(s); 10 | printf("%s\n", s); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/5-rev_string -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * rev_string - prints a string in reverse 5 | * @s: the used string to beb 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-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | int main(void) 4 | { 5 | char *str; 6 | 7 | str = "0123456789"; 8 | puts2(str); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/6-puts2 -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * puts2 - prints xters of a string 5 | * @str: the used string reference 6 | * Return: 0 7 | */ 8 | 9 | void puts2(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i] != '\0') 14 | { 15 | if (i % 2 == 0) 16 | { 17 | _putchar(str[i]); 18 | } 19 | i++; 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | int main(void) 4 | { 5 | char *str; 6 | 7 | str = "0123456789"; 8 | puts_half(str); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/7-puts_half -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for ALX School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int array[5]; 11 | 12 | array[0] = 98; 13 | array[1] = 402; 14 | array[2] = -198; 15 | array[3] = 298; 16 | array[4] = -1024; 17 | print_array(array, 5); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/8-print_array -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_array - print `n` elements of an array of integers 6 | * @a: int type array pointer 7 | * @n: int type integer 8 | * Description: Numbers must be separated by comma and space. 9 | * Numbers should be displayed in the same order they are stored in array. 10 | * You can only use _putchar to print. 11 | */ 12 | 13 | void print_array(int *a, int n) 14 | { 15 | int 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-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98]; 12 | char *ptr; 13 | 14 | ptr = _strcpy(s1, "First, solve the problem. Then, write the code\n"); 15 | printf("%s", s1); 16 | printf("%s", ptr); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x05-pointers_arrays_strings/9-strcpy -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | **0x05. C - Pointers, arrays and strings** 2 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/Test/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check my code 6 | * 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int n; 13 | 14 | n = 402; 15 | printf("n=%d\n",n); 16 | reset_to_98(&n); 17 | printf("n=%d\n",n); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/Test/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - checks my code 6 | * 7 | * Return: Always 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int a; 13 | int b; 14 | 15 | a = 98; 16 | b = 42; 17 | printf("a=%d, b=%d\n", a, b); 18 | swap_int(&a, &b); 19 | printf("a=%d, b=%d\n", a, b); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/holberton.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 | int _atoi(char *s); 13 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/100-rot13 -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_number - Prints an integer. 5 | * @n: The integer to be printed. 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int num = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | num = -num; 16 | } 17 | 18 | if ((num / 10) > 0) 19 | print_number(num / 10); 20 | 21 | _putchar((num % 10) + '0'); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/101-print_numbers -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/102-magic -------------------------------------------------------------------------------- /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-add: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/103-add -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-buffer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/104-buffer -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/2-strncpy -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/3-strcmp -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reverse_array - Reverses the content of an array of integers. 5 | * @a: The array of integers to be reversed. 6 | * @n: The number of elements in the array. 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int tmp, index; 12 | 13 | for (index = n - 1; index >= n / 2; index--) 14 | { 15 | tmp = a[n - 1 - index]; 16 | a[n - 1 - index] = a[index]; 17 | a[index] = tmp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/5-string_toupper -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * string_toupper - Changes all lowercase letters 5 | * of a string to uppercase. 6 | * @str: The string to be changed. 7 | * 8 | * Return: A pointer to the changed string. 9 | */ 10 | char *string_toupper(char *str) 11 | { 12 | int index = 0; 13 | 14 | while (str[index]) 15 | { 16 | if (str[index] >= 'a' && str[index] <= 'z') 17 | str[index] -= 32; 18 | 19 | index++; 20 | } 21 | 22 | return (str); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/6-cap -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * cap_string - Capitalizes all words of a string. 5 | * @str: The string to be capitalized. 6 | * 7 | * Return: A pointer to the changed string. 8 | */ 9 | char *cap_string(char *str) 10 | { 11 | int index = 0; 12 | 13 | while (str[index]) 14 | { 15 | while (!(str[index] >= 'a' && str[index] <= 'z')) 16 | index++; 17 | 18 | if (str[index - 1] == ' ' || 19 | str[index - 1] == '\t' || 20 | str[index - 1] == '\n' || 21 | str[index - 1] == ',' || 22 | str[index - 1] == ';' || 23 | str[index - 1] == '.' || 24 | str[index - 1] == '!' || 25 | str[index - 1] == '?' || 26 | str[index - 1] == '"' || 27 | str[index - 1] == '(' || 28 | str[index - 1] == ')' || 29 | str[index - 1] == '{' || 30 | str[index - 1] == '}' || 31 | index == 0) 32 | str[index] -= 32; 33 | 34 | index++; 35 | } 36 | 37 | return (str); 38 | } 39 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-1337: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x06-pointers_arrays_strings/7-1337 -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * leet - Encodes a string to 1337. 5 | * @str: The string to be encoded. 6 | * 7 | * Return: A pointer to the encoded string. 8 | */ 9 | char *leet(char *str) 10 | { 11 | int indx1 = 0, indx2; 12 | char leet[8] = {'O', 'L', '?', 'E', 'A', '?', '?', 'T'}; 13 | 14 | while (str[indx1]) 15 | { 16 | for (indx2 = 0; indx2 <= 7; indx2++) 17 | { 18 | if (str[indx1] == leet[indx2] || 19 | str[indx1] - 32 == leet[indx2]) 20 | str[indx1] = indx2 + '0'; 21 | } 22 | 23 | indx1++; 24 | } 25 | 26 | return (str); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | **More Pointers, Arrays and Strings** 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/0-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *ptr; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | ptr = _strcat(s1, s2); 18 | printf("%s", s1); 19 | printf("%s", s2); 20 | printf("%s", ptr); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/1-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98] = "Hello "; 12 | char s2[] = "World!\n"; 13 | char *ptr; 14 | 15 | printf("%s\n", s1); 16 | printf("%s", s2); 17 | ptr = _strncat(s1, s2, 1); 18 | printf("%s\n", s1); 19 | printf("%s", s2); 20 | printf("%s\n", ptr); 21 | ptr = _strncat(s1, s2, 1024); 22 | printf("%s", s1); 23 | printf("%s", s2); 24 | printf("%s", ptr); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/100-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher.\n"; 12 | char *p; 13 | 14 | p = rot13(s); 15 | printf("%s", p); 16 | printf("------------------------------------\n"); 17 | printf("%s", s); 18 | printf("------------------------------------\n"); 19 | p = rot13(s); 20 | printf("%s", p); 21 | printf("------------------------------------\n"); 22 | printf("%s", s); 23 | printf("------------------------------------\n"); 24 | p = rot13(s); 25 | printf("%s", p); 26 | printf("------------------------------------\n"); 27 | printf("%s", s); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/101-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.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_number(98); 11 | _putchar('\n'); 12 | print_number(402); 13 | _putchar('\n'); 14 | print_number(1024); 15 | _putchar('\n'); 16 | print_number(0); 17 | _putchar('\n'); 18 | print_number(-98); 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/104-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char buffer[] = "This is a string!\0And this is the rest of the #buffer :)\1\2\3\4\5\6\7#cisfun\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x20\x21\x34\x56#pointersarefun #infernumisfun\n"; 12 | 13 | printf("%s\n", buffer); 14 | printf("---------------------------------\n"); 15 | print_buffer(buffer, sizeof(buffer)); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/2-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[98]; 12 | char *ptr; 13 | int i; 14 | 15 | for (i = 0; i < 98 - 1; i++) 16 | { 17 | s1[i] = '*'; 18 | } 19 | s1[i] = '\0'; 20 | printf("%s\n", s1); 21 | ptr = _strncpy(s1, "First, solve the problem. Then, write the code\n", 5); 22 | printf("%s\n", s1); 23 | printf("%s\n", ptr); 24 | ptr = _strncpy(s1, "First, solve the problem. Then, write the code\n", 90); 25 | printf("%s", s1); 26 | printf("%s", ptr); 27 | for (i = 0; i < 98; i++) 28 | { 29 | if (i % 10) 30 | { 31 | printf(" "); 32 | } 33 | if (!(i % 10) && i) 34 | { 35 | printf("\n"); 36 | } 37 | printf("0x%02x", s1[i]); 38 | } 39 | printf("\n"); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/3-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s1[] = "Hello"; 12 | char s2[] = "World!"; 13 | 14 | printf("%d\n", _strcmp(s1, s2)); 15 | printf("%d\n", _strcmp(s2, s1)); 16 | printf("%d\n", _strcmp(s1, s1)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/4-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * @a: an array of integers 7 | * @n: the number of elements to swap 8 | * 9 | * Return: nothing. 10 | */ 11 | void print_array(int *a, int n) 12 | { 13 | int i; 14 | 15 | i = 0; 16 | while (i < n) 17 | { 18 | if (i != 0) 19 | { 20 | printf(", "); 21 | } 22 | printf("%d", a[i]); 23 | i++; 24 | } 25 | printf("\n"); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/5-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char str[] = "Holberton School!\n"; 12 | char *ptr; 13 | 14 | ptr = string_toupper(str); 15 | printf("%s", ptr); 16 | printf("%s", str); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/6-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char str[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\nhello world! hello-world 0123456hello world\thello world.hello world\n"; 12 | char *ptr; 13 | 14 | ptr = cap_string(str); 15 | printf("%s", ptr); 16 | printf("%s", str); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/Test/7-main.c: -------------------------------------------------------------------------------- 1 | #include "../holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char s[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\n"; 12 | char *p; 13 | 14 | p = leet(s); 15 | printf("%s", p); 16 | printf("%s", s); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 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 *rot13(char *); 11 | void print_number(int n); 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 13 | void print_buffer(char *b, int size); 14 | int main(void); 15 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x07-pointers_arrays_strings/0-memset -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x07-pointers_arrays_strings/1-memcpy -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s0 = "Bob Dylan"; 12 | char *s1 = "Robert Allen"; 13 | 14 | printf("%s, %s\n", s0, s1); 15 | set_string(&s1, s0); 16 | printf("%s, %s\n", s0, s1); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello"; 12 | char *f; 13 | 14 | f = _strchr(s, 'l'); 15 | 16 | if (f != NULL) 17 | { 18 | printf("%s\n", f); 19 | } 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x07-pointers_arrays_strings/2-strchr -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "oleh"; 13 | unsigned int n; 14 | 15 | n = _strspn(s, f); 16 | printf("%u\n", n); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x07-pointers_arrays_strings/3-strspn -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "world"; 13 | char *t; 14 | 15 | t = _strpbrk(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x07-pointers_arrays_strings/4-strpbrk -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char *s = "hello, world"; 12 | char *f = "world"; 13 | char *t; 14 | 15 | t = _strstr(s, f); 16 | printf("%s\n", t); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x07-pointers_arrays_strings/5-strstr -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char board[8][8] = { 12 | {'r', 'k', 'b', 'q', 'k', 'b', 'k', 'r'}, 13 | {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, 14 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 15 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 16 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 17 | {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, 18 | {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, 19 | {'R', 'K', 'B', 'Q', 'K', 'B', 'K', 'R'}, 20 | }; 21 | print_chessboard(board); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x07-pointers_arrays_strings/7-print_chessboard -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int c3[3][3] = { 12 | {0, 1, 5}, 13 | {10, 11, 12}, 14 | {1000, 101, 102}, 15 | }; 16 | int c5[5][5] = { 17 | {0, 1, 5, 12124, 1234}, 18 | {10, 11, 12, 123521, 12512}, 19 | {1000, 101, 102, 12545, 214543435}, 20 | {100, 1012451, 11102, 12545, 214543435}, 21 | {10, 12401, 10452, 11542545, 1214543435}, 22 | }; 23 | print_diagsums((int *)c3, 3); 24 | print_diagsums((int *)c5, 5); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | **7. C - Even more pointers, arrays and strings** 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/crackme2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x07-pointers_arrays_strings/crackme2 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | /** 5 | * File: holberton.h 6 | * Auth: Firdaus 7 | * Desc: Header file containing prototypes for all functions 8 | * used in the 0x07-pointers_arrays_strings directory. 9 | */ 10 | 11 | #include 12 | 13 | int _putchar(char c); 14 | void *_memset(void *s, int c, size_t n); 15 | void *_memcpy(void *dest, const void *src, size_t n); 16 | char *_strchr(char *s, char c); 17 | unsigned int _strspn(char *s, char *accept); 18 | char *_strpbrk(char *s, char *accept); 19 | char *_strstr(char *haystack, char *needle); 20 | void print_chessboard(char (*a)[8]); 21 | void print_diagsums(int *a, int size); 22 | void set_string(char **s, char *to); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts_recursion - Prints a string, followed by a new line. 5 | * @s: The string to be printed. 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s) 10 | { 11 | _putchar(*s); 12 | _puts_recursion(s + 1); 13 | } 14 | 15 | else 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x08-recursion/1-print_rev_recursion -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _print_rev_recursion - Prints a string in reverse. 5 | * @s: The string to be printed. 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s) 10 | { 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/100-palindrome: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x08-recursion/100-palindrome -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x08-recursion/101-wildcmp -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x08-recursion/2-strlen_recursion -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen_recursion - Returns the length of a string. 5 | * @s: The string to be measured. 6 | * 7 | * Return: The length of the string. 8 | */ 9 | int _strlen_recursion(char *s) 10 | { 11 | int len = 0; 12 | 13 | if (*s) 14 | { 15 | len++; 16 | len += _strlen_recursion(s + 1); 17 | } 18 | 19 | return (len); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x08-recursion/3-factorial -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * factorial - Returns the factorial of a given number. 5 | * @n: The number to find the factorial of. 6 | * 7 | * Return: If n > 0 - the factorial of n. 8 | * If n < 0 - 1 to indicate an error. 9 | */ 10 | int factorial(int n) 11 | { 12 | int result = n; 13 | 14 | if (n < 0) 15 | return (-1); 16 | 17 | else if (n >= 0 && n <= 1) 18 | return (1); 19 | 20 | result *= factorial(n - 1); 21 | 22 | return (result); 23 | } 24 | -------------------------------------------------------------------------------- /0x08-recursion/4-po: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x08-recursion/4-po -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _pow_recursion - Returns the value of x raised to the power of y. 5 | * @x: The number to be raised. 6 | * @y: The power. 7 | * 8 | * Return: The value of x raised to the power of y. 9 | */ 10 | int _pow_recursion(int x, int y) 11 | { 12 | int result = x; 13 | 14 | if (y < 0) 15 | return (-1); 16 | 17 | else if (y == 0) 18 | return (1); 19 | 20 | result *= _pow_recursion(x, y - 1); 21 | 22 | return (result); 23 | } 24 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x08-recursion/5-sqrt -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | int find_sqrt(int num, int root); 4 | int _sqrt_recursion(int n); 5 | 6 | /** 7 | * find_sqrt - Finds the natural square root of an inputted number. 8 | * @num: The number to find the square root of. 9 | * @root: The root to be tested. 10 | * 11 | * Return: If the number has a natural square root - the square root. 12 | * If the number does not have a natural square root - -1. 13 | */ 14 | int find_sqrt(int num, int root) 15 | { 16 | if ((root * root) == num) 17 | return (root); 18 | 19 | if (root == num / 2) 20 | return (-1); 21 | 22 | return (find_sqrt(num, root + 1)); 23 | } 24 | 25 | /** 26 | * _sqrt_recursion - Returns the natural square root of a number. 27 | * @n: The number to return the square root of. 28 | * 29 | * Return: If n has a natural square root - the natural square root of n. 30 | * If n does not have a natural square root - -1. 31 | */ 32 | int _sqrt_recursion(int n) 33 | { 34 | int root = 0; 35 | 36 | if (n < 0) 37 | return (-1); 38 | 39 | if (n == 1) 40 | return (1); 41 | 42 | return (find_sqrt(n, root)); 43 | } 44 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | int is_divisible(int num, int div); 4 | int is_prime_number(int n); 5 | 6 | /** 7 | * is_divisible - Checks if a number is divisible. 8 | * @num: The number to be checked. 9 | * @div: The divisor. 10 | * 11 | * Return: If the number is divisible - 0. 12 | * If the number is not divisible - 1. 13 | */ 14 | int is_divisible(int num, int div) 15 | { 16 | if (num % div == 0) 17 | return (0); 18 | 19 | if (div == num / 2) 20 | return (1); 21 | 22 | return (is_divisible(num, div + 1)); 23 | } 24 | 25 | /** 26 | * is_prime_number - Checks if a number is prime. 27 | * @n: The number to be checked. 28 | * 29 | * Return: If the integer is not prime - 0. 30 | * If the number is prime - 1. 31 | */ 32 | int is_prime_number(int n) 33 | { 34 | int div = 2; 35 | 36 | if (n <= 1) 37 | return (0); 38 | 39 | if (n >= 2 && n <= 3) 40 | return (1); 41 | 42 | return (is_divisible(n, div)); 43 | } 44 | -------------------------------------------------------------------------------- /0x08-recursion/6-prime: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x08-recursion/6-prime -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | **0x08. C - Recursion** 2 | -------------------------------------------------------------------------------- /0x08-recursion/Test/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - check the code for ALX School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | _puts_recursion("Betty Holberton"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x08-recursion/Test/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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_rev_recursion("\nColton Walker"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x08-recursion/Test/100-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_palindrome("level"); 14 | printf("%d\n", r); 15 | r = is_palindrome("redder"); 16 | printf("%d\n", r); 17 | r = is_palindrome("holberton"); 18 | printf("%d\n", r); 19 | r = is_palindrome("step on no pets"); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x08-recursion/Test/101-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = wildcmp("main.c", "*.c"); 14 | printf("%d\n", r); 15 | r = wildcmp("main.c", "m*a*i*n*.*c*"); 16 | printf("%d\n", r); 17 | r = wildcmp("main.c", "main.c"); 18 | printf("%d\n", r); 19 | r = wildcmp("main.c", "m*c"); 20 | printf("%d\n", r); 21 | r = wildcmp("main.c", "ma********************************c"); 22 | printf("%d\n", r); 23 | r = wildcmp("main.c", "*"); 24 | printf("%d\n", r); 25 | r = wildcmp("main.c", "***"); 26 | printf("%d\n", r); 27 | r = wildcmp("main.c", "m.*c"); 28 | printf("%d\n", r); 29 | r = wildcmp("main.c", "**.*c"); 30 | printf("%d\n", r); 31 | r = wildcmp("main-main.c", "ma*in.c"); 32 | printf("%d\n", r); 33 | r = wildcmp("main", "main*d"); 34 | printf("%d\n", r); 35 | r = wildcmp("abc", "*b"); 36 | printf("%d\n", r); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x08-recursion/Test/2-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = _strlen_recursion("Corbin Coleman"); 14 | printf("%d\n", n); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/Test/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = factorial(1); 14 | printf("%d\n", r); 15 | r = factorial(5); 16 | printf("%d\n", r); 17 | r = factorial(10); 18 | printf("%d\n", r); 19 | r = factorial(-1024); 20 | printf("%d\n", r); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x08-recursion/Test/4-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _pow_recursion(1, 10); 14 | printf("%d\n", r); 15 | r = _pow_recursion(1024, 0); 16 | printf("%d\n", r); 17 | r = _pow_recursion(2, 16); 18 | printf("%d\n", r); 19 | r = _pow_recursion(5, 2); 20 | printf("%d\n", r); 21 | r = _pow_recursion(5, -2); 22 | printf("%d\n", r); 23 | r = _pow_recursion(-5, 3); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x08-recursion/Test/5-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = _sqrt_recursion(1); 14 | printf("%d\n", r); 15 | r = _sqrt_recursion(1024); 16 | printf("%d\n", r); 17 | r = _sqrt_recursion(16); 18 | printf("%d\n", r); 19 | r = _sqrt_recursion(17); 20 | printf("%d\n", r); 21 | r = _sqrt_recursion(25); 22 | printf("%d\n", r); 23 | r = _sqrt_recursion(-1); 24 | printf("%d\n", r); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x08-recursion/Test/6-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int r; 12 | 13 | r = is_prime_number(1); 14 | printf("%d\n", r); 15 | r = is_prime_number(1024); 16 | printf("%d\n", r); 17 | r = is_prime_number(16); 18 | printf("%d\n", r); 19 | r = is_prime_number(17); 20 | printf("%d\n", r); 21 | r = is_prime_number(25); 22 | printf("%d\n", r); 23 | r = is_prime_number(-1); 24 | printf("%d\n", r); 25 | r = is_prime_number(113); 26 | printf("%d\n", r); 27 | r = is_prime_number(7919); 28 | printf("%d\n", r); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | /** 5 | * File: holberton.h 6 | * Desc: Header file containing prototypes for all functions 7 | * used in the 0x07-recursion directory. 8 | */ 9 | 10 | int _putchar(char c); 11 | void _puts_recursion(char *s); 12 | void _print_rev_recursion(char *s); 13 | int _strlen_recursion(char *s); 14 | int factorial(int n); 15 | int _pow_recursion(int x, int y); 16 | int _sqrt_recursion(int n); 17 | int is_prime_number(int n); 18 | int is_palindrome(char *s); 19 | int wildcmp(char *s1, char *s2); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c *.c 3 | ar -rcs liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | /* 5 | * File: holberton.h 6 | * Auth: Firdaus 7 | * Desc: Header file containing the prototypes of all functions 8 | * included in the static library libholberton.a. 9 | */ 10 | 11 | int _putchar(char c); 12 | int _islower(int c); 13 | int _isalpha(int c); 14 | int _abs(int n); 15 | int _isupper(int c); 16 | int _isdigit(int c); 17 | int _strlen(char *s); 18 | void _puts(char *s); 19 | char *_strcpy(char *dest, char *src); 20 | int _atoi(char *s); 21 | char *_strcat(char *dest, char *src); 22 | char *_strncat(char *dest, char *src, int n); 23 | char *_strncpy(char *dest, char *src, int n); 24 | int _strcmp(char *s1, char *s2); 25 | char *_memset(char *s, char b, unsigned int n); 26 | char *_memcpy(char *dest, char *src, unsigned int n); 27 | char *_strchr(char *s, char c); 28 | unsigned int _strspn(char *s, char *accept); 29 | char *_strpbrk(char *s, char *accept); 30 | char *_strstr(char *haystack, char *needle); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libholberton.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x09-static_libraries/libholberton.a -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - Prints the name of the program 6 | * @argc: Number of command line arguments 7 | * @argv: Array name 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | printf("%s\n", argv[argc * 0]); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - Prints the number of arguments passed into main 6 | * @argc: Number of command line arguments 7 | * @argv: Array name 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i; 14 | 15 | if (argc == 1) 16 | printf("%d\n", argc - 1); 17 | else 18 | { 19 | for (i = 0; *argv; i++, argv++) 20 | ; 21 | 22 | printf("%d\n", i - 1); 23 | } 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * main - Prints all arguments passed into main 6 | * @argc: Number of command line arguments 7 | * @argv: Array name 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < argc; i++) 16 | printf("%s\n", argv[i]); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * main - Program that takes first two integer arguments and prints the product 7 | * @argc: Number of command line arguments 8 | * @argv: Array name 9 | * Return: 1 if not enough arguments passed in, 0 otherwise 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int i, j; 15 | 16 | if (argc == 1 || argc == 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | else 22 | { 23 | j = 1; 24 | 25 | for (i = 1; i < 3; i++) 26 | j *= atoi(argv[i]); 27 | 28 | printf("%d\n", j); 29 | } 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "holberton.h" 6 | 7 | /** 8 | * main - Program that takes in all integer arguments and returns the sum 9 | * @argc: Number of command line arguments 10 | * @argv: Array name 11 | * Return: 1 if a non-integer is among the passed in arguments, 0 otherwise 12 | */ 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | int i, j, length, sum; 17 | char *ptr; 18 | 19 | if (argc < 2) 20 | printf("0\n"); 21 | else 22 | { 23 | sum = 0; 24 | for (i = 1; i < argc; i++) 25 | { 26 | ptr = argv[i]; 27 | length = strlen(ptr); 28 | 29 | for (j = 0; j < length; j++) 30 | { 31 | if (isdigit(*(ptr + j)) == 0) 32 | { 33 | printf("Error\n"); 34 | return (1); 35 | } 36 | } 37 | 38 | sum += atoi(argv[i]); 39 | } 40 | 41 | printf("%d\n", sum); 42 | } 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | **0x0A. C - argc, argv** -------------------------------------------------------------------------------- /0x0A-argc_argv/holberton.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef HOLBERTON_H 3 | #define HOLBERTON_H 4 | 5 | int _putchar(char c); 6 | 7 | #endif -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 "holberton.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 "holberton.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 "holberton.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/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | -------------------------------------------------------------------------------- /0x0B-malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | /** 5 | * File: holberton.h 6 | * Desc: Header file containing prototypes for all functions 7 | * used in the 0x0A-malloc_free directory. 8 | */ 9 | 10 | char *create_array(unsigned int size, char c); 11 | char *_strdup(char *str); 12 | char *str_concat(char *s1, char *s2); 13 | int **alloc_grid(int width, int height); 14 | void free_grid(int **grid, int height); 15 | char *argstostr(int ac, char **av); 16 | char **strtow(char *str); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - check the code for ALX School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | char *c; 14 | int *i; 15 | float *f; 16 | double *d; 17 | 18 | c = malloc_checked(sizeof(char) * 1024); 19 | printf("%p\n", (void *)c); 20 | i = malloc_checked(sizeof(int) * 402); 21 | printf("%p\n", (void *)i); 22 | f = malloc_checked(sizeof(float) * 100000000); 23 | printf("%p\n", (void *)f); 24 | d = malloc_checked(INT_MAX); 25 | printf("%p\n", (void *)d); 26 | free(c); 27 | free(i); 28 | free(f); 29 | free(d); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | void *malloc_checked(unsigned int b) 11 | { 12 | void *mem = malloc(b); 13 | 14 | if (mem == NULL) 15 | exit(98); 16 | 17 | return (mem); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *concat; 13 | 14 | concat = string_nconcat("Holberton ", "School !!!", 6); 15 | printf("%s\n", concat); 16 | free(concat); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | void *_calloc(unsigned int nmemb, unsigned int size) 14 | { 15 | void *mem; 16 | char *filler; 17 | unsigned int index; 18 | 19 | if (nmemb == 0 || size == 0) 20 | return (NULL); 21 | 22 | mem = malloc(size * nmemb); 23 | 24 | if (mem == NULL) 25 | return (NULL); 26 | 27 | filler = mem; 28 | 29 | for (index = 0; index < (size * nmemb); index++) 30 | filler[index] = '\0'; 31 | 32 | return (mem); 33 | } 34 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | int *array_range(int min, int max) 14 | { 15 | int *array, index, size; 16 | 17 | if (min > max) 18 | return (NULL); 19 | 20 | size = max - min + 1; 21 | 22 | array = malloc(sizeof(int) * size); 23 | 24 | if (array == NULL) 25 | return (NULL); 26 | 27 | for (index = 0; index < size; index++) 28 | array[index] = min++; 29 | 30 | return (array); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * simple_print_buffer - prints buffer in hexa 8 | * @buffer: the address of memory to print 9 | * @size: the size of the memory to print 10 | * 11 | * Return: Nothing. 12 | */ 13 | void simple_print_buffer(int *buffer, unsigned int size) 14 | { 15 | unsigned int i; 16 | 17 | i = 0; 18 | while (i < size) 19 | { 20 | if (i % 10) 21 | { 22 | printf(" "); 23 | } 24 | if (!(i % 10) && i) 25 | { 26 | printf("\n"); 27 | } 28 | printf("0x%02x", buffer[i]); 29 | i++; 30 | } 31 | printf("\n"); 32 | } 33 | 34 | /** 35 | * main - check the code for ALX School students. 36 | * 37 | * Return: Always 0. 38 | */ 39 | int main(void) 40 | { 41 | int *a; 42 | 43 | a = array_range(0, 10); 44 | simple_print_buffer(a, 11); 45 | free(a); 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | **0x0C. C - More malloc, free** 2 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0C-more_malloc_free/a -------------------------------------------------------------------------------- /0x0C-more_malloc_free/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0C-more_malloc_free/b -------------------------------------------------------------------------------- /0x0C-more_malloc_free/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0C-more_malloc_free/c -------------------------------------------------------------------------------- /0x0C-more_malloc_free/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0C-more_malloc_free/d -------------------------------------------------------------------------------- /0x0C-more_malloc_free/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0C-more_malloc_free/e -------------------------------------------------------------------------------- /0x0C-more_malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | /** 5 | * Auth: Brennan D Baraban 6 | * Desc: Header file containing prototypes for all functions 7 | * used in the 0x0B-more_malloc_free directory. 8 | */ 9 | 10 | int _putchar(char c); 11 | void *malloc_checked(unsigned int b); 12 | char *string_nconcat(char *s1, char *s2, unsigned int n); 13 | void *_calloc(unsigned int nmemb, unsigned int size); 14 | int *array_range(int min, int max); 15 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0C-more_malloc_free/mul -------------------------------------------------------------------------------- /0x0D-preprocessor/0-main.c: -------------------------------------------------------------------------------- 1 | #include "0-object_like_macro.h" 2 | #include "0-object_like_macro.h" 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = 98 + SIZE; 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | /* 5 | * File: 0-object_like_macro.h 6 | * Desc: Header file that defines a macro named 7 | * SIZE as an abbreviation for the token 1024. 8 | */ 9 | 10 | #define SIZE 1024 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-main.c: -------------------------------------------------------------------------------- 1 | #include "1-pi.h" 2 | #include "1-pi.h" 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | float a; 13 | float r; 14 | 15 | r = 98; 16 | a = PI * r * r; 17 | printf("%.3f\n", a); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | /* 5 | * File: 1-pi.h 6 | * Desc: Header file that defines a macro named PI 7 | * as an abbreviation for the token 3.14159265359. 8 | */ 9 | 10 | #define PI 3.14159265359 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the name of the file of the program 5 | * was compiled from, followed by a new line. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | printf("%s\n", __FILE__); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | /* 5 | * File: 3-function_like_macro.h 6 | * Desc: Header file that defines a function-like macro ABS(x) 7 | * that computes the absolute value of a number x. 8 | */ 9 | 10 | #define ABS(x) ((x) < 0 ? (-x) : (x)) 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "3-function_like_macro.h" 3 | #include "3-function_like_macro.h" 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = ABS(-98) * 10; 16 | j = ABS(98) * 10; 17 | printf("%d, %d\n", i, j); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "4-sum.h" 3 | #include "4-sum.h" 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | int s; 13 | 14 | s = SUM(98, 1024); 15 | printf("%d\n", s); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | /* 5 | * File: 4-sum.h 6 | * Desc: Header file that defines a function-like macro SUM(x, y) 7 | * that computes the sum of the numbers x and y. 8 | */ 9 | 10 | #define SUM(x, y) ((x) + (y)) 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | **0x0D. C - Preprocessor** 2 | -------------------------------------------------------------------------------- /0x0D-preprocessor/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0D-preprocessor/a -------------------------------------------------------------------------------- /0x0D-preprocessor/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0D-preprocessor/b -------------------------------------------------------------------------------- /0x0D-preprocessor/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0D-preprocessor/c -------------------------------------------------------------------------------- /0x0D-preprocessor/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0D-preprocessor/d -------------------------------------------------------------------------------- /0x0D-preprocessor/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0D-preprocessor/e -------------------------------------------------------------------------------- /0x0E-structures_typedef/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - Initializes a variable of type struct dog. 6 | * @d: The dog to be initialized. 7 | * @name: The name of the dog. 8 | * @age: The age of the dog. 9 | * @owner: The owner of the dog. 10 | */ 11 | void init_dog(struct dog *d, char *name, float age, char *owner) 12 | { 13 | if (d != NULL) 14 | { 15 | d->name = name; 16 | d->age = age; 17 | d->owner = owner; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | init_dog(&my_dog, "Poppy", 3.5, "Bob"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | print_dog(&my_dog); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_dog - Prints a struct dog. 7 | * @d: The struct dog to be printed. 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | if (d == NULL) 12 | return; 13 | 14 | if (d->name == NULL) 15 | printf("Name: (nil)\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | if (d->age < 0) 20 | printf("Age: (nil)\n"); 21 | else 22 | printf("Age: %f\n", d->age); 23 | 24 | if (d->owner == NULL) 25 | printf("Owner: (nil)\n"); 26 | else 27 | printf("Owner: %s\n", d->owner); 28 | } 29 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Poppy", 3.5, "Bob"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - Frees dogs. 6 | * @d: The dog to be freed. 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d == NULL) 11 | return; 12 | 13 | free(d->owner); 14 | free(d->name); 15 | free(d); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t *my_dog; 12 | 13 | my_dog = new_dog("Poppy", 3.5, "Bob"); 14 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); 15 | free_dog(my_dog); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | **0x0E. C - Structures, typedef** 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0E-structures_typedef/a -------------------------------------------------------------------------------- /0x0E-structures_typedef/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0E-structures_typedef/b -------------------------------------------------------------------------------- /0x0E-structures_typedef/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0E-structures_typedef/c -------------------------------------------------------------------------------- /0x0E-structures_typedef/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0E-structures_typedef/d -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /* 5 | * File: dog.h 6 | * Desc: Header file that defines a new type struct dog. 7 | */ 8 | 9 | /** 10 | * struct dog - A new type describing a dog. 11 | * @name: The name of the dog. 12 | * @age: The age of the dog. 13 | * @owner: The owner of the dog. 14 | */ 15 | struct dog 16 | { 17 | char *name; 18 | float age; 19 | char *owner; 20 | }; 21 | 22 | /** 23 | * dog_t - Typedef for struct dog 24 | */ 25 | typedef struct dog dog_t; 26 | 27 | void init_dog(struct dog *d, char *name, float age, char *owner); 28 | void print_dog(struct dog *d); 29 | dog_t *new_dog(char *name, float age, char *owner); 30 | void free_dog(dog_t *d); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0E-structures_typedef/e -------------------------------------------------------------------------------- /0x0E-structures_typedef/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x0E-structures_typedef/f -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - Prints a name. 5 | * @name: The name to be printed. 6 | * @f: A pointer to a function that prints a name. 7 | */ 8 | void print_name(char *name, void (*f)(char *)) 9 | { 10 | if (name == NULL || f == NULL) 11 | return; 12 | 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - Executes a function given as a 5 | * parameter on each element of an array. 6 | * @array: The array. 7 | * @size: The size of array. 8 | * @action: A pointer to the function to be executed. 9 | */ 10 | void array_iterator(int *array, size_t size, void (*action)(int)) 11 | { 12 | if (array == NULL || action == NULL) 13 | return; 14 | 15 | while (size-- > 0) 16 | { 17 | action(*array); 18 | array++; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Prints the opcodes of itself. 6 | * @argc: The number of arguments supplied to the program. 7 | * @argv: An array of pointers to the arguments. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int bytes, index; 14 | int (*address)(int, char **) = main; 15 | unsigned char opcode; 16 | 17 | if (argc != 2) 18 | { 19 | printf("Error\n"); 20 | exit(1); 21 | } 22 | 23 | bytes = atoi(argv[1]); 24 | 25 | if (bytes < 0) 26 | { 27 | printf("Error\n"); 28 | exit(2); 29 | } 30 | 31 | for (index = 0; index < bytes; index++) 32 | { 33 | opcode = *(unsigned char *)address; 34 | printf("%.2x", opcode); 35 | 36 | if (index == bytes - 1) 37 | continue; 38 | printf(" "); 39 | 40 | address++; 41 | } 42 | 43 | printf("\n"); 44 | 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - Searches for an integer in an array of integers. 5 | * @array: The array of integers. 6 | * @size: The size of the array. 7 | * @cmp: A pointer to the function to be used to compare values. 8 | * 9 | * Return: If no element matches or size <= 0 - -1. 10 | * Otherwise - The index of the first element for which 11 | * the cmp function does not return 0. 12 | */ 13 | int int_index(int *array, int size, int (*cmp)(int)) 14 | { 15 | int index; 16 | 17 | if (array == NULL || cmp == NULL) 18 | return (-1); 19 | 20 | for (index = 0; index < size; index++) 21 | { 22 | if (cmp(array[index]) != 0) 23 | return (index); 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | 4 | /* 5 | * Desc: Header file containing all structures and 6 | * prototypes used by the 3-main.c program. 7 | */ 8 | 9 | /** 10 | * struct op - A struct op. 11 | * @op: The operator. 12 | * @f: The associated function. 13 | */ 14 | typedef struct op 15 | { 16 | char *op; 17 | int (*f)(int a, int b); 18 | } op_t; 19 | 20 | int op_add(int a, int b); 21 | int op_sub(int a, int b); 22 | int op_mul(int a, int b); 23 | int op_div(int a, int b); 24 | int op_mod(int a, int b); 25 | int (*get_op_func(char *s))(int, int); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | * get_op_func - Selects the correct function to perform 6 | * the operation asked by the user. 7 | * @s: The operator passed as argument. 8 | * 9 | * Return: A pointer to the function corresponding 10 | * to the operator given as a parameter. 11 | */ 12 | int (*get_op_func(char *s))(int, int) 13 | { 14 | op_t ops[] = { 15 | {"+", op_add}, 16 | {"-", op_sub}, 17 | {"*", op_mul}, 18 | {"/", op_div}, 19 | {"%", op_mod}, 20 | {NULL, NULL} 21 | }; 22 | int i = 0; 23 | 24 | while (ops[i].op != NULL && *(ops[i].op) != *s) 25 | i++; 26 | 27 | return (ops[i].f); 28 | } 29 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints the result of simple operations. 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 | int num1, num2; 15 | char *op; 16 | 17 | if (argc != 4) 18 | { 19 | printf("Error\n"); 20 | exit(98); 21 | } 22 | 23 | num1 = atoi(argv[1]); 24 | op = argv[2]; 25 | num2 = atoi(argv[3]); 26 | 27 | if (get_op_func(op) == NULL || op[1] != '\0') 28 | { 29 | printf("Error\n"); 30 | exit(99); 31 | } 32 | 33 | if ((*op == '/' && num2 == 0) || 34 | (*op == '%' && num2 == 0)) 35 | { 36 | printf("Error\n"); 37 | exit(100); 38 | } 39 | 40 | printf("%d\n", get_op_func(op)(num1, num2)); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | **0x0F. C - Function pointers** 2 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | 4 | /* 5 | * Desc: Header file containing prototypes for all functions 6 | * used in the 0x0E-function_pointers directory. 7 | */ 8 | 9 | #include 10 | 11 | void print_name(char *name, void (*f)(char *)); 12 | void array_iterator(int *array, size_t size, void (*action)(int)); 13 | int int_index(int *array, int size, int (*cmp)(int)); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /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 | #include 3 | 4 | /** 5 | * sum_them_all - Returns the sum of all its paramters. 6 | * @n: The number of paramters passed to the function. 7 | * @...: A variable number of paramters to calculate the sum of. 8 | * 9 | * Return: If n == 0 - 0. 10 | * Otherwise - the sum of all parameters. 11 | */ 12 | int sum_them_all(const unsigned int n, ...) 13 | { 14 | va_list nums; 15 | unsigned int index, sum = 0; 16 | 17 | va_start(nums, n); 18 | 19 | for (index = 0; index < n; index++) 20 | sum += va_arg(nums, int); 21 | 22 | va_end(nums); 23 | 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 "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_numbers - Prints numbers, followed by a new line. 7 | * @separator: The string to be printed between numbers. 8 | * @n: The number of integers passed to the function. 9 | * @...: A variable number of numbers to be printed. 10 | */ 11 | void print_numbers(const char *separator, const unsigned int n, ...) 12 | { 13 | va_list nums; 14 | unsigned int index; 15 | 16 | va_start(nums, n); 17 | 18 | for (index = 0; index < n; index++) 19 | { 20 | printf("%d", va_arg(nums, int)); 21 | 22 | if (index != (n - 1) && separator != NULL) 23 | printf("%s", separator); 24 | } 25 | 26 | printf("\n"); 27 | 28 | va_end(nums); 29 | } 30 | -------------------------------------------------------------------------------- /0x10-variadic_functions/100-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | section .text 2 | global main 3 | 4 | main: 5 | mov edx,len 6 | mov ecx,msg 7 | mov ebx,1 8 | mov eax,4 9 | int 0x80 10 | 11 | mov eax,0 12 | int 0x80 13 | 14 | section .data 15 | msg: db 'Hello, Holberton', 0xa 16 | len: equ $ - msg 17 | -------------------------------------------------------------------------------- /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 | #include 3 | #include 4 | 5 | /** 6 | * print_strings - Prints strings, followed by a new line. 7 | * @separator: The string to be printed between strings. 8 | * @n: The number of strings passed to the function. 9 | * @...: A variable number of strings to be printed. 10 | * 11 | * Description: If separator is NULL, it is not printed. 12 | * If one of the strings if NULL, (nil) is printed instead. 13 | */ 14 | void print_strings(const char *separator, const unsigned int n, ...) 15 | { 16 | va_list strings; 17 | char *str; 18 | unsigned int index; 19 | 20 | va_start(strings, n); 21 | 22 | for (index = 0; index < n; index++) 23 | { 24 | str = va_arg(strings, char *); 25 | 26 | if (str == NULL) 27 | printf("(nil)"); 28 | else 29 | printf("%s", str); 30 | 31 | if (index != (n - 1) && separator != NULL) 32 | printf("%s", separator); 33 | } 34 | 35 | printf("\n"); 36 | 37 | va_end(strings); 38 | } 39 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-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_all("ceis", 'H', 0, "lberton"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | **0x10. C - Variadic functions** 2 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | 4 | /* 5 | * File: variadic_functions.h 6 | * Desc: Header file containing prototypes for all functions 7 | * used in the 0x0F-variadic_functions directory. 8 | */ 9 | 10 | #include 11 | 12 | /** 13 | * struct printer - A new struct type defining a printer. 14 | * @symbol: A symbol representing a data type. 15 | * @print: A function pointer to a function that prints 16 | * a data type corresponding to symbol. 17 | */ 18 | typedef struct printer 19 | { 20 | char *symbol; 21 | void (*print)(va_list arg); 22 | 23 | } printer_t; 24 | 25 | int sum_them_all(const unsigned int n, ...); 26 | void print_numbers(const char *separator, const unsigned int n, ...); 27 | void print_strings(const char *separator, const unsigned int n, ...); 28 | void print_all(const char * const format, ...); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_list - Prints all the elements of a list_t list. 6 | * @h: The list_t list. 7 | * 8 | * Return: The number of nodes in h. 9 | */ 10 | 11 | size_t print_list(const list_t *h) 12 | { 13 | size_t nodes = 0; 14 | 15 | while (h) 16 | { 17 | if (h->str == NULL) 18 | printf("[0] (nil)\n"); 19 | 20 | else 21 | printf("[%d] %s\n", h->len, h->str); 22 | 23 | nodes++; 24 | h = h->next; 25 | } 26 | 27 | return (nodes); 28 | } 29 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - Finds the number of elements in 5 | * a linked list_t list. 6 | * @h: The linked list_t list. 7 | * @elements: a counter var for size_t (nodes) 8 | * 9 | * Return: The number of elements in h. 10 | */ 11 | size_t list_len(const list_t *h) 12 | { 13 | size_t elements = 0; 14 | 15 | while (h) 16 | { 17 | elements++; 18 | h = h->next; 19 | } 20 | 21 | return (elements); 22 | } 23 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void __attribute__((constructor)) hare(void); 4 | 5 | /** 6 | * hare - Prints a string before the 7 | * main function is executed. 8 | */ 9 | void hare(void) 10 | { 11 | printf("You're beat! and yet, you must allow,\n" 12 | "I bore my house upon my back!\n"); 13 | } 14 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | ; My comments: It is so fun to write in assembly language 2 | ; File: 101-hello_holberton.asm 3 | ; Auth: Firdaus H. Salim 4 | ; Desc: 64-bit assembly program that prints 5 | ; Hello, Holberton followed by a new line. 6 | 7 | extern printf 8 | 9 | section .text 10 | global main 11 | 12 | main: 13 | push rbp 14 | 15 | mov rdi,fmt 16 | mov rsi,msg 17 | mov rax,0 18 | call printf 19 | 20 | pop rbp 21 | 22 | mov rax,0 23 | ret 24 | 25 | section .data 26 | msg: db "Hello, Holberton", 0 27 | fmt: db "%s", 10, 0 28 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_node - Adds a new node at the beginning 6 | * of a list_t list. 7 | * @head: A pointer to the head of the list_t list. 8 | * @str: The string to be added to the list_t list. 9 | * 10 | * Return: If the function fails - NULL. 11 | * Otherwise - the address of the new element. 12 | */ 13 | list_t *add_node(list_t **head, const char *str) 14 | { 15 | list_t *new; 16 | char *dup_str; 17 | int len; 18 | 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | dup_str = strdup(str); 24 | if (dup_str == NULL) 25 | { 26 | free(new); 27 | return (NULL); 28 | } 29 | 30 | for (len = 0; str[len];) 31 | len++; 32 | 33 | new->str = dup_str; 34 | new->len = len; 35 | new->next = *head; 36 | 37 | *head = new; 38 | 39 | return (new); 40 | } 41 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_node_end - Adds a new node at the end 6 | * of a list_t list. 7 | * @head: A pointer the head of the list_t list. 8 | * @str: The string to be added to the list_t list. 9 | * 10 | * Return: If the function fails - NULL. 11 | * Otherwise - the address of the new element. 12 | */ 13 | list_t *add_node_end(list_t **head, const char *str) 14 | { 15 | char *dup; 16 | int len; 17 | list_t *new, *last; 18 | 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | dup = strdup(str); 24 | if (str == NULL) 25 | { 26 | free(new); 27 | return (NULL); 28 | } 29 | 30 | for (len = 0; str[len];) 31 | len++; 32 | 33 | new->str = dup; 34 | new->len = len; 35 | new->next = NULL; 36 | 37 | if (*head == NULL) 38 | *head = new; 39 | 40 | else 41 | { 42 | last = *head; 43 | while (last->next != NULL) 44 | last = last->next; 45 | last->next = new; 46 | } 47 | 48 | return (*head); 49 | } 50 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_list - Frees a list_t list. 6 | * @head: A pointer to the list_t list. 7 | */ 8 | void free_list(list_t *head) 9 | { 10 | list_t *tmp; 11 | 12 | while (head) 13 | { 14 | tmp = head->next; 15 | free(head->str); 16 | free(head); 17 | head = tmp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Test_Files/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for ALX School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = print_list(head); 30 | printf("-> %lu elements\n", n); 31 | 32 | printf("\n"); 33 | free(new->str); 34 | new->str = NULL; 35 | n = print_list(head); 36 | printf("-> %lu elements\n", n); 37 | 38 | free(new); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Test_Files/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for ALX School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = list_len(head); 30 | printf("-> %lu elements\n", n); 31 | free(new->str); 32 | free(new); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Test_Files/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - check the code for ALX School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.)\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/Test_Files/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code for ALX School students. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Bob"); 17 | add_node_end(&head, "&"); 18 | add_node_end(&head, "Kris"); 19 | add_node_end(&head, "love"); 20 | add_node_end(&head, "asm"); 21 | print_list(head); 22 | free_list(head); 23 | head = NULL; 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | /* 5 | * Desc: header file containinf prototypes and definitions 6 | * for all functions and types written in this proj 7 | */ 8 | 9 | #include 10 | 11 | /** 12 | * struct list_s - singly linked list 13 | * @str: string - (malloc'ed string) 14 | * @len: length of the string 15 | * @next: points to the next node 16 | * 17 | * Description: singly linked list node structure 18 | * for Holberton project 19 | */ 20 | typedef struct list_s 21 | { 22 | char *str; 23 | unsigned int len; 24 | struct list_s *next; 25 | } list_t; 26 | 27 | size_t print_list(const list_t *h); 28 | size_t list_len(const list_t *h); 29 | list_t *add_node(list_t **head, const char *str); 30 | list_t *add_node_end(list_t **head, const char *str); 31 | void free_list(list_t *head); 32 | 33 | #endif /* LISTS_H */ 34 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/struct.c: -------------------------------------------------------------------------------- 1 | /** 2 | * struct list_s - singly linked list 3 | * @str: string - (malloc'ed string) 4 | * @len: length of the string 5 | * @next: points to the next node 6 | * 7 | * Description: singly linked list node structure 8 | * for Holberton project 9 | * All files in this project use this format 10 | */ 11 | typedef struct list_s 12 | { 13 | char *str; 14 | unsigned int len; 15 | struct list_s *next; 16 | } list_t; 17 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_listint - Prints all the elements of a listint_t list. 6 | * @h: A pointer to the head of the list_t list. 7 | * 8 | * Return: The number of nodes in the list_t list. 9 | */ 10 | size_t print_listint(const listint_t *h) 11 | { 12 | size_t nodes = 0; 13 | 14 | while (h) 15 | { 16 | nodes++; 17 | printf("%d\n", h->n); 18 | h = h->next; 19 | } 20 | 21 | return (nodes); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * listint_len - Returns the number of elements 6 | * in a linked listint_t list. 7 | * @h: A pointer to the head of the listint_t list. 8 | * 9 | * Return: The number of elements in the listint_t list. 10 | */ 11 | size_t listint_len(const listint_t *h) 12 | { 13 | size_t nodes = 0; 14 | 15 | while (h) 16 | { 17 | nodes++; 18 | h = h->next; 19 | } 20 | 21 | return (nodes); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - Deletes the node at a given 5 | * index of a listint_t list. 6 | * @head: A pointer to the address of the 7 | * head of the listint_t list. 8 | * @index: The index of the node to be deleted - indices start at 0. 9 | * 10 | * Return: On success - 1. 11 | * On failure - -1. 12 | */ 13 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 14 | { 15 | listint_t *tmp, *copy = *head; 16 | unsigned int node; 17 | 18 | if (copy == NULL) 19 | return (-1); 20 | 21 | if (index == 0) 22 | { 23 | *head = (*head)->next; 24 | free(copy); 25 | return (1); 26 | } 27 | 28 | for (node = 0; node < (index - 1); node++) 29 | { 30 | if (copy->next == NULL) 31 | return (-1); 32 | 33 | copy = copy->next; 34 | } 35 | 36 | tmp = copy->next; 37 | copy->next = tmp->next; 38 | free(tmp); 39 | return (1); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - Reverses a listint_t list. 5 | * @head: A pointer to the address of 6 | * the head of the list_t list. 7 | * 8 | * Return: A pointer to the first node of the reversed list. 9 | */ 10 | listint_t *reverse_listint(listint_t **head) 11 | { 12 | listint_t *ahead, *behind; 13 | 14 | if (head == NULL || *head == NULL) 15 | return (NULL); 16 | 17 | behind = NULL; 18 | 19 | while ((*head)->next != NULL) 20 | { 21 | ahead = (*head)->next; 22 | (*head)->next = behind; 23 | behind = *head; 24 | *head = ahead; 25 | } 26 | 27 | (*head)->next = behind; 28 | 29 | return (*head); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - Finds the loop contained in 5 | * a listint_t linked list. 6 | * @head: A pointer to the head of the listint_t list. 7 | * 8 | * Return: If there is no loop - NULL. 9 | * Otherwise - the address of the node where the loop starts. 10 | */ 11 | listint_t *find_listint_loop(listint_t *head) 12 | { 13 | listint_t *tortoise, *hare; 14 | 15 | if (head == NULL || head->next == NULL) 16 | return (NULL); 17 | 18 | tortoise = head->next; 19 | hare = (head->next)->next; 20 | 21 | while (hare) 22 | { 23 | if (tortoise == hare) 24 | { 25 | tortoise = head; 26 | 27 | while (tortoise != hare) 28 | { 29 | tortoise = tortoise->next; 30 | hare = hare->next; 31 | } 32 | 33 | return (tortoise); 34 | } 35 | 36 | tortoise = tortoise->next; 37 | hare = (hare->next)->next; 38 | } 39 | 40 | return (NULL); 41 | } 42 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - Adds a new node at the beginning 5 | * of a listint_t list. 6 | * @head: A pointer to the address of the 7 | * head of the listint_t list. 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 element. 12 | */ 13 | listint_t *add_nodeint(listint_t **head, const int n) 14 | { 15 | listint_t *new; 16 | 17 | new = malloc(sizeof(listint_t)); 18 | if (new == NULL) 19 | return (NULL); 20 | 21 | new->n = n; 22 | new->next = *head; 23 | 24 | *head = new; 25 | 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - Adds a new node at the 5 | * end of a listint_t list. 6 | * @head: A pointer to the address of the 7 | * head of the listint_t list. 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 element. 12 | */ 13 | listint_t *add_nodeint_end(listint_t **head, const int n) 14 | { 15 | listint_t *new, *last; 16 | 17 | new = malloc(sizeof(listint_t)); 18 | if (new == NULL) 19 | return (NULL); 20 | 21 | new->n = n; 22 | new->next = NULL; 23 | 24 | if (*head == NULL) 25 | *head = new; 26 | 27 | else 28 | { 29 | last = *head; 30 | while (last->next != NULL) 31 | last = last->next; 32 | last->next = new; 33 | } 34 | 35 | return (*head); 36 | } 37 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - Frees a listint_t list. 5 | * @head: A pointer to the head of the listint_t list to be freed. 6 | */ 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *tmp; 10 | 11 | while (head) 12 | { 13 | tmp = head->next; 14 | free(head); 15 | head = tmp; 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - Frees a listint_t list. 5 | * @head: A pointer to the address of the 6 | * head of the listint_t list. 7 | * 8 | * Description: Sets the head to NULL. 9 | */ 10 | void free_listint2(listint_t **head) 11 | { 12 | listint_t *tmp; 13 | 14 | if (head == NULL) 15 | return; 16 | 17 | while (*head) 18 | { 19 | tmp = (*head)->next; 20 | free(*head); 21 | *head = tmp; 22 | } 23 | 24 | head = NULL; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - Deletes the head node of a listint_t list. 5 | * @head: A pointer to the address of the 6 | * head of the listint_t list. 7 | * 8 | * Return: If the linked list is empty - 0. 9 | * Otherwise - The head node's data (n). 10 | */ 11 | int pop_listint(listint_t **head) 12 | { 13 | listint_t *tmp; 14 | int ret; 15 | 16 | if (*head == NULL) 17 | return (0); 18 | 19 | tmp = *head; 20 | ret = (*head)->n; 21 | *head = (*head)->next; 22 | 23 | free(tmp); 24 | 25 | return (ret); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - Locates a given node of 5 | * a listint_t linked list. 6 | * @head: A pointer to the head of the listint_t list. 7 | * @index: The index of the node to locate - indices start at 0. 8 | * 9 | * Return: If the node does not exist - NULL. 10 | * Otherwise - the located node. 11 | */ 12 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 13 | { 14 | unsigned int node; 15 | 16 | for (node = 0; node < index; node++) 17 | { 18 | if (head == NULL) 19 | return (NULL); 20 | 21 | head = head->next; 22 | } 23 | 24 | return (head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - Calculates the sum of all the 5 | * data (n) of a listint_t list. 6 | * @head: A pointer to the head of the listint_t list. 7 | * 8 | * Return: If the list is empty - 0. 9 | * Otherwise - the sum of all the data. 10 | */ 11 | int sum_listint(listint_t *head) 12 | { 13 | int sum = 0; 14 | 15 | while (head) 16 | { 17 | sum += head->n; 18 | head = head->next; 19 | } 20 | 21 | return (sum); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - Converts a binary number to an unsigned int. 5 | * @b: A pointer to a string of 0 and 1 chars. 6 | * 7 | * Return: If b is NULL or contains chars not 0 or 1 - 0. 8 | * Otherwise - the converted number. 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int num = 0; 13 | int len = 0; 14 | 15 | if (b[len] == '\0') 16 | return (0); 17 | 18 | while ((b[len] == '0') || (b[len] == '1')) 19 | { 20 | num <<= 1; 21 | num += b[len] - '0'; 22 | len++; 23 | } 24 | 25 | return (num); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - Prints the binary representation of a number. 5 | * @n: The number to be printed in binary. 6 | */ 7 | void print_binary(unsigned long int n) 8 | { 9 | if (n > 1) 10 | print_binary(n >> 1); 11 | 12 | _putchar((n & 1) + '0'); 13 | } 14 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - Checks the endianness. 5 | * 6 | * Return: If big-endian - 0. 7 | * If little-endian - 1. 8 | */ 9 | int get_endianness(void) 10 | { 11 | int num = 1; 12 | char *endian = (char *)# 13 | 14 | if (*endian == 1) 15 | return (1); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - Gets the value of a bit at a given index. 5 | * @n: The bit. 6 | * @index: The index to get the value at - indices start at 0. 7 | * 8 | * Return: If an error occurs - -1. 9 | * Otherwise - The value of bit at index. 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | if (index >= (sizeof(unsigned long int) * 8)) 14 | return (-1); 15 | 16 | if ((n & (1 << index)) == 0) 17 | return (0); 18 | 19 | return (1); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - Sets the value of a bit at a given index to 1. 5 | * @n: A pointer to the bit. 6 | * @index: The index to set the value at - indices start at 0. 7 | * 8 | * Return: If an error occurs - -1. 9 | * Otherwise - 1. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | if (index >= (sizeof(unsigned long int) * 8)) 14 | return (-1); 15 | 16 | *n ^= (1 << index); 17 | 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - Sets the value of a bit at a given index to 0. 5 | * @n: A pointer to the bit. 6 | * @index: The index to set the value at - indices start at 0. 7 | * 8 | * Return: If an error occurs - -1. 9 | * Otherwise - 1. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | if (index >= (sizeof(unsigned long int) * 8)) 14 | return (-1); 15 | 16 | *n &= ~(1 << index); 17 | 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - Counts the number of bits needed to be 5 | * flipped to get from one number to another. 6 | * @n: The number. 7 | * @m: The number to flip n to. 8 | * 9 | * Return: The necessary number of bits to flip to get from n to m. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned long int xor = n ^ m, bits = 0; 14 | 15 | while (xor > 0) 16 | { 17 | bits += (xor & 1); 18 | xor >>= 1; 19 | } 20 | 21 | return (bits); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # 0x14. C - Bit manipulation 2 | 3 | This README.md file will be updated accordingly soon 4 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/Test/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../main.h" 3 | 4 | /** 5 | * main - check the code for ALX School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = binary_to_uint("1"); 14 | printf("%u\n", n); 15 | n = binary_to_uint("101"); 16 | printf("%u\n", n); 17 | n = binary_to_uint("1e01"); 18 | printf("%u\n", n); 19 | n = binary_to_uint("1100010"); 20 | printf("%u\n", n); 21 | n = binary_to_uint("0000000000000000000110010010"); 22 | printf("%u\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /* 5 | * Desc: Header file containing prototypes for all functions 6 | * written in the 0x13-bit_manipulation directory. 7 | */ 8 | 9 | int _putchar(char c); 10 | unsigned int binary_to_uint(const char *b); 11 | void print_binary(unsigned long int n); 12 | int get_bit(unsigned long int n, unsigned int index); 13 | int set_bit(unsigned long int *n, unsigned int index); 14 | int clear_bit(unsigned long int *n, unsigned int index); 15 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 16 | int get_endianness(void); 17 | 18 | #endif /* MAIN_H */ 19 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * read_textfile - Reads a text file and prints it to POSIX stdout. 6 | * @filename: A pointer to the name of the file. 7 | * @letters: The number of letters the 8 | * function should read and print. 9 | * 10 | * Return: If the function fails or filename is NULL - 0. 11 | * O/w - the actual number of bytes the function can read and print. 12 | */ 13 | ssize_t read_textfile(const char *filename, size_t letters) 14 | { 15 | ssize_t o, r, w; 16 | char *buffer; 17 | 18 | if (filename == NULL) 19 | return (0); 20 | 21 | buffer = malloc(sizeof(char) * letters); 22 | if (buffer == NULL) 23 | return (0); 24 | 25 | o = open(filename, O_RDONLY); 26 | r = read(o, buffer, letters); 27 | w = write(STDOUT_FILENO, buffer, r); 28 | 29 | if (o == -1 || r == -1 || w == -1 || w != r) 30 | { 31 | free(buffer); 32 | return (0); 33 | } 34 | 35 | free(buffer); 36 | close(o); 37 | 38 | return (w); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - Creates a file. 5 | * @filename: A pointer to the name of the file to create. 6 | * @text_content: A pointer to a string to write to the file. 7 | * 8 | * Return: If the function fails - -1. 9 | * Otherwise - 1. 10 | */ 11 | int create_file(const char *filename, char *text_content) 12 | { 13 | int o, w, len = 0; 14 | 15 | if (filename == NULL) 16 | return (-1); 17 | 18 | if (text_content != NULL) 19 | { 20 | for (len = 0; text_content[len];) 21 | len++; 22 | } 23 | 24 | o = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600); 25 | w = write(o, text_content, len); 26 | 27 | if (o == -1 || w == -1) 28 | return (-1); 29 | 30 | close(o); 31 | 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /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: A pointer to the name of the file. 6 | * @text_content: The string to add to the end of the file. 7 | * 8 | * Return: If the function fails or filename is NULL - -1. 9 | * If the file does not exist the user lacks write permissions - -1. 10 | * Otherwise - 1. 11 | */ 12 | int append_text_to_file(const char *filename, char *text_content) 13 | { 14 | int o, w, len = 0; 15 | 16 | if (filename == NULL) 17 | return (-1); 18 | 19 | if (text_content != NULL) 20 | { 21 | for (len = 0; text_content[len];) 22 | len++; 23 | } 24 | 25 | o = open(filename, O_WRONLY | O_APPEND); 26 | w = write(o, text_content, len); 27 | 28 | if (o == -1 || w == -1) 29 | return (-1); 30 | 31 | close(o); 32 | 33 | return (1); 34 | } 35 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | **0x15. C - File I/O** 2 | -------------------------------------------------------------------------------- /0x15-file_io/Test/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../holberton.h" 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | ssize_t n; 13 | 14 | if (ac != 2) 15 | { 16 | dprintf(2, "Usage: %s filename\n", av[0]); 17 | exit(1); 18 | } 19 | n = read_textfile(av[1], 114); 20 | printf("\n(printed chars: %li)\n", n); 21 | n = read_textfile(av[1], 1024); 22 | printf("\n(printed chars: %li)\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x15-file_io/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | /* 5 | * Desc: Header file containing prototypes for all functions 6 | * written in the 0x14-file_io directory. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | int _putchar(char c); 15 | ssize_t read_textfile(const char *filename, size_t letters); 16 | int create_file(const char *filename, char *text_content); 17 | int append_text_to_file(const char *filename, char *text_content); 18 | 19 | #endif /* MAIN_H */ 20 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | /* 5 | * Desc: Header file containing prototypes for all functions 6 | * written in the 0x14-file_io directory. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | int _putchar(char c); 15 | ssize_t read_textfile(const char *filename, size_t letters); 16 | int create_file(const char *filename, char *text_content); 17 | int append_text_to_file(const char *filename, char *text_content); 18 | 19 | #endif /* MAIN_H */ 20 | -------------------------------------------------------------------------------- /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 2 | -------------------------------------------------------------------------------- /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 | # 0x17-doubly_linked_lists -------------------------------------------------------------------------------- /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-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always EXIT_SUCCESS. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", _strlen("My Dyn Lib")); 12 | return (EXIT_SUCCESS); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc *.c -c -fPIC 3 | gcc *.o -shared -o liball.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.c: -------------------------------------------------------------------------------- 1 | /** 2 | * add - addition 3 | * @a: number 4 | * @b: nuber 5 | * Return: result 6 | */ 7 | int add(int a, int b) 8 | { 9 | return (a + b); 10 | } 11 | /** 12 | * sub - addition 13 | * @a: number 14 | * @b: nuber 15 | * Return: result 16 | */ 17 | int sub(int a, int b) 18 | { 19 | return (a - b); 20 | } 21 | /** 22 | * mul - addition 23 | * @a: number 24 | * @b: nuber 25 | * Return: result 26 | */ 27 | int mul(int a, int b) 28 | { 29 | return (a * b); 30 | } 31 | /** 32 | * div - addition 33 | * @a: number 34 | * @b: nuber 35 | * Return: result 36 | */ 37 | int div(int a, int b) 38 | { 39 | return (a / b); 40 | } 41 | /** 42 | * mod - addition 43 | * @a: number 44 | * @b: nuber 45 | * Return: result 46 | */ 47 | int mod(int a, int b) 48 | { 49 | return (a % b); 50 | } 51 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x18-dynamic_libraries/100-operations.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-tests.py: -------------------------------------------------------------------------------- 1 | import random 2 | import ctypes 3 | 4 | cops = ctypes.CDLL('100-operations.so') 5 | a = random.randint(-111, 111) 6 | b = random.randint(-111, 111) 7 | print("{} + {} = {}".format(a, b, cops.add(a, b))) 8 | print("{} - {} = {}".format(a, b, cops.sub(a, b))) 9 | print("{} x {} = {}".format(a, b, cops.mul(a, b))) 10 | print("{} / {} = {}".format(a, b, cops.div(a, b))) 11 | print("{} % {} = {}".format(a, b, cops.mod(a, b))) 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/eyoul/alx-low_level_programming/raw/main/0x18-dynamic_libraries/iwin.so 3 | export LD_PRELOAD=/tmp/iwin.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-md5_gm: -------------------------------------------------------------------------------- 1 | d52e6c18e0723f5b025a75dea19ef365 gm -------------------------------------------------------------------------------- /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/liball.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x18-dynamic_libraries/liball.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | int _islower(int c); 9 | int _isalpha(int c); 10 | int _abs(int n); 11 | int _isupper(int c); 12 | int _isdigit(int c); 13 | int _strlen(char *s); 14 | void _puts(char *s); 15 | char *_strcpy(char *dest, char *src); 16 | int _atoi(char *s); 17 | char *_strcat(char *dest, char *src); 18 | char *_strncat(char *dest, char *src, int n); 19 | char *_strncpy(char *dest, char *src, int n); 20 | int _strcmp(char *s1, char *s2); 21 | char *_memset(char *s, char b, unsigned int n); 22 | char *_memcpy(char *dest, char *src, unsigned int n); 23 | char *_strchr(char *s, char c); 24 | unsigned int _strspn(char *s, char *accept); 25 | char *_strpbrk(char *s, char *accept); 26 | char *_strstr(char *haystack, char *needle); 27 | int add(int a, int b); 28 | int sub(int a, int b); 29 | int mul(int a, int b); 30 | int mod(int a, int b); 31 | extern div_t div(int __numer, int __denom); 32 | 33 | #endif /* MAIN_H */ 34 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/nrandom.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x18-dynamic_libraries/nrandom.so -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Creates a hash table. 5 | * @size: The size of the array. 6 | * 7 | * Return: If an error occurs - NULL. 8 | * Otherwise - a pointer to the new hash table. 9 | */ 10 | hash_table_t *hash_table_create(unsigned long int size) 11 | { 12 | hash_table_t *ht; 13 | unsigned long int i; 14 | 15 | ht = malloc(sizeof(hash_table_t)); 16 | if (ht == NULL) 17 | return (NULL); 18 | 19 | ht->size = size; 20 | ht->array = malloc(sizeof(hash_node_t *) * size); 21 | if (ht->array == NULL) 22 | return (NULL); 23 | for (i = 0; i < size; i++) 24 | ht->array[i] = NULL; 25 | 26 | return (ht); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - Hash function implementing the djb2 algorithm. 5 | * @str: The string to hash. 6 | * 7 | * Return: The calculated hash. 8 | */ 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hash; 12 | int c; 13 | 14 | hash = 5381; 15 | while ((c = *str++)) 16 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 17 | 18 | return (hash); 19 | } 20 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - Get the index at which a key/value pair should 5 | * be stored in array of a hash table. 6 | * @key: The key to get the index of. 7 | * @size: The size of the array of the hash table. 8 | * 9 | * Return: The index of the key. 10 | * 11 | * Description: Uses the djb2 algorithm. 12 | */ 13 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 14 | { 15 | return (hash_djb2(key) % size); 16 | } 17 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - Retrieve the value associated with 5 | * a key in a hash table. 6 | * @ht: A pointer to the hash table. 7 | * @key: The key to get the value of. 8 | * 9 | * Return: If the key cannot be matched - NULL. 10 | * Otherwise - the value associated with key in ht. 11 | */ 12 | char *hash_table_get(const hash_table_t *ht, const char *key) 13 | { 14 | hash_node_t *node; 15 | unsigned long int index; 16 | 17 | if (ht == NULL || key == NULL || *key == '\0') 18 | return (NULL); 19 | 20 | index = key_index((const unsigned char *)key, ht->size); 21 | if (index >= ht->size) 22 | return (NULL); 23 | 24 | node = ht->array[index]; 25 | while (node && strcmp(node->key, key) != 0) 26 | node = node->next; 27 | 28 | return ((node == NULL) ? NULL : node->value); 29 | } 30 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "./hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - Prints a hash table. 5 | * @ht: A pointer to the hash table to print. 6 | * 7 | * Description: Key/value pairs are printed in the order 8 | * they appear in the array of the hash table. 9 | */ 10 | void hash_table_print(const hash_table_t *ht) 11 | { 12 | hash_node_t *node; 13 | unsigned long int i; 14 | unsigned char comma_flag = 0; 15 | 16 | if (ht == NULL) 17 | return; 18 | 19 | printf("{"); 20 | for (i = 0; i < ht->size; i++) 21 | { 22 | if (ht->array[i] != NULL) 23 | { 24 | if (comma_flag == 1) 25 | printf(", "); 26 | 27 | node = ht->array[i]; 28 | while (node != NULL) 29 | { 30 | printf("'%s': '%s'", node->key, node->value); 31 | node = node->next; 32 | if (node != NULL) 33 | printf(", "); 34 | } 35 | comma_flag = 1; 36 | } 37 | } 38 | printf("}\n"); 39 | } 40 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - Deletes a hash table. 5 | * @ht: A pointer to a hash table. 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | hash_table_t *head = ht; 10 | hash_node_t *node, *tmp; 11 | unsigned long int i; 12 | 13 | for (i = 0; i < ht->size; i++) 14 | { 15 | if (ht->array[i] != NULL) 16 | { 17 | node = ht->array[i]; 18 | while (node != NULL) 19 | { 20 | tmp = node->next; 21 | free(node->key); 22 | free(node->value); 23 | free(node); 24 | node = tmp; 25 | } 26 | } 27 | } 28 | free(head->array); 29 | free(head); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | # C - Hash tables -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%p\n", (void *)ht); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | shash_table_t *ht; 14 | 15 | ht = shash_table_create(1024); 16 | shash_table_set(ht, "y", "0"); 17 | shash_table_print(ht); 18 | shash_table_set(ht, "j", "1"); 19 | shash_table_print(ht); 20 | shash_table_set(ht, "c", "2"); 21 | shash_table_print(ht); 22 | shash_table_set(ht, "b", "3"); 23 | shash_table_print(ht); 24 | shash_table_set(ht, "z", "4"); 25 | shash_table_print(ht); 26 | shash_table_set(ht, "n", "5"); 27 | shash_table_print(ht); 28 | shash_table_set(ht, "a", "6"); 29 | shash_table_print(ht); 30 | shash_table_set(ht, "m", "7"); 31 | shash_table_print(ht); 32 | shash_table_print_rev(ht); 33 | shash_table_delete(ht); 34 | return (EXIT_SUCCESS); 35 | } 36 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | unsigned long int hash_table_array_size; 15 | 16 | hash_table_array_size = 1024; 17 | s = "cisfun"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 20 | s = "Don't forget to tweet today"; 21 | printf("%lu\n", hash_djb2((unsigned char *)s)); 22 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 23 | s = "98"; 24 | printf("%lu\n", hash_djb2((unsigned char *)s)); 25 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 26 | return (EXIT_SUCCESS); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_set(ht, "betty", "holberton"); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_print(ht); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_print(ht); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 4 | all: 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean: 11 | $(RM) *~ $(NAME) 12 | 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: fclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(CLFAGS) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Return the perimiter of an island. 7 | 8 | The grid represents water by 0 and land by 1. 9 | 10 | Args: 11 | grid (list): A list of list of integers representing an island. 12 | Returns: 13 | The perimeter of the island defined in grid. 14 | """ 15 | width = len(grid[0]) 16 | height = len(grid) 17 | edges = 0 18 | size = 0 19 | 20 | for i in range(height): 21 | for j in range(width): 22 | if grid[i][j] == 1: 23 | size += 1 24 | if (j > 0 and grid[i][j - 1] == 1): 25 | edges += 1 26 | if (i > 0 and grid[i - 1][j] == 1): 27 | edges += 1 28 | return size * 4 - edges * 2 29 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | 17 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # 0x1B.c 2 | In this project folder, I have learnt about: 3 | 4 | - What are make, Makefiles 5 | - When, why and how to use Makefiles 6 | - What are rules and how to set and use them 7 | - What are explicit and implicit rules 8 | - What are the most common / useful rules 9 | - What are variables and how to set and use them 10 | 11 | -------------------------------------------------------------------------------- /0x1C-makefiles/__pycache__/5-island_perimeter.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1C-makefiles/__pycache__/5-island_perimeter.cpython-38.pyc -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_holberton(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_holberton(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1C-makefiles/main.o -------------------------------------------------------------------------------- /0x1C-makefiles/school: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1C-makefiles/school -------------------------------------------------------------------------------- /0x1C-makefiles/school.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1C-makefiles/school.o -------------------------------------------------------------------------------- /0x1E-search_algorithms/0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1E-search_algorithms/0 -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - Searches for a value in an array 5 | * of integers using linear search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @size: The number of elements in the array. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the array is NULL, -1. 11 | * else, the first index where the value is located. 12 | * 13 | * Description: Prints a value every time it is compared in the array. 14 | */ 15 | int linear_search(int *array, size_t size, int value) 16 | { 17 | size_t i; 18 | 19 | if (array == NULL) 20 | return (-1); 21 | 22 | for (i = 0; i < size; i++) 23 | { 24 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 25 | if (array[i] == value) 26 | return (i); 27 | } 28 | 29 | return (-1); 30 | } 31 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1E-search_algorithms/1 -------------------------------------------------------------------------------- /0x1E-search_algorithms/100: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1E-search_algorithms/100 -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1E-search_algorithms/102 -------------------------------------------------------------------------------- /0x1E-search_algorithms/103: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1E-search_algorithms/103 -------------------------------------------------------------------------------- /0x1E-search_algorithms/104: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1E-search_algorithms/104 -------------------------------------------------------------------------------- /0x1E-search_algorithms/105: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1E-search_algorithms/105 -------------------------------------------------------------------------------- /0x1E-search_algorithms/106: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/0x1E-search_algorithms/106 -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | # 0x1E-search_algorithms -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/create_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | void free_list(listint_t *list); 5 | 6 | /** 7 | * create_list - Creates a single linked list 8 | * 9 | * @array: Pointer to the array to use to fill the list 10 | * @size: Size of the array 11 | * 12 | * Return: A pointer to the head of the created list (NULL on failure) 13 | */ 14 | listint_t *create_list(int *array, size_t size) 15 | { 16 | listint_t *list; 17 | listint_t *node; 18 | 19 | list = NULL; 20 | while (array && size--) 21 | { 22 | node = malloc(sizeof(*node)); 23 | if (!node) 24 | { 25 | free_list(list); 26 | return (NULL); 27 | } 28 | node->n = array[size]; 29 | node->index = size; 30 | node->next = list; 31 | list = node; 32 | } 33 | return (list); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | /** 5 | * free_list - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_list(listint_t *list) 10 | { 11 | listint_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_list(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | /** 5 | * print_list - Prints the content of a listint_t 6 | * 7 | * @list: Pointer to the head of the list 8 | */ 9 | void print_list(const listint_t *list) 10 | { 11 | printf("List :\n"); 12 | while (list) 13 | { 14 | printf("Index[%lu] = [%d]\n", list->index, list->n); 15 | list = list->next; 16 | } 17 | printf("\n"); 18 | } 19 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/free_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | /** 5 | * free_skiplist - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_skiplist(skiplist_t *list) 10 | { 11 | skiplist_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_skiplist(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/print_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * print_skiplist - dump the content of a skiplist_t 7 | * 8 | * @list: Pointer to the head of the list 9 | * 10 | * Return: void 11 | */ 12 | void print_skiplist(const skiplist_t *list) 13 | { 14 | const skiplist_t *node; 15 | 16 | printf("List :\n"); 17 | for (node = list; node; node = node->next) 18 | { 19 | printf("Index[%lu] = [%d]\n", node->index, node->n); 20 | } 21 | printf("\nExpress lane :\n"); 22 | for (node = list; node; node = node->express) 23 | { 24 | printf("Index[%lu] = [%d]\n", node->index, node->n); 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/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/tests/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/tests/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 6, jump_search(array, size, 6)); 18 | printf("Found %d at index: %d\n\n", 1, jump_search(array, size, 1)); 19 | printf("Found %d at index: %d\n", 999, jump_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, interpolation_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 7, interpolation_search(array, size, 7)); 19 | printf("Found %d at index: %d\n", 999, interpolation_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 54, 61, 62, 76, 99 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 62, exponential_search(array, size, 62)); 18 | printf("Found %d at index: %d\n\n", 3, exponential_search(array, size, 3)); 19 | printf("Found %d at index: %d\n", 999, exponential_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/104-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 8, advanced_binary(array, size, 8)); 18 | printf("Found %d at index: %d\n\n", 5, advanced_binary(array, size, 5)); 19 | printf("Found %d at index: %d\n", 999, advanced_binary(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/105-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | listint_t *create_list(int *array, size_t size); 6 | void print_list(const listint_t *list); 7 | void free_list(listint_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | listint_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_list(array, size); 23 | print_list(list); 24 | 25 | res = jump_list(list, size, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = jump_list(list, size, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = jump_list(list, size, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_list(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/106-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | skiplist_t *create_skiplist(int *array, size_t size); 6 | void print_skiplist(const skiplist_t *list); 7 | void free_skiplist(skiplist_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | skiplist_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_skiplist(array, size); 23 | print_skiplist(list); 24 | 25 | res = linear_skip(list, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = linear_skip(list, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = linear_skip(list, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_skiplist(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /files.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hardope/alx-low_level_programming/f813a15a9fdf60aa2d1269dece402ef9b42c1378/files.zip --------------------------------------------------------------------------------