├── 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 └── use ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-holberton.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── 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 └── holberton.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── holberton.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── holberton.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── holberton.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── holberton.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md └── holberton.h ├── 0x09-static_libraries ├── README.md ├── create_static_lib.sh ├── holberton.h └── 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 ├── 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-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── holberton.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 100-hello_holberton.asm ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── lists.h ├── 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 ├── holberton.h └── 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 ├── 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 ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_win.sh ├── README.md ├── libdynamic.so └── main.h ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── m.h └── main.c ├── 0x1E-search_algorithms ├── 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 └── search_algos.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE > 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 | * main - prints to string 4 | * 5 | * Description: Prints "and that piece of art is useful..." without puts 6 | * 7 | * Return: 1 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 | * main - Print a string 4 | * Description: use printf 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | printf("%s", "with proper grammar, but the outcome is a piece of art,\n"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints sizes 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 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | #alx-low_level_programming 2 | **This is all about c, we start we end it 3 | -------------------------------------------------------------------------------- /0x00-hello_world/use: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trikcode/alx-low_level_programming/5abb846b13b6e7b6619e90581c0e02f179997210/0x00-hello_world/use -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - entry point 6 | * Description: prints "is it 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 | printf("%d is positive\n", n); 17 | else if (n == 0) 18 | printf("%d is zero\n", n); 19 | else if (n < 0) 20 | printf("%d is negative\n", n); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry code 6 | * Description: prints "last digit" 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | int last; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | last = n % 10; 17 | 18 | if (last > 5) 19 | printf("Last digit of %d is %d and is greater than 5\n", n, last); 20 | else if (last == 0) 21 | printf("Last digit of %d is %d and is 0\n", n, last); 22 | else if (last < 6) 23 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, last); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: Write a program that prints all possible 6 | * different combinations of two digits 7 | * Numbers must be separated by ,, followed by a space 8 | * The two digits must be different 9 | * 01 and 10 are considered the same combination of the two digits 0 and 1 10 | * Print only the smallest combination of two digits 11 | * Numbers should be printed in ascending order, with two digits 12 | * You can only use the putchar function 13 | * (every other function (printf, puts, etc…) is forbidden) 14 | * You can only use putchar five times maximum in your code 15 | * You are not allowed to use any variable of type char 16 | * All your code should be in the main function 17 | * Return: 0 18 | */ 19 | int main(void) 20 | { 21 | int c; 22 | int d = 0; 23 | 24 | while (d < 10) 25 | { 26 | c = 0; 27 | while (c < 10) 28 | { 29 | if (d != c && d < c) 30 | { 31 | putchar('0' + d); 32 | putchar('0' + c); 33 | 34 | if (c + d != 17) 35 | { 36 | putchar(','); 37 | putchar(' '); 38 | } 39 | } 40 | 41 | c++; 42 | } 43 | d++; 44 | } 45 | putchar('\n'); 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: Write a program that prints all possible 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int c; 11 | int d; 12 | int e = 0; 13 | 14 | while (e < 10) 15 | { 16 | d = 0; 17 | while (d < 10) 18 | { 19 | c = 0; 20 | while (c < 10) 21 | { 22 | if (c != d && d != e && e < d && d < c) 23 | { 24 | putchar('0' + e); 25 | putchar('0' + d); 26 | putchar('0' + c); 27 | 28 | if (c + d + e != 9 + 8 + 7) 29 | { 30 | putchar(','); 31 | putchar(' '); 32 | } 33 | } 34 | 35 | c++; 36 | } 37 | d++; 38 | } 39 | e++; 40 | } 41 | putchar('\n'); 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - create 2 pairs of numbers that do not repeat 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int j, i; 10 | 11 | for (j = 0; j <= 99; j++) 12 | { 13 | for (i = j; i <= 99; i++) 14 | { 15 | if (i != j) 16 | { 17 | 18 | putchar(j / 10 + 48); 19 | putchar(j % 10 + 48); 20 | putchar(' '); 21 | putchar(i / 10 + 48); 22 | putchar(i % 10 + 48); 23 | 24 | if (j * 100 + i != 9899) 25 | { 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | 31 | } 32 | 33 | } 34 | putchar('\n'); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - entry point 5 | * Description: print alphabets 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | putchar(c); 15 | c++; 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - entry block 5 | * Description: alphabets in lower and upper 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | putchar(c); 15 | c++; 16 | } 17 | c = 'A'; 18 | 19 | while (c <= 'Z') 20 | { 21 | putchar(c); 22 | c++; 23 | } 24 | putchar('\n'); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry block 5 | * Description: lowercase without q and e 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | if (c != 'q' && c != 'e') 15 | { 16 | putchar(c); 17 | } 18 | c++; 19 | } 20 | 21 | putchar('\n'); 22 | return (0); 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry block 6 | * Description: print single digit numbers 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char c = '0'; 12 | 13 | while (c <= '9') 14 | { 15 | putchar(c); 16 | c++; 17 | } 18 | 19 | putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: prints all single digit numbers of base 10 5 | * starting from 0, followed by a new line. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int i = 0; 11 | 12 | while (i < 10) 13 | { 14 | putchar(48 + i); 15 | i++; 16 | } 17 | 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - main block 6 | * Description: prints all single digit numbers of base 10 7 | * starting from 0, followed by a new line. 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | char i = 'z'; 13 | 14 | while (i >= 'a') 15 | { 16 | putchar(i); 17 | i--; 18 | } 19 | 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: prints all single digit numbers of base 10 6 | * starting from 0, followed by a new line. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | char d = '0'; 13 | 14 | while (d <= '9') 15 | { 16 | putchar(d); 17 | d++; 18 | } 19 | 20 | for (c = 'a'; c <= 'f'; c++) 21 | { 22 | putchar(c); 23 | } 24 | 25 | putchar('\n'); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: prints all single digit numbers of base 10 6 | * starting from 0, followed by a new line. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int c = 0; 12 | 13 | while (c < 10) 14 | { 15 | putchar(48 + c); 16 | if (c != 9) 17 | { 18 | putchar(','); 19 | putchar(' '); 20 | } 21 | c++; 22 | } 23 | putchar('\n'); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | # More of low level programming languages 2 | **C - Variables, if, else, while 3 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-holberton.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * main - Entry block 4 | * Description: prints holberton on new line 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c[10] = "Holberton"; 10 | 11 | int i = 0; 12 | 13 | while (i < 9) 14 | { 15 | _putchar(c[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_alphabet - Printing the alphabet in lowercase 5 | * 6 | * Return 0 7 | */ 8 | void print_alphabet(void) 9 | { 10 | char c; 11 | 12 | for (c = 'a'; c <= 'z'; c++) 13 | { 14 | _putchar(c); 15 | } 16 | 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * add - returns the sum of its parameters 4 | * @a: int type number 5 | * @b: int type number 6 | * Return:0 7 | */ 8 | 9 | 10 | int add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_times_table - prints times table 5 | * @n : times table to use 6 | * Return:void 7 | */ 8 | 9 | void print_times_table(int n) 10 | { 11 | 12 | int a = 0, rep, b; 13 | 14 | if (n < 0 || n > 15) 15 | return; 16 | 17 | while (a <= n) 18 | { 19 | for (b = 0; b <= n; b++) 20 | { 21 | rep = a * b; 22 | if (b == 0) 23 | _putchar('0' + rep); 24 | else if (rep < 10) 25 | { 26 | _putchar(' '); 27 | _putchar(' '); 28 | _putchar('0' + rep); 29 | } 30 | else if (rep < 100) 31 | { 32 | _putchar(' '); 33 | _putchar('0' + rep / 10); 34 | _putchar('0' + rep % 10); 35 | } 36 | else 37 | { 38 | _putchar('0' + rep / 100); 39 | _putchar('0' + (rep - 100) / 10); 40 | _putchar('0' + rep % 10); 41 | } 42 | if (b < n) 43 | { 44 | _putchar(','); 45 | _putchar(' '); 46 | } 47 | } 48 | _putchar('\n'); 49 | a++; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: computes and prints the sum of all the multiples of 3 or 6 | * 5 below 1024 (excluded), followed by a new line 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int c = 0; 12 | int sum = 0; 13 | 14 | while (c < 1024) 15 | { 16 | if (c % 3 == 0 || c % 5 == 0) 17 | { 18 | sum += c; 19 | } 20 | 21 | c++; 22 | } 23 | printf("%i\n", sum); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * main - prints first 50 numbers of the fibonacci sequence 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int i, n; 10 | long f1, f2, next; 11 | 12 | f1 = 1; 13 | f2 = 2; 14 | n = 50; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | if (i == 49) 19 | { 20 | printf("%lu", f1); 21 | } else 22 | { 23 | printf("%lu, ", f1); 24 | next = f1 + f2; 25 | f1 = f2; 26 | f2 = next; 27 | } 28 | } 29 | printf("\n"); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | /** 4 | * main - main block 5 | * Description: computes and prints even number < 4,000,000 6 | * 5 below 1024 (excluded), followed by a new line 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int a = 0, b = 1, next = 0; 12 | int sum = 0; 13 | 14 | while (next < 4000000) 15 | { 16 | next = a + b; 17 | a = b; 18 | b = next; 19 | if (next % 2 == 0) 20 | sum += next; 21 | } 22 | printf("%i\n", sum); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | /** 5 | *numLength - returns the lenth of string 6 | *@num : operand number 7 | *Return: number of digits 8 | */ 9 | int numLength(int num) 10 | { 11 | int length = 0; 12 | 13 | if (!num) 14 | { 15 | return (1); 16 | } 17 | 18 | while (num) 19 | { 20 | num = num / 10; 21 | length += 1; 22 | } 23 | 24 | 25 | return (length); 26 | } 27 | 28 | /** 29 | *main - prints the first 98 fibonaci sequences 30 | *Return: 0 31 | */ 32 | int main(void) 33 | { 34 | 35 | unsigned long f1 = 1, f2 = 2, tmp, mx = 100000000, f1o = 0, f2o = 0, tmpo = 0; 36 | short int i = 1, initial0s; 37 | 38 | while (i <= 98) 39 | { 40 | 41 | if (f1o > 0) 42 | printf("%lu", f1o); 43 | initial0s = numLength(mx) - 1 - numLength(f1); 44 | while (f1o > 0 && initial0s > 0) 45 | { 46 | printf("%i", 0); 47 | initial0s--; 48 | } 49 | printf("%lu", f1); 50 | 51 | tmp = (f1 + f2) % mx; 52 | tmpo = f1o + f2o + (f1 + f2) / mx; 53 | f1 = f2; 54 | f1o = f2o; 55 | f2 = tmp; 56 | f2o = tmpo; 57 | 58 | if (i != 98) 59 | printf(", "); 60 | else 61 | printf("\n"); 62 | i++; 63 | } 64 | return (0); 65 | } 66 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_to_98 - prints all the natural numbers from n to 98 6 | * 7 | * @n: interger to be tested 8 | * Return: list of numbers 9 | */ 10 | void print_to_98(int n) 11 | { 12 | int i; 13 | 14 | if (n <= 98) 15 | { 16 | for (i = n; i < 98; i++) 17 | { 18 | printf("%i, ", i); 19 | } 20 | } 21 | else 22 | { 23 | for (i = n; i > 98; i--) 24 | { 25 | printf("%i, ", i); 26 | } 27 | } 28 | printf("98\n"); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_alphabet_x10 - prints alphabets 10 times 5 | * Return:void 6 | */ 7 | 8 | void print_alphabet_x10(void) 9 | { 10 | char c; 11 | int i = 0; 12 | 13 | while (i < 10) 14 | { 15 | c = 'a'; 16 | while (c <= 'z') 17 | { 18 | _putchar(c); 19 | c++; 20 | } 21 | _putchar('\n'); 22 | i++; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _islower - check for lower case letter 5 | * @c : character to check the case 6 | * Return:0 or 1 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | return (c >= 97 && c <= 122); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isalpha - check for lower case letter 5 | * @c : character to check 6 | * Return:0 or 1 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | return ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_sign - prints the sign of a number 5 | * @n: interger to be tested 6 | * Return: 1, 0, -1 7 | */ 8 | int print_sign(int n) 9 | { 10 | if (n > 0) 11 | { 12 | _putchar('+'); 13 | return (1); 14 | } 15 | else if (n == 0) 16 | { 17 | _putchar('0'); 18 | return (0); 19 | } 20 | else 21 | { 22 | _putchar('-'); 23 | return (-1); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _abs - computes the absolute value for an interger 5 | * @n: interger to be tested 6 | * Return: 0 or 1 7 | */ 8 | 9 | int _abs(int n) 10 | 11 | { 12 | return (n < 0 ? -1 * n : n); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_last_digit - return last digit 4 | * @n : number to check 5 | * Return:0 or 1 6 | */ 7 | 8 | int print_last_digit(int n) 9 | { 10 | int nv; 11 | 12 | if (n < 0) 13 | nv = -1 * (n % 10); 14 | else 15 | nv = n % 10; 16 | 17 | _putchar(nv + '0'); 18 | return (nv); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day of Jack Bauer 5 | */ 6 | void jack_bauer(void) 7 | { 8 | int i, j; 9 | 10 | for (i = 0; i < 24; i++) 11 | { 12 | for (j = 0; j < 60; j++) 13 | { 14 | _putchar(i / 10 + 48); 15 | _putchar(i % 10 + 48); 16 | _putchar(':'); 17 | _putchar(j / 10 + 48); 18 | _putchar(j % 10 + 48); 19 | _putchar('\n'); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * times_table - prints the times table 4 | */ 5 | void times_table(void) 6 | { 7 | int i, j, product; 8 | 9 | for (i = 0; i < 10; i++) 10 | { 11 | for (j = 0; j < 10; j++) 12 | { 13 | if (j == 0) 14 | { 15 | _putchar(48); 16 | continue; 17 | } 18 | product = i * j; 19 | _putchar(','); 20 | _putchar(' '); 21 | if (product >= 10) 22 | { 23 | _putchar(product / 10 + 48); 24 | _putchar(product % 10 + 48); 25 | } 26 | else 27 | { 28 | _putchar(' '); 29 | _putchar(product + 48); 30 | } 31 | } 32 | _putchar('\n'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # still low level programming 2 | **functions and nested loops 3 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | #define HOLBERTON_H 2 | 3 | /* _putchar.c */ 4 | int _putchar(char); 5 | 6 | /*10 x alphabet */ 7 | void print_alphabet(void); 8 | 9 | /* 2-print_alphabet_x10.c */ 10 | void print_alphabet_x10(void); 11 | 12 | int _islower(int c); 13 | 14 | int _isalpha(int n); 15 | 16 | int print_sign(int n); 17 | 18 | int _abs(int n); 19 | 20 | int print_last_digit(int n); 21 | 22 | void jack_bauer(void); 23 | 24 | void times_table(void); 25 | 26 | int add(int a, int b); 27 | 28 | void print_to_98(int n); 29 | 30 | void print_times_table(int n); 31 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - Test function for positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | 14 | positive_or_negative(i); 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 | 2 | #include "holberton.h" 3 | 4 | /** 5 | * largest_number - returns the largest of 3 numbers 6 | * @a: first integer 7 | * @b: second integer 8 | * @c: third integer 9 | * Return: largest number 10 | */ 11 | 12 | int largest_number(int a, int b, int c) 13 | { 14 | int largest; 15 | 16 | if (a >= b && a >= c) 17 | { 18 | largest = a; 19 | } 20 | 21 | else if (b >= a && b >= c) 22 | { 23 | largest = b; 24 | } 25 | 26 | else 27 | { 28 | largest = c; 29 | } 30 | 31 | return (largest); 32 | } 33 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if ((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0)) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | # alx-low_level programming 2 | **lets debug some programs 3 | -------------------------------------------------------------------------------- /0x03-debugging/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int); 7 | int largest_number(int a, int b, int c); 8 | void print_remaining_days(int month, int day, int year); 9 | int convert_day(int month, int day); 10 | 11 | #endif /* HOLBERTON_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isupper - check for upper case letter 5 | * @c : character to check the case 6 | * Return:0 or 1 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | return (c >= 65 && c <= 90); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isdigit - check for a digit 5 | * @c : character to check 6 | * Return:0 or 1 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | return (c >= 48 && c <= 57); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /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 | 12 | int i = 1, ii; 13 | 14 | while (i <= size && size > 0) 15 | { 16 | ii = 0; 17 | while (ii < size - i) 18 | { 19 | _putchar(' '); 20 | ii++; 21 | } 22 | ii = 0; 23 | while (ii < i) 24 | { 25 | _putchar('#'); 26 | ii++; 27 | } 28 | 29 | _putchar('\n'); 30 | i++; 31 | } 32 | if (i == 1) 33 | _putchar('\n'); 34 | } 35 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trikcode/alx-low_level_programming/5abb846b13b6e7b6619e90581c0e02f179997210/0x04-more_functions_nested_loops/100-prime_factor -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the largest prime factor of the number 5 | * 612852475143 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-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | *print_number - print integer. 4 | *@n: integer. 5 | * 6 | *Return: void. 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/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * mul - returns the product of its parameters 4 | * @a: int type number 5 | * @b: int type number 6 | * Return:0 7 | */ 8 | 9 | 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_numbers - prints numbers 5 | * Return:0 6 | */ 7 | 8 | void print_numbers(void) 9 | { 10 | char n = 0; 11 | 12 | while (n <= 9) 13 | { 14 | _putchar('0' + n); 15 | n++; 16 | 17 | } 18 | _putchar('\n'); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_most_numbers - prints numbers 5 | * Return:0 6 | */ 7 | 8 | void print_most_numbers(void) 9 | { 10 | char n = 0; 11 | 12 | while (n <= 9) 13 | { 14 | if (n != 2 && n != 4) 15 | { 16 | _putchar('0' + n); 17 | } 18 | 19 | n++; 20 | } 21 | _putchar('\n'); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * more_numbers - prints more numbers 5 | * Return:void 6 | */ 7 | 8 | void more_numbers(void) 9 | { 10 | char n, c; 11 | int i = 0; 12 | 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 | 25 | _putchar('0' + c); 26 | } 27 | 28 | _putchar('\n'); 29 | i++; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_line - check for a digit 5 | * @n : number of _ to be printed 6 | * Return:void 7 | */ 8 | 9 | void print_line(int n) 10 | { 11 | 12 | int i = 0; 13 | 14 | while (i < n && n > 0) 15 | { 16 | _putchar('_'); 17 | i++; 18 | } 19 | _putchar('\n'); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_diagonal - check for a digit 5 | * @n : number of \\ to be printed 6 | * Return:void 7 | */ 8 | 9 | void print_diagonal(int n) 10 | { 11 | 12 | int i = 0, ii; 13 | 14 | while (i < n && n > 0) 15 | { 16 | ii = 0; 17 | while (ii < i) 18 | { 19 | _putchar(' '); 20 | ii++; 21 | } 22 | 23 | _putchar('\\'); 24 | _putchar('\n'); 25 | i++; 26 | } 27 | if (i == 0) 28 | _putchar('\n'); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /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 | 12 | int i = 0, ii; 13 | 14 | while (i < n && n > 0) 15 | { 16 | ii = 0; 17 | while (ii < n) 18 | { 19 | _putchar('#'); 20 | ii++; 21 | } 22 | 23 | _putchar('\n'); 24 | i++; 25 | } 26 | if (i == 0) 27 | _putchar('\n'); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - check the code for Holberton School students. 4 | * 5 | * Return: void. 6 | */ 7 | 8 | int main(void) 9 | { 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 | 32 | if (i != 100) 33 | { 34 | putchar(' '); 35 | } 36 | 37 | i++; 38 | } 39 | putchar('\n'); 40 | return (0); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # low level programming 2 | **functions and nested loops 3 | -------------------------------------------------------------------------------- /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 more_numbers(void); 8 | void print_line(int n); 9 | void print_diagonal(int n); 10 | void print_square(int size); 11 | void print_triangle(int size); 12 | void print_number(int n); 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reset_to_98 - reset number to 98 5 | * @n: pointer 6 | * Return: void 7 | */ 8 | 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | } 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * swap_int - swap variable values 4 | * @a: pointer 1 5 | * @b: pointer 2 6 | * Return: void 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | *a += *b; 12 | *b = *a - *b; 13 | *a = *a - *b; 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _atoi - convert a string to an integer. 5 | * @s: the string to be comverted. 6 | * 7 | *Return: The integer value of the comverted string. 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | 13 | int sign = 1; 14 | unsigned int num = 0; 15 | 16 | do { 17 | 18 | if (*s == '-') 19 | sign *= -1; 20 | 21 | else if (*s >= '0' && *s <= '9') 22 | num = (num * 10) + (*s - '0'); 23 | 24 | else if (num > 0) 25 | break; 26 | 27 | } while (*s++); 28 | 29 | return (num * sign); 30 | } 31 | -------------------------------------------------------------------------------- /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-strlen.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strlen - find the length of a string 4 | * @s: pointer to the string to check 5 | * Return: void 6 | */ 7 | 8 | 9 | int _strlen(char *s) 10 | { 11 | int i = 0; 12 | while (s[i]) 13 | i++; 14 | 15 | return (i); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _puts - prints a string, followed by a new line, 4 | * @str: pointer to the string to print 5 | * Return: void 6 | */ 7 | 8 | 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | while (str[i]) 13 | { 14 | _putchar(str[i]); 15 | i++; 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_rev - prints reversed string, followed by a new line 4 | * @s: pointer to the string to print 5 | * Return: void 6 | */ 7 | 8 | void print_rev(char *s) 9 | { 10 | int i = 0; 11 | while (s[i]) 12 | i++; 13 | 14 | while (i--) 15 | { 16 | _putchar(s[i]); 17 | } 18 | _putchar('\n'); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * rev_string - prints reversed string, followed by a new line 4 | * @s: pointer to the string to print 5 | * Return: void 6 | */ 7 | 8 | void rev_string(char *s) 9 | { 10 | 11 | int len, i, half; 12 | char temp; 13 | 14 | for (len = 0; s[len] != '\0'; len++) 15 | ; 16 | 17 | i = 0; 18 | 19 | half = len / 2; 20 | 21 | while (half--) 22 | { 23 | temp = s[len - i - 1]; 24 | s[len - i - 1] = s[i]; 25 | s[i] = temp; 26 | i++; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * puts2 - prints a string, followed by a new line, 4 | * @str: pointer to the string to print 5 | * Return: void 6 | */ 7 | 8 | void puts2(char *str) 9 | { 10 | int i = 0; 11 | while (str[i] != '\0') 12 | { 13 | if (i % 2 == 0) 14 | { 15 | _putchar(str[i]); 16 | } 17 | 18 | i++; 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * puts2 - prints a string, followed by a new line, 4 | * @str: pointer to the string to print 5 | * Return: void 6 | */ 7 | 8 | void puts2(char *str) 9 | { 10 | int i = 0; 11 | while (str[i] != '\0') 12 | { 13 | if (i % 2 == 0) 14 | { 15 | _putchar(str[i]); 16 | } 17 | 18 | i++; 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * puts_half - print second half of a string 5 | * @str: char array string type 6 | * Description: If odd number of chars, print (length - 1) / 2 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | ; 15 | 16 | i++; 17 | for (i /= 2; str[i] != '\0'; i++) 18 | { 19 | _putchar(str[i]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | 2 | #include "holberton.h" 3 | #include 4 | 5 | /** 6 | * print_array - print `n` elements of an array of integers 7 | * @a: int type array pointer 8 | * @n: int type integer 9 | * Description: Numbers must be separated by comma and space. 10 | * Numbers should be displayed in the same order they are stored in array. 11 | * You can only use _putchar to print. 12 | */ 13 | 14 | void print_array(int *a, int n) 15 | { 16 | int i; 17 | 18 | i = 0; 19 | for (n--; n >= 0; n--, i++) 20 | { 21 | printf("%d", a[i]); 22 | if (n > 0) 23 | { 24 | printf(", "); 25 | } 26 | } 27 | printf("\n"); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * @dest: char type string 6 | * @src: char type string 7 | * Description: Copy the string pointed to by pointer `src` to 8 | * the buffer pointed to by `dest` 9 | * Return: Pointer to `dest` 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int i = -1; 15 | 16 | do { 17 | i++; 18 | dest[i] = src[i]; 19 | } while (src[i] != '\0'); 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # C is pure fun 2 | **pointers, arrays and strings 3 | -------------------------------------------------------------------------------- /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/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * *_strcat - concatenates two strings 4 | * @dest: pointer destination 5 | * @src: pointer source 6 | * Return: void 7 | */ 8 | 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int a = -1, i; 12 | for (i = 0; dest[i] != '\0'; i++) 13 | ; 14 | 15 | do { 16 | a++; 17 | dest[i] = src[a]; 18 | i++; 19 | } while (src[a] != '\0'); 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * *_strncat - concatenates n bytes of two strings 4 | * @dest: pointer destination 5 | * @src: pointer source 6 | * @n: number of bytes 7 | * Return: void 8 | */ 9 | 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | 13 | int dest_len, i; 14 | for (dest_len = 0; dest[dest_len] != '\0'; dest_len++) 15 | ; 16 | 17 | for (i = 0; i < n && src[i] != '\0'; i++) 18 | dest[dest_len + i] = src[i]; 19 | 20 | /*should end with a end of string char*/ 21 | dest[dest_len + i] = '\0'; 22 | 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * rot13 - encodes a string using rot13 5 | * @s: input string 6 | * Return: encoded string 7 | */ 8 | char *rot13(char *s) 9 | { 10 | int i; 11 | 12 | char rot13[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 13 | char ROT13[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; 14 | char *ptr = s; 15 | 16 | while (*s) 17 | { 18 | for (i = 0; i <= 52; i++) 19 | { 20 | if (*s == rot13[i]) 21 | { 22 | *s = ROT13[i]; 23 | break; 24 | } 25 | } 26 | s++; 27 | } 28 | return (ptr); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_number - print numbers chars 5 | * @n: integer params 6 | * Return: 0 7 | **/ 8 | 9 | void print_number(int n) 10 | { 11 | unsigned int n1; 12 | 13 | n1 = n; 14 | 15 | if (n < 0) 16 | { 17 | _putchar('-'); 18 | n1 = -n; 19 | } 20 | 21 | if (n1 / 10 != 0) 22 | { 23 | print_number(n1 / 10); 24 | } 25 | _putchar((n1 % 10) + '0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * infinite_add - adds two numbers 4 | * @n1: first number 5 | * @n2: second number 6 | * @r: result 7 | * @size_r: result lenght 8 | * Return: sum 9 | * 10 | */ 11 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 12 | 13 | { 14 | /* local variable declaration */ 15 | int i = 0, j = 0, k, l = 0, f, s, d = 0; 16 | 17 | while (n1[i] != '\0') 18 | i++; 19 | while (n2[j] != '\0') 20 | j++; 21 | if (i > j) 22 | l = i; 23 | else 24 | l = j; 25 | if (l + 1 > size_r) 26 | return (0); 27 | r[l] = '\0'; 28 | for (k = l - 1 ; k >= 0 ; k--) 29 | { 30 | i--; 31 | j--; 32 | if (i >= 0) 33 | f = n1[i] - '0'; 34 | else 35 | f = 0; 36 | if (j >= 0) 37 | s = n2[j] - '0'; 38 | else 39 | s = 0; 40 | r[k] = (f + s + d) % 10 + '0'; 41 | d = (f + s + d) / 10; 42 | } 43 | if (d == 1) 44 | { 45 | r[l + 1] = '\0'; 46 | if (l + 2 > size_r) 47 | return (0); 48 | while (l-- >= 0) 49 | r[l + 1] = r[l]; 50 | r[0] = d + '0'; 51 | } 52 | return (r); 53 | } 54 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * isPrintableASCII - determines if n is a printable ASCII char 6 | * @n: integer 7 | * Return: 1 if true, 0 if false 8 | */ 9 | int isPrintableASCII(int n) 10 | { 11 | return (n >= 32 && n <= 126); 12 | } 13 | 14 | /** 15 | * printHexes - print hex values for string b in formatted form 16 | * @b: string to print 17 | * @start: starting position 18 | * @end: ending position 19 | */ 20 | void printHexes(char *b, int start, int end) 21 | { 22 | int i = 0; 23 | 24 | while (i < 10) 25 | { 26 | if (i < end) 27 | printf("%02x", *(b + start + i)); 28 | else 29 | printf(" "); 30 | if (i % 2) 31 | printf(" "); 32 | i++; 33 | } 34 | } 35 | 36 | /** 37 | * printASCII - print ascii values for string b, 38 | * formatted to replace nonprintable chars with '.' 39 | * @b: string to print 40 | * @start: starting position 41 | * @end: ending position 42 | */ 43 | void printASCII(char *b, int start, int end) 44 | { 45 | int ch, i = 0; 46 | 47 | while (i < end) 48 | { 49 | ch = *(b + i + start); 50 | if (!isPrintableASCII(ch)) 51 | ch = 46; 52 | printf("%c", ch); 53 | i++; 54 | } 55 | } 56 | 57 | /** 58 | * print_buffer - prints a buffer 59 | * @b: string 60 | * @size: size of buffer 61 | */ 62 | void print_buffer(char *b, int size) 63 | { 64 | int start, end; 65 | 66 | if (size > 0) 67 | { 68 | for (start = 0; start < size; start += 10) 69 | { 70 | end = (size - start < 10) ? size - start : 10; 71 | printf("%08x: ", start); 72 | printHexes(b, start, end); 73 | printASCII(b, start, end); 74 | printf("\n"); 75 | } 76 | } else 77 | printf("\n"); 78 | } 79 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * *_strncpy - copies the string pointed to by src. 5 | * 6 | * @dest: pointer to the buffer. 7 | * @src: pointer to the source string. 8 | * @n: length of src. 9 | * Return: char 10 | */ 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n && src[i] != '\0'; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | for (; i < n; i++) 20 | dest[i] = '\0'; 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcmp - compares two strings 5 | * @s1: string 1 6 | * @s2: string 2 7 | * Return: 1 if true, 0 if false 8 | */ 9 | int _strcmp(char *s1, char *s2) 10 | { 11 | int equal = 0; 12 | 13 | while (*s1) 14 | { 15 | if (*s1 != *s2) 16 | { 17 | equal = ((int)*s1 - 48) - ((int)*s2 - 48); 18 | break; 19 | } 20 | s1++; 21 | s2++; 22 | } 23 | return (equal); 24 | } 25 | -------------------------------------------------------------------------------- /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: an array of integers 6 | * @n: the number of elements to swap 7 | * 8 | * Return: nothing. 9 | */ 10 | void reverse_array(int *a, int n) 11 | { 12 | int i, j, t; 13 | 14 | for (i = 0, j = (n - 1); i < j; i++, j--) 15 | { 16 | t = a[i]; 17 | a[i] = a[j]; 18 | a[j] = t; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * *string_toupper - changes all lowercase letters of a string to upper. 4 | * 5 | * @str: string to return. 6 | * Return: string. 7 | */ 8 | char *string_toupper(char *str) 9 | { 10 | int i; 11 | 12 | for (i = 0; str[i] != '\0'; i++) 13 | { 14 | if (str[i] >= 97 && str[i] <= 122) 15 | { 16 | str[i] = str[i] - 32; 17 | } 18 | } 19 | return (str); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * isLower - determines whether ascii is lowercase 6 | * @c: character 7 | * Return: 1 if true, 0 if false 8 | */ 9 | int isLower(char c) 10 | { 11 | return (c >= 97 && c <= 122); 12 | } 13 | 14 | /** 15 | * isDelimiter - determines whether ascii is a delimiter 16 | * @c: character 17 | * Return: 1 if true, 0 if false 18 | */ 19 | int isDelimiter(char c) 20 | { 21 | int i; 22 | char delimiter[] = " \t\n,.!?\"(){}"; 23 | 24 | for (i = 0; i < 12; i++) 25 | if (c == delimiter[i]) 26 | return (1); 27 | return (0); 28 | } 29 | 30 | /** 31 | * cap_string - capitalizes all words of a string 32 | * @s: input string 33 | * Return: string with capitalized words 34 | */ 35 | char *cap_string(char *s) 36 | { 37 | char *ptr = s; 38 | int foundDelimit = 1; 39 | 40 | while (*s) 41 | { 42 | if (isDelimiter(*s)) 43 | foundDelimit = 1; 44 | else if (isLower(*s) && foundDelimit) 45 | { 46 | *s -= 32; 47 | foundDelimit = 0; 48 | } 49 | else 50 | foundDelimit = 0; 51 | s++; 52 | } 53 | return (ptr); 54 | } 55 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * leet - encodes a string into 1337 4 | * @c: String 5 | * Return: string that is encoded 6 | */ 7 | char *leet(char *c) 8 | { 9 | char *cp = c; 10 | char key[] = {'A', 'E', 'O', 'T', 'L'}; 11 | int value[] = {4, 3, 0, 7, 1}; 12 | unsigned int i; 13 | 14 | while (*c) 15 | { 16 | for (i = 0; i < sizeof(key) / sizeof(char); i++) 17 | { 18 | /*32 is the difference between lower case letters and apper case letters*/ 19 | if (*c == key[i] || *c == key[i] + 32) 20 | { 21 | *c = 48 + value[i]; 22 | } 23 | } 24 | c++; 25 | } 26 | 27 | return (cp); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # c gives me joy 2 | **More about pointers and strings 3 | -------------------------------------------------------------------------------- /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 *leet(char *); 8 | char *rot13(char *); 9 | void print_number(int n); 10 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 11 | void print_buffer(char *b, int size); 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * *_memset - fills memory with a constant byte. 5 | * @s: pointer to put the constant 6 | * @b: constant 7 | * @n: max bytes to use 8 | * Return: s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; n > 0; i++, n--) 16 | { 17 | s[i] = b; 18 | } 19 | 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * *_memcpy - copies memory area 5 | * @dest: memory area 6 | * @src: source 7 | * @n: length of src 8 | * 9 | * Return: pointer to dest. 10 | */ 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /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: pointer to pointer 6 | * @to: pointer char 7 | */ 8 | 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * *_strchr - fills memory with a constant byte. 5 | * @s: pointer to put the constant 6 | * @c: constant 7 | * Return: s 8 | */ 9 | 10 | char *_strchr(char *s, char c) 11 | { 12 | int i; 13 | 14 | for (i = 0; s[i] >= '\0' ; i++) 15 | { 16 | if (s[i] == c) 17 | { 18 | return (s + i); 19 | } 20 | } 21 | 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * 6 | * @s: string. 7 | * @accept: bytes. 8 | * Return: unsigned int. 9 | */ 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | { 16 | for (j = 0; accept[j] != s[i]; j++) 17 | { 18 | if (accept[j] == '\0') 19 | return (i); 20 | } 21 | } 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strpbrk - Search a string for any of a set of bytes. 5 | * @s: string 6 | * @accept: string to match 7 | * Return: Pointer to the byte in `s` that matches one of the bytes in `accept` 8 | * or NULL if no such byte is found. 9 | */ 10 | 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i, j; 14 | char *p; 15 | 16 | i = 0; 17 | while (s[i] != '\0') 18 | { 19 | j = 0; 20 | while (accept[j] != '\0') 21 | { 22 | if (accept[j] == s[i]) 23 | { 24 | p = &s[i]; 25 | return (p); 26 | } 27 | j++; 28 | } 29 | i++; 30 | } 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * *_strstr - description 5 | * @haystack: string 6 | * @needle: pointer 7 | * Return: pointer 8 | */ 9 | 10 | 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | int i, j; 14 | 15 | for (i = 0; haystack[i] > '\0'; i++) 16 | { 17 | for (j = i; haystack[j] > '\0' && needle[j - i] > '\0'; j++) 18 | { 19 | if (haystack[j] != needle[j - i]) 20 | { 21 | break; 22 | } 23 | } 24 | if (needle[j - i] == '\0') 25 | { 26 | return (haystack + i); 27 | } 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_chessboard - prints chessboard. 5 | * 6 | * @a: rows. 7 | * Return: nothing. 8 | */ 9 | void print_chessboard(char (*a)[8]) 10 | { 11 | int i, j; 12 | 13 | for (i = 0; i < 8; i++) 14 | { 15 | for (j = 0; j < 8; j++) 16 | { 17 | _putchar(a[i][j]); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * print_diagsums - description 5 | * @a: 2d array of int types 6 | * @size: size of array (square) 7 | */ 8 | 9 | void print_diagsums(int *a, int size) 10 | { 11 | int i, sum1 = 0, sum2 = 0; 12 | 13 | for (i = 0; i < size; i++) 14 | { 15 | sum1 += a[i]; 16 | sum2 += a[size - i - 1]; 17 | a += size; 18 | } 19 | printf("%d, ", sum1); 20 | printf("%d\n", sum2); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # pointers_arrays_strings 2 | **These are pointer, check what they actually do 3 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *_memset(char *s, char b, unsigned int n); 3 | char *_memcpy(char *dest, char *src, unsigned int n); 4 | char *_strchr(char *s, char c); 5 | unsigned int _strspn(char *s, char *accept); 6 | char *_strpbrk(char *s, char *accept); 7 | char *_strstr(char *haystack, char *needle); 8 | void print_chessboard(char (*a)[8]); 9 | void print_diagsums(int *a, int size); 10 | void set_string(char **s, char *to); 11 | -------------------------------------------------------------------------------- /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: pointer to the string 6 | * Return: void 7 | */ 8 | 9 | void _puts_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | { 13 | _putchar('\n'); 14 | } 15 | else 16 | { 17 | _putchar(*s); 18 | _puts_recursion(s + 1); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints string in reverse 5 | * @s: pointer to string 6 | * *Return: void 7 | */ 8 | 9 | void _print_rev_recursion(char *s) 10 | { 11 | if (*s > '\0') 12 | { 13 | _print_rev_recursion(s + 1); 14 | _putchar(*s); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * last_index - returns the last index of a string (counts the null char) 5 | * @s: pointer the string 6 | * Return: int 7 | */ 8 | int is_palindrome(char *s); 9 | int check(char *s, int start, int end, int pair); 10 | int last_index(char *s) 11 | { 12 | int n = 0; 13 | 14 | if (*s > '\0') 15 | n += last_index(s + 1) + 1; 16 | 17 | return (n); 18 | } 19 | 20 | /** 21 | * is_palindrome - check if a string is a palindrome 22 | * @s: string to check 23 | * Return: 0 or 1 24 | */ 25 | 26 | int is_palindrome(char *s) 27 | { 28 | int end = last_index(s); 29 | 30 | return (check(s, 0, end - 1, end % 2)); 31 | } 32 | 33 | /** 34 | * check - checker for the palindrome 35 | * @s: string 36 | * @start: int moves from right to left 37 | * @end: int moves from left to right 38 | * @pair: int 39 | * Return: 0 or 1 40 | */ 41 | 42 | 43 | int check(char *s, int start, int end, int pair) 44 | { 45 | 46 | if ((start == end && pair != 0) || (start == end + 1 && pair == 0)) 47 | return (1); 48 | else if (s[start] != s[end]) 49 | return (0); 50 | else 51 | return (check(s, start + 1, end - 1, pair)); 52 | } 53 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * wildcmp - check the code for Holberton School students. 6 | * @s1: string 7 | * @s2: string 8 | * @a: int 9 | * @b: int 10 | * @wildUsed: int 11 | * Return: Always 0. 12 | */ 13 | int checker(char *s1, char *s2, int a, int b, int wildUsed); 14 | int wildcmp(char *s1, char *s2) 15 | { 16 | return (checker(s1, s2, 0, 0, -1)); 17 | } 18 | 19 | /** 20 | * checkLast - check last char of s2 when s1 ends 21 | * @s: string 22 | * @i: int 23 | * Return: 0 or 1 24 | */ 25 | int checkLast(char *s, int i) 26 | { 27 | if (s[i] == '*') 28 | return (checkLast(s, i + 1)); 29 | else if (s[i] == '\0') 30 | return (1); 31 | 32 | return (0); 33 | 34 | } 35 | /** 36 | * checker - helper 37 | * @s1: string 38 | * @s2: string 39 | * @a: int 40 | * @b: int 41 | * @wildUsed: int 42 | * Return: Always 0. 43 | */ 44 | int checker(char *s1, char *s2, int a, int b, int wildUsed) 45 | { 46 | 47 | if (s1[a] != '\0') 48 | { 49 | if (s2[b] == '\0') 50 | return (0); 51 | else if (s2[b] == '*') 52 | { 53 | if (s2[b + 1] == '*') 54 | return (checker(s1, s2, a, b + 1, b)); 55 | else if (s2[b + 1] == s1[a]) 56 | return (checker(s1, s2, a, b + 1, b)); 57 | else if (s1[a + 1] != s2[b + 1]) 58 | return (checker(s1, s2, a + 1, b, b)); 59 | else if (s1[a + 1] == s2[b + 1]) 60 | return (checker(s1, s2, a + 1, b + 1, b)); 61 | } 62 | else if ((s1[a] == s2[b]) || (s2[b] == '*' && s2[b + 1] == s1[a + 1])) 63 | return (checker(s1, s2, a + 1, b + 1, wildUsed)); 64 | 65 | if (wildUsed == -1) 66 | return (0); 67 | 68 | return (checker(s1, s2, a, wildUsed, wildUsed)); 69 | 70 | } 71 | if (s2[b] != '\0') 72 | return (checkLast(s2, b)); 73 | 74 | return (1); 75 | 76 | } 77 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string 5 | * @s: pointer the string 6 | * Return: int 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | int n = 0; 12 | 13 | if (*s > '\0') 14 | { 15 | n += _strlen_recursion(s + 1) + 1; 16 | } 17 | 18 | return (n); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * factorial - finds factorial 5 | * @n: int 6 | * Return: int 7 | */ 8 | 9 | int factorial(int n) 10 | { 11 | 12 | if (n < 0) 13 | { 14 | return (-1); 15 | } 16 | else if (n == 0) 17 | { 18 | return (1); 19 | } 20 | 21 | return (n * factorial(n - 1)); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _pow_recursion - Search a string for any of a set of bytes. 5 | * @x: base 6 | * @y: exposant 7 | * Return: Pointer to the byte in `s` that matches one of the bytes in `accept` 8 | * or NULL if no such byte is found. 9 | */ 10 | 11 | int _pow_recursion(int x, int y) 12 | { 13 | 14 | if (y < 0) 15 | return (-1); 16 | else if (y == 1) 17 | return (x); 18 | else if (y == 0) 19 | return (1); 20 | 21 | return (x * _pow_recursion(x, y - 1)); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _sqrt_recursion - find natural square root 5 | * @n: int 6 | * @val: square root 7 | * Return: int 8 | */ 9 | int square(int n, int val); 10 | int _sqrt_recursion(int n) 11 | { 12 | return (square(n, 1)); 13 | } 14 | 15 | /** 16 | * square - find square root 17 | * @n: int to find square root 18 | * @val: square root 19 | * Return: int 20 | */ 21 | 22 | int square(int n, int val) 23 | { 24 | 25 | if (val * val == n) 26 | return (val); 27 | else if (val * val < n) 28 | return (square(n, val + 1)); 29 | else 30 | return (-1); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * is_prime_number - check if n is a prime number 5 | * @resp:int 6 | * @n: int 7 | * Return: 0 or 1 8 | */ 9 | 10 | int check_prime(int n, int resp); 11 | int is_prime_number(int n) 12 | { 13 | return (check_prime(n, 2)); 14 | } 15 | 16 | /** 17 | * check_prime - check all number < n if they can divide it 18 | * @n: int 19 | * @resp: int 20 | * Return: int 21 | */ 22 | 23 | int check_prime(int n, int resp) 24 | { 25 | 26 | if (resp >= n && n > 1) 27 | return (1); 28 | else if (n % resp == 0 || n <= 1) 29 | return (0); 30 | else 31 | return (check_prime(n, resp + 1)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | # Recursion 2 | **Can you call yourself and reply to yourself gain....I love recursion 3 | -------------------------------------------------------------------------------- /0x08-recursion/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void _puts_recursion(char *s); 3 | void _print_rev_recursion(char *s); 4 | int _strlen_recursion(char *s); 5 | int factorial(int n); 6 | int _pow_recursion(int x, int y); 7 | int _sqrt_recursion(int n); 8 | int is_prime_number(int n); 9 | int is_palindrome(char *s); 10 | int wildcmp(char *s1, char *s2); 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # static libraries 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c *.c 3 | ar rc liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif /*"HOLBERTON_H*/ 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/libholberton.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trikcode/alx-low_level_programming/5abb846b13b6e7b6619e90581c0e02f179997210/0x09-static_libraries/libholberton.a -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints program name, followed by a new line 5 | * @argc: int 6 | * @argv: list 7 | * Return: 0 8 | */ 9 | 10 | int main(int argc, char const *argv[]) 11 | { 12 | (void)argc; 13 | printf("%s\n", argv[0]); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main-print number of arguments passed. 4 | *@argc:number of commandline arguments. 5 | *@argv:pointer to an array of commmand line arguments. 6 | *Return:0-success, non-zero-fail. 7 | */ 8 | 9 | int main(int argc, char *argv[] __attribute__((unused))) 10 | { 11 | printf("%d\n", argc - 1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main-prints the minimum number of coins 5 | *to make change for an amount of money. 6 | *@argc:number of commandline arguments. 7 | *@argv:pointer to an array of commmand line arguments. 8 | *Return:0-success, non-zero-fail. 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | if (argc == 2) 14 | { 15 | int i, leastcents = 0, money = atoi(argv[1]); 16 | int cents[] = {25, 10, 5, 2, 1}; 17 | 18 | for (i = 0; i < 5; i++) 19 | { 20 | if (money >= cents[i]) 21 | { 22 | leastcents += money / cents[i]; 23 | money = money % cents[i]; 24 | if (money % cents[i] == 0) 25 | { 26 | break; 27 | } 28 | } 29 | } 30 | printf("%d\n", leastcents); 31 | } 32 | else 33 | { 34 | printf("Error\n"); 35 | return (1); 36 | } 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the number of arguments passed into the program 5 | * @argc: int 6 | * @argv: list 7 | * Return: 0 8 | */ 9 | 10 | int main(int argc, char const *argv[]) 11 | { 12 | int i = 0; 13 | 14 | while (argc--) 15 | { 16 | printf("%s\n", argv[i]); 17 | i++; 18 | } 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main-print sum of 2 numbers. 5 | *@argc:number of commandline arguments. 6 | *@argv:pointer to an array of commmand line arguments. 7 | *Return:0-success, non-zero-fail. 8 | */ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int sum; 13 | 14 | if (argc == 3) 15 | { 16 | sum = atoi(argv[1]) * atoi(argv[2]); 17 | printf("%d\n", sum); 18 | } 19 | else 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * isInteger - checks if s is an integer 5 | * @s: string to check 6 | * Return: 0 or 1 7 | */ 8 | 9 | int isInteger(const char *s) 10 | { 11 | int i = 0; 12 | while (s[i] != '\0') 13 | { 14 | if (s[i] < '0' || s[i] > '9') 15 | return (1); 16 | i++; 17 | } 18 | return (0); 19 | } 20 | 21 | /** 22 | * main - adds positive numbers 23 | * @argc: int 24 | * @argv: list 25 | * Return: 0 26 | */ 27 | 28 | int main(int argc, char const *argv[]) 29 | { 30 | int sum = 0; 31 | while (--argc) 32 | { 33 | if (isInteger(argv[argc])) 34 | { 35 | printf("Error\n"); 36 | return (1); 37 | } 38 | sum += atoi(argv[argc]); 39 | } 40 | 41 | printf("%i\n", sum); 42 | 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # ARGC AND ARGV 2 | *Argc - number of command line arguments 3 | *Argv - An array containing command line args 4 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * *create_array - creates an array of chars, 5 | * and initializes it with a specific char. 6 | * @size: size of the array 7 | * @c: char to initialize 8 | * Return: pointer to the array initialized or NULL 9 | */ 10 | 11 | char *create_array(unsigned int size, char c) 12 | { 13 | char *n = malloc(size); 14 | 15 | if (size == 0 || n == 0) 16 | return (0); 17 | 18 | while (size--) 19 | n[size] = c; 20 | 21 | return (n); 22 | } 23 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * *_strdup - return a pointer to a newly allocated space in memory 7 | * which contains a copy of the string given as a parameter. 8 | * @str: string 9 | * Return: 0 10 | */ 11 | 12 | char *_strdup(char *str) 13 | { 14 | int i = 0, size = 0; 15 | char *m; 16 | 17 | if (str == NULL) 18 | return (NULL); 19 | 20 | for (; str[size] != '\0'; size++) 21 | ; 22 | 23 | /*+1 on the size puts the end of string character*/ 24 | m = malloc(size * sizeof(*str) + 1); 25 | 26 | if (m == 0) 27 | return (NULL); 28 | else 29 | { 30 | for (; i < size; i++) 31 | m[i] = str[i]; 32 | } 33 | return (m); 34 | } 35 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * _strlen - find length of a string 6 | * @s: string 7 | * Return: int 8 | */ 9 | 10 | 11 | int _strlen(char *s) 12 | { 13 | int size = 0; 14 | for (; s[size] != '\0'; size++) 15 | ; 16 | return (size); 17 | } 18 | 19 | /** 20 | * *argstostr - description 21 | * @ac: int 22 | * @av: arguments 23 | * Return: string 24 | */ 25 | 26 | char *argstostr(int ac, char **av) 27 | { 28 | int i = 0, nc = 0, j = 0, cmpt = 0; 29 | char *s; 30 | 31 | if (ac == 0 || av == NULL) 32 | return (NULL); 33 | 34 | for (; i < ac; i++, nc++) 35 | nc += _strlen(av[i]); 36 | 37 | s = malloc(sizeof(char) * nc + 1); 38 | if (s == 0) 39 | return (NULL); 40 | 41 | for (i = 0; i < ac; i++) 42 | { 43 | for (j = 0; av[i][j] != '\0'; j++, cmpt++) 44 | s[cmpt] = av[i][j]; 45 | 46 | s[cmpt] = '\n'; 47 | cmpt++; 48 | } 49 | s[cmpt] = '\0'; 50 | 51 | return (s); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * wrdcnt - counts the number of words in a string 6 | * @s: string to count 7 | * 8 | * Return: int of number of words 9 | */ 10 | int wrdcnt(char *s) 11 | { 12 | int i, n = 0; 13 | 14 | for (i = 0; s[i]; i++) 15 | { 16 | if (s[i] == ' ') 17 | { 18 | if (s[i + 1] != ' ' && s[i + 1] != '\0') 19 | n++; 20 | } 21 | else if (i == 0) 22 | n++; 23 | } 24 | n++; 25 | return (n); 26 | } 27 | 28 | /** 29 | * strtow - splits a string into words 30 | * @str: string to split 31 | * 32 | * Return: pointer to an array of strings 33 | */ 34 | char **strtow(char *str) 35 | { 36 | int i, j, k, l, n = 0, wc = 0; 37 | char **w; 38 | 39 | if (str == NULL || *str == '\0') 40 | return (NULL); 41 | n = wrdcnt(str); 42 | if (n == 1) 43 | return (NULL); 44 | w = (char **)malloc(n * sizeof(char *)); 45 | if (w == NULL) 46 | return (NULL); 47 | w[n - 1] = NULL; 48 | i = 0; 49 | while (str[i]) 50 | { 51 | if (str[i] != ' ' && (i == 0 || str[i - 1] == ' ')) 52 | { 53 | for (j = 1; str[i + j] != ' ' && str[i + j]; j++) 54 | ; 55 | j++; 56 | w[wc] = (char *)malloc(j * sizeof(char)); 57 | j--; 58 | if (w[wc] == NULL) 59 | { 60 | for (k = 0; k < wc; k++) 61 | free(w[k]); 62 | free(w[n - 1]); 63 | free(w); 64 | return (NULL); 65 | } 66 | for (l = 0; l < j; l++) 67 | w[wc][l] = str[i + l]; 68 | w[wc][l] = '\0'; 69 | wc++; 70 | i += j; 71 | } 72 | else 73 | i++; 74 | } 75 | return (w); 76 | } 77 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "holberton.h" 4 | 5 | /** 6 | * _strlen - find length of a string 7 | * @s: string 8 | * Return: int 9 | */ 10 | 11 | 12 | int _strlen(char *s) 13 | { 14 | int size = 0; 15 | for (; s[size] != '\0'; size++) 16 | ; 17 | return (size); 18 | } 19 | 20 | /** 21 | * *str_concat - concatenates two strings 22 | * @s1: string 1 23 | * @s2: string 2 24 | * Return: pointer 25 | */ 26 | 27 | char *str_concat(char *s1, char *s2) 28 | { 29 | int size1, size2, i; 30 | char *m; 31 | 32 | if (s1 == NULL) 33 | s1 = "\0"; 34 | if (s2 == NULL) 35 | s2 = "\0"; 36 | 37 | size1 = _strlen(s1); 38 | size2 = _strlen(s2); 39 | m = malloc((size1 + size2) *sizeof(char) + 1); 40 | if (m == 0) 41 | return (0); 42 | 43 | for (i = 0; i <= size1 + size2; i++) 44 | { 45 | if (i < size1) 46 | m[i] = s1[i]; 47 | else 48 | m[i] = s2[i - size1]; 49 | } 50 | m[i] = '\0'; 51 | return (m); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * **alloc_grid - returns a pointer to a 2 dimensional array of integers. 5 | * @width: int 6 | * @height: int 7 | * Return: 2d array 8 | */ 9 | 10 | int **alloc_grid(int width, int height) 11 | { 12 | int **tab, i, j; 13 | tab = malloc(sizeof(*tab) * height); 14 | 15 | if (width <= 0 || height <= 0 || tab == 0) 16 | return (NULL); 17 | 18 | else 19 | { 20 | for (i = 0; i < height; i++) 21 | { 22 | tab[i] = malloc(sizeof(**tab) * width); 23 | if (tab[i] == 0) 24 | { 25 | /*Free everything if malloc fails*/ 26 | while (i--) 27 | free(tab[i]); 28 | free(tab); 29 | return (NULL); 30 | } 31 | 32 | for (j = 0; j < width; j++) 33 | tab[i][j] = 0; 34 | } 35 | } 36 | 37 | return (tab); 38 | } 39 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * free_grid - free the grid 5 | * @grid: grid of memories 6 | * @height: int 7 | * Return: void 8 | */ 9 | 10 | void free_grid(int **grid, int height) 11 | { 12 | int i = 0; 13 | for (; i < height; i++) 14 | free(grid[i]); 15 | free(grid); 16 | } 17 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # the purpose of Malloc 2 |

Allocate memory using malloc at run time

3 | -------------------------------------------------------------------------------- /0x0B-malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #if !defined(HOLBERTON_H) 2 | #define HOLBERTON_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | char *create_array(unsigned int size, char c); 9 | char *_strdup(char *str); 10 | char *str_concat(char *s1, char *s2); 11 | int **alloc_grid(int width, int height); 12 | void free_grid(int **grid, int height); 13 | char *argstostr(int ac, char **av); 14 | char **strtow(char *str); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | /** 5 | * *malloc_checked - allocates memory using malloc and exit if failed 6 | * @b: int 7 | * Return: pointer to the array initialized or NULL 8 | */ 9 | 10 | void *malloc_checked(unsigned int b) 11 | { 12 | int *m = malloc(b); 13 | 14 | if (m == 0) 15 | exit(98); 16 | 17 | return (m); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | /** 5 | * string_nconcat - concatenates two strings 6 | * @s1: pointer to first string. 7 | * @s2: pointer to 2nd string. 8 | * @n: Number of bytes from n2 to concatenate. 9 | * 10 | * Return:Pointer to space in memory containing concatenated string. 11 | */ 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | char *str; 15 | unsigned int i, j, s1_length, s2_length; 16 | 17 | /*Check if the strings passed are null*/ 18 | if (s1 == NULL) 19 | s1 = ""; 20 | if (s2 == NULL) 21 | s2 = ""; 22 | /*Compute the length of the strings*/ 23 | 24 | for (s1_length = 0; s1[s1_length] != '\0'; s1_length++) 25 | ; 26 | 27 | for (s2_length = 0; s2[s2_length] != '\0'; s2_length++) 28 | ; 29 | /*Memory reservation-for case 1 & 2.*/ 30 | str = malloc(s1_length + n + 1); 31 | if (str == NULL) 32 | { 33 | return (NULL); 34 | } 35 | /*Copy first string into str.*/ 36 | for (i = 0; s1[i] != '\0'; i++) 37 | str[i] = s1[i]; 38 | /*copy second stringg into str.*/ 39 | for (j = 0; j < n; j++) 40 | { 41 | str[i] = s2[j]; 42 | i++; 43 | } 44 | 45 | str[i] = '\0'; 46 | return (str); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * *_realloc - allocates a memory block using malloc and free. 5 | * @ptr: pointer to memory allocated with malloc(old_size). 6 | * @old_size: size in bytes, of the allocated space for ptr 7 | * @new_size: size in bytes of the new memory block. 8 | * 9 | * Return: NULL if new_size = 0 and ptr is not NULL. 10 | */ 11 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 12 | { 13 | void *p; 14 | unsigned int i; 15 | 16 | if (new_size == old_size) 17 | return (ptr); 18 | if (new_size == 0 && ptr != NULL) 19 | { 20 | free(ptr); 21 | return (NULL); 22 | } 23 | if (ptr == NULL) 24 | { 25 | p = malloc(new_size); 26 | if (p == NULL) 27 | return (NULL); 28 | return (p); 29 | } 30 | if (new_size > old_size) 31 | { 32 | p = malloc(new_size); 33 | if (p == NULL) 34 | return (NULL); 35 | for (i = 0; i < old_size && i < new_size; i++) 36 | *((char *)p + i) = *((char *)ptr + i); 37 | free(ptr); 38 | } 39 | return (p); 40 | } 41 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, 5 | * @str: pointer to the string to print 6 | * Return: void 7 | */ 8 | 9 | 10 | void _puts(char *str) 11 | { 12 | int i = 0; 13 | while (str[i]) 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | 19 | } 20 | 21 | /** 22 | * _atoi - convert a string to an integer. 23 | * @s: char type string 24 | * Return: integer converted 25 | */ 26 | 27 | int _atoi(const char *s) 28 | { 29 | int sign = 1; 30 | unsigned long int resp = 0, firstNum, i; 31 | 32 | for (firstNum = 0; !(s[firstNum] >= 48 && s[firstNum] <= 57); firstNum++) 33 | { 34 | if (s[firstNum] == '-') 35 | { 36 | sign *= -1; 37 | } 38 | } 39 | 40 | for (i = firstNum; s[i] >= 48 && s[i] <= 57; i++) 41 | { 42 | resp *= 10; 43 | resp += (s[i] - 48); 44 | } 45 | 46 | return (sign * resp); 47 | } 48 | 49 | /** 50 | * print_int - prints an integer. 51 | * @n: int 52 | * Return: 0 53 | */ 54 | 55 | void print_int(unsigned long int n) 56 | { 57 | 58 | unsigned long int divisor = 1, i, resp; 59 | 60 | for (i = 0; n / divisor > 9; i++, divisor *= 10) 61 | ; 62 | 63 | for (; divisor >= 1; n %= divisor, divisor /= 10) 64 | { 65 | resp = n / divisor; 66 | _putchar('0' + resp); 67 | } 68 | 69 | } 70 | 71 | /** 72 | * main - print the result of the multiplication, followed by a new line 73 | * @argc: int 74 | * @argv: list 75 | * Return: 0 76 | */ 77 | 78 | int main(int argc, char const *argv[]) 79 | { 80 | (void)argc; 81 | 82 | if (argc != 3) 83 | { 84 | _puts("Error "); 85 | exit(98); 86 | } 87 | print_int(_atoi(argv[1]) * _atoi(argv[2])); 88 | _putchar('\n'); 89 | 90 | return (0); 91 | } 92 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * *_memset - fills memory with a constant byte. 5 | * @s: pointer to put the constant 6 | * @b: constant 7 | * @n: max bytes to use 8 | * Return: s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | char *ptr = s; 14 | 15 | while (n--) 16 | *s++ = b; 17 | 18 | return (ptr); 19 | } 20 | 21 | /** 22 | * *_calloc - allocates memory for an array, using malloc 23 | * @nmemb: array length 24 | * @size: size of each element 25 | * Return: pointer 26 | */ 27 | 28 | void *_calloc(unsigned int nmemb, unsigned int size) 29 | { 30 | void *m; 31 | 32 | if (size == 0 || nmemb == 0) 33 | return (NULL); 34 | m = malloc(sizeof(int) * nmemb); 35 | 36 | if (m == 0) 37 | return (NULL); 38 | 39 | _memset(m, 0, nmemb * sizeof(int)); 40 | 41 | return (m); 42 | } 43 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.h" 3 | 4 | /** 5 | * array_range - creates an array of integers 6 | * @min: starting int 7 | * @max: max int 8 | * Return: array of integers 9 | */ 10 | int *array_range(int min, int max) 11 | { 12 | int len, i; 13 | int *ptr; 14 | 15 | if (min > max) 16 | return (NULL); 17 | len = max - min + 1; 18 | ptr = malloc(sizeof(int) * len); 19 | if (!ptr) 20 | return (NULL); 21 | for (i = 0; i < len; i++) 22 | ptr[i] = min++; 23 | return (ptr); 24 | } 25 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # More of Malloc and Free 2 |

You can allocate, free as well as exit

3 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | 6 | void _puts(char *str); 7 | void *malloc_checked(unsigned int b); 8 | char *string_nconcat(char *s1, char *s2, unsigned int n); 9 | void *_calloc(unsigned int nmemb, unsigned int size); 10 | int *array_range(int min, int max); 11 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 12 | int _putchar(char c); 13 | int _atoi(const char *s); 14 | void print_int(unsigned long int n); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef SIZE 2 | #define SIZE 1024 3 | #endif 4 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI 2 | #define PI 3.14159265359 3 | #endif 4 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the name of the file that was compiled 5 | * Return: int 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef ABS_DEF 2 | #define ABS_DEF 3 | #define ABS(x) (x > 0 ? (x) : (x) * -1) 4 | #endif /* end ABS_DEF */ 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_DEF 2 | #define SUM_DEF 3 | #define SUM(x, y) (x + y) 4 | #endif /*end SUM_DEF*/ 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # Prepocessor 2 |

All Prepocessor directives

3 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - initialize a variable of type `struct dog` 6 | * @d: struct dog 7 | * @name: string for name 8 | * @age: integer for age 9 | * @owner: string for owners 10 | */ 11 | 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d != NULL) 15 | { 16 | d->name = name; 17 | d->age = age; 18 | d->owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_dog - prints dog data 7 | * @d: dog struct 8 | */ 9 | 10 | void print_dog(struct dog *d) 11 | { 12 | if (d != NULL) 13 | { 14 | printf("Name: %s\nAge: %f\nOwner: %s\n", 15 | (*d).name != NULL ? d->name : "(nil)", 16 | (*d).age, 17 | (*d).owner != NULL ? d->owner : "(nil)"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | /** 5 | * new_dog - create new data structure for dog 6 | * @name: name of dog 7 | * @age: age of dog 8 | * @owner: dog owner 9 | * 10 | * Return: Pointer to new dog 11 | **/ 12 | dog_t *new_dog(char *name, float age, char *owner) 13 | { 14 | dog_t *new_dog; 15 | int len_name, len_owner; 16 | 17 | new_dog = malloc(sizeof(dog_t)); 18 | if (new_dog == NULL) 19 | return (NULL); 20 | 21 | len_name = _strlen(name); 22 | new_dog->name = malloc(sizeof(char) * len_name + 1); 23 | if (new_dog->name == NULL) 24 | { 25 | free(new_dog); 26 | return (NULL); 27 | } 28 | new_dog->name = _strcpy(new_dog->name, name); 29 | len_owner = _strlen(owner); 30 | new_dog->owner = malloc(sizeof(char) * len_owner + 1); 31 | if (new_dog->owner == NULL) 32 | { 33 | free(new_dog->name); 34 | free(new_dog); 35 | return (NULL); 36 | } 37 | 38 | new_dog->owner = _strcpy(new_dog->owner, owner); 39 | new_dog->age = age; 40 | 41 | return (new_dog); 42 | } 43 | 44 | /** 45 | * _strlen - determinates the lenght of a string 46 | * @s: pointer to string 47 | * Return: the length 48 | */ 49 | int _strlen(char *s) 50 | { 51 | int a; 52 | 53 | for (a = 0; s[a] != '\0'; a++) 54 | ; 55 | return (a); 56 | } 57 | 58 | /** 59 | * _strcpy - copies a pointed string 60 | * @dest: pointer to the destine string 61 | * @src: pointer to the source string 62 | * Return: the adress of the destiny string 63 | */ 64 | char *_strcpy(char *dest, char *src) 65 | { 66 | int a = 0; 67 | 68 | while (src[a] != '\0') 69 | { 70 | dest[a] = src[a]; 71 | a++; 72 | } 73 | 74 | dest[a] = '\0'; 75 | return (dest); 76 | } 77 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - free dog memory 6 | * @d: dog 7 | */ 8 | 9 | 10 | void free_dog(dog_t *d) 11 | { 12 | if (d == NULL) 13 | return; 14 | 15 | free(d->owner); 16 | free(d->name); 17 | free(d); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # structures_typedef 2 |

typedef can create an alias data type

3 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * struct dog - structure definition of a dog 6 | * @name: character string 7 | * @age: integer 8 | * @owner: character string 9 | */ 10 | 11 | typedef struct dog 12 | { 13 | char *name; 14 | float age; 15 | char *owner; 16 | } dog_t; 17 | 18 | void init_dog(struct dog *d, char *name, float age, char *owner); 19 | void print_dog(struct dog *d); 20 | dog_t *new_dog(char *name, float age, char *owner); 21 | void free_dog(dog_t *d); 22 | char *_strcpy(char *dest, char *src); 23 | int _strlen(char *s); 24 | #endif 25 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - prints a name 5 | * @name: string 6 | * @f: function 7 | */ 8 | 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | if (f != NULL) 12 | f(name); 13 | } 14 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - xecutes a function given as a parameter on each 5 | * element of an array. 6 | * @array: array of intege 7 | * @size: size of array 8 | * @action: function pointer 9 | */ 10 | 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | size_t i; 14 | 15 | if (array != NULL && action != NULL) 16 | { 17 | for (i = 0; i < size; i++) 18 | action(array[i]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - entry point 6 | * 7 | * @argc: argument count 8 | * 9 | * @argv: argument vector 10 | * 11 | * Return: always 0 12 | */ 13 | int main(int argc, char *argv[]) 14 | { 15 | int i, n; 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | n = atoi(argv[1]); 22 | if (n < 0) 23 | { 24 | printf("Error\n"); 25 | exit(2); 26 | } 27 | for (i = 0; i < n; i++) 28 | { 29 | printf("%02hhx", *((char *)main + i)); 30 | if (i < n - 1) 31 | { 32 | printf(" "); 33 | } 34 | else 35 | { 36 | printf("\n"); 37 | } 38 | } 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer 5 | * @array: array of integers 6 | * @size: size of array 7 | * @cmp: function pointer 8 | * Return: index of first element that matches with `cmp`, or -1 if none found 9 | */ 10 | 11 | int int_index(int *array, int size, int (*cmp)(int)) 12 | { 13 | int i; 14 | 15 | if (size < 1 || array == NULL || cmp == NULL) 16 | return (-1); 17 | 18 | for (i = 0; i < size; i++) 19 | { 20 | if (cmp(array[i])) 21 | return (i); 22 | } 23 | 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | /** 4 | * struct op - Struct op 5 | * 6 | * @op: operator 7 | * @f: function 8 | */ 9 | typedef struct op 10 | { 11 | char *op; 12 | int (*f)(int a, int b); 13 | } op_t; 14 | 15 | int op_add(int a, int b); 16 | int op_sub(int a, int b); 17 | int op_mul(int a, int b); 18 | int op_div(int a, int b); 19 | int op_mod(int a, int b); 20 | int (*get_op_func(char *s))(int, int); 21 | #endif 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | * get_op_func - select the correct operation function asked by user 6 | * @s: operator argument 7 | * Return: function pointer corresponding to operator given 8 | */ 9 | int (*get_op_func(char *s))(int, int) 10 | { 11 | op_t ops[] = { 12 | {"+", op_add}, 13 | {"-", op_sub}, 14 | {"*", op_mul}, 15 | {"/", op_div}, 16 | {"%", op_mod}, 17 | {NULL, NULL} 18 | }; 19 | int i = 0; 20 | 21 | while (ops[i].op != NULL && *(ops[i].op) != *s) 22 | i++; 23 | return (ops[i].f); 24 | } 25 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | /** 5 | * main - Prints the result of simple operations. 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 num1, num2; 14 | char *op; 15 | 16 | if (argc != 4) 17 | { 18 | printf("Error\n"); 19 | exit(98); 20 | } 21 | 22 | num1 = atoi(argv[1]); 23 | op = argv[2]; 24 | num2 = atoi(argv[3]); 25 | 26 | if (get_op_func(op) == NULL || op[1] != '\0') 27 | { 28 | printf("Error\n"); 29 | exit(99); 30 | } 31 | if ((*op == '/' && num2 == 0) || 32 | (*op == '%' && num2 == 0)) 33 | { 34 | printf("Error\n"); 35 | exit(100); 36 | } 37 | printf("%d\n", get_op_func(op)(num1, num2)); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * op_add - addition operator 5 | * @a: first int 6 | * @b: second int 7 | * Return: sum of 2 numbers 8 | */ 9 | int op_add(int a, int b) 10 | { 11 | return (a + b); 12 | } 13 | 14 | /** 15 | * op_sub - subtraction operator 16 | * @a: first in 17 | * @b: second int 18 | * Return: difference of 2 numbers 19 | */ 20 | int op_sub(int a, int b) 21 | { 22 | return (a - b); 23 | } 24 | 25 | /** 26 | * op_mul - product of 2 numbers 27 | * @a: first int 28 | * @b: second int 29 | * Return: product of the numbers 30 | */ 31 | int op_mul(int a, int b) 32 | { 33 | return (a * b); 34 | } 35 | 36 | /** 37 | * op_div - division of 2 numbers 38 | * @a: first int 39 | * @b: second int 40 | * Return: result of the division of the numbers 41 | */ 42 | int op_div(int a, int b) 43 | { 44 | return (a / b); 45 | } 46 | 47 | /** 48 | * op_mod - get remainder of the division of 2 numbers 49 | * @a: first int 50 | * @b: second int 51 | * Return: remainder of division of the numbers 52 | */ 53 | int op_mod(int a, int b) 54 | { 55 | return (a % b); 56 | } 57 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # Function Pointers 2 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | #include 4 | void print_name(char *name, void (*f)(char *)); 5 | void array_iterator(int *array, size_t size, void (*action)(int)); 6 | int int_index(int *array, int size, int (*cmp)(int)); 7 | 8 | #endif /* FUNCTION_POINTERS_H */ 9 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | * sum_them_all - sum all arguments 6 | * @n: int, number of undefined arguments 7 | * 8 | * Return: sum of args 9 | * On error, 0 is returned 10 | */ 11 | 12 | int sum_them_all(const unsigned int n, ...) 13 | { 14 | va_list list; 15 | unsigned int i, sum = 0; 16 | 17 | va_start(list, n); 18 | if (n != 0) 19 | for (i = 0; i < n; sum += va_arg(list, unsigned int), i++) 20 | ; 21 | va_end(list); 22 | 23 | return (sum); 24 | } 25 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * print_numbers - prints all numbers with a separator 8 | * @separator: separe numbers 9 | * @n: int, number of undefined arguments 10 | */ 11 | 12 | void print_numbers(const char *separator, const unsigned int n, ...) 13 | { 14 | va_list list; 15 | unsigned int i; 16 | 17 | va_start(list, n); 18 | 19 | for (i = 0; i < n; i++) 20 | { 21 | printf("%i", va_arg(list, int)); 22 | if (i != n - 1 && separator != NULL) 23 | printf("%s", separator); 24 | } 25 | va_end(list); 26 | 27 | putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x10-variadic_functions/100-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | ; ----------------------------------------------------------------------------------- 2 | ; writes Hello, Holberton to the console using system calls. Runs on 64-bit Linux 3 | ; To assemble and run: 4 | ; 5 | ; nasm -f elf64 100-hello_holberton.asm && gcc 100-hello_holberton.o -o hello 6 | ; ----------------------------------------------------------------------------------- 7 | 8 | section .text 9 | global main 10 | 11 | main: 12 | push rbp 13 | mov rbp, rsp 14 | sub rsp, 32 15 | mov rax, 1 16 | mov rdi, 1 17 | mov rsi, message 18 | mov rdx, 17 19 | syscall 20 | mov rax, 60 21 | xor rdi, rdi 22 | syscall 23 | 24 | section .data 25 | message: db "Hello, Holberton", 10 26 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * print_strings - prints all strings with a separator 8 | * @separator: separe strings 9 | * @n: int, number of undefined arguments 10 | */ 11 | 12 | void print_strings(const char *separator, const unsigned int n, ...) 13 | { 14 | va_list list; 15 | unsigned int i; 16 | char *c; 17 | va_start(list, n); 18 | 19 | for (i = 0; i < n; i++) 20 | { 21 | c = va_arg(list, char*); 22 | if (c != NULL) 23 | printf("%s", c); 24 | else 25 | printf("(nil)"); 26 | 27 | if (i != n - 1 && separator != NULL) 28 | printf("%s", separator); 29 | } 30 | va_end(list); 31 | 32 | putchar('\n'); 33 | } 34 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_int - prints int 7 | * @list: arguments from print_all 8 | */ 9 | void print_int(va_list list) 10 | { 11 | printf("%d", va_arg(list, int)); 12 | } 13 | 14 | /** 15 | * print_float - prints float 16 | * @list: arguments from print_all 17 | */ 18 | void print_float(va_list list) 19 | { 20 | printf("%f", va_arg(list, double)); 21 | } 22 | 23 | /** 24 | * print_char - prints int 25 | * @list: arguments from print_all 26 | */ 27 | void print_char(va_list list) 28 | { 29 | printf("%c", va_arg(list, int)); 30 | } 31 | 32 | /** 33 | * print_str - prints string 34 | * @list: arguments from print_all 35 | */ 36 | void print_str(va_list list) 37 | { 38 | char *s = va_arg(list, char *); 39 | 40 | s == NULL ? printf("(nil)") : printf("%s", s); 41 | 42 | } 43 | 44 | /** 45 | * print_all - prints any type 46 | * @format: arguments to print 47 | */ 48 | 49 | void print_all(const char * const format, ...) 50 | { 51 | va_list list; 52 | int i = 0, j = 0; 53 | char *sep = ""; 54 | 55 | printTypeStruct printType[] = { 56 | { "i", print_int }, 57 | { "f", print_float }, 58 | { "c", print_char }, 59 | { "s", print_str }, 60 | {NULL, NULL} 61 | }; 62 | 63 | 64 | va_start(list, format); 65 | 66 | while (format && format[i]) 67 | { 68 | j = 0; 69 | while (j < 4) 70 | { 71 | if (*printType[j].type == format[i]) 72 | { 73 | printf("%s", sep); 74 | printType[j].printer(list); 75 | sep = ", "; 76 | break; 77 | } 78 | j++; 79 | } 80 | i++; 81 | } 82 | 83 | printf("\n"); 84 | va_end(list); 85 | } 86 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # Variadic Functions 2 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_H 2 | #define VARIADIC_H 3 | #include 4 | 5 | int sum_them_all(const unsigned int n, ...); 6 | void print_numbers(const char *separator, const unsigned int n, ...); 7 | void print_strings(const char *separator, const unsigned int n, ...); 8 | void print_all(const char * const format, ...); 9 | int _putchar(char c); 10 | 11 | void print_int(va_list list); 12 | void print_float(va_list list); 13 | void print_char(va_list list); 14 | void print_str(va_list list); 15 | 16 | /** 17 | * struct printTypeStruct - structure definition of a printTypeStruct 18 | * @type: type 19 | * @printer: function to print 20 | */ 21 | typedef struct printTypeStruct 22 | { 23 | char *type; 24 | void (*printer)(va_list); 25 | } printTypeStruct; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_list - print elements of linkedlist 6 | * @h: linked list 7 | * 8 | * Return: number of nodes 9 | */ 10 | 11 | size_t print_list(const list_t *h) 12 | { 13 | size_t nbr_nodes = 0; 14 | const list_t *current = h; 15 | 16 | while (current != NULL) 17 | { 18 | printf("[%d] %s\n", current->len, 19 | current->str != NULL ? current->str : "(nil)"); 20 | current = current->next; 21 | nbr_nodes++; 22 | } 23 | return (nbr_nodes); 24 | } 25 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * list_len - returns the number of elements in a linked list_t list. 6 | * @h: linked list 7 | * 8 | * Return: number of elements 9 | */ 10 | 11 | size_t list_len(const list_t *h) 12 | { 13 | size_t nbr_element = 0; 14 | const list_t *current = h; 15 | 16 | while (current != NULL) 17 | { 18 | current = current->next; 19 | nbr_element++; 20 | } 21 | return (nbr_element); 22 | } 23 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * premain - run before main 5 | */ 6 | void __attribute__ ((constructor)) premain() 7 | { 8 | printf( 9 | "You're beat! and yet, you must allow,\nI bore my house upon my back!\n" 10 | ); 11 | } 12 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | global main 2 | extern printf 3 | 4 | section .text 5 | main: 6 | push rbp 7 | mov rdi, format 8 | mov rsi, message 9 | mov rax, 0 10 | call printf 11 | pop rbp 12 | mov rax, 0 13 | ret 14 | message: db "Hello, Holberton", 0 15 | format: db "%s", 10, 0 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * *add_node - adds a node to a linked list head 6 | * @head: linked list head 7 | * @str: string element of the linked list 8 | * Return: number of elements 9 | */ 10 | 11 | list_t *add_node(list_t **head, const char *str) 12 | { 13 | list_t *new_node; 14 | int len_str; 15 | 16 | new_node = (list_t *) malloc(sizeof(list_t)); 17 | 18 | if (new_node == NULL) 19 | { 20 | free(new_node); 21 | return (NULL); 22 | } 23 | 24 | new_node->str = strdup(str); 25 | if (new_node->str == NULL) 26 | { 27 | free(new_node); 28 | return (NULL); 29 | } 30 | for (len_str = 0; str[len_str] != '\0'; len_str++) 31 | ; 32 | 33 | new_node->len = len_str; 34 | new_node->next = *head; 35 | *head = new_node; 36 | 37 | return (new_node); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * add_node_end - add a node at the end of a linked list 7 | * @head: The character to print 8 | * @str: string for the new node 9 | * 10 | * Return: new node 11 | */ 12 | list_t *add_node_end(list_t **head, const char *str) 13 | { 14 | list_t *current; 15 | list_t *new_node; 16 | int c; 17 | 18 | current = *head; 19 | while (current && current->next != NULL) 20 | current = current->next; 21 | 22 | for (c = 0; str[c] != '\0'; c++) 23 | ; 24 | 25 | new_node = malloc(sizeof(list_t)); 26 | if (new_node == NULL) 27 | { 28 | free(new_node); 29 | return (NULL); 30 | } 31 | new_node->str = strdup(str); 32 | if (new_node->str == NULL) 33 | { 34 | free(new_node); 35 | return (NULL); 36 | } 37 | new_node->len = c; 38 | new_node->next = NULL; 39 | 40 | if (current) 41 | current->next = new_node; 42 | else 43 | *head = new_node; 44 | return (new_node); 45 | } 46 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list_t list 5 | * @head: head of linked list 6 | */ 7 | void free_list(list_t *head) 8 | { 9 | list_t *current; 10 | list_t *nxt; 11 | 12 | current = head; 13 | 14 | while (current != NULL) 15 | { 16 | nxt = current->next; 17 | free(current->str); 18 | free(current); 19 | current = nxt; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # Linked Lists 2 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | 4 | #include 5 | /** 6 | * struct list_s - singly linked list 7 | * @str: string - (malloc'ed string) 8 | * @len: length of the string 9 | * @next: points to the next node 10 | * 11 | * Description: singly linked list node structure 12 | * for Holberton project 13 | */ 14 | typedef struct list_s 15 | { 16 | char *str; 17 | unsigned int len; 18 | struct list_s *next; 19 | } list_t; 20 | 21 | size_t print_list(const list_t *h); 22 | size_t list_len(const list_t *h); 23 | list_t *add_node(list_t **head, const char *str); 24 | list_t *add_node_end(list_t **head, const char *str); 25 | void free_list(list_t *head); 26 | 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_listint - print elements of linkedlist 6 | * @h: head 7 | * Return: length of a string 8 | */ 9 | 10 | size_t print_listint(const listint_t *h) 11 | { 12 | size_t no_nodes = 0; 13 | 14 | const listint_t *currentv = h; 15 | 16 | while (currentv != NULL) 17 | { 18 | printf("%i\n", currentv->n); 19 | currentv = currentv->next; 20 | no_nodes++; 21 | } 22 | return (no_nodes); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * listint_len - return number of elements in a linkedlist 4 | * @h: head 5 | * Return: number of elements 6 | */ 7 | size_t listint_len(const listint_t *h) 8 | { 9 | size_t no_nodes = 0; 10 | 11 | while (h) 12 | { 13 | no_nodes++; 14 | h = h->next; 15 | } 16 | 17 | return (no_nodes); 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * delete_nodeint_at_index - deletes a node at a given position. 5 | * @head: head of linked list 6 | * @index: index to delete 7 | * Return: 1 if it succeeded, -1 if it failed 8 | */ 9 | 10 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 11 | { 12 | listint_t *current; 13 | listint_t *tmp; 14 | 15 | if (head == NULL || (*head) == NULL) 16 | return (-1); 17 | 18 | current = *head; 19 | if (index == 0) 20 | { 21 | if ((*head)->next) 22 | (*head) = (*head)->next; 23 | else 24 | (*head) = NULL; 25 | free(current); 26 | return (1); 27 | } 28 | 29 | while (index != 1) 30 | { 31 | if (current->next == NULL) 32 | return (-1); 33 | 34 | current = current->next; 35 | --index; 36 | } 37 | tmp = current->next; 38 | if (current->next->next) 39 | current->next = current->next->next; 40 | else 41 | current->next = NULL; 42 | free(tmp); 43 | 44 | return (1); 45 | } 46 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * *reverse_listint - function to reverse order of nodes 4 | * @head: pointer to head 5 | * Return: always successful 6 | */ 7 | listint_t *reverse_listint(listint_t **head) 8 | { 9 | listint_t *forward; 10 | listint_t *back = NULL; 11 | 12 | if (head == NULL) 13 | return (NULL); 14 | forward = *head; 15 | while (forward != NULL) 16 | { 17 | forward = forward->next; 18 | forward = (*head)->next; 19 | (*head)->next = back; 20 | back = *head; 21 | *head = forward; 22 | } 23 | *head = back; 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * _r - reallocates memory for an array of pointers 7 | * to the nodes in a linked list 8 | * @list: the old list to append 9 | * @size: size of the new list (always one more than the old list) 10 | * @new: new node to add to the list 11 | * 12 | * Return: pointer to the new list 13 | */ 14 | const listint_t **_r(const listint_t **list, size_t size, const listint_t *new) 15 | { 16 | const listint_t **newlist; 17 | size_t i; 18 | 19 | newlist = malloc(size * sizeof(listint_t *)); 20 | if (newlist == NULL) 21 | { 22 | free(list); 23 | exit(98); 24 | } 25 | for (i = 0; i < size - 1; i++) 26 | newlist[i] = list[i]; 27 | newlist[i] = new; 28 | free(list); 29 | return (newlist); 30 | } 31 | 32 | /** 33 | * print_listint_safe - prints a listint_t linked list. 34 | * @head: pointer to the start of the list 35 | * 36 | * Return: the number of nodes in the list 37 | */ 38 | size_t print_listint_safe(const listint_t *head) 39 | { 40 | size_t i, num = 0; 41 | const listint_t **list = NULL; 42 | 43 | while (head != NULL) 44 | { 45 | for (i = 0; i < num; i++) 46 | { 47 | if (head == list[i]) 48 | { 49 | printf("-> [%p] %d\n", (void *)head, head->n); 50 | free(list); 51 | return (num); 52 | } 53 | } 54 | num++; 55 | list = _r(list, num, head); 56 | printf("[%p] %d\n", (void *)head, head->n); 57 | head = head->next; 58 | } 59 | free(list); 60 | return (num); 61 | } 62 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * _ra - reallocates memory for an array of pointers 7 | * to the nodes in a linked list 8 | * @list: the old list to append 9 | * @size: size of the new list (always one more than the old list) 10 | * @new: new node to add to the list 11 | * 12 | * Return: pointer to the new list 13 | */ 14 | listint_t **_ra(listint_t **list, size_t size, listint_t *new) 15 | { 16 | listint_t **newlist; 17 | size_t i; 18 | 19 | newlist = malloc(size * sizeof(listint_t *)); 20 | if (newlist == NULL) 21 | { 22 | free(list); 23 | exit(98); 24 | } 25 | for (i = 0; i < size - 1; i++) 26 | newlist[i] = list[i]; 27 | newlist[i] = new; 28 | free(list); 29 | return (newlist); 30 | } 31 | 32 | /** 33 | * free_listint_safe - frees a listint_t linked list. 34 | * @head: double pointer to the start of the list 35 | * 36 | * Return: the number of nodes in the list 37 | */ 38 | size_t free_listint_safe(listint_t **head) 39 | { 40 | size_t i, num = 0; 41 | listint_t **list = NULL; 42 | listint_t *next; 43 | 44 | if (head == NULL || *head == NULL) 45 | return (num); 46 | while (*head != NULL) 47 | { 48 | for (i = 0; i < num; i++) 49 | { 50 | if (*head == list[i]) 51 | { 52 | *head = NULL; 53 | free(list); 54 | return (num); 55 | } 56 | } 57 | num++; 58 | list = _ra(list, num, *head); 59 | next = (*head)->next; 60 | free(*head); 61 | *head = next; 62 | } 63 | free(list); 64 | return (num); 65 | } 66 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - Finds the loop 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 node at the beginning 5 | *@head: head 6 | *@n: int 7 | *Return: null or address of new element 8 | */ 9 | 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | listint_t *new; 13 | 14 | new = malloc(sizeof(listint_t)); 15 | if (new == NULL) 16 | return (NULL); 17 | new->n = n; 18 | new->next = *head; 19 | *head = new; 20 | return (new); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | **add_nodeint_end - adds a node at the beginning 5 | *@head: head 6 | *@n: int 7 | *Return: null or address of new element 8 | */ 9 | 10 | listint_t *add_nodeint_end(listint_t **head, const int n) 11 | { 12 | listint_t *new; 13 | listint_t *tmp = *head; 14 | 15 | new = malloc(sizeof(listint_t)); 16 | 17 | if (!new) 18 | return (NULL); 19 | new->n = n; 20 | 21 | if (tmp == NULL) 22 | *head = new; 23 | else 24 | { 25 | while (tmp->next != NULL) 26 | { 27 | tmp = tmp->next; 28 | } 29 | tmp->next = new; 30 | } 31 | return (new); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | *free_listint - The function that free a memory location 4 | *@head: the head of the linked list 5 | */ 6 | 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *tmp; 10 | 11 | if (head == NULL) 12 | 13 | return; 14 | 15 | while (head) 16 | 17 | { 18 | tmp = head->next; 19 | free(head); 20 | head = tmp; 21 | } 22 | free(head); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a listint_t list 5 | * Description: at the end, the head will point to NULL. 6 | * So, as we know the tail always points to NULL, we will 7 | * be moving a temp pointer, free its memory if not NULL 8 | * Untill we reach the tail 9 | * @head: head of linked list 10 | */ 11 | 12 | void free_listint2(listint_t **head) 13 | { 14 | listint_t *current; 15 | 16 | if (head == NULL) 17 | return; 18 | 19 | while (*head != NULL) 20 | { 21 | current = *head; 22 | *head = (*head)->next; 23 | free(current); 24 | } 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 linked list 5 | * @head: head of linked list 6 | * Return: the head node’s data (n). 7 | */ 8 | 9 | int pop_listint(listint_t **head) 10 | { 11 | listint_t *current; 12 | int v; 13 | 14 | if (head == NULL) 15 | return (0); 16 | 17 | current = *head; 18 | if (current == NULL) 19 | return (0); 20 | 21 | v = current->n; 22 | *head = current->next; 23 | free(current); 24 | return (v); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the nth node of a listint_t linked list. 5 | * @head: head of linked list 6 | * @index: node index to return 7 | * Return: node 8 | */ 9 | 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | listint_t *current; 13 | 14 | if (head == NULL) 15 | return (0); 16 | 17 | current = head; 18 | 19 | while (index != 0) 20 | { 21 | current = current->next; 22 | index--; 23 | if (current == NULL) 24 | return (0); /*Out of range*/ 25 | } 26 | 27 | return (current); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - returns the sum of all the data (n) of a listint_t linked list 5 | * @head: head of linked list 6 | * Return: sum 7 | */ 8 | 9 | int sum_listint(listint_t *head) 10 | { 11 | listint_t *current; 12 | int sum = 0; 13 | 14 | if (head == NULL) 15 | return (0); 16 | 17 | current = head; 18 | 19 | while (current != NULL) 20 | { 21 | sum += current->n; 22 | current = current->next; 23 | } 24 | 25 | return (sum); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a new node at a given position. 5 | * @head: head of linked list 6 | * @idx: index to insert the node 7 | * @n: new node data 8 | * Return: new node 9 | */ 10 | 11 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 12 | { 13 | listint_t *current; 14 | listint_t *new; 15 | 16 | if (head == NULL) 17 | return (0); 18 | new = malloc(sizeof(listint_t)); 19 | if (!new) 20 | return (0); 21 | 22 | new->next = NULL; 23 | new->n = n; 24 | 25 | if (idx == 0) 26 | { 27 | new->next = *head; 28 | (*head) = new; 29 | return (new); 30 | } 31 | 32 | current = *head; 33 | 34 | while (idx != 1) 35 | { 36 | current = current->next; 37 | --idx; 38 | if (current == NULL) 39 | { 40 | free(new); 41 | return (NULL); 42 | } 43 | } 44 | new->next = current->next; 45 | current->next = new; 46 | 47 | return (new); 48 | } 49 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # More singly linked lists 2 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | #include 4 | #include 5 | #include 6 | /** 7 | * struct listint_s - singly linked list 8 | * @n: integer 9 | * @next: points to the next node 10 | * 11 | * Description: singly linked list node structure 12 | * for Holberton project 13 | */ 14 | typedef struct listint_s 15 | { 16 | int n; 17 | struct listint_s *next; 18 | } listint_t; 19 | size_t print_listint(const listint_t *h); 20 | size_t listint_len(const listint_t *h); 21 | listint_t *add_nodeint(listint_t **head, const int n); 22 | listint_t *add_nodeint_end(listint_t **head, const int n); 23 | void free_listint(listint_t *head); 24 | void free_listint2(listint_t **head); 25 | int pop_listint(listint_t **head); 26 | int pop_listint(listint_t **head); 27 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 28 | int sum_listint(listint_t *head); 29 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 30 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 31 | listint_t *reverse_listint(listint_t **head); 32 | size_t print_listint_safe(const listint_t *head); 33 | size_t free_listint_safe(listint_t **h); 34 | listint_t *find_listint_loop(listint_t *head); 35 | #endif 36 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to an unsigned int. 5 | * @b: pointer to a string containing a binary number 6 | * 7 | * Return: unsigned int with decimal value of binsry number, or 0 if error 8 | */ 9 | unsigned int binary_to_uint(const char *b) 10 | { 11 | int i; 12 | unsigned int num; 13 | 14 | num = 0; 15 | if (!b) 16 | return (0); 17 | for (i = 0; b[i] != '\0'; i++) 18 | { 19 | if (b[i] != '0' && b[i] != '1') 20 | return (0); 21 | } 22 | for (i = 0; b[i] != '\0'; i++) 23 | { 24 | num <<= 1; 25 | if (b[i] == '1') 26 | num += 1; 27 | } 28 | return (num); 29 | } 30 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _pow - calculates (base ^ power) 5 | * @base: base of the exponent 6 | * @power: power of the exponent 7 | * 8 | * Return: value of (base ^ power) 9 | */ 10 | unsigned long int _pow(unsigned int base, unsigned int power) 11 | { 12 | unsigned long int num; 13 | unsigned int i; 14 | 15 | num = 1; 16 | for (i = 1; i <= power; i++) 17 | num *= base; 18 | return (num); 19 | } 20 | 21 | /** 22 | * print_binary - prints a number in binary notation 23 | * @n: number to print 24 | * 25 | * Return: void 26 | */ 27 | void print_binary(unsigned long int n) 28 | { 29 | unsigned long int divisor, check; 30 | char flag; 31 | 32 | flag = 0; 33 | divisor = _pow(2, sizeof(unsigned long int) * 8 - 1); 34 | while (divisor != 0) 35 | { 36 | check = n & divisor; 37 | if (check == divisor) 38 | { 39 | flag = 1; 40 | _putchar('1'); 41 | } 42 | else if (flag == 1 || divisor == 1) 43 | { 44 | _putchar('0'); 45 | } 46 | divisor >>= 1; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * 6 | * Return: 0 if big endian, 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | int x; 11 | char *y; 12 | 13 | x = 1; 14 | y = (char *)&x; 15 | return (*y); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given index. 5 | * @n: number to check bits in 6 | * @index: index at which to check bit 7 | * 8 | * Return: value of the bit, or -1 if there is an error 9 | */ 10 | int get_bit(unsigned long int n, unsigned int index) 11 | { 12 | unsigned long int divisor, check; 13 | 14 | if (index > (sizeof(unsigned long int) * 8 - 1)) 15 | return (-1); 16 | divisor = 1 << index; 17 | check = n & divisor; 18 | if (check == divisor) 19 | return (1); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * set_bit - sets the value of a bit to 1 at a given index. 5 | * @n: number to set 6 | * @index: index at which to set bit 7 | * 8 | * Return: 1 if it worked, or -1 if an error occurred 9 | */ 10 | int set_bit(unsigned long int *n, unsigned int index) 11 | { 12 | unsigned long int set; 13 | 14 | if (index > (sizeof(unsigned long int) * 8 - 1)) 15 | return (-1); 16 | set = 1 << index; 17 | *n = *n | set; 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a bit to 0 at a given index. 5 | * @n: number to set 6 | * @index: index at which to set bit 7 | * 8 | * Return: 1 if it worked, or -1 if an error occurred 9 | */ 10 | int clear_bit(unsigned long int *n, unsigned int index) 11 | { 12 | unsigned long int set; 13 | 14 | if (index > (sizeof(unsigned long int) * 8 - 1)) 15 | return (-1); 16 | set = ~(1 << index); 17 | *n = *n & set; 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * flip_bits - number of different bits between two numbers 5 | * @n: first number 6 | * @m: second number 7 | * 8 | * Return: number of bits you would need to flip 9 | * to get from one number to another. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned long int diff, check; 14 | unsigned int count, i; 15 | 16 | count = 0; 17 | check = 1; 18 | diff = n ^ m; 19 | for (i = 0; i < (sizeof(unsigned long int) * 8); i++) 20 | { 21 | if (check == (diff & check)) 22 | count++; 23 | check <<= 1; 24 | } 25 | return (count); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # Bit manipulation 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef _HOLBERTON_H 2 | #define _HOLBERTON_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int _putchar(char c); 9 | 10 | unsigned int binary_to_uint(const char *b); 11 | 12 | void print_binary(unsigned long int n); 13 | 14 | int get_bit(unsigned long int n, unsigned int index); 15 | 16 | int set_bit(unsigned long int *n, unsigned int index); 17 | 18 | int clear_bit(unsigned long int *n, unsigned int index); 19 | 20 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 21 | 22 | int get_endianness(void); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _HOLBERTON_H 2 | #define _HOLBERTON_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int _putchar(char c); 9 | 10 | unsigned int binary_to_uint(const char *b); 11 | 12 | void print_binary(unsigned long int n); 13 | 14 | int get_bit(unsigned long int n, unsigned int index); 15 | 16 | int set_bit(unsigned long int *n, unsigned int index); 17 | 18 | int clear_bit(unsigned long int *n, unsigned int index); 19 | 20 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 21 | 22 | int get_endianness(void); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * read_textfile - check the code for Holberton School students. 4 | * @filename: file to read and write 5 | * @letters: number of letters to read and write. 6 | * Return: letters printed 7 | */ 8 | ssize_t read_textfile(const char *filename, size_t letters) 9 | { 10 | ssize_t nletters; 11 | int file; 12 | char *text; 13 | 14 | if (!filename) 15 | return (0); 16 | text = malloc(sizeof(char) * letters + 1); 17 | if (text == NULL) 18 | return (0); 19 | file = open(filename, O_RDONLY); 20 | if (file == -1) 21 | { 22 | free(text); 23 | return (0); 24 | } 25 | nletters = read(file, text, sizeof(char) * letters); 26 | if (nletters == -1) 27 | { 28 | free(text); 29 | close(file); 30 | return (0); 31 | } 32 | nletters = write(STDOUT_FILENO, text, nletters); 33 | if (nletters == -1) 34 | { 35 | free(text); 36 | close(file); 37 | return (0); 38 | } 39 | free(text); 40 | close(file); 41 | return (nletters); 42 | } 43 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strlen - length of a string 4 | * @s: input char 5 | * Return: length of a string 6 | **/ 7 | int _strlen(char *s) 8 | { 9 | int i = 0; 10 | 11 | while (s[i]) 12 | { 13 | i++; 14 | } 15 | return (i); 16 | } 17 | /** 18 | * create_file - check the code for Holberton School students. 19 | * @filename: file to create. 20 | * @text_content: info to write into the file. 21 | * Return: 1 on success, -1 on failure 22 | */ 23 | int create_file(const char *filename, char *text_content) 24 | { 25 | ssize_t nletters; 26 | int file; 27 | 28 | if (!filename) 29 | return (1); 30 | file = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 31 | if (file == -1) 32 | { 33 | return (-1); 34 | } 35 | if (text_content) 36 | { 37 | nletters = write(file, text_content, _strlen(text_content)); 38 | if (nletters == -1) 39 | { 40 | close(file); 41 | return (-1); 42 | } 43 | } 44 | close(file); 45 | return (1); 46 | } 47 | -------------------------------------------------------------------------------- /0x15-file_io/100-elf_header.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - main entry point 5 | * @argc: argument count 6 | * @argv: argument vector 7 | * Return: 0 if success 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | register int fd, r, c; 12 | Elf64_Ehdr *header; 13 | 14 | if (argc != 2) 15 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"), exit(98); 16 | header = malloc(sizeof(Elf64_Ehdr)); 17 | if (!header) 18 | dprintf(STDERR_FILENO, "Malloc error\n"), exit(98); 19 | fd = open(argv[1], O_RDONLY); 20 | if (fd < 0) 21 | { 22 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 23 | exit(98); 24 | } 25 | r = read(fd, header, sizeof(Elf64_Ehdr)); 26 | if (r < 0) 27 | { 28 | free(header); 29 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 30 | exit(98); 31 | } 32 | validate_elf(header->e_ident); 33 | print_magic(header->e_ident); 34 | print_class(header->e_ident); 35 | print_data(header->e_ident); 36 | print_version(header->e_ident); 37 | print_osabi(header->e_ident); 38 | printf(" ABI Version: "); 39 | printf("%i\n", header->e_ident[EI_ABIVERSION]); 40 | print_type(header->e_type, header->e_ident); 41 | print_entry(header->e_entry, header->e_ident); 42 | free(header); 43 | c = close(fd); 44 | if (c) 45 | dprintf(STDERR_FILENO, "Error: Can't close fd\n"), exit(98); 46 | return (0); 47 | } 48 | 49 | /** 50 | * validate_elf - helper to check if input is valid elf file 51 | * @e_ident: pointer to char array 52 | */ 53 | void validate_elf(unsigned char *e_ident) 54 | { 55 | if (e_ident[0] == 0x7f && e_ident[1] == 'E' && 56 | e_ident[2] == 'L' && e_ident[3] == 'F') 57 | { 58 | printf("ELF Header:\n"); 59 | } else 60 | dprintf(STDERR_FILENO, "Error: Not valid ELF\n"), exit(98); 61 | } 62 | 63 | /** 64 | * print_magic - print ELF's magic number 65 | * @e_ident: pointer to char array 66 | */ 67 | void print_magic(unsigned char *e_ident) 68 | { 69 | register int i; 70 | 71 | printf(" Magic: "); 72 | for (i = 0; i < EI_NIDENT - 1; i++) 73 | printf("%02x ", e_ident[i]); 74 | printf("%02x\n", e_ident[i]); 75 | } 76 | /** 77 | * print_class - print ELF's class 78 | * @e_ident: pointer to char array 79 | */ 80 | void print_class(unsigned char *e_ident) 81 | { 82 | printf(" Class: "); 83 | switch (e_ident[EI_CLASS]) 84 | { 85 | case ELFCLASSNONE: 86 | printf("This class is invalid\n"); 87 | break; 88 | case ELFCLASS32: 89 | printf("ELF32\n"); 90 | break; 91 | case ELFCLASS64: 92 | printf("ELF64\n"); 93 | break; 94 | default: 95 | printf("\n", e_ident[EI_CLASS]); 96 | } 97 | } 98 | /** 99 | * print_data - print ELF's data 100 | * @e_ident: pointer to char array 101 | */ 102 | void print_data(unsigned char *e_ident) 103 | { 104 | printf(" Data: "); 105 | switch (e_ident[EI_DATA]) 106 | { 107 | case ELFDATANONE: 108 | printf("Unknown data format\n"); 109 | break; 110 | case ELFDATA2LSB: 111 | printf("2's complement, little endian\n"); 112 | break; 113 | case ELFDATA2MSB: 114 | printf("2's complement, big endian\n"); 115 | break; 116 | default: 117 | printf("\n", e_ident[EI_DATA]); 118 | } 119 | } 120 | /** 121 | * print_version - print ELF's version 122 | * @e_ident: pointer to char array 123 | */ 124 | void print_version(unsigned char *e_ident) 125 | { 126 | printf(" Version: "); 127 | if (e_ident[EI_VERSION] == EV_CURRENT) 128 | printf("%i (current)\n", EV_CURRENT); 129 | else 130 | printf("%i\n", e_ident[EI_VERSION]); 131 | } 132 | 133 | /** 134 | * print_osabi - print ELF's osabi 135 | * @e_ident: pointer to char array 136 | */ 137 | void print_osabi(unsigned char *e_ident) 138 | { 139 | printf(" OS/ABI: "); 140 | switch (e_ident[EI_OSABI]) 141 | { 142 | case ELFOSABI_SYSV: 143 | printf("UNIX - System V\n"); 144 | break; 145 | case ELFOSABI_HPUX: 146 | printf("UNIX - HP-UX\n"); 147 | break; 148 | case ELFOSABI_NETBSD: 149 | printf("UNIX - NetBSD\n"); 150 | break; 151 | case ELFOSABI_LINUX: 152 | printf("UNIX - Linux\n"); 153 | break; 154 | case ELFOSABI_SOLARIS: 155 | printf("UNIX - Solaris\n"); 156 | break; 157 | case ELFOSABI_IRIX: 158 | printf("UNIX - IRIX\n"); 159 | break; 160 | case ELFOSABI_FREEBSD: 161 | printf("UNIX - FreeBSD\n"); 162 | break; 163 | case ELFOSABI_TRU64: 164 | printf("UNIX - TRU64\n"); 165 | break; 166 | case ELFOSABI_ARM: 167 | printf("ARM\n"); 168 | break; 169 | case ELFOSABI_STANDALONE: 170 | printf("Standalone App\n"); 171 | break; 172 | default: 173 | printf("\n", e_ident[EI_OSABI]); 174 | } 175 | } 176 | 177 | /** 178 | * print_type - print ELF's type 179 | * @e_type: e_type address 180 | * @e_ident: pointer to char array 181 | */ 182 | void print_type(unsigned int e_type, unsigned char *e_ident) 183 | { 184 | if (e_ident[EI_DATA] == ELFDATA2MSB) 185 | e_type = e_type >> 8; 186 | 187 | printf(" Type: "); 188 | switch (e_type) 189 | { 190 | case ET_NONE: 191 | printf("NONE (Unknown type)\n"); 192 | break; 193 | case ET_REL: 194 | printf("REL (Relocatable file)\n"); 195 | break; 196 | case ET_EXEC: 197 | printf("EXEC (Executable file)\n"); 198 | break; 199 | case ET_DYN: 200 | printf("DYN (Shared object file)\n"); 201 | break; 202 | case ET_CORE: 203 | printf("CORE (Core file)\n"); 204 | break; 205 | default: 206 | printf("\n", e_type); 207 | } 208 | } 209 | 210 | /** 211 | * print_entry - print ELF's entry 212 | * @e_entry: e_entry address 213 | * @e_ident: pointer to char array 214 | */ 215 | void print_entry(unsigned int e_entry, unsigned char *e_ident) 216 | { 217 | if (e_ident[EI_DATA] == ELFDATA2MSB) 218 | e_entry = lit_to_big_endian(e_entry); 219 | 220 | printf(" Entry point address: "); 221 | printf("%#x\n", (unsigned int)e_entry); 222 | } 223 | 224 | /** 225 | * lit_to_big_endian - converts little endian hexes to big hexes 226 | * @x: input uint 227 | * Return: output uint 228 | */ 229 | unsigned int lit_to_big_endian(unsigned int x) 230 | { 231 | return (((x >> 24) & 0x000000ff) | 232 | ((x >> 8) & 0x0000ff00) | 233 | ((x << 8) & 0x00ff0000) | 234 | ((x << 24) & 0xff000000)); 235 | } 236 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strlen - length of a string 4 | * @s: input char 5 | * Return: length of a string 6 | **/ 7 | int _strlen(char *s) 8 | { 9 | int i = 0; 10 | 11 | while (s[i]) 12 | { 13 | i++; 14 | } 15 | return (i); 16 | } 17 | /** 18 | * append_text_to_file - appends text at the end of a file. 19 | * @filename: file to append. 20 | * @text_content: info to append into the file. 21 | * Return: 1 on success, -1 on failure 22 | */ 23 | int append_text_to_file(const char *filename, char *text_content) 24 | { 25 | ssize_t nletters; 26 | int file; 27 | 28 | if (!filename) 29 | return (-1); 30 | file = open(filename, O_WRONLY | O_APPEND); 31 | if (file == -1) 32 | { 33 | return (-1); 34 | } 35 | if (text_content) 36 | { 37 | nletters = write(file, text_content, _strlen(text_content)); 38 | if (nletters == -1) 39 | { 40 | close(file); 41 | return (-1); 42 | } 43 | } 44 | close(file); 45 | return (1); 46 | } 47 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * main - copy info from file_from to file_to. 4 | * @ac: number of arguments 5 | * @av: array of arguments 6 | * Return: Always 0. 7 | */ 8 | int main(int ac, char **av) 9 | { 10 | int file_from, file_to; 11 | ssize_t l_read = 1024, l_write, close_file; 12 | char content[1024]; 13 | 14 | if (ac != 3) 15 | { dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"); 16 | exit(97); } 17 | file_from = open(av[1], O_RDONLY); 18 | if (file_from == -1) 19 | { dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", av[1]), 20 | exit(98); } 21 | file_to = open(av[2], O_CREAT | O_WRONLY | O_TRUNC, 0664); 22 | if (file_to == -1) 23 | { dprintf(STDERR_FILENO, "Error: Can't write to %s\n", av[2]); 24 | exit(99); } 25 | while (l_read == 1024) 26 | { 27 | l_read = read(file_from, content, 1024); 28 | if (l_read == -1) 29 | { dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", av[1]); 30 | exit(98); } 31 | l_write = write(file_to, content, l_read); 32 | if (l_write == -1) 33 | { dprintf(STDERR_FILENO, "Error: Can't write to %s\n", av[2]); 34 | exit(99); } 35 | } 36 | close_file = close(file_from); 37 | if (close_file == -1) 38 | { dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 39 | exit(100); } 40 | close_file = close(file_to); 41 | if (close_file == -1) 42 | { dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_to); 43 | exit(100); } 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | # FILE I/O 2 | -------------------------------------------------------------------------------- /0x15-file_io/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_HOLBERTON 2 | #define FILE_HOLBERTON 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int append_text_to_file(const char *filename, char *text_content); 13 | int create_file(const char *filename, char *text_content); 14 | ssize_t read_textfile(const char *filename, size_t letters); 15 | int _putchar(char c); 16 | void print_magic(unsigned char *e_ident); 17 | void print_class(unsigned char *e_ident); 18 | void print_data(unsigned char *e_ident); 19 | void print_version(unsigned char *e_ident); 20 | void print_osabi(unsigned char *e_ident); 21 | void print_type(unsigned int e_type, unsigned char *e_ident); 22 | void validate_elf(unsigned char *e_ident); 23 | unsigned int lit_to_big_endian(unsigned int x); 24 | void print_entry(unsigned int e_type, unsigned char *e_ident); 25 | 26 | #endif /* HOLBERTON_H */ 27 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_HOLBERTON 2 | #define FILE_HOLBERTON 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int append_text_to_file(const char *filename, char *text_content); 13 | int create_file(const char *filename, char *text_content); 14 | ssize_t read_textfile(const char *filename, size_t letters); 15 | int _putchar(char c); 16 | void print_magic(unsigned char *e_ident); 17 | void print_class(unsigned char *e_ident); 18 | void print_data(unsigned char *e_ident); 19 | void print_version(unsigned char *e_ident); 20 | void print_osabi(unsigned char *e_ident); 21 | void print_type(unsigned int e_type, unsigned char *e_ident); 22 | void validate_elf(unsigned char *e_ident); 23 | unsigned int lit_to_big_endian(unsigned int x); 24 | void print_entry(unsigned int e_type, unsigned char *e_ident); 25 | 26 | #endif /* HOLBERTON_H */ 27 | -------------------------------------------------------------------------------- /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 | 23 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Generates and prints passwords for the crackme5 executable. 7 | * @argc: The number of arguments supplied to the program. 8 | * @argv: An array of pointers to the arguments. 9 | * 10 | * Return: Always 0. 11 | */ 12 | int main(int __attribute__((__unused__)) argc, char *argv[]) 13 | { 14 | char password[7], *codex; 15 | int len = strlen(argv[1]), i, tmp; 16 | 17 | codex = "A-CHRDw87lNS0E9B2TibgpnMVys5XzvtOGJcYLU+4mjW6fxqZeF3Qa1rPhdKIouk"; 18 | 19 | tmp = (len ^ 59) & 63; 20 | password[0] = codex[tmp]; 21 | 22 | tmp = 0; 23 | for (i = 0; i < len; i++) 24 | tmp += argv[1][i]; 25 | password[1] = codex[(tmp ^ 79) & 63]; 26 | 27 | tmp = 1; 28 | for (i = 0; i < len; i++) 29 | tmp *= argv[1][i]; 30 | password[2] = codex[(tmp ^ 85) & 63]; 31 | 32 | tmp = 0; 33 | for (i = 0; i < len; i++) 34 | { 35 | if (argv[1][i] > tmp) 36 | tmp = argv[1][i]; 37 | } 38 | srand(tmp ^ 14); 39 | password[3] = codex[rand() & 63]; 40 | 41 | tmp = 0; 42 | for (i = 0; i < len; i++) 43 | tmp += (argv[1][i] * argv[1][i]); 44 | password[4] = codex[(tmp ^ 239) & 63]; 45 | 46 | for (i = 0; i < argv[1][0]; i++) 47 | tmp = rand(); 48 | password[5] = codex[(tmp ^ 229) & 63]; 49 | 50 | password[6] = '\0'; 51 | printf("%s", password); 52 | return (0); 53 | } 54 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - Adds a new node at the beginning of a dlistint_t list. 5 | * @head: A pointer to the head of the dlistint_t list. 6 | * @n: The integer for the new node to contain. 7 | * 8 | * Return: If the function fails - NULL. 9 | * Otherwise - the address of the new node. 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | 15 | new = malloc(sizeof(dlistint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->prev = NULL; 21 | new->next = *head; 22 | if (*head != NULL) 23 | (*head)->prev = new; 24 | *head = new; 25 | 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - Adds a new node at the end of a dlistint_t list. 5 | * @head: A pointer to the head of the dlistint_t list. 6 | * @n: The integer for the new node to contain. 7 | * 8 | * Return: If the function fails - NULL. 9 | * Otherwise - the address of the new node. 10 | */ 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new, *last; 14 | 15 | new = malloc(sizeof(dlistint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->next = NULL; 21 | 22 | if (*head == NULL) 23 | { 24 | new->prev = NULL; 25 | *head = new; 26 | return (new); 27 | } 28 | 29 | last = *head; 30 | while (last->next != NULL) 31 | last = last->next; 32 | last->next = new; 33 | new->prev = last; 34 | 35 | return (new); 36 | } 37 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - Frees a linked dlistint_t list. 5 | * @head: The head of the dlistint_t list. 6 | */ 7 | void free_dlistint(dlistint_t *head) 8 | { 9 | dlistint_t *tmp; 10 | 11 | while (head) 12 | { 13 | tmp = head->next; 14 | free(head); 15 | head = tmp; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - Locates a node in a dlistint_t list. 5 | * @head: The head of the dlistint_t list. 6 | * @index: The node to locate. 7 | * 8 | * Return: If the node does not exist - NULL. 9 | * Otherwise - the address of the located node. 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | for (; index != 0; index--) 14 | { 15 | if (head == NULL) 16 | return (NULL); 17 | head = head->next; 18 | } 19 | 20 | return (head); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - Sums all the data of a dlistint_t list. 5 | * @head: The head of the dlistint_t list. 6 | * 7 | * Return: The sum of all the data. 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | 19 | return (sum); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - Inserts a new node in a dlistint_t 5 | * list at a given position. 6 | * @h: A pointer to the head of the dlistint_t list. 7 | * @idx: The position to insert the new node. 8 | * @n: The integer for the new node to contain. 9 | * 10 | * Return: If the function fails - NULL. 11 | * Otherwise - the address of the new node. 12 | */ 13 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 14 | { 15 | dlistint_t *tmp = *h, *new; 16 | 17 | if (idx == 0) 18 | return (add_dnodeint(h, n)); 19 | 20 | for (; idx != 1; idx--) 21 | { 22 | tmp = tmp->next; 23 | if (tmp == NULL) 24 | return (NULL); 25 | } 26 | 27 | if (tmp->next == NULL) 28 | return (add_dnodeint_end(h, n)); 29 | 30 | new = malloc(sizeof(dlistint_t)); 31 | if (new == NULL) 32 | return (NULL); 33 | 34 | new->n = n; 35 | new->prev = tmp; 36 | new->next = tmp->next; 37 | tmp->next->prev = new; 38 | tmp->next = new; 39 | 40 | return (new); 41 | } 42 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - Deletes a node from a dlistint_t 5 | * at a given index. 6 | * @head: A pointer to the head of the dlistint_t. 7 | * @index: The index of the node to delete. 8 | * 9 | * Return: Upon success - 1. 10 | * Otherwise - -1. 11 | */ 12 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 13 | { 14 | dlistint_t *tmp = *head; 15 | 16 | if (*head == NULL) 17 | return (-1); 18 | 19 | for (; index != 0; index--) 20 | { 21 | if (tmp == NULL) 22 | return (-1); 23 | tmp = tmp->next; 24 | } 25 | 26 | if (tmp == *head) 27 | { 28 | *head = tmp->next; 29 | if (*head != NULL) 30 | (*head)->prev = NULL; 31 | } 32 | 33 | else 34 | { 35 | tmp->prev->next = tmp->next; 36 | if (tmp->next != NULL) 37 | tmp->next->prev = tmp->prev; 38 | } 39 | 40 | free(tmp); 41 | return (1); 42 | } 43 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # Doubly linked lists 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | /* 5 | * File: lists.h 6 | * Auth: Nobert Ayesiga 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | /** 13 | * struct dlistint_s - doubly linked list 14 | * @n: integer 15 | * @prev: points to the previous node 16 | * @next: points to the next node 17 | * 18 | * Description: doubly linked list node structure 19 | * for Holberton project 20 | */ 21 | typedef struct dlistint_s 22 | { 23 | int n; 24 | struct dlistint_s *prev; 25 | struct dlistint_s *next; 26 | } dlistint_t; 27 | 28 | size_t print_dlistint(const dlistint_t *h); 29 | size_t dlistint_len(const dlistint_t *h); 30 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 31 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 32 | void free_dlistint(dlistint_t *head); 33 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 34 | int sum_dlistint(dlistint_t *head); 35 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 36 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 37 | 38 | #endif /* LISTS_H */ 39 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -fPIC -c *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trikcode/alx-low_level_programming/5abb846b13b6e7b6619e90581c0e02f179997210/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P .. https://raw.githubusercontent.com/monoprosito/holbertonschool-low_level_programming/master/0x18-dynamic_libraries/libgiga.so 3 | export LD_PRELOAD="$PWD/../libgiga.so" 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # dynamic libraries 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Trikcode/alx-low_level_programming/5abb846b13b6e7b6619e90581c0e02f179997210/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 | 28 | #endif /* MAIN_H */ 29 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creates a hash table 5 | * @size: size of the array 6 | * 7 | * Return: pointer to the newly created hash table 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *hash_table; 12 | unsigned long int i; 13 | 14 | hash_table = malloc(sizeof(hash_table_t)); 15 | if (hash_table == NULL) 16 | return (NULL); 17 | hash_table->size = size; 18 | hash_table->array = malloc(size * sizeof(hash_node_t *)); 19 | if (hash_table->array == NULL) 20 | { 21 | free(hash_table); 22 | return (NULL); 23 | } 24 | for (i = 0; i < size; i++) 25 | hash_table->array[i] = NULL; 26 | return (hash_table); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - implementation of the djb2 algorithm 5 | * @str: string used to generate hash value 6 | * 7 | * Return: hash value 8 | */ 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hash; 12 | int c; 13 | 14 | hash = 5381; 15 | while ((c = *str++)) 16 | { 17 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 18 | } 19 | return (hash); 20 | } 21 | -------------------------------------------------------------------------------- /0x1A-hash_tables/100-sorted_hash_table.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * shash_table_create - creates a sorted hash table 5 | * @size: size of the hash table 6 | * 7 | * Return: pointer to the new table, or NULL on failure 8 | */ 9 | shash_table_t *shash_table_create(unsigned long int size) 10 | { 11 | shash_table_t *sht; 12 | unsigned long int i; 13 | 14 | sht = malloc(sizeof(shash_table_t)); 15 | if (sht == NULL) 16 | return (NULL); 17 | sht->size = size; 18 | sht->shead = NULL; 19 | sht->stail = NULL; 20 | sht->array = malloc(sizeof(shash_node_t) * size); 21 | if (sht->array == NULL) 22 | { 23 | free(sht); 24 | return (NULL); 25 | } 26 | for (i = 0; i < size; i++) 27 | { 28 | sht->array[i] = NULL; 29 | } 30 | return (sht); 31 | } 32 | 33 | /** 34 | * make_shash_node - makes a node for the sorted hash table 35 | * @key: key for the data 36 | * @value: data to be stored 37 | * 38 | * Return: pointer to the new node, or NULL on failure 39 | */ 40 | shash_node_t *make_shash_node(const char *key, const char *value) 41 | { 42 | shash_node_t *shn; 43 | 44 | shn = malloc(sizeof(shash_node_t)); 45 | if (shn == NULL) 46 | return (NULL); 47 | shn->key = strdup(key); 48 | if (shn->key == NULL) 49 | { 50 | free(shn); 51 | return (NULL); 52 | } 53 | shn->value = strdup(value); 54 | if (shn->value == NULL) 55 | { 56 | free(shn->key); 57 | free(shn); 58 | return (NULL); 59 | } 60 | shn->next = shn->snext = shn->sprev = NULL; 61 | return (shn); 62 | } 63 | 64 | /** 65 | * add_to_sorted_list - add a node to the sorted (by key's ASCII) linked list 66 | * @table: the sorted hash table 67 | * @node: the node to add 68 | * 69 | * Return: void 70 | */ 71 | void add_to_sorted_list(shash_table_t *table, shash_node_t *node) 72 | { 73 | shash_node_t *tmp; 74 | 75 | if (table->shead == NULL && table->stail == NULL) 76 | { 77 | table->shead = table->stail = node; 78 | return; 79 | } 80 | tmp = table->shead; 81 | while (tmp != NULL) 82 | { 83 | if (strcmp(node->key, tmp->key) < 0) 84 | { 85 | node->snext = tmp; 86 | node->sprev = tmp->sprev; 87 | tmp->sprev = node; 88 | if (node->sprev != NULL) 89 | node->sprev->snext = node; 90 | else 91 | table->shead = node; 92 | return; 93 | } 94 | tmp = tmp->snext; 95 | } 96 | node->sprev = table->stail; 97 | table->stail->snext = node; 98 | table->stail = node; 99 | } 100 | 101 | /** 102 | * shash_table_set - sets a key to a value in the hash table 103 | * @ht: sorted hash table 104 | * @key: key to the data 105 | * @value: data to add 106 | * 107 | * Return: 1 on success, 0 otherwise 108 | */ 109 | int shash_table_set(shash_table_t *ht, const char *key, const char *value) 110 | { 111 | unsigned long int index; 112 | char *new_value; 113 | shash_node_t *shn, *tmp; 114 | 115 | if (ht == NULL || ht->array == NULL || ht->size == 0 || 116 | key == NULL || strlen(key) == 0 || value == NULL) 117 | return (0); 118 | index = key_index((const unsigned char *)key, ht->size); 119 | tmp = ht->array[index]; 120 | while (tmp != NULL) 121 | { 122 | if (strcmp(tmp->key, key) == 0) 123 | { 124 | new_value = strdup(value); 125 | if (new_value == NULL) 126 | return (0); 127 | free(tmp->value); 128 | tmp->value = new_value; 129 | return (1); 130 | } 131 | tmp = tmp->next; 132 | } 133 | shn = make_shash_node(key, value); 134 | if (shn == NULL) 135 | return (0); 136 | shn->next = ht->array[index]; 137 | ht->array[index] = shn; 138 | add_to_sorted_list(ht, shn); 139 | return (1); 140 | } 141 | 142 | /** 143 | * shash_table_get - retrieve a value from the hash table 144 | * @ht: hash table 145 | * @key: key to the data 146 | * 147 | * Return: the value associated with key, or NULL on failure 148 | */ 149 | char *shash_table_get(const shash_table_t *ht, const char *key) 150 | { 151 | unsigned long int index; 152 | shash_node_t *tmp; 153 | 154 | if (ht == NULL || ht->array == NULL || ht->size == 0 || 155 | key == NULL || strlen(key) == 0) 156 | return (NULL); 157 | index = key_index((const unsigned char *)key, ht->size); 158 | tmp = ht->array[index]; 159 | while (tmp != NULL) 160 | { 161 | if (strcmp(tmp->key, key) == 0) 162 | return (tmp->value); 163 | tmp = tmp->next; 164 | } 165 | return (NULL); 166 | } 167 | 168 | /** 169 | * shash_table_print - prints a sorted hash table 170 | * @ht: hash table to print 171 | * 172 | * Return: void 173 | */ 174 | void shash_table_print(const shash_table_t *ht) 175 | { 176 | shash_node_t *tmp; 177 | char flag = 0; /* 0 before printing any data, 1 after*/ 178 | 179 | if (ht == NULL || ht->array == NULL) 180 | return; 181 | printf("{"); 182 | tmp = ht->shead; 183 | while (tmp != NULL) 184 | { 185 | if (flag == 1) 186 | printf(", "); 187 | printf("'%s': '%s'", tmp->key, tmp->value); 188 | flag = 1; 189 | tmp = tmp->snext; 190 | } 191 | printf("}\n"); 192 | } 193 | 194 | /** 195 | * shash_table_print_rev - prints a sorted hash table in reverse 196 | * @ht: hash table to print 197 | * 198 | * Return: void 199 | */ 200 | void shash_table_print_rev(const shash_table_t *ht) 201 | { 202 | shash_node_t *tmp; 203 | char flag = 0; /* 0 before printing any data, 1 after*/ 204 | 205 | if (ht == NULL || ht->array == NULL) 206 | return; 207 | printf("{"); 208 | tmp = ht->stail; 209 | while (tmp != NULL) 210 | { 211 | if (flag == 1) 212 | printf(", "); 213 | printf("'%s': '%s'", tmp->key, tmp->value); 214 | flag = 1; 215 | tmp = tmp->sprev; 216 | } 217 | printf("}\n"); 218 | } 219 | 220 | /** 221 | * shash_table_delete - deletes a sorted hash table 222 | * @ht: hash table to delete 223 | * 224 | * Return: void 225 | */ 226 | void shash_table_delete(shash_table_t *ht) 227 | { 228 | unsigned long int i; 229 | shash_node_t *next; 230 | 231 | if (ht == NULL || ht->array == NULL || ht->size == 0) 232 | return; 233 | for (i = 0; i < ht->size; i++) 234 | { 235 | while (ht->array[i] != NULL) 236 | { 237 | next = ht->array[i]->next; 238 | free(ht->array[i]->key); 239 | free(ht->array[i]->value); 240 | free(ht->array[i]); 241 | ht->array[i] = next; 242 | } 243 | } 244 | free(ht->array); 245 | ht->array = NULL; 246 | ht->shead = ht->stail = NULL; 247 | ht->size = 0; 248 | free(ht); 249 | } 250 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - gives the index of a key 5 | * @key: key to get index for 6 | * @size: size of the hash table 7 | * 8 | * Return: index for the key 9 | */ 10 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 11 | { 12 | return (hash_djb2(key) % size); 13 | } 14 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * make_hash_node - creates a new hash node 5 | * @key: key for the node 6 | * @value: for the node 7 | * 8 | * Return: the new node, or NULL on failure 9 | */ 10 | hash_node_t *make_hash_node(const char *key, const char *value) 11 | { 12 | hash_node_t *node; 13 | 14 | node = malloc(sizeof(hash_node_t)); 15 | if (node == NULL) 16 | return (NULL); 17 | node->key = strdup(key); 18 | if (node->key == NULL) 19 | { 20 | free(node); 21 | return (NULL); 22 | } 23 | node->value = strdup(value); 24 | if (node->value == NULL) 25 | { 26 | free(node->key); 27 | free(node); 28 | return (NULL); 29 | } 30 | node->next = NULL; 31 | return (node); 32 | } 33 | 34 | 35 | /** 36 | * hash_table_set - sets a key to a value in the hash table 37 | * @ht: hash table to add elemt to 38 | * @key: key for the data 39 | * @value: data to store 40 | * 41 | * Return: 1 if successful, 0 otherwise 42 | */ 43 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 44 | { 45 | unsigned long int index; 46 | hash_node_t *hash_node, *tmp; 47 | char *new_value; 48 | 49 | if (ht == NULL || ht->array == NULL || ht->size == 0 || 50 | key == NULL || strlen(key) == 0 || value == NULL) 51 | return (0); 52 | index = key_index((const unsigned char *)key, ht->size); 53 | tmp = ht->array[index]; 54 | while (tmp != NULL) 55 | { 56 | if (strcmp(tmp->key, key) == 0) 57 | { 58 | new_value = strdup(value); 59 | if (new_value == NULL) 60 | return (0); 61 | free(tmp->value); 62 | tmp->value = new_value; 63 | return (1); 64 | } 65 | tmp = tmp->next; 66 | } 67 | hash_node = make_hash_node(key, value); 68 | if (hash_node == NULL) 69 | return (0); 70 | hash_node->next = ht->array[index]; 71 | ht->array[index] = hash_node; 72 | return (1); 73 | } 74 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - retrieves a value associated with a key 5 | * @ht: table to retrieve value from 6 | * @key: key to find value 7 | * 8 | * Return: value associated with key, or NULL if key cannot be found 9 | */ 10 | char *hash_table_get(const hash_table_t *ht, const char *key) 11 | { 12 | unsigned long int index; 13 | hash_node_t *tmp; 14 | 15 | if (ht == NULL || ht->array == NULL || ht->size == 0 || 16 | key == NULL || strlen(key) == 0) 17 | return (NULL); 18 | index = key_index((const unsigned char *)key, ht->size); 19 | tmp = ht->array[index]; 20 | while (tmp != NULL) 21 | { 22 | if (strcmp(tmp->key, key) == 0) 23 | return (tmp->value); 24 | tmp = tmp->next; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - prints a hash table 5 | * @ht: hash table to print 6 | * 7 | * Return: void 8 | */ 9 | void hash_table_print(const hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *tmp; 13 | char flag = 0; /* 0 while no data has been printed yet */ 14 | 15 | if (ht == NULL || ht->array == NULL) 16 | return; 17 | printf("{"); 18 | for (i = 0; i < ht->size; i++) 19 | { 20 | tmp = ht->array[i]; 21 | while (tmp != NULL) 22 | { 23 | if (flag == 1) 24 | printf(", "); 25 | printf("'%s': '%s'", tmp->key, tmp->value); 26 | flag = 1; 27 | tmp = tmp->next; 28 | } 29 | } 30 | printf("}\n"); 31 | } 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - deletes a hash table 5 | * @ht: hash table to delete 6 | * 7 | * Return: void 8 | */ 9 | void hash_table_delete(hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *next; 13 | 14 | if (ht == NULL || ht->array == NULL || ht->size == 0) 15 | return; 16 | for (i = 0; i < ht->size; i++) 17 | { 18 | while (ht->array[i] != NULL) 19 | { 20 | next = ht->array[i]->next; 21 | free(ht->array[i]->key); 22 | free(ht->array[i]->value); 23 | free(ht->array[i]); 24 | ht->array[i] = next; 25 | } 26 | } 27 | free(ht->array); 28 | ht->array = NULL; 29 | ht->size = 0; 30 | free(ht); 31 | } 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | # hash tables 2 | -------------------------------------------------------------------------------- /0x1A-hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef _HASH_TABLES_H_ 2 | #define _HASH_TABLES_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct hash_node_s - Node of a hash table 10 | * 11 | * @key: The key, string 12 | * The key is unique in the HashTable 13 | * @value: The value corresponding to a key 14 | * @next: A pointer to the next node of the List 15 | */ 16 | typedef struct hash_node_s 17 | { 18 | char *key; 19 | char *value; 20 | struct hash_node_s *next; 21 | } hash_node_t; 22 | 23 | /** 24 | * struct hash_table_s - Hash table data structure 25 | * 26 | * @size: The size of the array 27 | * @array: An array of size @size 28 | * Each cell of this array is a pointer to the first node of a linked list, 29 | * because we want our HashTable to use a Chaining collision handling 30 | */ 31 | typedef struct hash_table_s 32 | { 33 | unsigned long int size; 34 | hash_node_t **array; 35 | } hash_table_t; 36 | 37 | hash_table_t *hash_table_create(unsigned long int size); 38 | unsigned long int hash_djb2(const unsigned char *str); 39 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 40 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 41 | char *hash_table_get(const hash_table_t *ht, const char *key); 42 | void hash_table_print(const hash_table_t *ht); 43 | void hash_table_delete(hash_table_t *ht); 44 | 45 | /** 46 | * struct shash_node_s - Node of a sorted hash table 47 | * 48 | * @key: The key, string 49 | * The key is unique in the HashTable 50 | * @value: The value corresponding to a key 51 | * @next: A pointer to the next node of the List 52 | * @sprev: A pointer to the previous element of the sorted linked list 53 | * @snext: A pointer to the next element of the sorted linked list 54 | */ 55 | typedef struct shash_node_s 56 | { 57 | char *key; 58 | char *value; 59 | struct shash_node_s *next; 60 | struct shash_node_s *sprev; 61 | struct shash_node_s *snext; 62 | } shash_node_t; 63 | 64 | /** 65 | * struct shash_table_s - Sorted hash table data structure 66 | * 67 | * @size: The size of the array 68 | * @array: An array of size @size 69 | * Each cell of this array is a pointer to the first node of a linked list, 70 | * because we want our HashTable to use a Chaining collision handling 71 | * @shead: A pointer to the first element of the sorted linked list 72 | * @stail: A pointer to the last element of the sorted linked list 73 | */ 74 | typedef struct shash_table_s 75 | { 76 | unsigned long int size; 77 | shash_node_t **array; 78 | shash_node_t *shead; 79 | shash_node_t *stail; 80 | } shash_table_t; 81 | 82 | shash_table_t *shash_table_create(unsigned long int size); 83 | int shash_table_set(shash_table_t *ht, const char *key, const char *value); 84 | char *shash_table_get(const shash_table_t *ht, const char *key); 85 | void shash_table_print(const shash_table_t *ht); 86 | void shash_table_print_rev(const shash_table_t *ht); 87 | void shash_table_delete(shash_table_t *ht); 88 | 89 | #endif /* _HASH_TABLES_H_ */ 90 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | 3 | SRC = school.c main.c 4 | 5 | all: 6 | $(CC) $(SRC) -o school 7 | -------------------------------------------------------------------------------- /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: $(NAME) 7 | 8 | $(NAME): $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | .c.o: 12 | $(CC) -c -o $@ $< 13 | -------------------------------------------------------------------------------- /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: $(NAME) 9 | 10 | $(NAME): $(OBJ) 11 | $(CC) $(OBJ) -o $(NAME) 12 | 13 | .c.o: 14 | $(CC) $(CFLAGS) -c -o $@ $< 15 | 16 | clean: 17 | $(RM) *~ $(NAME) 18 | 19 | oclean: 20 | $(RM) $(OBJ) 21 | 22 | fclean: 23 | $(RM) *~ $(OBJ) $(NAME) 24 | 25 | re: oclean all 26 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """island perimeter""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """returns perimeter""" 7 | count = 0 8 | index = 0 9 | 10 | if not isinstance(grid, list): 11 | return 0 12 | 13 | for row in grid: 14 | row_len = len(row) 15 | row_len += 2 16 | 17 | grid_line = [] 18 | i = 0 19 | while i < row_len: 20 | grid_line.append(0) 21 | i += 1 22 | for row in grid: 23 | grid_line.append(0) 24 | for item in row: 25 | grid_line.append(item) 26 | grid_line.append(0) 27 | i = 0 28 | while i < row_len: 29 | grid_line.append(0) 30 | i += 1 31 | 32 | for item in grid_line: 33 | if item == 1: 34 | if grid_line[index - row_len] == 0: 35 | count += 1 36 | if grid_line[index - 1] == 0: 37 | count += 1 38 | if grid_line[index + 1] == 0: 39 | count += 1 40 | if grid_line[index + row_len] == 0: 41 | count += 1 42 | index += 1 43 | 44 | return count 45 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # Make File 2 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | /* School */ 14 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - linear search 5 | * @array: pointer to first element in search array 6 | * @size: size of array 7 | * @value: value to find 8 | * 9 | * Return: index where found or -1 10 | */ 11 | 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | unsigned int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | for (i = 0; i < size; i++) 19 | { 20 | printf("Value checked array[%d] = [%d]\n", i, array[i]); 21 | if (array[i] == value) 22 | return (i); 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - binary search 5 | * @array: pointer to first element in search array 6 | * @size: size of array 7 | * @value: value to find 8 | * 9 | * Return: index where found or -1 10 | */ 11 | int binary_search(int *array, size_t size, int value) 12 | { 13 | int left, right, pivot; 14 | 15 | if (array == NULL) 16 | return (-1); 17 | left = 0; 18 | right = size - 1; 19 | while (left <= right) 20 | { 21 | print_array(array, left, right); 22 | pivot = (left + right) / 2; 23 | if (array[pivot] > value) 24 | right = pivot - 1; 25 | else if (array[pivot] < value) 26 | left = pivot + 1; 27 | else 28 | return (pivot); 29 | } 30 | return (-1); 31 | } 32 | 33 | /** 34 | * print_array - print array 35 | * @array: array 36 | * @left: starting print 37 | * @right: ending print 38 | */ 39 | 40 | void print_array(int *array, int left, int right) 41 | { 42 | int i; 43 | 44 | printf("Searching in array: "); 45 | for (i = left; i < right; i++) 46 | printf("%d, ", array[i]); 47 | printf("%d\n", array[i]); 48 | } 49 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * minimum_value - finds the minimum of two values 6 | * @a: first value to compare 7 | * @b: second value to compare 8 | * 9 | * Return: The smaller of the two values, or a if equal 10 | */ 11 | size_t minimum_value(size_t a, size_t b) 12 | { 13 | if (b < a) 14 | return (b); 15 | return (a); 16 | } 17 | 18 | 19 | /** 20 | * jump_search - searches for a value in a sorted array of integers using the 21 | * Jump search algorithm 22 | * @array: pointer to the first element of the array to search in 23 | * @size: number of elements in array 24 | * @value: value to search for 25 | * 26 | * Return: first index where value is located, or -1 on failure 27 | */ 28 | int jump_search(int *array, size_t size, int value) 29 | { 30 | size_t l, r, jump; 31 | int val; 32 | 33 | if (array != NULL && size > 0) 34 | { 35 | jump = sqrt(size); 36 | l = 0; 37 | r = jump; 38 | val = array[l]; 39 | printf("Value checked array[%lu] = [%d]\n", l, val); 40 | while (r < size && val < value) 41 | { 42 | if (array[r] >= value) 43 | break; 44 | l += jump; 45 | r += jump; 46 | val = array[l]; 47 | printf("Value checked array[%lu] = [%d]\n", l, val); 48 | } 49 | if (l >= size || val > value) 50 | return (-1); 51 | printf("Value found between indexes [%lu] and [%lu]\n", l, r); 52 | while (l <= minimum_value(size - 1, r) && val <= value) 53 | { 54 | val = array[l]; 55 | printf("Value checked array[%lu] = [%d]\n", l, val); 56 | if (val == value) 57 | return (l); 58 | l++; 59 | } 60 | } 61 | return (-1); 62 | } 63 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in a sorted array of integers 5 | * using the Interpolation search algorithm 6 | * @array: pointer to the first element of the array to search in 7 | * @size: number of elements in array 8 | * @value: value to search for 9 | * 10 | * Return: the first index where value is located, or -1 on failure 11 | */ 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t l, m, r; 15 | 16 | if (array != NULL && size > 0) 17 | { 18 | l = 0; 19 | r = size - 1; 20 | while (array[l] != array[r]) 21 | { 22 | m = l + (((double)(r - l) / (array[r] - array[l])) * (value - array[l])); 23 | if (m < l || m > r) 24 | { 25 | printf("Value checked array[%lu] is out of range\n", m); 26 | break; 27 | } 28 | printf("Value checked array[%lu] = [%d]\n", m, array[m]); 29 | if (array[m] < value) 30 | l = m + 1; 31 | else if (array[m] > value) 32 | r = m - 1; 33 | else 34 | return (m); 35 | } 36 | } 37 | return (-1); 38 | } 39 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 103-exponential.c 3 | * Auth: Nobert Patrick 4 | */ 5 | 6 | #include "search_algos.h" 7 | 8 | /** 9 | * _binary_search - Searches for a value in a sorted array 10 | * of integers using binary search. 11 | * @array: A pointer to the first element of the array to search. 12 | * @left: The starting index of the [sub]array to search. 13 | * @right: The ending index of the [sub]array to search. 14 | * @value: The value to search for. 15 | * 16 | * Return: If the value is not present or the array is NULL, -1. 17 | * Otherwise, the index where the value is located. 18 | * 19 | * Description: Prints the [sub]array being searched after each change. 20 | */ 21 | int _binary_search(int *array, size_t left, size_t right, int value) 22 | { 23 | size_t i; 24 | 25 | if (array == NULL) 26 | return (-1); 27 | 28 | while (right >= left) 29 | { 30 | printf("Searching in array: "); 31 | for (i = left; i < right; i++) 32 | printf("%d, ", array[i]); 33 | printf("%d\n", array[i]); 34 | 35 | i = left + (right - left) / 2; 36 | if (array[i] == value) 37 | return (i); 38 | if (array[i] > value) 39 | right = i - 1; 40 | else 41 | left = i + 1; 42 | } 43 | 44 | return (-1); 45 | } 46 | 47 | /** 48 | * exponential_search - Searches for a value in a sorted array 49 | * of integers using exponential search. 50 | * @array: A pointer to the first element of the array to search. 51 | * @size: The number of elements in the array. 52 | * @value: The value to search for. 53 | * 54 | * Return: If the value is not present or the array is NULL, -1. 55 | * Otherwise, the index where the value is located. 56 | * 57 | * Description: Prints a value every time it is compared in the array. 58 | */ 59 | int exponential_search(int *array, size_t size, int value) 60 | { 61 | size_t i = 0, right; 62 | 63 | if (array == NULL) 64 | return (-1); 65 | 66 | if (array[0] != value) 67 | { 68 | for (i = 1; i < size && array[i] <= value; i = i * 2) 69 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 70 | } 71 | 72 | right = i < size ? i : size - 1; 73 | printf("Value found between indexes [%ld] and [%ld]\n", i / 2, right); 74 | return (_binary_search(array, i / 2, right, value)); 75 | } 76 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * advanced_binary_recursive - Searches recursively for a value in a sorted 5 | * array of integers using binary search. 6 | * @array: A pointer to the first element of the [sub]array to search. 7 | * @left: The starting index of the [sub]array to search. 8 | * @right: The ending index of the [sub]array to search. 9 | * @value: The value to search for. 10 | * 11 | * Return: If the value is not present, -1. 12 | * Otherwise, the index where the value is located. 13 | * 14 | * Description: Prints the [sub]array being searched after each change. 15 | */ 16 | int advanced_binary_recursive(int *array, size_t left, size_t right, int value) 17 | { 18 | size_t i; 19 | 20 | if (right < left) 21 | return (-1); 22 | 23 | printf("Searching in array: "); 24 | for (i = left; i < right; i++) 25 | printf("%d, ", array[i]); 26 | printf("%d\n", array[i]); 27 | 28 | i = left + (right - left) / 2; 29 | if (array[i] == value && (i == left || array[i - 1] != value)) 30 | return (i); 31 | if (array[i] >= value) 32 | return (advanced_binary_recursive(array, left, i, value)); 33 | return (advanced_binary_recursive(array, i + 1, right, value)); 34 | } 35 | 36 | /** 37 | * advanced_binary - Searches for a value in a sorted array 38 | * of integers using advanced binary search. 39 | * @array: A pointer to the first element of the array to search. 40 | * @size: The number of elements in the array. 41 | * @value: The value to search for. 42 | * 43 | * Return: If the value is not present or the array is NULL, -1. 44 | * Otherwise, the first index where the value is located. 45 | * 46 | * Description: Prints the [sub]array being searched after each change. 47 | */ 48 | int advanced_binary(int *array, size_t size, int value) 49 | { 50 | if (array == NULL || size == 0) 51 | return (-1); 52 | 53 | return (advanced_binary_recursive(array, 0, size - 1, value)); 54 | } 55 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "search_algos.h" 3 | 4 | /** 5 | * move_forward - moves a list forward until the index matches a desired 6 | * index, or the last node in the list 7 | * @list: list to move forward 8 | * @index: desired index 9 | * 10 | * Return: node with desired index, or last node in the list 11 | */ 12 | listint_t *move_forward(listint_t *list, size_t index) 13 | { 14 | while (list->next != NULL && list->index < index) 15 | list = list->next; 16 | return (list); 17 | } 18 | 19 | /** 20 | * jump_list - searches for a value in a sorted list of integers using the Jump 21 | * search algorithm 22 | * @list: pointer to the head of the list to search in 23 | * @size: number of nodes in list 24 | * @value: value to search for 25 | * 26 | * Return: pointer to the first node where value is located, or NULL on failure 27 | */ 28 | listint_t *jump_list(listint_t *list, size_t size, int value) 29 | { 30 | size_t jump; 31 | listint_t *left, *right; 32 | 33 | if (list != NULL && size > 0) 34 | { 35 | jump = sqrt(size); 36 | left = list; 37 | right = move_forward(left, jump); 38 | printf("Value checked at index [%lu] = [%d]\n", right->index, right->n); 39 | while (right->index < (size - 1) && right->n < value) 40 | { 41 | left = right; 42 | right = move_forward(left, right->index + jump); 43 | printf("Value checked at index [%lu] = [%d]\n", right->index, right->n); 44 | } 45 | printf("Value found between indexes [%lu] and [%lu]\n", 46 | left->index, right->index); 47 | printf("Value checked at index [%lu] = [%d]\n", left->index, left->n); 48 | while (left->index < size - 1 && left->n < value) 49 | { 50 | left = left->next; 51 | if (left == NULL) 52 | return (NULL); 53 | printf("Value checked at index [%lu] = [%d]\n", left->index, left->n); 54 | } 55 | if (left->n == value) 56 | return (left); 57 | } 58 | return (NULL); 59 | } 60 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - Searches for an algorithm in a sorted singly 5 | * linked list of integers using linear skip. 6 | * @list: A pointer to the head of the linked list to search. 7 | * @value: The value to search for. 8 | * 9 | * Return: If the value is not present or the head of the list is NULL, NULL. 10 | * Otherwise, a pointer to the first node where the value is located. 11 | * 12 | * Description: Prints a value every time it is compared in the list. 13 | * Uses the square root of the list size as the jump step. 14 | */ 15 | skiplist_t *linear_skip(skiplist_t *list, int value) 16 | { 17 | skiplist_t *node, *jump; 18 | 19 | if (list == NULL) 20 | return (NULL); 21 | 22 | for (node = jump = list; jump->next != NULL && jump->n < value;) 23 | { 24 | node = jump; 25 | if (jump->express != NULL) 26 | { 27 | jump = jump->express; 28 | printf("Value checked at index [%ld] = [%d]\n", 29 | jump->index, jump->n); 30 | } 31 | else 32 | { 33 | while (jump->next != NULL) 34 | jump = jump->next; 35 | } 36 | } 37 | 38 | printf("Value found between indexes [%ld] and [%ld]\n", 39 | node->index, jump->index); 40 | 41 | for (; node->index < jump->index && node->n < value; node = node->next) 42 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 43 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 44 | 45 | return (node->n == value ? node : NULL); 46 | } 47 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | # search algorithms 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS_H 2 | #define SEARCH_ALGOS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct listint_s - singly linked list 10 | * 11 | * @n: Integer 12 | * @index: Index of the node in the list 13 | * @next: Pointer to the next node 14 | * 15 | * Description: singly linked list node structure 16 | * for Holberton project 17 | */ 18 | typedef struct listint_s 19 | { 20 | int n; 21 | size_t index; 22 | struct listint_s *next; 23 | } listint_t; 24 | 25 | /** 26 | * struct skiplist_s - Singly linked list with an express lane 27 | * 28 | * @n: Integer 29 | * @index: Index of the node in the list 30 | * @next: Pointer to the next node 31 | * @express: Pointer to the next node in the express lane 32 | * 33 | * Description: singly linked list node structure with an express lane 34 | * for Holberton project 35 | */ 36 | typedef struct skiplist_s 37 | { 38 | int n; 39 | size_t index; 40 | struct skiplist_s *next; 41 | struct skiplist_s *express; 42 | } skiplist_t; 43 | 44 | int linear_search(int *array, size_t size, int value); 45 | int binary_search(int *array, size_t size, int value); 46 | int jump_search(int *array, size_t size, int value); 47 | int interpolation_search(int *array, size_t size, int value); 48 | int exponential_search(int *array, size_t size, int value); 49 | int advanced_binary(int *array, size_t size, int value); 50 | listint_t *jump_list(listint_t *list, size_t size, int value); 51 | skiplist_t *linear_skip(skiplist_t *list, int value); 52 | void print_array(int *array, int left, int right); 53 | 54 | #endif /* SEARCH_ALGOS_H */ 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Alx-low_level_programming 2 |

This is all about c, we start we end it

3 | --------------------------------------------------------------------------------