├── .gitignore ├── .gitmodules ├── 0x00-hello_world ├── .checker ├── .gitignore ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c ├── README.md └── main.s ├── 0x01-variables_if_else_while ├── .gitignore ├── 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 └── tester ├── 0x02-functions_nested_loops ├── .gitignore ├── 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 └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── .gitignore ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── .gitignore ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── .gitignore ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── .gitignore ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md ├── _putchar.c └── main.h ├── 0x08-recursion ├── .gitignore ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── liball.a ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── 0x0B-malloc_free ├── .gitignore ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── main.h ├── 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 ├── .gitignore ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── .gitignore ├── 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 ├── .gitignore ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── .gitignore ├── 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 ├── .gitignore ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.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 ├── .gitignore ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md └── main.h ├── 0x15-file_io ├── .gitignore ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── .gitignore ├── 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 ├── .gitignore ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_win.sh ├── README.md ├── liball.so ├── libdynamic.so ├── main.h └── nrandom.so ├── 0x1A-hash_tables ├── .gitignore ├── 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 ├── .gitignore ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py └── README.md ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 1-binary_recursive.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 /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | tests/ 3 | main.c 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "0x11-printf"] 2 | path = 0x11-printf 3 | url = https://github.com/Pericles001/printf.git 4 | [submodule "0x16-simple_shell"] 5 | path = 0x16-simple_shell 6 | url = https://github.com/Pericles001/simple_shell.git 7 | [submodule "0x19-stacks_queues_lifo_fifo"] 8 | path = 0x19-stacks_queues_lifo_fifo 9 | url = https://github.com/Pericles001/monty.git 10 | [submodule "0x1B-sorting_algorithms"] 11 | path = 0x1B-sorting_algorithms 12 | url = https://github.com/Pericles001/sorting_algorithms.git 13 | [submodule "0x1D-binary_trees"] 14 | path = 0x1D-binary_trees 15 | url = https://github.com/Pericles001/binary_trees.git 16 | -------------------------------------------------------------------------------- /0x00-hello_world/.checker: -------------------------------------------------------------------------------- 1 | {"email":"periclesadjovi@gmail.com","password":"Mahounan","project":212,"api_key":1733} -------------------------------------------------------------------------------- /0x00-hello_world/.gitignore: -------------------------------------------------------------------------------- 1 | main.c 2 | tester 3 | main.s 4 | -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main function in the task 5 | * 6 | * 7 | *Return: return 1 8 | */ 9 | int main(void) 10 | { 11 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 12 | return (1); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - Main function of my project 6 | * 7 | * 8 | *Return: return 0 everytime 9 | */ 10 | int main(void) 11 | { 12 | 13 | char displayed[52]; 14 | 15 | strcpy(displayed, "\"Programming is like building a multilingual puzzle"); 16 | puts(displayed); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - Main function 4 | * 5 | * 6 | *Return: return 0 everytime 7 | */ 8 | int main(void) 9 | { 10 | printf("with proper grammar, but the outcome is a piece of art,\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - main function 4 | * 5 | * 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | printf("Size of a char: %d byte(s)\n", sizeof(char)); 11 | printf("Size of an int: %d byte(s)\n", sizeof(int)); 12 | printf("Size of a long int: %d byte(s)\n", sizeof(long int)); 13 | printf("Size of a long long int: %d byte(s)\n", sizeof(long long int)); 14 | printf("Size of a float: %d byte(s)\n", sizeof(float)); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/main.s: -------------------------------------------------------------------------------- 1 | .file "main.c" 2 | .text 3 | .globl main 4 | .type main, @function 5 | main: 6 | .LFB0: 7 | .cfi_startproc 8 | pushq %rbp 9 | .cfi_def_cfa_offset 16 10 | .cfi_offset 6, -16 11 | movq %rsp, %rbp 12 | .cfi_def_cfa_register 6 13 | movl $0, %eax 14 | popq %rbp 15 | .cfi_def_cfa 7, 8 16 | ret 17 | .cfi_endproc 18 | .LFE0: 19 | .size main, .-main 20 | .ident "GCC: (GNU) 11.1.0" 21 | .section .note.GNU-stack,"",@progbits 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/.gitignore: -------------------------------------------------------------------------------- 1 | test.* 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | *main - entry point of programs 6 | * 7 | *Return: return 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | if (n < 0) 16 | { 17 | printf("%d is negative\n", n); 18 | } 19 | else if (n > 0) 20 | { 21 | printf("%d is positive\n", n); 22 | } 23 | else 24 | printf("%d is zero\n", n); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | *main - Prints the last digit of random number 6 | * 7 | *Return: 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 == 0) 19 | { 20 | printf("Last digit of %d is %d and is 0\n", n, last); 21 | } 22 | else 23 | { 24 | if (last > 5) 25 | { 26 | printf("Last digit of %d is %d and is greater than 5\n", n, last); 27 | } 28 | else if (last < 6) 29 | { 30 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, last); 31 | } 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - Prints all combinations of two digits with, 4 | * and space followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit1, digit2; 11 | 12 | for (digit1 = 0; digit1 < 9; digit1++) 13 | { 14 | for (digit2 = digit1 + 1; digit2 < 10; digit2++) 15 | { 16 | 17 | putchar((digit1 % 10) + '0'); 18 | putchar((digit2 % 10) + '0'); 19 | 20 | if (digit1 == 8 && digit2 == 9) 21 | continue; 22 | 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - Prints all combinations of three digits with, 4 | * and space followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit1, digit2, digit3; 11 | 12 | for (digit1 = 0; digit1 < 9; digit1++) 13 | { 14 | for (digit2 = digit1 + 1; digit2 < 9; digit2++) 15 | { 16 | for (digit3 = digit2 + 1; digit3 < 10; digit3++) 17 | { 18 | 19 | putchar((digit1 % 10) + '0'); 20 | putchar((digit2 % 10) + '0'); 21 | putchar((digit3 % 10) + '0'); 22 | 23 | if (digit1 == 7 && digit2 == 8 && digit3 == 9) 24 | continue; 25 | 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - Prints all combinations of two two digits with, 4 | * and space followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit1, digit2; 11 | 12 | for (digit1 = 0; digit1 <= 98; digit1++) 13 | { 14 | for (digit2 = digit1 + 1; digit2 <= 99; digit2++) 15 | { 16 | putchar((digit1 / 10) + '0'); 17 | putchar((digit1 % 10) + '0'); 18 | putchar(32); 19 | putchar((digit2 / 10) + '0'); 20 | putchar((digit2 % 10) + '0'); 21 | 22 | if (digit1 / 10 != 9 || digit1 % 10 != 8) 23 | { 24 | putchar(44); 25 | putchar(32); 26 | } 27 | } 28 | } 29 | putchar(10); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Program to print alphabet letter followed by new line 5 | * 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | int letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | { 14 | letter = tolower(letter); 15 | putchar(letter); 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 - Program to print alphabet letters in all cases followed by new line 5 | * 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | int letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | { 14 | letter = tolower(letter); 15 | putchar(letter); 16 | if (letter == 'z') 17 | { 18 | letter = 'A'; 19 | for (; letter <= 'Z'; letter++) 20 | { 21 | putchar(letter); 22 | } 23 | break; 24 | } 25 | } 26 | putchar('\n'); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Program to print alphabet letters except q and e followed by new line 5 | * 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | int letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | { 14 | if (letter == 'q' || letter == 'e') 15 | { 16 | continue; 17 | } 18 | putchar(letter); 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Prints all single digits of base 10 followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit; 11 | 12 | for (digit = '0'; digit <= '9'; digit++) 13 | { 14 | putchar(digit); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Prints all single digits of base 10 followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit; 11 | 12 | for (digit = '0'; digit <= '9'; digit++) 13 | { 14 | putchar(digit); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Program to print alphabet letters in reverse followed by new line 5 | * 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | int letter; 11 | 12 | for (letter = 'z'; letter >= 'a'; letter--) 13 | { 14 | putchar(letter); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Prints all single digits of base 16 followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit; 11 | 12 | for (digit = '0'; digit <= '9'; digit++) 13 | { 14 | putchar(digit); 15 | if (digit == '9') 16 | { 17 | digit = 'a'; 18 | for (; digit <= 'f'; digit++) 19 | { 20 | putchar(digit); 21 | } 22 | break; 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 - Prints all single digits with , and space followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit; 11 | 12 | for (digit = '0'; digit <= '9'; digit++) 13 | { 14 | putchar(digit); 15 | if (digit == '9') 16 | continue; 17 | 18 | putchar(','); 19 | putchar(' '); 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/tester: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x01-variables_if_else_while/tester -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.gitignore: -------------------------------------------------------------------------------- 1 | main.c 2 | _putchar.c 3 | tests/ 4 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-holberton.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *main - function prints text as output 5 | * 6 | *Return:return 0 7 | */ 8 | int main(void) 9 | { 10 | char school[10] = "_putchar"; 11 | 12 | int i; 13 | 14 | for (i = 0; i < 8; i++) 15 | { 16 | _putchar(school[i]); 17 | } 18 | _putchar(10); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_alphabet - function that print alphabet 4 | *it prints in lowercase 5 | *Return: no return 6 | */ 7 | void print_alphabet(void) 8 | { 9 | int letter; 10 | for (letter = 'a'; letter <= 'z'; letter++) 11 | { 12 | _putchar (letter); 13 | } 14 | _putchar (10); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *add - sums up two numbers 4 | *@n:first integer 5 | *@m:second integer 6 | *Return: returns 0 7 | */ 8 | int add(int n, int m) 9 | { 10 | return (n + m); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_times_table - prints the times table with 4 | *parameter 5 | *@n: parameter 6 | *Return: returns nothing 7 | */ 8 | void print_times_table(int n) 9 | { 10 | int digit, mult, result; 11 | if (n <= 15 && n >= 0) 12 | { 13 | for (digit = 0; digit <= n; digit++) 14 | { 15 | _putchar('0'); 16 | 17 | for (mult = 1; mult <= n; mult++) 18 | { 19 | _putchar(','); 20 | _putchar(' '); 21 | result = digit * mult; 22 | if (result <= 99) 23 | _putchar(' '); 24 | 25 | if (result <= 9) 26 | _putchar(' '); 27 | if (result >= 100) 28 | { 29 | _putchar((result / 100) + '0'); 30 | _putchar((result / 10) % 10 + '0'); 31 | } 32 | else if (result <= 99 && result >= 10) 33 | { 34 | _putchar((result / 10) + '0'); 35 | } 36 | _putchar((result % 10) + '0'); 37 | } 38 | _putchar('\n'); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - prints count of multiples 4 | *of 3 or 5 below 1024 5 | *Return: return 0 6 | */ 7 | int main(void) 8 | { 9 | int n, sum = 0; 10 | for (n = 0; n < 1024; n++) 11 | { 12 | if ((n % 3) == 0 || (n % 5) == 0) 13 | sum += n; 14 | } 15 | printf("%d\n", sum); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - prints out first 50 4 | *fibonacci suit numbers 5 | *Return: return 0 6 | */ 7 | int main(void) 8 | { 9 | int inc; 10 | unsigned long n1 = 0, n2 = 1, n3; 11 | for (inc = 0; inc < 50; inc++) 12 | { 13 | n3 = n1 + n2; 14 | printf("%lu", n3); 15 | n1 = n2; 16 | n2 = n3; 17 | 18 | if (inc == 49) 19 | printf("\n"); 20 | else 21 | printf(", "); 22 | 23 | } 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - prints sum of even fibonacci 4 | *suit elements 5 | *fibonacci suit numbers 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | unsigned long n1 = 0, n2 = 1, n3 = 0, sum = 0; 11 | while (n3 <= 4000000) 12 | { 13 | n3 = n1 + n2; 14 | n1 = n2; 15 | n2 = n3; 16 | 17 | if ((n1 % 2) == 0) 18 | sum += n1; 19 | 20 | } 21 | printf("%ld\n", sum); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - prints out first 98 4 | *fibonacci suit numbers 5 | *Return: return 0 6 | */ 7 | int main(void) 8 | { 9 | int inc; 10 | unsigned long n1 = 0, n2 = 1, n3; 11 | unsigned long n1_h1, n1_h2, n2_h1, n2_h2; 12 | unsigned long h1, h2; 13 | 14 | for (inc = 0; inc < 92; inc++) 15 | { 16 | n3 = n1 + n2; 17 | printf("%lu, ", n3); 18 | n1 = n2; 19 | n2 = n3; 20 | } 21 | n1_h1 = n1 / 10000000000; 22 | n2_h1 = n2 / 10000000000; 23 | n1_h2 = n1 % 10000000000; 24 | n2_h2 = n2 % 10000000000; 25 | for (inc = 93; inc < 99; inc++) 26 | { 27 | h1 = n1_h1 + n2_h1; 28 | h2 = n1_h2 + n2_h2; 29 | if ((n1_h2 + n2_h2) > 9999999999) 30 | { 31 | h1 += 1; 32 | h2 %= 10000000000; 33 | } 34 | printf("%lu%lu", h1, h2); 35 | if (inc != 98) 36 | printf(", "); 37 | 38 | n1_h1 = n2_h1; 39 | n1_h2 = n2_h2; 40 | n2_h1 = h1; 41 | n2_h2 = h2; 42 | } 43 | printf("\n"); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *print_to_98 - prints natural 5 | *numbers from n to 98 6 | *@n:integer to start by 7 | *return: returns nothing 8 | */ 9 | void print_to_98(int n) 10 | { 11 | if (n >= 98) 12 | { 13 | while (n > 98) 14 | printf("%d, ", n--); 15 | 16 | printf("%d\n", n); 17 | } 18 | else 19 | { 20 | while (n < 98) 21 | printf("%d, ", n++); 22 | 23 | printf("%d\n", n); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_alphabet_x10 - prints alphabet letters 4 | * ten times 5 | *desc: The function uses loop to operate 6 | *Return: no return 7 | */ 8 | void print_alphabet_x10(void) 9 | { 10 | int counter = 0; 11 | int letter; 12 | while (counter++ <= 9) 13 | { 14 | for (letter = 'a'; letter <= 'z'; letter++) 15 | _putchar(letter); 16 | 17 | _putchar(10); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_islower - controls if a character is in lowercase 4 | *@c: character to be verified 5 | *Return: return 0 or 1 6 | */ 7 | int _islower(int c) 8 | { 9 | if (c >= 'a' && c <= 'z') 10 | return (1); 11 | 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isalpha - controls if a character is alphabetical 4 | *@c: character to be verified 5 | *Return: return 0 or 1 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 10 | return (1); 11 | 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_sign-prints sign of a number 4 | *@n: character to be verified 5 | *Return: return 0 or 1 6 | */ 7 | int print_sign(int n) 8 | { 9 | if (n == 0) 10 | { 11 | _putchar('0'); 12 | return (0); 13 | } 14 | else if (n > 0) 15 | { 16 | _putchar('+'); 17 | return (1); 18 | } 19 | else 20 | { 21 | _putchar('-'); 22 | return (-1); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - prints the absolute value of a number 4 | *@i: number targeted 5 | *Return: returns 0 6 | */ 7 | int _abs(int i) 8 | { 9 | if (i > 0) 10 | return (i); 11 | else if (i < 0) 12 | return (-i); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_last_digit - function that print last digit of a number 4 | *@n: number to be targeted 5 | *Return: returns 0 6 | */ 7 | int print_last_digit(int n) 8 | { 9 | int lastdigit = n % 10; 10 | if (lastdigit < 0) 11 | lastdigit *= -1; 12 | 13 | _putchar (lastdigit + '0'); 14 | return (lastdigit); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *jack_bauer - prints each minute of the day 4 | * 5 | *Return:returns 0 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int min, hour; 10 | for (hour = 0; hour <= 23; hour++) 11 | { 12 | for (min = 0; min <= 59; min++) 13 | { 14 | _putchar((hour / 10) + '0'); 15 | _putchar((hour % 10) + '0'); 16 | _putchar(':'); 17 | _putchar((min / 10) + '0'); 18 | _putchar((min % 10) + '0'); 19 | _putchar(10); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *times_table - prints the 9 times table 4 | * 5 | *Return: returns nothing 6 | */ 7 | void times_table(void) 8 | { 9 | int digit, mult, result; 10 | for (digit = 0; digit <= 9; digit++) 11 | { 12 | _putchar('0'); 13 | 14 | for (mult = 1; mult <= 9; mult++) 15 | { 16 | _putchar(','); 17 | _putchar(' '); 18 | result = digit * mult; 19 | if (result <= 9) 20 | _putchar(' '); 21 | else 22 | _putchar((result / 10) + '0'); 23 | 24 | _putchar((result % 10) + '0'); 25 | } 26 | _putchar('\n'); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | *File:main.h 4 | *Author:Pericles Adjovi 5 | *Desc:header file containing functions declaration 6 | */ 7 | 8 | void print_alphabet(void); 9 | void jack_bauer(void); 10 | void print_alphabet_x10(void); 11 | void times_table(); 12 | void print_to_98(int); 13 | void print_times_table(int); 14 | int _putchar(char); 15 | int _islower(int c); 16 | int _isalpha(int c); 17 | int print_sign(int n); 18 | int _abs(int); 19 | int add(int n, int m); 20 | int print_last_digit(int n); 21 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | /* 16 | *while (i < 10) 17 | *{ 18 | *putchar(i); 19 | *} 20 | */ 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | if (a >= b && a >= c) 15 | { 16 | largest = a; 17 | } 18 | else if (b >= a && b >= c) 19 | { 20 | largest = b; 21 | } 22 | else 23 | { 24 | largest = c; 25 | } 26 | return (largest); 27 | } 28 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if ((year % 4 == 0) && (year % 400 == 0 || year % 100 != 0)) 16 | { 17 | if (month >= 3 && 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 | 0x03. C - Debugging 2 | =================== 3 | 4 | - By Carrie Ybay 5 | - Ongoing project - started 08-31-2021, must end by 09-03-2021 (in 1 day) - you're done with 0% of tasks. 6 | - Checker was released at 09-01-2021 12:00 PM 7 | - QA review fully automated. 8 | 9 | Resources 10 | --------- 11 | 12 | **Read or watch:** 13 | 14 | - [Debugging](https://alx-intranet.hbtn.io/rltoken/faGcpiJiejHH6GhqpmbhUw "Debugging") 15 | - [Rubber Duck Debugging](https://alx-intranet.hbtn.io/rltoken/RaecqJBNkmZ92vLMpNDuGg "Rubber Duck Debugging") 16 | 17 | Debugging is the process of finding and fixing errors in software that prevents it from running correctly. As you become a more advanced programmer and an industry engineer, you will learn how to use debugging tools such as `gdb` or built-in tools that IDEs have. However, it's important to understand the concepts and processes of debugging manually. 18 | 19 | ![](https://s3.amazonaws.com/alx-intranet.hbtn.io/uploads/medias/2019/5/af682f2cbb6d73fd4e42.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIARDDGGGOU65GPZGY3%2F20210901%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210901T181220Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=e1b094ca0dcfc4a561ddbf396c0224cdb014cb52c8d3d03efe6fc24e372e6573) 20 | 21 | Learning Objectives 22 | ------------------- 23 | 24 | At the end of this project, you are expected to be able to [explain to anyone](https://alx-intranet.hbtn.io/rltoken/fbQbYSz0Qxw5MEyb6yR05w "explain to anyone"), without the help of Google: 25 | 26 | ### General 27 | 28 | - What is debugging 29 | - What are some methods of debugging manually 30 | - How to read the error messages 31 | 32 | Requirements 33 | ------------ 34 | 35 | ### General 36 | 37 | - Allowed editors: `vi`, `vim`, `emacs` 38 | - All your files will be compiled on Ubuntu 20.04 LTS using `gcc`, using the options `-Wall -Werror -Wextra -pedantic -std=gnu89` 39 | - All your files should end with a new line 40 | - Your code should use the `Betty` style. It will be checked using `betty-style.pl` and `betty-doc.pl` 41 | - A README.md file at the root of the repo, containing a description of the repository 42 | - A README.md file, at the root of the folder of this project (i.e. `0x03-debugging`), describing what this project is about 43 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int, int, int); 8 | void print_remaining_days(int, int, int); 9 | int convert_day(int, int); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/.gitignore: -------------------------------------------------------------------------------- 1 | _putchar.c 2 | main.c 3 | tests/ 4 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isupper - function that verifies if a character is uppercase or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isupper(int c) 9 | { 10 | if ((c >= 'A') && (c <= 'Z')) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isdigit - function that verifies if a character is a digit or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isdigit(int c) 9 | { 10 | if ((c >= 48) && (c <= 57)) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_triangle - prints a triangle 4 | *@size:size parameter of triangle 5 | *Return:returns nothing 6 | */ 7 | void print_triangle(int size) 8 | { 9 | int inc1, inc2; 10 | 11 | if (size > 0) 12 | { 13 | for (inc1 = 1; inc1 <= size; inc1++) 14 | { 15 | for ((inc2 = size - inc1); inc2 > 0; inc2--) 16 | _putchar(' '); 17 | 18 | for (inc2 = 0; inc2 < inc1; inc2++) 19 | _putchar('#'); 20 | 21 | if (inc1 == size) 22 | continue; 23 | 24 | _putchar('\n'); 25 | } 26 | } 27 | _putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main- prints the largest prime factor 5 | *of a number 6 | * 7 | *Return: returns 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | long number = 612852475143; 13 | int inc; 14 | while (inc++ < number / 2) 15 | { 16 | if (number % inc == 0) 17 | { 18 | number /= 2; 19 | continue; 20 | } 21 | 22 | for (inc = 3; inc < number / 2; inc += 2) 23 | { 24 | if (number % inc == 0) 25 | number /= inc; 26 | 27 | } 28 | } 29 | printf("%ld\n", number); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_number - Prints a number 4 | * @n: The number to print 5 | * 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int num = n; 10 | if (n < 0) 11 | { 12 | _putchar('-'); 13 | num = -num; 14 | } 15 | if (num > 9) 16 | { 17 | print_number(num / 10); 18 | } 19 | _putchar(num % 10 + '0'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *mul - function that multipliestwo numbers 4 | *@a: first number 5 | *@b: second number 6 | *Return: returns result 7 | */ 8 | 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_numbers - function that print all numbers from 4 | *0 to 9 5 | * 6 | *Return: returns nothing 7 | */ 8 | void print_numbers(void) 9 | { 10 | int number = 48; 11 | for (number = 48; number < 58; number++) 12 | { 13 | _putchar(number); 14 | } 15 | _putchar(10); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_most_numbers - prints numbers except 2 and 4 4 | * 5 | *Return: returns nothing 6 | */ 7 | void print_most_numbers(void) 8 | { 9 | int number; 10 | for (number = 48; number < 58; number++) 11 | { 12 | if ((number == 50) || (number == 52)) 13 | { 14 | continue; 15 | } 16 | _putchar(number); 17 | } 18 | _putchar(10); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *more_numbers - prints numbers to 14 4 | * 5 | *Return: returns nothing 6 | */ 7 | void more_numbers(void) 8 | { 9 | int number1, number2; 10 | 11 | for (number1 = 0; number1 < 10; number1++) 12 | { 13 | for (number2 = 0; number2 <= 14; number2++) 14 | 15 | { 16 | if (number2 > 9) 17 | { 18 | _putchar((number2 / 10) + '0'); 19 | } 20 | _putchar((number2 % 10) + '0'); 21 | } 22 | _putchar(10); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_line - prints a straight line 4 | *@n:parameter 5 | *Return:returns nothing 6 | */ 7 | void print_line(int n) 8 | { 9 | while (n-- > 0) 10 | { 11 | _putchar('_'); 12 | } 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_diagonal - prints a diagonal 4 | *@n:parameter 5 | *Return:returns nothing 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | int len, space; 10 | if (n > 0) 11 | { 12 | for (len = 0; len < n; len++) 13 | { 14 | for (space = 0; space < len; space++) 15 | _putchar(' '); 16 | 17 | _putchar('\\'); 18 | 19 | if (len == (n - 1)) 20 | continue; 21 | _putchar('\n'); 22 | } 23 | } 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_square - prints squares 4 | *@size: parameter 5 | *Return:returns nothing 6 | */ 7 | void print_square(int size) 8 | { 9 | int inc1, inc2; 10 | if (size > 0) 11 | { 12 | for (inc1 = 0; inc1 < size; inc1++) 13 | { 14 | for (inc2 = 0; inc2 < (size - 1); inc2++) 15 | _putchar('#'); 16 | 17 | _putchar('#'); 18 | _putchar('\n'); 19 | } 20 | } 21 | else 22 | { 23 | _putchar('\n'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - program that prints either number 4 | *or fizz or buzz or fizzBuzz 5 | * 6 | * 7 | *Return: returns 0 8 | */ 9 | int main(void) 10 | { 11 | int num; 12 | while (num++ < 100) 13 | 14 | if ((num % 3 == 0) && (num % 5 == 0)) 15 | printf("FizzBuzz "); 16 | 17 | else if ((num % 3) == 0) 18 | printf("Fizz "); 19 | 20 | else if ((num % 5) == 0) 21 | { 22 | if (num != 100) 23 | printf("Buzz "); 24 | 25 | else 26 | printf("Buzz"); 27 | } 28 | 29 | else 30 | printf("%d ", num); 31 | 32 | printf("\n"); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | /** 4 | *main - header file for protoypes 5 | *void protoypes (void) 6 | *int protoypes (int) 7 | */ 8 | void print_numbers(void); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | int _putchar(char); 17 | int _isupper(int); 18 | int _isdigit(int); 19 | int mul(int, int); 20 | 21 | #endif /* MAIN_H */ 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ 2 | main.c 3 | _putchar.c 4 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *reset_to_98 - reset value of an integer to 98 4 | *@n: parameter targeted 5 | *Return : nothing 6 | */ 7 | 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *swap_int - swap two integers 4 | *@a: first integer 5 | *@b: second integer 6 | *Return: nothing 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | int tmp = *a; 12 | *a = *b; 13 | *b = tmp; 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _atoi - changes a string to an int 3 | * @s: the string to be changed 4 | * 5 | * Return: the converted int 6 | */ 7 | int _atoi(char *s) 8 | { 9 | int i = 1; 10 | unsigned int num = 0; 11 | do { 12 | if (*s == '-') 13 | i *= -1; 14 | else if (*s >= '0' && *s <= '9') 15 | num = num * 10 + (*s - '0'); 16 | else if (num > 0) 17 | break; 18 | } while (*s++); 19 | return (num *i); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - entry point generates a random password 6 | * 7 | * Return: the generated password 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | int x; 13 | 14 | srand(time(0)); 15 | while (x <= 2645) 16 | { 17 | c = rand() % 128; 18 | x += c; 19 | putchar(c); 20 | } 21 | putchar(2772 - x); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strlen - returns the length of a string 4 | * @s: string 5 | *Return: returns lenght; 6 | */ 7 | int _strlen(char *s) 8 | { 9 | int count, inc; 10 | inc = 0; 11 | for (count = 0; s[count] != '\0'; count++) 12 | inc++; 13 | 14 | return (inc); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_puts - prints a string 4 | * @str: string to print 5 | * 6 | * Description: prints a string 7 | * On success: return the number of characters printed 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | while (*str) 13 | _putchar(*str++); 14 | 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_rev - prints a string in reverse 4 | * @s: string to print 5 | */ 6 | 7 | void print_rev(char *s) 8 | { 9 | int len = 0, index = 0; 10 | len = _strlen(s); 11 | 12 | for (index = len - 1; index >= 0; index--) 13 | _putchar(s[index]); 14 | 15 | _putchar('\n'); 16 | } 17 | 18 | /** 19 | *_strlen - returns the length of a string 20 | * @s: string 21 | *Return: returns lenght; 22 | */ 23 | int _strlen(char *s) 24 | { 25 | int count, inc; 26 | inc = 0; 27 | for (count = 0; s[count] != '\0'; count++) 28 | inc++; 29 | 30 | return (inc); 31 | } 32 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *rev_string - Reverse a string 4 | *@s: String to reverse 5 | *Return: Nothing 6 | */ 7 | void rev_string(char *s) 8 | { 9 | int i = 0, length; 10 | length = _strlen(s) - 1; 11 | while (length > i) 12 | { 13 | swap_char(s + length, s + i); 14 | i++; 15 | length--; 16 | } 17 | } 18 | 19 | /** 20 | *_strlen - returns the length of a string 21 | * @s: string 22 | *Return: returns lenght; 23 | */ 24 | int _strlen(char *s) 25 | { 26 | int count, inc; 27 | inc = 0; 28 | for (count = 0; s[count] != '\0'; count++) 29 | inc++; 30 | 31 | return (inc); 32 | } 33 | 34 | /** 35 | *swap_char - swap two characters 36 | *@a: first character 37 | *@b: second character 38 | *Return: nothing 39 | */ 40 | 41 | void swap_char(char *a, char *b) 42 | { 43 | char tmp = *a; 44 | *a = *b; 45 | *b = tmp; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints part of a string 5 | * @str: string to be printed 6 | * 7 | * Return: void 8 | **/ 9 | void puts2(char *str) 10 | { 11 | int i = 0; 12 | while (str[i] != '\0') 13 | { 14 | if (i % 2 == 0) 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | 19 | _putchar('\n'); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *puts_half - puts half of a string 4 | *@str: string 5 | *Return:void 6 | */ 7 | void puts_half(char *str) 8 | { 9 | int len = _strlen(str); 10 | 11 | if (len % 2 == 0) 12 | len = len / 2; 13 | 14 | else 15 | len = (len + 1) / 2; 16 | 17 | while (str[len] != '\0') 18 | { 19 | _putchar(str[len]); 20 | len++; 21 | } 22 | _putchar('\n'); 23 | } 24 | 25 | 26 | /** 27 | *_strlen - returns the length of a string 28 | * @s: string 29 | *Return: returns lenght; 30 | */ 31 | int _strlen(char *s) 32 | { 33 | int count, inc; 34 | inc = 0; 35 | for (count = 0; s[count] != '\0'; count++) 36 | inc++; 37 | 38 | return (inc); 39 | } 40 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_array - print n element of array 4 | *@a: array 5 | *@n: number of elements 6 | *Return: nothing 7 | */ 8 | void print_array(int *a, int n) 9 | { 10 | int inc; 11 | for (inc = 0; inc < n ; inc++) 12 | { 13 | if (inc != n - 1) 14 | printf("%d, ", a[inc]); 15 | else 16 | printf("%d", a[inc]); 17 | 18 | } 19 | printf("\n"); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - Copy paste string 4 | *@dest: destination 5 | *@src: source 6 | *Return: dest 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int inc = 0; 11 | while (*(src + inc) != '\0') 12 | { 13 | *(dest + inc) = *(src + inc); 14 | inc++; 15 | } 16 | *(dest + inc) = '\0'; 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | 5 | /** 6 | * void prototypes() 7 | *char prototypes() 8 | * int prototypes() 9 | */ 10 | void reset_to_98(int *n); 11 | void _puts(char *str); 12 | void print_rev(char *s); 13 | void rev_string(char *s); 14 | void swap_int(int *a, int *b); 15 | void swap_char(char *a, char *b); 16 | void puts2(char *str); 17 | void puts_half(char *str); 18 | void print_array(int *a, int n); 19 | char *_strcpy(char *dest, char *src); 20 | int _putchar(char c); 21 | int _strlen(char *s); 22 | int _atoi(char *s); 23 | #endif /*MAIN_H*/ -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ 2 | main.c 3 | _putchar.c 4 | .vscode/ -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strcat - concatenates the string pointed to by @src to 5 | * the end of the string pointed to by @dest 6 | *@dest: String that will be appended 7 | *@src: String to be concatenated upon 8 | * 9 | * Return: returns poiner to @dest 10 | */ 11 | 12 | char *_strcat(char *dest, char *src) 13 | { 14 | 15 | int index = 0, dest_len = 0; 16 | 17 | while (dest[index++]) 18 | dest_len++; 19 | 20 | for (index = 0; src[index]; index++) 21 | dest[dest_len++] = src[index]; 22 | 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strncat - concatenate two strings but add inputted number of bytes 4 | *@dest: string to be appended upon 5 | *@src: string to be completed at end of dest 6 | *@n:integer parameter to compare index to 7 | *Return: returns new concatenated string 8 | */ 9 | 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | 13 | int index = 0, dest_len = 0; 14 | 15 | while (dest[index++]) 16 | dest_len++; 17 | 18 | for (index = 0; src[index] && index < n; index++) 19 | dest[dest_len++] = src[index]; 20 | 21 | return (dest); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string using rot13 5 | * @str:the string targeted 6 | *Return: returns the encoded string 7 | */ 8 | 9 | char *rot13(char *str) 10 | { 11 | int index1, index2; 12 | 13 | char alphabet[52] = {'A', 'B', 'C', 'D', 'E', 'F', 14 | 'G', 'H', 'I', 'J', 'K', 'L', 15 | 'M', 'N', 'O', 'P', 'Q', 'R', 16 | 'S', 'T', 'U', 'V', 'W', 'X', 17 | 'Y', 'Z', 'a', 'b', 'c', 'd', 18 | 'e', 'f', 'g', 'h', 'i', 'j', 19 | 'k', 'l', 'm', 'n', 'o', 'p', 20 | 'q', 'r', 's', 't', 'u', 'v', 21 | 'w', 'x', 'y', 'z'}; 22 | 23 | char rot13key[52] = {'N', 'O', 'P', 'Q', 'R', 'S', 24 | 'T', 'U', 'V', 'W', 'X', 'Y', 25 | 'Z', 'A', 'B', 'C', 'D', 'E', 26 | 'F', 'G', 'H', 'I', 'J', 'K', 27 | 'L', 'M', 'n', 'o', 'p', 'q', 28 | 'r', 's', 't', 'u', 'v', 'w', 29 | 'x', 'y', 'z', 'a', 'b', 'c', 30 | 'd', 'e', 'f', 'g', 'h', 'i', 31 | 'j', 'k', 'l', 'm'}; 32 | while (str[++index1]) 33 | { 34 | for (index2 = 0; index2 < 52; index2++) 35 | { 36 | if (str[index1] == alphabet[index2]) 37 | { 38 | str[index1] = rot13key[index2]; 39 | break; 40 | } 41 | } 42 | } 43 | return (str); 44 | } 45 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_number - prints an integer 4 | *@n:integer to be printed 5 | * 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int num = n; 10 | 11 | if (n < 0) 12 | { 13 | _putchar('-'); 14 | num = -num; 15 | } 16 | 17 | if ((num / 10) > 0) 18 | print_number(num / 10); 19 | 20 | _putchar((num % 10) + '0'); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /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 | 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * infinite_add - C function that adds two numbers stored 5 | *in strings to a buffer. 6 | *Assumes that strings are never empty and 7 | *that numbers will always be positive, or 0. 8 | *Assumes there are only digits stored in the number strings. 9 | *If result can be stored in the buffer, 10 | *returns a pointer to the result. 11 | *If result cannot be stored in the buffer, returns `0`. 12 | *@n1:first number to be added 13 | *@n2:second number to be added 14 | *@r: store result 15 | *@size_r: size of buffer 16 | *Return:returns pointer to result 17 | */ 18 | 19 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 20 | { 21 | } 22 | 23 | /** 24 | * add_strings - Adds the numbers stored in two strings. 25 | * @n1: The string containing the first number to be added. 26 | * @n2: The string containing the second number to be added. 27 | * @r: The buffer to store the result. 28 | * @r_index: The current index of the buffer. 29 | * 30 | * Return: If r can store the sum - a pointer to the result. 31 | * If r cannot store the sum - 0. 32 | */ 33 | 34 | char *add_strings(char *n1, char *n2, char *r, int r_index) 35 | { 36 | int num, tens = 0; 37 | 38 | for (; *n1 && *n2; n1--, n2--, r_index--) 39 | { 40 | num = (*n1 - '0') + (*n2 - '0'); 41 | num += tens; 42 | *(r + r_index) = (num % 10) + '0'; 43 | tens = num / 10; 44 | } 45 | 46 | for (; *n1; n1--; r_index++) 47 | { 48 | num = *(n1 - '0') + tens; 49 | *(r + r_index) = (num % 10) + '0'; 50 | tens = num / 10; 51 | } 52 | 53 | for (; *n2; n2--; r_index--) 54 | { 55 | num = (*n2 - '0') + tens; 56 | *(r + r_index) = (num % 10) + '0'; 57 | tens = num / 10; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_buffer - C function that prints the content of an 4 | * inputted number of bytes from a buffer. 5 | * Prints 10 bytes per line. 6 | * Starts with the position of the first byte in hexadecimal (8 chars), 7 | * starting with `0`. 8 | * Each line shows the hexadecimal content (2 chars) of the buffer, 9 | * 2 bytes at a time, separated by a space. 10 | * Each line shows the content of the buffer. 11 | * Prints the byte if it is printable; if not, prints `.`. 12 | * Each line ends with a new line `\n`. 13 | * If the inputted byte size is 0 or less, the function only prints a new line. 14 | *@b: number of bytes 15 | *@size: size of the byte 16 | */ 17 | void print_buffer(char *b, int size) 18 | { 19 | int i = 0, j; 20 | 21 | if (size < 0) 22 | { 23 | printf('\n'); 24 | return; 25 | } 26 | 27 | while (i < size) 28 | { 29 | if (i % 10 == 0) 30 | printf("%08x: ", i); 31 | for (j = i; j < i + 9; j += 2) 32 | { 33 | if ((j < size) && ((j + 1) < size)) 34 | printf("%02x%02x: ", b[j], b[j + 1]); 35 | else 36 | { 37 | while (++j <= i + 10) 38 | printf(" "); 39 | printf(" "); 40 | } 41 | } 42 | for (j = i; j < i + 9 && j < size; j++) 43 | { 44 | if (b[j] >= 32 && b[j] <= 126) 45 | printf("%c", b[j]); 46 | else 47 | printf("."); 48 | } 49 | printf('\n'); 50 | i += 10; 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - C function that copies a string, including the 4 | * terminating null byte, using at most an inputted number of bytes. 5 | * If the length of the source string is less than the maximum byte number, 6 | * the remainder of the destination string is filled with null bytes. 7 | * Works identically to the standard library function `strncpy`. 8 | *@dest: buffer storing the string copy 9 | *@src:the source string 10 | *@n:max nummber of byte copied 11 | *Return: returns 12 | */ 13 | 14 | char *_strncpy(char *dest, char *src, int n) 15 | { 16 | int i; 17 | 18 | for (i = 0; i < n && src[i] != '\0'; i++) 19 | dest[i] = src[i]; 20 | 21 | for ( ; i < n; i++) 22 | dest[i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Function which compare two strings and 5 | *@s1: first string 6 | *@s2:second string 7 | *Return: 8 | * returns zero if s1 == s2 9 | * returns negative number if s1 < s2 10 | * returns positive number if s1 > s2 11 | */ 12 | 13 | int _strcmp(char *s1, char *s2) 14 | { 15 | int i = 0, diff = 0; 16 | 17 | while (1) 18 | { 19 | if (s1[i] == '\0' && s2[i] == '\0') 20 | break; 21 | else if (s1[i] == '\0') 22 | { 23 | diff = s2[i]; 24 | break; 25 | } 26 | else if (s2[i] == '\0') 27 | { 28 | diff = s1[i]; 29 | break; 30 | } 31 | else if (s1[i] != s2[i]) 32 | { 33 | diff = s1[i] - s2[i]; 34 | break; 35 | } 36 | else 37 | i++; 38 | 39 | } 40 | return (diff); 41 | } 42 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - function that reverse content of array 5 | * @a: array 6 | * @n: number of elements 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int tmp, index; 12 | for (index = n - 1; index > n / 2; index--) 13 | { 14 | tmp = a[n - 1 - index]; 15 | a[n - 1 - index] = a[index]; 16 | a[index] = tmp; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *string_toupper - changes all lowercases letters of a string to uppercase 5 | *@str: parameter 6 | *Return: returns a character 7 | */ 8 | 9 | char *string_toupper(char *str) 10 | { 11 | int index = 0; 12 | 13 | while (str[index++]) 14 | { 15 | if (str[index] >= 'a' && str[index] <= 'z') 16 | str[index] -= 32; 17 | } 18 | 19 | return (str); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *cap_string - function that capitalize first character of a word 4 | *@str: string to capitalize 5 | *Return:returns the capitalized string 6 | */ 7 | char *cap_string(char *str) 8 | { 9 | int index = 0; 10 | 11 | while (str[++index]) 12 | { 13 | while (!(str[index] >= 'a' && str[index] <= 'z')) 14 | index++; 15 | 16 | if (str[index - 1] == ' ' || 17 | str[index - 1] == '\t' || 18 | str[index - 1] == '\n' || 19 | str[index - 1] == ',' || 20 | str[index - 1] == ';' || 21 | str[index - 1] == '.' || 22 | str[index - 1] == '!' || 23 | str[index - 1] == '?' || 24 | str[index - 1] == '"' || 25 | str[index - 1] == '(' || 26 | str[index - 1] == ')' || 27 | str[index - 1] == '{' || 28 | str[index - 1] == '}') 29 | str[index] -= 32; 30 | } 31 | return (str); 32 | } 33 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - function that encode a string 4 | *@str:string that will be encoded 5 | *Return:returns encoded string 6 | */ 7 | 8 | char *leet(char *str) 9 | { 10 | int index1 = 0, index2; 11 | char leet[8] = {'O', 'L', '?', 'E', 'A', '?', '?', 'T'}; 12 | 13 | while (str[++index1]) 14 | { 15 | for (index2 = 0; index2 <= 7; index2++) 16 | { 17 | if (str[index1] == leet[index2] || 18 | str[index1] - 32 == leet[index2]) 19 | str[index1] = index2 + '0'; 20 | } 21 | } 22 | return (str); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | /** 5 | *void prototypes() 6 | *int prototypes() 7 | *char prototypes() 8 | */ 9 | void reverse_array(int *a, int n); 10 | void print_number(int n); 11 | void print_buffer(char *b, int size); 12 | int _strcmp(char *s1, char *s2); 13 | char *_strcat(char *dest, char *src); 14 | char *_strncat(char *dest, char *src, int n); 15 | char *_strncpy(char *dest, char *src, int n); 16 | char *string_toupper(char *); 17 | char *cap_string(char *); 18 | char *leet(char *); 19 | char *rot13(char *); 20 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 21 | char *add_strings(char *n1, char *n2, char *r, int r_index); 22 | 23 | #endif /*MAIN_H*/ -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/.gitignore: -------------------------------------------------------------------------------- 1 | main.c 2 | tests/ 3 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memset - The _memset() function fills 4 | * the first n bytes of the memory area 5 | * pointed to by s with the constant byte b 6 | *@s:target 7 | *@b: constant byte 8 | *@n:number of byte 9 | *Return: returns new value of target 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | while (n) 15 | { 16 | s[n - 1] = b; 17 | n--; 18 | } 19 | return (s); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memcpy - The _memcpy() function copies n bytes 4 | * from memory area src to memory area dest 5 | *@dest:area where bytes are copied to 6 | *@src:area where bytes are copied from 7 | *@n:number of bytes to copy 8 | *Return: returns a pointer to n 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i = 0; 13 | 14 | for (; i < n; i++) 15 | dest[i] = src[i]; 16 | 17 | return (dest); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * set_string - a function that sets the value of a pointer to a char. 4 | *@s: pointer value 5 | *@to: value pointed to 6 | * 7 | */ 8 | void set_string(char **s, char *to) 9 | { 10 | *s = to; 11 | } 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strchr - Returns a pointer to the first occurrence 4 | *of the character c in the string s, or NULL if the 5 | *character is not found 6 | * 7 | *@s:string targeted 8 | *@c:character targeted 9 | * 10 | *Return: returns pointer to first occcurence of c 11 | */ 12 | char *_strchr(char *s, char c) 13 | { 14 | int i; 15 | 16 | for (i = 0; (s[i] != c) && (s[i] != '\0'); i++) 17 | ; 18 | if (s[i] == c) 19 | return (s + i); 20 | else 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strspn - search the number of bytes in the initial 4 | * segment of s which consist only of bytes from accept 5 | *@s:segment targeted 6 | *@accept:reference bytes container 7 | * 8 | *Return:returns the number of bytes in the initial 9 | * segment of s which consist only of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int bytes = 0; 14 | int i; 15 | 16 | while (*s) 17 | { 18 | for (i = 0; accept[i]; i++) 19 | { 20 | if (accept[i] == *s) 21 | { 22 | bytes++; 23 | break; 24 | } 25 | else if ((accept[i + 1]) == '\0') 26 | return (bytes); 27 | } 28 | s++; 29 | } 30 | return (bytes); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strpbrk - The _strpbrk() function locates the first 4 | * occurrence in the string s of any of the bytes in 5 | *the string accept 6 | * 7 | *@s: string where search is made 8 | *@accept: string where searched bytes are located 9 | * 10 | *Return:Returns a pointer to the byte in s that matches 11 | * one of the bytes in accept, or NULL if no such byte is found 12 | */ 13 | 14 | char *_strpbrk(char *s, char *accept) 15 | { 16 | int i; 17 | 18 | while (*s) 19 | { 20 | for (i = 0; accept[i]; i++) 21 | { 22 | if (accept[i] == *s) 23 | return (s); 24 | } 25 | s++; 26 | } 27 | 28 | return ('\0'); 29 | } 30 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strstr - The _strstr() function finds the first occurrence 4 | * of the substring needle in the string haystack. 5 | * The terminating null bytes (\0) are not compared 6 | *@haystack: string where the search is made 7 | *@needle: string whose occurence is searched in haystack 8 | *Return:Returns a pointer to the beginning of the located 9 | * substring, or NULL if the substring is not found. 10 | */ 11 | 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | int i; 15 | 16 | if (*needle == 0) 17 | return (haystack); 18 | 19 | while (*haystack) 20 | { 21 | i = 0; 22 | 23 | if (haystack[i] == needle[i]) 24 | { 25 | do { 26 | if (needle[i + 1] == '\0') 27 | return (haystack); 28 | i++; 29 | } while (haystack[i] == needle[i]); 30 | } 31 | haystack++; 32 | } 33 | return ('\0'); 34 | } 35 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_chessboard - a function that prints the chessboard. 4 | *@a: array containing board elements 5 | *returns nothing 6 | */ 7 | 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; a[i][7]; i++) 13 | { 14 | for (j = 0; j < 8; j++) 15 | _putchar(a[i][j]); 16 | _putchar('\n'); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_diagsums - a function that prints the sum of 4 | * the two diagonals of a square matrix of integers. 5 | *@a:matrix to be summed 6 | *@size:size of the matrix 7 | */ 8 | void print_diagsums(int *a, int size) 9 | { 10 | int i, sum1 = 0, sum2 = 0; 11 | 12 | for (i = 0; i < size; i++) 13 | { 14 | sum1 += a[i]; 15 | a += size; 16 | } 17 | 18 | a -= size; 19 | 20 | for (i = 0; i < size; i++) 21 | { 22 | sum2 += a[i]; 23 | a -= size; 24 | } 25 | 26 | printf("%d, %d\n", sum1, sum2); 27 | } 28 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | #include 5 | /** 6 | *void prototypes(void) 7 | *int prototypes(int) 8 | *char prototypes(char) 9 | */ 10 | void print_chessboard(char (*a)[8]); 11 | void print_diagsums(int *a, int size); 12 | void set_string(char **s, char *to); 13 | unsigned int _strspn(char *s, char *accept); 14 | int _putchar(char c); 15 | char *_memset(char *s, char b, unsigned int n); 16 | char *_memcpy(char *dest, char *src, unsigned int n); 17 | char *_strchr(char *s, char c); 18 | char *_strpbrk(char *s, char *accept); 19 | char *_strstr(char *haystack, char *needle); 20 | #endif /* MAIN_H */ -------------------------------------------------------------------------------- /0x08-recursion/.gitignore: -------------------------------------------------------------------------------- 1 | main.c 2 | tests/ 3 | _putchar.c 4 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts_recursion - prints a string followed by a new line 4 | *@s:character to be printed 5 | *Return:returns void 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | _putchar('\n'); 11 | else 12 | { 13 | _putchar(s[0]); 14 | _puts_recursion(s + 1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _print_rev_recursion - function that prints a string in reverse. 4 | *@s: parameter string 5 | * 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s != '\0') 10 | { 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * is_palindrome - returns true if the given string is a palindrome 5 | *@s: string to check 6 | *Return: true if the given string is a palindrome 7 | */ 8 | 9 | int is_palindrome(char *s) 10 | { 11 | int index = 0; 12 | int len = find_strlen(s); 13 | 14 | if (!(*s)) 15 | return (1); 16 | 17 | return (check_palindrome(s, len, index)); 18 | } 19 | 20 | /** 21 | * find_strlen - Returns the length of a string. 22 | * @s: The string to be measured. 23 | * 24 | * Return: The length of the string. 25 | */ 26 | int find_strlen(char *s) 27 | { 28 | int len = 0; 29 | 30 | if (*(s + len)) 31 | { 32 | len++; 33 | len += find_strlen(s + len); 34 | } 35 | 36 | return (len); 37 | } 38 | 39 | /** 40 | * check_palindrome - Checks if a string is a palindrome. 41 | * @s: The string to be checked. 42 | * @len: The length of s. 43 | * @index: The index of the string to be checked. 44 | * 45 | * Return: If the string is a palindrome - 1. 46 | * If the string is not a palindrome - 0. 47 | */ 48 | int check_palindrome(char *s, int len, int index) 49 | { 50 | if (s[index] == s[len / 2]) 51 | return (1); 52 | 53 | if (s[index] == s[len - index - 1]) 54 | return (check_palindrome(s, len, index + 1)); 55 | 56 | return (0); 57 | } 58 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two strings and returns 1 if identical 5 | * @s1: string to be checked 6 | * @s2: pattern to be used 7 | * 8 | * Return: 1 if identical, 0 otherwise 9 | */ 10 | 11 | int wildcmp(char *s1, char *s2) 12 | { 13 | if (*s2 == '\0') 14 | return (*s1 == '\0'); 15 | if (*s2 == *s1) 16 | return (*s1 != '\0' && wildcmp(s1 + 1, s2 + 1)); 17 | if (*s2 == '*') 18 | return (wildcmp(s1, s2 + 1) || (*s1 != '\0' && wildcmp(s1 + 1, s2))); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen_recursion - gives length of a string 4 | * @s: string to be measured 5 | *Return: length of the string 6 | */ 7 | 8 | int _strlen_recursion(char *s) 9 | { 10 | int len = 0; 11 | 12 | if (*s != '\0') 13 | { 14 | len++; 15 | len += _strlen_recursion(s + 1); 16 | } 17 | return (len); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - searchs for the factorial of a number 5 | * @n: the number to find the factorial of 6 | * 7 | * Return: the factorial of the number 8 | */ 9 | 10 | int factorial(int n) 11 | { 12 | int next; 13 | 14 | if (n == 0) 15 | return (1); 16 | else if (n < 0) 17 | return (-1); 18 | 19 | next = factorial(n - 1); 20 | return (n * next); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _pow_recursion - function that returns the 4 | * value of x raised to the power of y 5 | *@x: the base 6 | *@y: the exponent 7 | *Return: the value of x raised to the power of y 8 | */ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | return (-1); 13 | else if (y == 0) 14 | return (1); 15 | return (_pow_recursion(x, y - 1) * x); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _sqrt_recursion - returns the natural square root of a number 5 | * @n: number to be used 6 | * 7 | * Return: the square root of n 8 | */ 9 | 10 | int _sqrt_recursion(int n) 11 | { 12 | if (n == 0 || n == 1) 13 | return (n); 14 | return (_sqrt(0, n)); 15 | } 16 | 17 | /** 18 | * _sqrt - returns the square root of a number 19 | * @n: test number 20 | * @x: squared number 21 | * 22 | * Return: the square root of n 23 | */ 24 | 25 | int _sqrt(int n, int x) 26 | { 27 | if (n > x / 2) 28 | return (-1); 29 | else if (n * n == x) 30 | return (n); 31 | return (_sqrt(n + 1, x)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * is_prime_number - returns true if the number is prime 4 | *@n: the number to check 5 | * 6 | *Return: true if the number is prime 7 | */ 8 | int is_prime_number(int n) 9 | { 10 | int start = n / 2; 11 | 12 | if (n <= 1) 13 | return (0); 14 | return (is_prime(n, start)); 15 | } 16 | 17 | /** 18 | * is_prime - returns the 1 if n is prime 19 | * @n: number to be checked 20 | * @start: number to start checking from 21 | * 22 | * Return: 1 if n is prime, 0 otherwise 23 | */ 24 | 25 | int is_prime(int n, int start) 26 | { 27 | if (start <= 1) 28 | return (1); 29 | else if (n % start == 0) 30 | return (0); 31 | return (is_prime(n, start - 1)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | /** 6 | *void prototypes(void) 7 | *int prototypes(int) 8 | *char prototypes(char) 9 | */ 10 | void _puts_recursion(char *s); 11 | void _print_rev_recursion(char *s); 12 | int _putchar(char c); 13 | int _strlen_recursion(char *s); 14 | int factorial(int n); 15 | int _pow_recursion(int x, int y); 16 | int _sqrt_recursion(int n); 17 | int is_prime_number(int n); 18 | int is_palindrome(char *s); 19 | int wildcmp(char *s1, char *s2); 20 | int _sqrt(int n, int x); 21 | int is_prime(int n, int start); 22 | int find_strlen(char *s); 23 | int check_palindrome(char *s, int len, int index); 24 | 25 | #endif /* MAIN_H */ -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isupper - function that verifies if a character is uppercase or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isupper(int c) 9 | { 10 | if ((c >= 'A') && (c <= 'Z')) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memset - The _memset() function fills 4 | * the first n bytes of the memory area 5 | * pointed to by s with the constant byte b 6 | *@s:target 7 | *@b: constant byte 8 | *@n:number of byte 9 | *Return: returns new value of target 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | while (n) 15 | { 16 | s[n - 1] = b; 17 | n--; 18 | } 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strcat - concatenates the string pointed to by @src to 5 | * the end of the string pointed to by @dest 6 | *@dest: String that will be appended 7 | *@src: String to be concatenated upon 8 | * 9 | * Return: returns poiner to @dest 10 | */ 11 | 12 | char *_strcat(char *dest, char *src) 13 | { 14 | 15 | int index = 0, dest_len = 0; 16 | 17 | while (dest[index++]) 18 | dest_len++; 19 | 20 | for (index = 0; src[index]; index++) 21 | dest[dest_len++] = src[index]; 22 | 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isdigit - function that verifies if a character is a digit or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isdigit(int c) 9 | { 10 | if ((c >= 48) && (c <= 57)) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memcpy - The _memcpy() function copies n bytes 4 | * from memory area src to memory area dest 5 | *@dest:area where bytes are copied to 6 | *@src:area where bytes are copied from 7 | *@n:number of bytes to copy 8 | *Return: returns a pointer to n 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i = 0; 13 | 14 | for (; i < n; i++) 15 | dest[i] = src[i]; 16 | 17 | return (dest); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strncat - concatenate two strings but add inputted number of bytes 4 | *@dest: string to be appended upon 5 | *@src: string to be completed at end of dest 6 | *@n:integer parameter to compare index to 7 | *Return: returns new concatenated string 8 | */ 9 | 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | 13 | int index = 0, dest_len = 0; 14 | 15 | while (dest[index++]) 16 | dest_len++; 17 | 18 | for (index = 0; src[index] && index < n; index++) 19 | dest[dest_len++] = src[index]; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _atoi - changes a string to an int 3 | * @s: the string to be changed 4 | * 5 | * Return: the converted int 6 | */ 7 | int _atoi(char *s) 8 | { 9 | int i = 1; 10 | unsigned int num = 0; 11 | do { 12 | if (*s == '-') 13 | i *= -1; 14 | else if (*s >= '0' && *s <= '9') 15 | num = num * 10 + (*s - '0'); 16 | else if (num > 0) 17 | break; 18 | } while (*s++); 19 | return (num *i); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include   3 | /** 4 | *_strchr - Returns a pointer to the first occurrence 5 | *of the character c in the string s, or NULL if the 6 | *character is not found 7 | * 8 | *@s:string targeted 9 | *@c:character targeted 10 | * 11 | *Return: returns pointer to first occcurence of c 12 | */ 13 | char *_strchr(char *s, char c) 14 | { 15 | int i; 16 | 17 | for (i = 0; (s[i] != c) && (s[i] != '\0'); i++) 18 | ; 19 | if (s[i] == c) 20 | return (s + i); 21 | else 22 | return (NULL); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strlen - returns the length of a string 4 | * @s: string 5 | *Return: returns lenght; 6 | */ 7 | int _strlen(char *s) 8 | { 9 | int count, inc; 10 | inc = 0; 11 | for (count = 0; s[count] != '\0'; count++) 12 | inc++; 13 | 14 | return (inc); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - C function that copies a string, including the 4 | * terminating null byte, using at most an inputted number of bytes. 5 | * If the length of the source string is less than the maximum byte number, 6 | * the remainder of the destination string is filled with null bytes. 7 | * Works identically to the standard library function `strncpy`. 8 | *@dest: buffer storing the string copy 9 | *@src:the source string 10 | *@n:max nummber of byte copied 11 | *Return: returns 12 | */ 13 | 14 | char *_strncpy(char *dest, char *src, int n) 15 | { 16 | int i; 17 | 18 | for (i = 0; i < n && src[i] != '\0'; i++) 19 | dest[i] = src[i]; 20 | 21 | for ( ; i < n; i++) 22 | dest[i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_islower - controls if a character is in lowercase 4 | *@c: character to be verified 5 | *Return: return 0 or 1 6 | */ 7 | int _islower(int c) 8 | { 9 | if (c >= 'a' && c <= 'z') 10 | return (1); 11 | 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_puts - prints a string 4 | * @str: string to print 5 | * 6 | * Description: prints a string 7 | * On success: return the number of characters printed 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | while (*str) 13 | _putchar(*str++); 14 | 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Function which compare two strings and 5 | *@s1: first string 6 | *@s2:second string 7 | *Return: 8 | * returns zero if s1 == s2 9 | * returns negative number if s1 < s2 10 | * returns positive number if s1 > s2 11 | */ 12 | 13 | int _strcmp(char *s1, char *s2) 14 | { 15 | int i = 0, diff = 0; 16 | 17 | while (1) 18 | { 19 | if (s1[i] == '\0' && s2[i] == '\0') 20 | break; 21 | else if (s1[i] == '\0') 22 | { 23 | diff = s2[i]; 24 | break; 25 | } 26 | else if (s2[i] == '\0') 27 | { 28 | diff = s1[i]; 29 | break; 30 | } 31 | else if (s1[i] != s2[i]) 32 | { 33 | diff = s1[i] - s2[i]; 34 | break; 35 | } 36 | else 37 | i++; 38 | 39 | } 40 | return (diff); 41 | } 42 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strspn - search the number of bytes in the initial 4 | * segment of s which consist only of bytes from accept 5 | *@s:segment targeted 6 | *@accept:reference bytes container 7 | * 8 | *Return:returns the number of bytes in the initial 9 | * segment of s which consist only of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int bytes = 0; 14 | int i; 15 | 16 | while (*s) 17 | { 18 | for (i = 0; accept[i]; i++) 19 | { 20 | if (accept[i] == *s) 21 | { 22 | bytes++; 23 | break; 24 | } 25 | else if ((accept[i + 1]) == '\0') 26 | return (bytes); 27 | } 28 | s++; 29 | } 30 | return (bytes); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isalpha - controls if a character is alphabetical 4 | *@c: character to be verified 5 | *Return: return 0 or 1 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 10 | return (1); 11 | 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strpbrk - The _strpbrk() function locates the first 4 | * occurrence in the string s of any of the bytes in 5 | *the string accept 6 | * 7 | *@s: string where search is made 8 | *@accept: string where searched bytes are located 9 | * 10 | *Return:Returns a pointer to the byte in s that matches 11 | * one of the bytes in accept, or NULL if no such byte is found 12 | */ 13 | 14 | char *_strpbrk(char *s, char *accept) 15 | { 16 | int i; 17 | 18 | while (*s) 19 | { 20 | for (i = 0; accept[i]; i++) 21 | { 22 | if (accept[i] == *s) 23 | return (s); 24 | } 25 | s++; 26 | } 27 | 28 | return ('\0'); 29 | } 30 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strstr - The _strstr() function finds the first occurrence 4 | * of the substring needle in the string haystack. 5 | * The terminating null bytes (\0) are not compared 6 | *@haystack: string where the search is made 7 | *@needle: string whose occurence is searched in haystack 8 | *Return:Returns a pointer to the beginning of the located 9 | * substring, or NULL if the substring is not found. 10 | */ 11 | 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | int i; 15 | 16 | if (*needle == 0) 17 | return (haystack); 18 | 19 | while (*haystack) 20 | { 21 | i = 0; 22 | 23 | if (haystack[i] == needle[i]) 24 | { 25 | do { 26 | if (needle[i + 1] == '\0') 27 | return (haystack); 28 | i++; 29 | } while (haystack[i] == needle[i]); 30 | } 31 | haystack++; 32 | } 33 | return ('\0'); 34 | } 35 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - prints the absolute value of a number 4 | *@i: number targeted 5 | *Return: returns 0 6 | */ 7 | int _abs(int i) 8 | { 9 | if (i > 0) 10 | return (i); 11 | else if (i < 0) 12 | return (-i); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - Copy paste string 4 | *@dest: destination 5 | *@src: source 6 | *Return: dest 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int inc = 0; 11 | while (*(src + inc) != '\0') 12 | { 13 | *(dest + inc) = *(src + inc); 14 | inc++; 15 | } 16 | *(dest + inc) = '\0'; 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c ./*.c 3 | ar -rc liball.a ./*.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | 5 | int _putchar(char c); 6 | int _islower(int c); 7 | int _isalpha(int c); 8 | int _abs(int n); 9 | int _isupper(int c); 10 | int _isdigit(int c); 11 | int _strlen(char *s); 12 | void _puts(char *s); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | char *_strcat(char *dest, char *src); 16 | char *_strncat(char *dest, char *src, int n); 17 | char *_strncpy(char *dest, char *src, int n); 18 | int _strcmp(char *s1, char *s2); 19 | char *_memset(char *s, char b, unsigned int n); 20 | char *_memcpy(char *dest, char *src, unsigned int n); 21 | char *_strchr(char *s, char c); 22 | unsigned int _strspn(char *s, char *accept); 23 | char *_strpbrk(char *s, char *accept); 24 | char *_strstr(char *haystack, char *needle); 25 | 26 | #endif /* MAIN_H */ 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints program name 5 | *@argc: number of arguments 6 | *@argv: array of arguments 7 | *Return: 0 8 | */ 9 | int main(int __attribute__((__unused__)) argc, char *argv[]) 10 | { 11 | printf("%s\n", argv[0]); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the number of arguments 5 | *@argc: number of arguments 6 | *@argv:array of arguments 7 | *Return: the number of arguments 8 | */ 9 | 10 | int main(int argc, char __attribute__((__unused__)) *argv[]) 11 | { 12 | printf("%d\n", argc - 1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - prints the minimum number of coins to make change 6 | * for an amount of money 7 | *@argc: number of arguments 8 | *@argv: array of arguments 9 | *Return: returns 1 if there is an error; else returns 0 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int cents, coins = 0; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | cents = atoi(argv[1]); 22 | while (cents > 0) 23 | { 24 | coins++; 25 | if ((cents - 25) >= 0) 26 | { 27 | cents -= 25; 28 | continue; 29 | } 30 | if ((cents - 10) >= 0) 31 | { 32 | cents -= 10; 33 | continue; 34 | } 35 | if ((cents - 5) >= 0) 36 | { 37 | cents -= 5; 38 | continue; 39 | } 40 | if ((cents - 2) >= 0) 41 | { 42 | cents -= 2; 43 | continue; 44 | } 45 | cents--; 46 | } 47 | printf("%d\n", coins); 48 | return (0); 49 | } 50 | 51 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all arguments it received 5 | *@argc: argument count 6 | *@argv: argument vector 7 | *Return: returns 0 8 | */ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < argc; i++) 15 | printf("%s\n", argv[i]); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - prints multiplication 5 | *of two numbers 6 | *@argc: number of arguments 7 | *@argv: array of arguments 8 | *Return: returns 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, j, mul; 14 | 15 | if (argc <= 2) 16 | { 17 | printf("Error\n"); 18 | return (1); 19 | } 20 | 21 | i = atoi(argv[1]); 22 | j = atoi(argv[2]); 23 | mul = i * j; 24 | 25 | printf("%d\n", mul); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - adds positive numbers 6 | *@argc: number of arguments 7 | *@argv: array of arguments 8 | *Return: 0 on success, 1 on failure 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, j, sum = 0; 14 | 15 | for (i = 1; i < argc; i++) 16 | { 17 | for (j = 0; argv[i][j] != '\0'; j++) 18 | { 19 | if (argv[i][j] < '0' || argv[i][j] > '9') 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | } 25 | 26 | sum += atoi(argv[i]); 27 | } 28 | 29 | printf("%d\n", sum); 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x0B-malloc_free/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_array - creates an array of chars, and 5 | * initializes it with a specific char. 6 | *@size: Size of the array 7 | *@c: Character to insert 8 | *Return: NULL if size is zero or if it fails, 9 | *pointer to array if everything is normal. 10 | */ 11 | 12 | char *create_array(unsigned int size, char c) 13 | { 14 | char *array; 15 | unsigned int index; 16 | 17 | if (size == 0) 18 | return (NULL); 19 | array = malloc(sizeof(char) * size); 20 | 21 | if (array == NULL) 22 | return (NULL); 23 | 24 | for (index = 0; index < size; index++) 25 | array[index] = c; 26 | 27 | return (array); 28 | } 29 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strdup - returns a pointer to a newly allocated 5 | *space in memory, which contains a copy of the 6 | *string given as a parameter. 7 | *@str:String to be copied 8 | * 9 | *Return: NULL in case of error, pointer to allocated 10 | *space 11 | */ 12 | 13 | char *_strdup(char *str) 14 | { 15 | char *cpy; 16 | int index, len; 17 | 18 | if (str == NULL) 19 | return (NULL); 20 | 21 | for (index = 0; str[index]; index++) 22 | len++; 23 | cpy = malloc(sizeof(char) * (len + 1)); 24 | 25 | if (cpy == NULL) 26 | return (NULL); 27 | 28 | for (index = 0; str[index]; index++) 29 | { 30 | cpy[index] = str[index]; 31 | } 32 | 33 | cpy[len] = '\0'; 34 | 35 | return (cpy); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * argstostr - a function that concatenates all the arguments of your program 4 | *@ac: count of args passed to the function 5 | *@av:array of arguments 6 | * 7 | *Return: pointer to the new string 8 | */ 9 | 10 | char *argstostr(int ac, char **av) 11 | { 12 | char *new_string = NULL; 13 | int k = 0, i = ac, j, sum = 0, temp = 0; 14 | 15 | if (ac == 0 || av == NULL) 16 | return (NULL); 17 | 18 | while (ac--) 19 | sum += (len(av[ac]) + 1); 20 | new_string = (char *) malloc(sum + 1); 21 | 22 | if (new_string != NULL) 23 | { 24 | while (k < i) 25 | { 26 | for (j = 0; av[k][j] != '\0'; j++) 27 | new_string[j + temp] = av[k][j]; 28 | new_string[temp + j] = '\n'; 29 | temp += (j + 1); 30 | k++; 31 | } 32 | new_string[temp] = '\0'; 33 | } 34 | else 35 | { 36 | return (NULL); 37 | } 38 | return (new_string); 39 | } 40 | 41 | /** 42 | *len - returns length of str 43 | *@str: string counted 44 | *Return: returns the length 45 | */ 46 | int len(char *str) 47 | { 48 | int len = 0; 49 | 50 | if (str != NULL) 51 | { 52 | while (str[len]) 53 | len++; 54 | } 55 | return (len); 56 | } 57 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *strtow - splits a stirng into words 5 | *@str: string to be splitted 6 | * 7 | *Return: pointer to the array of splitted words 8 | */ 9 | 10 | char **strtow(char *str) 11 | { 12 | char **split; 13 | int i, j = 0, temp = 0, size = 0, words = num_words(str); 14 | 15 | if (words == 0) 16 | return (NULL); 17 | split = (char **) malloc(sizeof(char *) * (words + 1)); 18 | if (split != NULL) 19 | { 20 | for (i = 0; i <= len(str) && words; i++) 21 | { 22 | if ((str[i] != ' ') && (str[i] != '\0')) 23 | size++; 24 | else if (((str[i] == ' ') || (str[i] == '\0')) && i && (str[i - 1] != ' ')) 25 | { 26 | split[j] = (char *) malloc(sizeof(char) * size + 1); 27 | if (split[j] != NULL) 28 | { 29 | while (temp < size) 30 | { 31 | split[j][temp] = str[(i - size) +temp]; 32 | temp++; 33 | } 34 | split[j][temp] = '\0'; 35 | size = temp = 0; 36 | j++; 37 | } 38 | else 39 | { 40 | while (j-- >= 0) 41 | free(split[j]); 42 | free(split); 43 | return (NULL); 44 | } 45 | } 46 | } 47 | split[words] = NULL; 48 | return (split); 49 | } 50 | else 51 | return (NULL); 52 | } 53 | 54 | 55 | /** 56 | * num_words - counts the number of words in str 57 | *@str: string to be used 58 | * 59 | *Return: number of words 60 | */ 61 | int num_words(char *str) 62 | { 63 | int i = 0, words = 0; 64 | 65 | while (i <= len(str)) 66 | { 67 | if ((str[i] != ' ') && (str[i] != '\0')) 68 | { 69 | i++; 70 | } 71 | else if (((str[i] == ' ') || (str[i] == '\0')) && i && (str[i - 1] != ' ')) 72 | { 73 | words += 1; 74 | i++; 75 | } 76 | else 77 | { 78 | i++; 79 | } 80 | } 81 | return (words); 82 | } 83 | 84 | /** 85 | * len - returns length of str 86 | *@str: string to be counted 87 | * 88 | * Return: length of the string 89 | */ 90 | 91 | int len(char *str) 92 | { 93 | int len = 0; 94 | 95 | if (str != NULL) 96 | { 97 | while (str[len]) 98 | len++; 99 | } 100 | return (len); 101 | } 102 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * str_concat - a function that concatenates two strings. 5 | *@s1:First string 6 | *@s2:Second string 7 | * 8 | *Return: NULL in case of failure , but pointer to new string in 9 | *case of success 10 | */ 11 | 12 | char *str_concat(char *s1, char *s2) 13 | { 14 | char *concat_str; 15 | int index, concat_index = 0, len = 0; 16 | 17 | if (s1 == NULL) 18 | s1 = ""; 19 | 20 | if (s2 == NULL) 21 | s2 = ""; 22 | 23 | for (index = 0; s1[index] || s2[index]; index++) 24 | len++; 25 | 26 | concat_str = malloc(sizeof(char) * len); 27 | 28 | if (concat_str == NULL) 29 | return (NULL); 30 | 31 | for (index = 0; s1[index]; index++) 32 | concat_str[concat_index++] = s1[index]; 33 | 34 | for (index = 0; s2[index]; index++) 35 | concat_str[concat_index++] = s2[index]; 36 | 37 | return (concat_str); 38 | } 39 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | /** 6 | * alloc_grid - returns a pointer to a 7 | *2 dimensional array of integers. 8 | *@width: width oàf array 9 | *@height: height of array 10 | * 11 | *Return: pointer to the array 12 | */ 13 | 14 | int **alloc_grid(int width, int height) 15 | { 16 | int **array; 17 | int i = 0, j; 18 | 19 | if (width == 0 || height == 0) 20 | return (NULL); 21 | array = (int **) malloc(sizeof(int *) * height); 22 | if (array != NULL) 23 | { 24 | for (; i < height; i++) 25 | { 26 | array[i] = (int *) malloc(sizeof(int) * width); 27 | if (array[i] != NULL) 28 | { 29 | for (j = 0; j < width; j++) 30 | array[i][j] = 0; 31 | } 32 | else 33 | { 34 | while (i >= 0) 35 | { 36 | free(array[i]); 37 | i--; 38 | } 39 | free(array); 40 | return (NULL); 41 | } 42 | } 43 | return (array); 44 | } 45 | else 46 | { 47 | return (NULL); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * free_grid - frees a 2d array 5 | *@grid:memory block to be freed 6 | *@height:height of the array 7 | *Return:returns void 8 | */ 9 | 10 | void free_grid(int **grid, int height) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < height; i++) 15 | free(grid[i]); 16 | 17 | free(grid); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void free_grid(int **grid, int height); 7 | char *create_array(unsigned int size, char c); 8 | char *_strdup(char *str); 9 | char *str_concat(char *s1, char *s2); 10 | char *argstostr(int ac, char **av); 11 | char **strtow(char *str); 12 | int **alloc_grid(int width, int height); 13 | int len(char *str); 14 | int num_words(char *str); 15 | #endif /* MAIN_H */ -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * malloc_checked - allocates memory using malloc, exit(98) if it fails 5 | * @b: size of the memory block to be allocated 6 | * 7 | * Return: pointer to the address of the memory block 8 | */ 9 | void *malloc_checked(unsigned int b) 10 | { 11 | void *block; 12 | 13 | block = malloc(b); 14 | if (block == NULL) 15 | exit(98); 16 | return (block); 17 | } 18 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *string_nconcat - Concatenates two strings using at 5 | *most an inputted number of bytes. 6 | *@s1: The first string. 7 | *@s2: The second string. 8 | *@n: The maximum number of bytes of s2 to concatenate to s1. 9 | * 10 | *Return: If the function fails - NULL. 11 | *Otherwise - a pointer to the concatenated space in memory. 12 | */ 13 | 14 | char *string_nconcat(char *s1, char *s2, unsigned int n) 15 | { 16 | char *concat; 17 | unsigned int len = n, index; 18 | 19 | if (s1 == NULL) 20 | s1 = ""; 21 | 22 | if (s2 == NULL) 23 | s2 = ""; 24 | 25 | for (index = 0; s1[index]; index++) 26 | len++; 27 | 28 | concat = malloc(sizeof(char) * (len + 1)); 29 | 30 | if (concat == NULL) 31 | return (NULL); 32 | 33 | len = 0; 34 | 35 | for (index = 0; s1[index]; index++) 36 | concat[len++] = s1[index]; 37 | 38 | for (index = 0; s2[index] && index < n; index++) 39 | concat[len++] = s2[index]; 40 | 41 | concat[len] = '\0'; 42 | 43 | return (concat); 44 | } 45 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | /** 5 | * _realloc - reallocates a memory block 6 | * @ptr: pointer to the memory previously allocated with a call to malloc 7 | * @old_size: size of ptr 8 | * @new_size: size of the new memory to be allocated 9 | * 10 | * Return: pointer to the address of the new memory block 11 | */ 12 | 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 14 | { 15 | void *temp_block; 16 | unsigned int i; 17 | 18 | if (ptr == NULL) 19 | { 20 | temp_block = malloc(new_size); 21 | return (temp_block); 22 | } 23 | else if (new_size == old_size) 24 | return (ptr); 25 | 26 | else if (new_size == 0 && ptr != NULL) 27 | { 28 | free(ptr); 29 | return (NULL); 30 | } 31 | else 32 | { 33 | temp_block = malloc(new_size); 34 | if (temp_block != NULL) 35 | { 36 | for (i = 0; i < min(old_size, new_size); i++) 37 | *((char *)temp_block + i) = *((char *) ptr + i); 38 | free(ptr); 39 | return (temp_block); 40 | } 41 | else 42 | return (NULL); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int find_len(char *str); 4 | char *create_xarray(int size); 5 | char *iterate_zeroes(char *str); 6 | void get_prod(char *prod, char *mult, int digit, int zeroes); 7 | void add_nums(char *final_prod, char *next_prod, int next_len); 8 | 9 | /** 10 | * find_len - Finds the length of a string. 11 | * @str: The string to be measured. 12 | * 13 | * Return: The length of the string. 14 | */ 15 | int find_len(char *str) 16 | { 17 | int len = 0; 18 | 19 | while (*str++) 20 | len++; 21 | 22 | return (len); 23 | } 24 | 25 | /** 26 | * create_xarray - Creates an array of chars and initializes it with 27 | * the character 'x'. Adds a terminating null byte. 28 | * @size: The size of the array to be initialized. 29 | * 30 | * Description: If there is insufficient space, the 31 | * function exits with a status of 98. 32 | * Return: A pointer to the array. 33 | */ 34 | char *create_xarray(int size) 35 | { 36 | char *array; 37 | int index; 38 | 39 | array = malloc(sizeof(char) * size); 40 | 41 | if (array == NULL) 42 | exit(98); 43 | 44 | for (index = 0; index < (size - 1); index++) 45 | array[index] = 'x'; 46 | 47 | array[index] = '\0'; 48 | 49 | return (array); 50 | } 51 | 52 | /** 53 | * iterate_zeroes - Iterates through a string of numbers containing 54 | * leading zeroes until it hits a non-zero number. 55 | * @str: The string of numbers to be iterate through. 56 | * 57 | * Return: A pointer to the next non-zero element. 58 | */ 59 | char *iterate_zeroes(char *str) 60 | { 61 | while (*str && *str == '0') 62 | str++; 63 | 64 | return (str); 65 | } 66 | 67 | /** 68 | * get_digit - Converts a digit character to a corresponding int. 69 | * @c: The character to be converted. 70 | * 71 | * Description: If c is a non-digit, the function 72 | * exits with a status of 98. 73 | * Return: The converted int. 74 | */ 75 | int get_digit(char c) 76 | { 77 | int digit = c - '0'; 78 | 79 | if (digit < 0 || digit > 9) 80 | { 81 | printf("Error\n"); 82 | exit(98); 83 | } 84 | 85 | return (digit); 86 | } 87 | 88 | /** 89 | * get_prod - Multiplies a string of numbers by a single digit. 90 | * @prod: The buffer to store the result. 91 | * @mult: The string of numbers. 92 | * @digit: The single digit. 93 | * @zeroes: The necessary number of leading zeroes. 94 | * 95 | * Description: If mult contains a non-digit, the function 96 | * exits with a status value of 98. 97 | */ 98 | void get_prod(char *prod, char *mult, int digit, int zeroes) 99 | { 100 | int mult_len, num, tens = 0; 101 | 102 | mult_len = find_len(mult) - 1; 103 | mult += mult_len; 104 | 105 | while (*prod) 106 | { 107 | *prod = 'x'; 108 | prod++; 109 | } 110 | 111 | prod--; 112 | 113 | while (zeroes--) 114 | { 115 | *prod = '0'; 116 | prod--; 117 | } 118 | 119 | for (; mult_len >= 0; mult_len--, mult--, prod--) 120 | { 121 | if (*mult < '0' || *mult > '9') 122 | { 123 | printf("Error\n"); 124 | exit(98); 125 | } 126 | 127 | num = (*mult - '0') * digit; 128 | num += tens; 129 | *prod = (num % 10) + '0'; 130 | tens = num / 10; 131 | } 132 | 133 | if (tens) 134 | *prod = (tens % 10) + '0'; 135 | } 136 | 137 | /** 138 | * add_nums - Adds the numbers stored in two strings. 139 | * @final_prod: The buffer storing the running final product. 140 | * @next_prod: The next product to be added. 141 | * @next_len: The length of next_prod. 142 | */ 143 | void add_nums(char *final_prod, char *next_prod, int next_len) 144 | { 145 | int num, tens = 0; 146 | 147 | while (*(final_prod + 1)) 148 | final_prod++; 149 | 150 | while (*(next_prod + 1)) 151 | next_prod++; 152 | 153 | for (; *final_prod != 'x'; final_prod--) 154 | { 155 | num = (*final_prod - '0') + (*next_prod - '0'); 156 | num += tens; 157 | *final_prod = (num % 10) + '0'; 158 | tens = num / 10; 159 | 160 | next_prod--; 161 | next_len--; 162 | } 163 | 164 | for (; next_len >= 0 && *next_prod != 'x'; next_len--) 165 | { 166 | num = (*next_prod - '0'); 167 | num += tens; 168 | *final_prod = (num % 10) + '0'; 169 | tens = num / 10; 170 | 171 | final_prod--; 172 | next_prod--; 173 | } 174 | 175 | if (tens) 176 | *final_prod = (tens % 10) + '0'; 177 | } 178 | 179 | /** 180 | * main - Multiplies two positive numbers. 181 | * @argv: The number of arguments passed to the program. 182 | * @argc: An array of pointers to the arguments. 183 | * 184 | * Description: If the number of arguments is incorrect or one number 185 | * contains non-digits, the function exits with a status of 98. 186 | * Return: Always 0. 187 | */ 188 | int main(int argc, char *argv[]) 189 | { 190 | char *final_prod, *next_prod; 191 | int size, index, digit, zeroes = 0; 192 | 193 | if (argc != 3) 194 | { 195 | printf("Error\n"); 196 | exit(98); 197 | } 198 | 199 | if (*(argv[1]) == '0') 200 | argv[1] = iterate_zeroes(argv[1]); 201 | if (*(argv[2]) == '0') 202 | argv[2] = iterate_zeroes(argv[2]); 203 | if (*(argv[1]) == '\0' || *(argv[2]) == '\0') 204 | { 205 | printf("0\n"); 206 | return (0); 207 | } 208 | 209 | size = find_len(argv[1]) + find_len(argv[2]); 210 | final_prod = create_xarray(size + 1); 211 | next_prod = create_xarray(size + 1); 212 | 213 | for (index = find_len(argv[2]) - 1; index >= 0; index--) 214 | { 215 | digit = get_digit(*(argv[2] + index)); 216 | get_prod(next_prod, argv[1], digit, zeroes++); 217 | add_nums(final_prod, next_prod, size - 1); 218 | } 219 | for (index = 0; final_prod[index]; index++) 220 | { 221 | if (final_prod[index] != 'x') 222 | putchar(final_prod[index]); 223 | } 224 | putchar('\n'); 225 | 226 | free(next_prod); 227 | free(final_prod); 228 | 229 | return (0); 230 | } 231 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _calloc - allocate memory using malloc and initialize it to zero 5 | *@nmemb: number of elements 6 | *@size: size of the memory block to be allocated 7 | * 8 | *Return: poiner to the address of the memory block 9 | */ 10 | void *_calloc(unsigned int nmemb, unsigned int size) 11 | { 12 | char *block; 13 | unsigned int i; 14 | 15 | if (nmemb == 0 || size == 0) 16 | return (NULL); 17 | block = malloc(nmemb * size); 18 | if (block != NULL) 19 | { 20 | for (i = 0; i < (nmemb * size); i++) 21 | block[i] = 0; 22 | return (block); 23 | } 24 | else 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * array_range - creates an array of integers 5 | * @min: smallest number in the array 6 | * @max: lagrest value in the array 7 | * 8 | * Return: pointer to the address of the memory block 9 | */ 10 | 11 | int *array_range(int min, int max) 12 | { 13 | int *block; 14 | int i, j = 0; 15 | 16 | if (min > max) 17 | return (NULL); 18 | block = malloc(sizeof(*block) * ((max - min) + 1)); 19 | if (block != NULL) 20 | { 21 | for (i = min; i <= max; i++) 22 | { 23 | block[j] = i; 24 | j++; 25 | } 26 | return (block); 27 | } 28 | else 29 | return (NULL); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | #define min(x, y) (((x) < (y)) ? (x) : (y)) 8 | 9 | int _putchar(char c); 10 | void *malloc_checked(unsigned int b); 11 | char *string_nconcat(char *s1, char *s2, unsigned int n); 12 | int len(char *str); 13 | void *_calloc(unsigned int nmemb, unsigned int size); 14 | int *array_range(int min, int max); 15 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 16 | int find_len(char *str); 17 | char *create_xarray(int size); 18 | char *iterate_zeroes(char *str); 19 | void get_prod(char *prod, char *mult, int digit, int zeroes); 20 | void add_nums(char *final_prod, char *next_prod, int next_len); 21 | 22 | #endif /*MAIN_H*/ 23 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | #define SIZE 1024 4 | 5 | #endif 6 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - prints the name of 5 | *the file it was compiled from 6 | * 7 | *Return: nothing to be returned 8 | */ 9 | 10 | int main(void) 11 | { 12 | printf("%s\n", __FILE__); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) (((x) < (0)) ? ((x) * (-1)) : (x)) 5 | 6 | #endif 7 | 8 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | 0x0D. C - Preprocessor 2 | ====================== 3 | 4 | - By Julien Barbier & Johan Euphrosine, Software Engineer at Google 5 | 6 | 7 | Resources 8 | --------- 9 | 10 | **Read or watch**: 11 | 12 | - [Understanding C program Compilation Process](https://alx-intranet.hbtn.io/rltoken/73Xp4zJlc-orfeEzmlYVDg "Understanding C program Compilation Process") 13 | - [Object-like Macros](https://alx-intranet.hbtn.io/rltoken/kaqIw352MSJ8xoi1xU09ZA "Object-like Macros") 14 | - [Macro Arguments](https://alx-intranet.hbtn.io/rltoken/wcQZzunlgjepxExZFc2ORQ "Macro Arguments") 15 | - [Pre Processor Directives in C](https://alx-intranet.hbtn.io/rltoken/S4zfCHzg82fUAxdt8_SaZQ "Pre Processor Directives in C") 16 | - [The C Preprocessor](https://alx-intranet.hbtn.io/rltoken/G33GiOIZofiIN4Tx9_acbQ "The C Preprocessor") 17 | - [Standard Predefined Macros](https://alx-intranet.hbtn.io/rltoken/0OYhpL2cJfsIMBWhTuZsAA "Standard Predefined Macros") 18 | - [include guard](https://alx-intranet.hbtn.io/rltoken/oF2vgIZNePdU965jCEZLHA "include guard") 19 | - [Common Predefined Macros](https://alx-intranet.hbtn.io/rltoken/ROl5xAMKX-JpenEqmf7FnQ "Common Predefined Macros") 20 | 21 | Learning Objectives 22 | ------------------- 23 | 24 | At the end of this project, you are expected to be able to [explain to anyone](https://alx-intranet.hbtn.io/rltoken/OnMlvyxq0Mzr-7GHRPI9tQ "explain to anyone"), **without the help of Google**: 25 | 26 | ### General 27 | 28 | - What are macros and how to use them 29 | - What are the most common predefined macros 30 | - How to include guard your header files 31 | 32 | Requirements 33 | ------------ 34 | 35 | ### General 36 | 37 | - Allowed editors: `vi`, `vim`, `emacs` 38 | - All your files will be compiled on Ubuntu 20.04 LTS using `gcc`, using the options `-Wall -Werror -Wextra -pedantic -std=gnu89` 39 | - All your files should end with a new line 40 | - A `README.md` file, at the root of the folder of the project is mandatory 41 | - Your code should use the `Betty` style. It will be checked using [betty-style.pl](https://github.com/holbertonschool/Betty/blob/master/betty-style.pl "betty-style.pl") and [betty-doc.pl](https://github.com/holbertonschool/Betty/blob/master/betty-doc.pl "betty-doc.pl") 42 | - You are not allowed to use global variables 43 | - No more than 5 functions per file 44 | - The only C standard library functions allowed are `malloc`, `free` and `exit`. Any use of functions like `printf`, `puts`, `calloc`, `realloc` etc... is forbidden 45 | - You are allowed to use [_putchar](https://github.com/holbertonschool/_putchar.c/blob/master/_putchar.c "_putchar") 46 | - You don't have to push `_putchar.c`, we will use our file. If you do it won't be taken into account 47 | - In the following examples, the `main.c` files are shown as examples. You can use them to test your functions, but you don't have to push them to your repo (if you do we won't take them into account). We will use our own `main.c` files at compilation. Our `main.c` files might be different from the one shown in the examples 48 | - Don't forget to push your header file 49 | - All your header files should be include guarded 50 | 51 | Tasks 52 | ----- 53 | 54 | ### 0\. Object-like Macro 55 | 56 | mandatory 57 | 58 | Create a header file that defines a macro named `SIZE` as an abbreviation for the token `1024`. 59 | 60 | ``` 61 | julien@ubuntu:~/0x0c. macro, structures$ cat 0-main.c 62 | #include "0-object_like_macro.h" 63 | #include "0-object_like_macro.h" 64 | #include 65 | 66 | /** 67 | * main - check the code 68 | * 69 | * Return: Always 0. 70 | */ 71 | int main(void) 72 | { 73 | int s; 74 | 75 | s = 98 + SIZE; 76 | printf("%d\n", s); 77 | return (0); 78 | } 79 | julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c -o a 80 | julien@ubuntu:~/0x0c. macro, structures$ ./a 81 | 1122 82 | julien@ubuntu:~/0x0c. macro, structures$ 83 | 84 | ``` 85 | 86 | **Repo:** 87 | 88 | - GitHub repository: `alx-low_level_programming` 89 | - Directory: `0x0D-preprocessor` 90 | - File: `0-object_like_macro.h` 91 | 92 | ### 1\. Pi 93 | 94 | mandatory 95 | 96 | Create a header file that defines a macro named `PI` as an abbreviation for the token `3.14159265359`. 97 | 98 | ``` 99 | julien@ubuntu:~/0x0c. macro, structures$ cat 1-main.c 100 | #include "1-pi.h" 101 | #include "1-pi.h" 102 | #include 103 | 104 | /** 105 | * main - check the code 106 | * 107 | * Return: Always 0. 108 | */ 109 | int main(void) 110 | { 111 | float a; 112 | float r; 113 | 114 | r = 98; 115 | a = PI * r * r; 116 | printf("%.3f\n", a); 117 | return (0); 118 | } 119 | julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c -o b 120 | julien@ubuntu:~/0x0c. macro, structures$ ./b 121 | 30171.855 122 | julien@ubuntu:~/0x0c. macro, structures$ 123 | 124 | ``` 125 | 126 | **Repo:** 127 | 128 | - GitHub repository: `alx-low_level_programming` 129 | - Directory: `0x0D-preprocessor` 130 | - File: `1-pi.h` 131 | 132 | ### 2\. File name 133 | 134 | mandatory 135 | 136 | Write a program that prints the name of the file it was compiled from, followed by a new line. 137 | 138 | - You are allowed to use the standard library 139 | 140 | ``` 141 | julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c -o c 142 | julien@ubuntu:~/0x0c. macro, structures$ ./c 143 | 2-main.c 144 | julien@ubuntu:~/0x0c. macro, structures$ cp 2-main.c 02-main.c 145 | julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 02-main.c -o cc 146 | julien@ubuntu:~/0x0c. macro, structures$ ./cc 147 | 02-main.c 148 | julien@ubuntu:~/0x0c. macro, structures$ 149 | 150 | ``` 151 | 152 | **Repo:** 153 | 154 | - GitHub repository: `alx-low_level_programming` 155 | - Directory: `0x0D-preprocessor` 156 | - File: `2-main.c` 157 | 158 | ### 3\. Function-like macro 159 | 160 | mandatory 161 | 162 | Write a function-like macro `ABS(x)` that computes the absolute value of a number `x`. 163 | 164 | ``` 165 | julien@ubuntu:~/0x0c. macro, structures$ cat 3-main.c 166 | #include 167 | #include "3-function_like_macro.h" 168 | #include "3-function_like_macro.h" 169 | 170 | /** 171 | * main - check the code 172 | * 173 | * Return: Always 0. 174 | */ 175 | int main(void) 176 | { 177 | int i; 178 | int j; 179 | 180 | i = ABS(-98) * 10; 181 | j = ABS(98) * 10; 182 | printf("%d, %d\n", i, j); 183 | return (0); 184 | } 185 | julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c -o d 186 | julien@ubuntu:~/0x0c. macro, structures$ ./d 187 | 980, 980 188 | julien@ubuntu:~/0x0c. macro, structures$ 189 | 190 | ``` 191 | 192 | **Repo:** 193 | 194 | - GitHub repository: `alx-low_level_programming` 195 | - Directory: `0x0D-preprocessor` 196 | - File: `3-function_like_macro.h` 197 | 198 | ### 4\. SUM 199 | 200 | mandatory 201 | 202 | Write a function-like macro `SUM(x, y)` that computes the sum of the numbers `x` and `y`. 203 | 204 | ``` 205 | julien@ubuntu:~/0x0c. macro, structures$ cat 4-main.c 206 | #include 207 | #include "4-sum.h" 208 | #include "4-sum.h" 209 | 210 | /** 211 | * main - check the code 212 | * 213 | * Return: Always 0. 214 | */ 215 | int main(void) 216 | { 217 | int s; 218 | 219 | s = SUM(98, 1024); 220 | printf("%d\n", s); 221 | return (0); 222 | } 223 | julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c -o e 224 | julien@ubuntu:~/0x0c. macro, structures$ ./e 225 | 1122 226 | julien@ubuntu:~/0x0c. macro, structures$ 227 | 228 | ``` 229 | 230 | **Repo:** 231 | 232 | - GitHub repository: `alx-low_level_programming` 233 | - Directory: `0x0D-preprocessor` 234 | - File: `4-sum.h` 235 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/.gitignore: -------------------------------------------------------------------------------- 1 | main.c 2 | tests/ -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - inits a variable of 5 | *type dog 6 | *@d: dog identification 7 | *@name: name of dog 8 | *@age: age of dog 9 | *@owner: owner's name 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 | 3 | /** 4 | * print_dog - prints a struct dog 5 | *@d: dog identification 6 | * 7 | */ 8 | 9 | void print_dog(struct dog *d) 10 | { 11 | if (d == NULL ) 12 | return; 13 | 14 | if ((*d).name == NULL) 15 | printf("nil\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | if ((*d).age < 0) 20 | printf("nil\n"); 21 | else 22 | printf("Age: %f\n", d->age); 23 | 24 | if ((*d).owner == NULL) 25 | printf("nil\n"); 26 | else 27 | printf("Owner: %s\n", d->owner); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | 4 | /** 5 | *_strlen - returns length of 6 | *a string 7 | *@str: string to be counted 8 | *Return: returns length of string 9 | */ 10 | int _strlen(char *str) 11 | { 12 | int len = 0; 13 | while (str) 14 | len++; 15 | 16 | return (len); 17 | } 18 | 19 | 20 | /** 21 | *_strcopy - copy string pointed by src 22 | *into dest variable 23 | *@dest:buffer storing string copy 24 | *@src: buffer storing string to copy 25 | *Return:returns copied string 26 | */ 27 | char *_strcopy(char *dest, char *src) 28 | { 29 | int index = 0; 30 | 31 | for (; src[index] ; index++) 32 | dest[index] = src[index]; 33 | 34 | dest[index] = '\0'; 35 | return (dest); 36 | } 37 | 38 | 39 | 40 | 41 | /** 42 | *new_dog - creates a new dog 43 | *@name: name of new dog 44 | *@age: age of new dog 45 | *@owner: owner of new dog 46 | *Return: returns NULL in case 47 | *of failure 48 | */ 49 | dog_t *new_dog(char *name, float age, char *owner) 50 | { 51 | dog_t *doggo; 52 | 53 | if (name == NULL || age < 0 || owner == NULL) 54 | return (NULL); 55 | 56 | doggo = malloc(sizeof(dog_t)); 57 | if (doggo == NULL) 58 | return (NULL); 59 | 60 | doggo->name = malloc(sizeof(char) * (_strlen(name) + 1)); 61 | if (doggo->name == NULL) 62 | { 63 | free(doggo); 64 | return (NULL); 65 | } 66 | 67 | doggo->owner = malloc(sizeof(char) * (_strlen(owner) + 1)); 68 | if (doggo->owner == NULL) 69 | { 70 | free(doggo->name); 71 | free(doggo); 72 | return (NULL); 73 | } 74 | 75 | doggo->name = _strcopy(doggo->name, name); 76 | doggo->age = age; 77 | doggo->owner = _strcopy(doggo->owner, owner); 78 | 79 | return (doggo); 80 | } 81 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | *free_dog - Frees dog 5 | *@d: dog to be freed 6 | * 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d == NULL) 11 | return; 12 | 13 | free(d->name); 14 | free(d->owner); 15 | free(d); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | *struct dog - a new type of data 9 | *representing a dog 10 | *@name: name of dog 11 | *@age: age of dog 12 | *@owner: owner's name 13 | */ 14 | struct dog 15 | { 16 | char *name; 17 | float age; 18 | char *owner; 19 | }; 20 | 21 | 22 | /** 23 | *dog_t - Typedef for struct dog 24 | */ 25 | typedef struct dog dog_t; 26 | 27 | 28 | void init_dog(struct dog *d, char *name, float age, char *owner); 29 | void print_dog(struct dog *d); 30 | dog_t *new_dog(char *name, float age, char *owner); 31 | void free_dog(dog_t *d); 32 | int _strlen(char *str); 33 | char *_strcopy(char *dest, char *src); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /0x0F-function_pointers/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ 2 | main.c 3 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | *print_name - prints a name 5 | *@name:name to print 6 | *@f: function which print name 7 | */ 8 | 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | 12 | if (name == NULL || f == NULL) 13 | return; 14 | 15 | f(name); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | *array_iterator - executes a function on each element of an array 5 | *@array: array to be targeted 6 | *@size: size of array 7 | *@action: function to be executed 8 | * 9 | */ 10 | 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | 14 | if (array == NULL || action == NULL) 15 | return; 16 | 17 | while (size-- > 0) 18 | { 19 | action(*array); 20 | array++; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - program that prints the opcodes of its own main function. 6 | *@argc: number of arguments passed to the program 7 | *@argv: array of arguments 8 | * 9 | *Return: on success, 1 or 2 in case of failure 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | 15 | int index, bytes; 16 | int (*address)(int, char **) = main; 17 | unsigned char opcode; 18 | 19 | if (argc != 2) 20 | { 21 | printf("Error\n"); 22 | exit(1); 23 | } 24 | 25 | bytes = atoi(argv[1]); 26 | 27 | if (bytes < 0) 28 | { 29 | printf("Error\n"); 30 | exit(2); 31 | } 32 | 33 | for (index = 0; index < bytes; index++) 34 | { 35 | opcode = *(unsigned char *)address; 36 | printf("%.2x", opcode); 37 | 38 | if (index == bytes - 1) 39 | continue; 40 | printf(" "); 41 | address++; 42 | } 43 | printf("\n"); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | *int_index - searches for an integer 5 | *@array: Array containing elements 6 | *@size: number of elements in array 7 | *@cmp: function which compare values 8 | * 9 | *Return: returns -1 if no element match 10 | * or when size is less than zero 11 | *return pointer to the first corresponding element 12 | * 13 | */ 14 | 15 | int int_index(int *array, int size, int (*cmp)(int)) 16 | { 17 | int index = 0; 18 | 19 | if (array == NULL || cmp == NULL) 20 | return (-1); 21 | 22 | for (; index < size; index++) 23 | { 24 | if (cmp(array[index]) != 0) 25 | return (index); 26 | } 27 | 28 | return (-1); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | 4 | /** 5 | *struct op - Struct op 6 | *@op: The operator 7 | *@f: The function associated 8 | */ 9 | typedef struct op 10 | { 11 | char *op; 12 | int (*f)(int a, int b); 13 | } op_t; 14 | 15 | int op_add(int a, int b); 16 | int op_sub(int a, int b); 17 | int op_mul(int a, int b); 18 | int op_div(int a, int b); 19 | int op_mod(int a, int b); 20 | int (*get_op_func(char *s))(int, int); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | *get_op_func - selects the correct function to perform 6 | *the operation asked by the user. You’re not allowed 7 | *to declare any other function. 8 | *@s: operator passed as argument 9 | * 10 | *Return: A pointer to the function corresponding to 11 | *the operator given in parameter 12 | */ 13 | int (*get_op_func(char *s))(int, int) 14 | { 15 | 16 | op_t ops[] = { 17 | {"+", op_add}, 18 | {"-", op_sub}, 19 | {"*", op_mul}, 20 | {"/", op_div}, 21 | {"%", op_mod}, 22 | {NULL, NULL} 23 | }; 24 | int i = 0; 25 | 26 | while (ops[i].op != NULL && *(ops[i].op) != *s) 27 | i++; 28 | 29 | return (ops[i].f); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints the result of simple operations. 7 | * @argc: The number of arguments supplied to the program. 8 | * @argv: An array of pointers to the arguments. 9 | * 10 | * Return: Always 0. 11 | */ 12 | 13 | int main(int __attribute__((__unused__)) argc, char *argv[]) 14 | { 15 | int num1, num2; 16 | char *op; 17 | 18 | if (argc != 4) 19 | { 20 | printf("Error\n"); 21 | exit(98); 22 | } 23 | 24 | num1 = atoi(argv[1]); 25 | op = argv[2]; 26 | num2 = atoi(argv[3]); 27 | 28 | if (get_op_func(op) == NULL || op[1] != '\0') 29 | { 30 | printf("Error\n"); 31 | exit(99); 32 | } 33 | 34 | if ((*op == '/' && num2 == 0) || (*op == '%' && num2 == 0)) 35 | { 36 | printf("Error\n"); 37 | exit(100); 38 | } 39 | printf("%d\n", get_op_func(op)(num1, num2)); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | *op_add - Returns the sum of two numbers 5 | *@a:the first number 6 | *@b:the second number 7 | * 8 | *Return:The sum of a and b 9 | */ 10 | 11 | int op_add(int a, int b) 12 | { 13 | return (a + b); 14 | } 15 | 16 | 17 | /** 18 | *op_sub - Returns the substraction of two numbers 19 | *@a:The first number 20 | *@b:The second number 21 | * 22 | *Return: The Difference of a and b 23 | */ 24 | int op_sub(int a, int b) 25 | { 26 | return (a - b); 27 | } 28 | 29 | /** 30 | *op_mul - Returns the multiplication of two numbers 31 | *@a:The first number 32 | *@b: The second number 33 | * 34 | *Return: The multiplication of a and b 35 | */ 36 | int op_mul(int a, int b) 37 | { 38 | return (a * b); 39 | } 40 | 41 | 42 | /** 43 | *op_div - Returns the division of two numbers. 44 | *@a: The first number. 45 | *@b: The second number. 46 | * 47 | *Return: The quotient of a and b. 48 | */ 49 | int op_div(int a, int b) 50 | { 51 | return (a / b); 52 | } 53 | 54 | /** 55 | *op_mod - Returns the remainder of the division of two numbers. 56 | *@a: The first number. 57 | *@b: The second number. 58 | * 59 | *Return: The remainder of the division of a by b. 60 | */ 61 | int op_mod(int a, int b) 62 | { 63 | return (a % b); 64 | } 65 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | 4 | #include 5 | #include 6 | 7 | void print_name(char *name, void (*f)(char *)); 8 | void array_iterator(int *array, size_t size, void (*action)(int)); 9 | int int_index(int *array, int size, int (*cmp)(int)); 10 | 11 | #endif /*MAIN_H*/ 12 | -------------------------------------------------------------------------------- /0x10-variadic_functions/.gitignore: -------------------------------------------------------------------------------- 1 | main.c 2 | 3 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | *sum_them_all - sums all its parameters 6 | *@n: first parameter 7 | * 8 | *Return: 0 if n is null 9 | * or sum of parameters in other case 10 | */ 11 | 12 | int sum_them_all(const unsigned int n, ...) 13 | { 14 | 15 | va_list li; 16 | int sum = 0; 17 | unsigned int i; 18 | 19 | va_start(li, n); 20 | 21 | if (n != 0) 22 | { 23 | for (i = 0; i < n; i++) 24 | sum += va_arg(li, int); 25 | } 26 | 27 | va_end(li); 28 | return (sum); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | *print_numbers - prints numbers followed by 5 | *new line 6 | *@separator: string to be printed between numbers 7 | *@n: number of integer to be passed 8 | * 9 | *Return: nothing 10 | */ 11 | 12 | void print_numbers(const char *separator, const unsigned int n, ...) 13 | { 14 | 15 | va_list li; 16 | unsigned int i; 17 | 18 | va_start(li, n); 19 | 20 | for (i = 0; i < n; i++) 21 | { 22 | 23 | 24 | printf("%d", va_arg(li, int)); 25 | 26 | if (i != (n - 1) && separator != NULL) 27 | printf("%s", separator); 28 | } 29 | printf("\n"); 30 | va_end(li); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - prints strings 5 | *@separator: separator 6 | *@n: number of strings 7 | * 8 | *Return: nothing 9 | */ 10 | void print_strings(const char *separator, const unsigned int n, ...) 11 | { 12 | 13 | va_list li; 14 | unsigned int i; 15 | char *str; 16 | 17 | va_start(li, n); 18 | 19 | for (i = 0; i < n; i++) 20 | { 21 | str = va_arg(li, char *); 22 | if (str == NULL) 23 | printf("(nil)"); 24 | else 25 | printf("%s", str); 26 | 27 | if (i != (n - 1) && separator != NULL) 28 | printf("%s", separator); 29 | 30 | } 31 | 32 | printf("\n"); 33 | va_end(li); 34 | } 35 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | 4 | /** 5 | * print_char - Prints a char. 6 | * @arg: A list of arguments pointing to 7 | * the character to be printed. 8 | */ 9 | void print_char(va_list arg) 10 | { 11 | char letter; 12 | 13 | letter = va_arg(arg, int); 14 | 15 | printf("%c", letter); 16 | } 17 | 18 | /** 19 | * print_int - Prints an int. 20 | * @arg: A list of arguments pointing to 21 | * the integer to be printed. 22 | */ 23 | void print_int(va_list arg) 24 | { 25 | int num; 26 | 27 | num = va_arg(arg, int); 28 | 29 | printf("%d", num); 30 | } 31 | 32 | 33 | /** 34 | * print_float - Prints a float. 35 | * @arg: A list of arguments pointing to 36 | * the float to be printed. 37 | */ 38 | void print_float(va_list arg) 39 | { 40 | float num; 41 | 42 | num = va_arg(arg, double); 43 | 44 | printf("%f", num); 45 | } 46 | 47 | 48 | 49 | /** 50 | * print_string - Prints a string. 51 | * @arg: A list of arguments pointing to 52 | * the string to be printed. 53 | */ 54 | void print_string(va_list arg) 55 | { 56 | char *str; 57 | 58 | str = va_arg(arg, char *); 59 | 60 | if (str == NULL) 61 | { 62 | printf("(nil)"); 63 | return; 64 | } 65 | 66 | printf("%s", str); 67 | } 68 | 69 | /** 70 | *print_all - prints anything 71 | *@format: format of input 72 | * 73 | *Return: nothing 74 | */ 75 | void print_all(const char * const format, ...) 76 | { 77 | va_list args; 78 | 79 | int i = 0, j = 0; 80 | 81 | char *separator = ""; 82 | 83 | printer_t funcs[] = { 84 | {"c", print_char}, 85 | {"i", print_int}, 86 | {"f", print_float}, 87 | {"s", print_string} 88 | }; 89 | 90 | va_start(args, format); 91 | 92 | while (format && (*(format + i))) 93 | { 94 | j = 0; 95 | 96 | while (j < 4 && (*(format + i) != *(funcs[j].symbol))) 97 | j++; 98 | 99 | if (j < 4) 100 | { 101 | printf("%s", separator); 102 | funcs[j].print(args); 103 | separator = ", "; 104 | } 105 | 106 | i++; 107 | 108 | } 109 | 110 | printf("\n"); 111 | 112 | va_end(args); 113 | } 114 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | 4 | #include 5 | #include 6 | 7 | typedef struct printer{ 8 | char *symbol; 9 | void (*print)(va_list arg); 10 | } printer_t; 11 | 12 | 13 | int sum_them_all(const unsigned int n, ...); 14 | void print_numbers(const char *separator, const unsigned int n, ...); 15 | void print_strings(const char *separator, const unsigned int n, ...); 16 | void print_char(va_list arg); 17 | void print_int(va_list arg); 18 | void print_float(va_list arg); 19 | void print_string(va_list arg); 20 | void print_all(const char * const format, ...); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ 2 | main.c 3 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_list - prints all the elements of a list 5 | * 6 | * @h:pointer to the list 7 | * 8 | *Return: the number of nodes 9 | */ 10 | 11 | size_t print_list(const list_t *h) 12 | { 13 | const list_t *cursor = h; 14 | size_t count = 0; 15 | 16 | while (cursor != NULL) 17 | { 18 | if (cursor->str != NULL) 19 | printf("[%d] %s\n", cursor->len, cursor->str); 20 | else 21 | printf("[0] (nil)\n"); 22 | count += 1; 23 | cursor = cursor->next; 24 | } 25 | 26 | return (count); 27 | } 28 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - finds the number of elements in a linked list 5 | *@h: pointer to the list 6 | * 7 | *Return: the number of elements 8 | */ 9 | 10 | size_t list_len(const list_t *h) 11 | { 12 | size_t elements = 0; 13 | 14 | while (h) 15 | { 16 | elements++; 17 | h = h->next; 18 | } 19 | 20 | return (elements); 21 | } 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void __attribute__((constructor)) hare(void); 4 | 5 | /** 6 | * hare - Prints a string before the 7 | * main function is executed. 8 | */ 9 | void hare(void) 10 | { 11 | printf("You're beat! and yet, you must allow,\n" 12 | "I bore my house upon my back!\n"); 13 | } 14 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | extern printf 2 | 3 | section .text 4 | global main 5 | 6 | main: 7 | push rbp 8 | 9 | mov rdi, fmt 10 | mov rsi, msg 11 | mov rax, 0 12 | call printf 13 | 14 | pop rbp 15 | 16 | mov rax, 0 17 | ret 18 | 19 | section .data 20 | msg: db "Hello, Holberton", 0 21 | fmt: db "%s", 10, 0 -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node - adds a new node at the beginning of a list 5 | *@head: pointer to the head of the list 6 | *@str: string to be added 7 | * 8 | * Return: returns the address to the new element or NULL 9 | * if failed 10 | */ 11 | 12 | list_t *add_node(list_t **head, const char *str) 13 | { 14 | char *dup; 15 | int len; 16 | list_t *new; 17 | 18 | new = malloc(sizeof(list_t)); 19 | if (new == NULL) 20 | return (NULL); 21 | 22 | dup = strdup(str); 23 | if (dup == NULL) 24 | { 25 | free(new); 26 | return (NULL); 27 | } 28 | for (len = 0; str[len];) 29 | len++; 30 | 31 | new->str = dup; 32 | new->len = len; 33 | new->next = *head; 34 | 35 | *head = new; 36 | 37 | return (new); 38 | 39 | } 40 | 41 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - Adds a new node at the end of a list 5 | *@head: pointer to the head of the list 6 | *@str: string to be added to the list 7 | * 8 | *Return: NULL in case of failure 9 | *or address or the element 10 | */ 11 | 12 | list_t *add_node_end(list_t **head, const char *str) 13 | { 14 | char *dup; 15 | int len; 16 | list_t *new, *last; 17 | 18 | new = malloc(sizeof(list_t)); 19 | if (new == NULL) 20 | return (NULL); 21 | 22 | dup = strdup(str); 23 | if (str == NULL) 24 | { 25 | free(new); 26 | return (NULL); 27 | } 28 | 29 | for (len = 0; str[len];) 30 | len++; 31 | 32 | new->str = dup; 33 | new->len = len; 34 | new->next = NULL; 35 | 36 | if (*head == NULL) 37 | *head = new; 38 | else 39 | { 40 | last = *head; 41 | while (last->next != NULL) 42 | last = last->next; 43 | last->next = new; 44 | } 45 | return (*head); 46 | } 47 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *free_list - Frees a list 5 | *@head: A pointer to the list head 6 | * 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *tmp; 12 | 13 | while (head) 14 | { 15 | tmp = head->next; 16 | free(head->str); 17 | free(head); 18 | head = tmp; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | */ 16 | typedef struct list_s 17 | { 18 | char *str; 19 | unsigned int len; 20 | struct list_s *next; 21 | } list_t; 22 | 23 | size_t print_list(const list_t *h); 24 | size_t list_len(const list_t *h); 25 | list_t *add_node(list_t **head, const char *str); 26 | list_t *add_node_end(list_t **head, const char *str); 27 | void free_list(list_t *head); 28 | 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ 2 | main.c 3 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the elements of a linked list 5 | * @h: head of the list 6 | * 7 | * Return: the number of nodes 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | const listint_t *cursor = h; 12 | size_t count = 0; 13 | 14 | while (cursor != NULL) 15 | { 16 | printf("%d\n", cursor->n); 17 | count += 1; 18 | cursor = cursor->next; 19 | } 20 | return (count); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | 4 | /** 5 | *listint_len - Returns the number of elements 6 | *in a linked listint_t list 7 | *@h: A pointer to the head of the list 8 | * 9 | * 10 | *Return: the number of elements in the listint_t list 11 | */ 12 | 13 | size_t listint_len(const listint_t *h) 14 | { 15 | size_t nodes = 0; 16 | 17 | while (h) 18 | { 19 | nodes++; 20 | h = h->next; 21 | } 22 | return (nodes); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes a node at an index 5 | * @head: pointer to the head of the list 6 | * @index: index of the node to be added 7 | * 8 | * Return: the address of the node 9 | */ 10 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 11 | { 12 | listint_t *old_node = NULL; 13 | listint_t *previous_node = NULL; 14 | unsigned int i = 0, list_len = listint_len(*head); 15 | 16 | if ((index > list_len) || (list_len == 0)) 17 | return (-1); 18 | while (head != NULL) 19 | { 20 | if (i == index) 21 | { 22 | old_node = *head; 23 | if (i == 0) 24 | { 25 | *head = old_node->next; 26 | free(old_node); 27 | return (1); 28 | } 29 | previous_node->next = old_node->next; 30 | free(old_node); 31 | return (1); 32 | } 33 | else if ((i + 1) == index) 34 | previous_node = *head; 35 | head = &((*head)->next); 36 | i++; 37 | } 38 | return (-1); 39 | } 40 | 41 | /** 42 | * listint_len - counts the number of nodes in a linked list 43 | * @h: head of the list 44 | * 45 | * Return: the number of elements 46 | */ 47 | size_t listint_len(const listint_t *h) 48 | { 49 | const listint_t *cursor = h; 50 | size_t count = 0; 51 | 52 | while (cursor != NULL) 53 | { 54 | count += 1; 55 | cursor = cursor->next; 56 | } 57 | return (count); 58 | } 59 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *add_nodeint - Adds a new node at the beginning 5 | *of a listint_t list 6 | *@head: A pointer to the address of the head of the listint_t list 7 | *@n: the integer for the new node to contain 8 | * 9 | *Return: NULL if function fails 10 | *Or address of element 11 | */ 12 | 13 | listint_t *add_nodeint(listint_t **head, const int n) 14 | { 15 | listint_t *new; 16 | 17 | new = malloc(sizeof(listint_t)); 18 | if (new == NULL) 19 | return (NULL); 20 | 21 | new->n = n; 22 | new->next = *head; 23 | 24 | *head = new; 25 | 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *add_nodeint_end - adds a node to the end of a linked list 5 | *@head: pointer to the head of the list 6 | *@n: number to be used as content 7 | * 8 | * 9 | *Return: address of the newly added node 10 | */ 11 | 12 | listint_t *add_nodeint_end(listint_t **head, const int n) 13 | { 14 | listint_t *new_node; 15 | listint_t *cursor = *head; 16 | 17 | new_node = malloc(sizeof(listint_t)); 18 | if (new_node != NULL) 19 | { 20 | new_node->n = n; 21 | new_node->next = NULL; 22 | } 23 | else 24 | return (NULL); 25 | if (cursor != NULL) 26 | { 27 | while (cursor->next != NULL) 28 | cursor = cursor->next; 29 | 30 | cursor->next = new_node; 31 | } 32 | else 33 | *head = new_node; 34 | return (new_node); 35 | } 36 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *free_listint - frees a linked list 5 | *@head: head of the list 6 | * 7 | *Return: void 8 | */ 9 | 10 | void free_listint(listint_t *head) 11 | { 12 | listint_t *temp; 13 | 14 | while (head != NULL) 15 | { 16 | temp = head; 17 | head = head->next; 18 | free(temp); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *free_listint2 - frees a linked list 5 | *@head: pointer to the head of the list 6 | * 7 | *Return: void 8 | */ 9 | 10 | void free_listint2(listint_t **head) 11 | { 12 | listint_t *cursor; 13 | listint_t **temp = head; 14 | 15 | if (temp != NULL) 16 | { 17 | while (*head != NULL) 18 | { 19 | cursor = *head; 20 | free(cursor); 21 | *head = (*head)->next; 22 | } 23 | 24 | *temp = NULL; 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *pop_listint - deletes the head node of a linked list 5 | *@head: head of the list 6 | * 7 | *Return: the deleted node data 8 | */ 9 | 10 | int pop_listint(listint_t **head) 11 | { 12 | listint_t *popped; 13 | int content; 14 | 15 | if (*head == NULL) 16 | return (0); 17 | 18 | popped = *head; 19 | content = popped->n; 20 | free(popped); 21 | 22 | *head = (*head)->next; 23 | return (content); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *get_nodeint_at_index - return the nth node of a linked list 5 | *@head: pointer to the head of the list 6 | *@index: index of the node required 7 | * 8 | *Return: the address of the node 9 | */ 10 | 11 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 12 | { 13 | listint_t *cursor = NULL; 14 | unsigned int i = 0; 15 | 16 | while (head != NULL) 17 | { 18 | if (i <= index) 19 | { 20 | if (i == index) 21 | { 22 | cursor = head; 23 | break; 24 | } 25 | head = head->next; 26 | i++; 27 | } 28 | else 29 | return (NULL); 30 | } 31 | return (cursor); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *sum_listint - sums up all the data in a linked list 5 | *@head: head of the list 6 | * 7 | *Return: sum of the number 8 | */ 9 | 10 | int sum_listint(listint_t *head) 11 | { 12 | listint_t *cursor = head; 13 | size_t sum = 0; 14 | 15 | while (cursor != NULL) 16 | { 17 | sum += cursor->n; 18 | cursor = cursor->next; 19 | } 20 | return (sum); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - returns the nth node of a linked list 5 | * @head: pointer to the head of the list 6 | * @idx: index of the node to be added 7 | * @n: content of the new node 8 | * 9 | * Return: the address of the node 10 | */ 11 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 12 | { 13 | listint_t *new_node = NULL; 14 | listint_t *previous_node = NULL; 15 | unsigned int i = 0; 16 | 17 | new_node = malloc(sizeof(listint_t)); 18 | if (new_node == NULL || idx > listint_len(*head)) 19 | { 20 | free(new_node); 21 | return (NULL); 22 | } 23 | new_node->n = n; 24 | new_node->next = NULL; 25 | while (head != NULL) 26 | { 27 | if (i == idx) 28 | { 29 | if (i == 0) 30 | { 31 | new_node->next = *head; 32 | *head = new_node; 33 | return (new_node); 34 | } 35 | new_node->next = previous_node->next; 36 | previous_node->next = new_node; 37 | return (new_node); 38 | } 39 | else if ((i + 1) == idx) 40 | previous_node = *head; 41 | head = &((*head)->next); 42 | i++; 43 | } 44 | return (NULL); 45 | } 46 | 47 | /** 48 | * listint_len - counts the number of nodes in a linked list 49 | * @h: head of the list 50 | * 51 | * Return: the number of elements 52 | */ 53 | size_t listint_len(const listint_t *h) 54 | { 55 | const listint_t *cursor = h; 56 | size_t count = 0; 57 | 58 | while (cursor != NULL) 59 | { 60 | count += 1; 61 | cursor = cursor->next; 62 | } 63 | return (count); 64 | } 65 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * @n: integer 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | * 14 | */ 15 | typedef struct listint_s 16 | { 17 | int n; 18 | struct listint_s *next; 19 | } listint_t; 20 | 21 | 22 | size_t print_listint(const listint_t *h); 23 | size_t listint_len(const listint_t *h); 24 | listint_t *add_nodeint(listint_t **head, const int n); 25 | listint_t *add_nodeint_end(listint_t **head, const int n); 26 | void free_listint(listint_t *head); 27 | void free_listint2(listint_t **head); 28 | int pop_listint(listint_t **head); 29 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 30 | int sum_listint(listint_t *head); 31 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 32 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ 2 | main.c 3 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * binary_to_uint - converts a binary to an unsigned int 4 | * @b: the binary number as a string 5 | * 6 | * Return: the converted value 7 | */ 8 | unsigned int binary_to_uint(const char *b) 9 | { 10 | unsigned int decimal = 0; 11 | int str_len = 0, base = 1; 12 | 13 | if (!check_valid_string(b)) 14 | return (0); 15 | 16 | while (b[str_len] != '\0') 17 | str_len++; 18 | 19 | while (str_len) 20 | { 21 | decimal += ((b[str_len - 1] - '0') * base); 22 | base *= 2; 23 | str_len--; 24 | } 25 | return (decimal); 26 | } 27 | 28 | /** 29 | * check_valid_string - checks if a string has only 0's and 1's 30 | * @b: string to be checked 31 | * 32 | * Return: 1 if string is valid, 0 otherwise 33 | */ 34 | int check_valid_string(const char *b) 35 | { 36 | if (b == NULL) 37 | return (0); 38 | 39 | while (*b) 40 | { 41 | if (*b != '1' && *b != '0') 42 | return (0); 43 | b++; 44 | } 45 | return (1); 46 | } -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *print_binary - prints the binary representation of a number 5 | *@n: number to be printed 6 | * 7 | *Return: void 8 | */ 9 | 10 | void print_binary(unsigned long int n) 11 | { 12 | if (n > 1) 13 | print_binary(n >> 1); 14 | 15 | _putchar((n & 1) + '0'); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks whether a machine is big endian or little 5 | * 6 | * Return: 1 if little endian, 0 otherwise 7 | */ 8 | 9 | int get_endianness(void) 10 | { 11 | unsigned int test = 1; 12 | char *endian = (char *)&test; 13 | 14 | if (*endian) 15 | return (1); 16 | return (0); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *get_bit - get the value of a bit at the given index 5 | *@n: the number 6 | *@index: index starting from 0 of the bit required 7 | * 8 | *Return: The converted value 9 | */ 10 | 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | if (index > 63) 14 | return (-1); 15 | 16 | return ((n >> index) & 1); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *set_bit - sets a bit at position to 1 5 | *@n: number to be used 6 | *@index: index to be set 7 | * 8 | *Return: 1 on success, -1 on failure 9 | */ 10 | 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | if (n == NULL || (index > (sizeof(unsigned long int) * 8) - 1)) 14 | return (-1); 15 | 16 | *n |= (1 << index); 17 | return (1); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *clear_bit - sets a bit at a position to 0 5 | *@n: number to be used 6 | *@index: position to be cleared 7 | * 8 | *Return: 1 on success, -1 on fail 9 | */ 10 | 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | if (n == NULL || (index > (sizeof(unsigned long int) * 8) - 1)) 14 | return (-1); 15 | 16 | *n &= ~(1 << index); 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *flip_bits - gets the number of bits to flip to get from n to m 5 | *@n: initial number 6 | *@m: final number 7 | * 8 | *Return: the number of flipped bits 9 | */ 10 | 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned long int flipped = n ^ m; 14 | int count = 0; 15 | 16 | while (flipped) 17 | { 18 | if (flipped & 1) 19 | count++; 20 | flipped >>= 1; 21 | } 22 | return (count); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | 8 | unsigned int binary_to_uint(const char *b); 9 | int check_valid_string(const char *b); 10 | int _putchar(char c); 11 | void print_binary(unsigned long int n); 12 | int get_bit(unsigned long int n, unsigned int index); 13 | int set_bit(unsigned long int *n, unsigned int index); 14 | int clear_bit(unsigned long int *n, unsigned int index); 15 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 16 | int get_endianness(void); 17 | 18 | 19 | #endif -------------------------------------------------------------------------------- /0x15-file_io/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ 2 | main.c -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * read_textfile - Reads a text file and prints it to POSIX stdout. 6 | * @filename: A pointer to the name of the file. 7 | * @letters: The number of letters the 8 | * function should read and print. 9 | * 10 | * Return: If the function fails or filename is NULL - 0. 11 | * O/w - the actual number of bytes the function can read and print. 12 | */ 13 | 14 | ssize_t read_textfile(const char *filename, size_t letters) 15 | { 16 | ssize_t o, r, w; 17 | char *buffer; 18 | 19 | if (filename == NULL) 20 | return (0); 21 | 22 | buffer = malloc(sizeof(char) * letters); 23 | if (buffer == NULL) 24 | return (0); 25 | 26 | o = open(filename, O_RDONLY); 27 | r = read(o, buffer, letters); 28 | w = write(STDOUT_FILENO, buffer, r); 29 | 30 | if (o == -1 || r == -1 || w == -1 || w != r) 31 | { 32 | free(buffer); 33 | return (0); 34 | } 35 | 36 | free(buffer); 37 | close(o); 38 | 39 | return (w); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - Creates a file. 5 | * @filename: A pointer to the name of the file to create. 6 | * @text_content: A pointer to a string to write to the file. 7 | * 8 | * Return: If the function fails - -1. 9 | * Otherwise - 1. 10 | */ 11 | 12 | int create_file(const char *filename, char *text_content) 13 | { 14 | int o, w, len = 0; 15 | 16 | if (filename == NULL) 17 | return (-1); 18 | 19 | if (text_content != NULL) 20 | { 21 | for (len = 0; text_content[len];) 22 | len++; 23 | } 24 | 25 | o = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600); 26 | w = write(o, text_content, len); 27 | 28 | if (o == -1 || w == -1) 29 | return (-1); 30 | 31 | close(o); 32 | 33 | return (1); 34 | } 35 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include"main.h" 2 | 3 | /** 4 | *append_text_to_file - appends text at the end of a file 5 | *@filename: A pointer to the name of the file. 6 | *@text_content: The string to add to the end of the file. 7 | * 8 | *Return: If the function fails or filename is NULL -1 9 | * if the file does not exist the user lacks write permissions -1 10 | * otherwise 1 11 | */ 12 | 13 | int append_text_to_file(const char *filename, char *text_content) 14 | { 15 | int o, w, len = 0; 16 | 17 | if (filename == NULL) 18 | return (-1); 19 | 20 | if (text_content != NULL) 21 | { 22 | for (len = 0; text_content[len];) 23 | len++; 24 | } 25 | 26 | o = open(filename, O_WRONLY | O_APPEND); 27 | w = write(o, text_content, len); 28 | 29 | if (o == -1 || w == -1) 30 | return (-1); 31 | 32 | close(o); 33 | 34 | return (1); 35 | } 36 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | char *create_buffer(char *file); 6 | void close_file(int fd); 7 | 8 | /** 9 | * create_buffer - Allocates 1024 bytes for a buffer. 10 | * @file: The name of the file buffer is storing chars for. 11 | * 12 | * Return: A pointer to the newly-allocated buffer. 13 | */ 14 | char *create_buffer(char *file) 15 | { 16 | char *buffer; 17 | 18 | buffer = malloc(sizeof(char) * 1024); 19 | 20 | if (buffer == NULL) 21 | { 22 | dprintf(STDERR_FILENO, 23 | "Error: Can't write to %s\n", file); 24 | exit(99); 25 | } 26 | 27 | return (buffer); 28 | } 29 | 30 | /** 31 | * close_file - Closes file descriptors. 32 | * @fd: The file descriptor to be closed. 33 | */ 34 | void close_file(int fd) 35 | { 36 | int c; 37 | 38 | c = close(fd); 39 | 40 | if (c == -1) 41 | { 42 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd); 43 | exit(100); 44 | } 45 | } 46 | 47 | /** 48 | * main - Copies the contents of a file to another file. 49 | * @argc: The number of arguments supplied to the program. 50 | * @argv: An array of pointers to the arguments. 51 | * 52 | * Return: 0 on success. 53 | * 54 | * Description: If the argument count is incorrect - exit code 97. 55 | * If file_from does not exist or cannot be read - exit code 98. 56 | * If file_to cannot be created or written to - exit code 99. 57 | * If file_to or file_from cannot be closed - exit code 100. 58 | */ 59 | int main(int argc, char *argv[]) 60 | { 61 | int from, to, r, w; 62 | char *buffer; 63 | 64 | if (argc != 3) 65 | { 66 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"); 67 | exit(97); 68 | } 69 | 70 | buffer = create_buffer(argv[2]); 71 | from = open(argv[1], O_RDONLY); 72 | r = read(from, buffer, 1024); 73 | to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0664); 74 | 75 | do { 76 | if (from == -1 || r == -1) 77 | { 78 | dprintf(STDERR_FILENO, 79 | "Error: Can't read from file %s\n", argv[1]); 80 | free(buffer); 81 | exit(98); 82 | } 83 | 84 | w = write(to, buffer, r); 85 | if (to == -1 || w == -1) 86 | { 87 | dprintf(STDERR_FILENO, 88 | "Error: Can't write to %s\n", argv[2]); 89 | free(buffer); 90 | exit(99); 91 | } 92 | 93 | r = read(from, buffer, 1024); 94 | to = open(argv[2], O_WRONLY | O_APPEND); 95 | 96 | } while (r > 0); 97 | 98 | free(buffer); 99 | close_file(from); 100 | close_file(to); 101 | 102 | return (0); 103 | } 104 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif /* MAIN_H */ 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ 2 | -------------------------------------------------------------------------------- /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: pointer to the head of the list 6 | * 7 | * Return: number of nodes 8 | */ 9 | 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | 13 | int counter = 0; 14 | 15 | if (h == NULL) 16 | return (counter); 17 | 18 | while (h->prev != NULL) 19 | h = h->prev; 20 | 21 | while (h != NULL) 22 | { 23 | printf("%d\n", h->n); 24 | h = h->next; 25 | counter++; 26 | } 27 | 28 | return (counter); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements in a linked dlistint_t list. 5 | * @h: pointer to the head of the list 6 | * 7 | * Return: number of nodes 8 | */ 9 | 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | int counter = 0; 13 | 14 | if (h == NULL) 15 | return (counter); 16 | 17 | while (h != NULL) 18 | { 19 | counter++; 20 | h = h->next; 21 | } 22 | 23 | return (counter); 24 | } 25 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x17-doubly_linked_lists/103-keygen.c -------------------------------------------------------------------------------- /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: double pointer to the head of the list 6 | * @n: data to be added 7 | * 8 | * Return: he address of the new element, or NULL if it failed 9 | */ 10 | 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new_node; 14 | 15 | new_node = malloc(sizeof(dlistint_t)); 16 | if (new_node == NULL) 17 | return (NULL); 18 | 19 | new_node->n = n; 20 | new_node->next = *head; 21 | new_node->prev = NULL; 22 | if (*head != NULL) 23 | (*head)->prev = new_node; 24 | *head = new_node; 25 | return (new_node); 26 | } 27 | -------------------------------------------------------------------------------- /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: double pointer to the head of the list 6 | * @n: integer to add to the new node 7 | * 8 | * Return: the address of the new element, or NULL if it failed 9 | */ 10 | 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new_node; 14 | 15 | new_node = malloc(sizeof(dlistint_t)); 16 | 17 | if (new_node == NULL) 18 | return (NULL); 19 | 20 | new_node->n = n; 21 | new_node->next = NULL; 22 | 23 | if (*head == NULL) 24 | { 25 | new_node->prev = NULL; 26 | *head = new_node; 27 | return (new_node); 28 | } 29 | 30 | while ((*head)->next != NULL) 31 | *head = (*head)->next; 32 | 33 | (*head)->next = new_node; 34 | new_node->prev = *head; 35 | 36 | return (new_node); 37 | } 38 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a dlistint_t list. 5 | * @head: pointer to the head of the list. 6 | * 7 | * 8 | */ 9 | 10 | void free_dlistint(dlistint_t *head) 11 | { 12 | 13 | while (head != NULL) 14 | { 15 | dlistint_t *next = head->next; 16 | 17 | free(head); 18 | head = next; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - eturns the nth node of a dlistint_t linked list. 5 | * @head: double pointer to the head of the list 6 | * @index: index of the node to return 7 | * 8 | * Return: if the node does not exist, return NULL 9 | * where index is the index of the node, starting from 0 10 | */ 11 | 12 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 13 | { 14 | dlistint_t *current; 15 | unsigned int i; 16 | 17 | current = head; 18 | i = 0; 19 | while (current != NULL) 20 | { 21 | if (i == index) 22 | return (current); 23 | current = current->next; 24 | i++; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all 5 | * the data (n) of a dlistint_t linked list. 6 | * @head: double pointer to the head of the list 7 | * 8 | * Return: if the list is empty, return 0 9 | */ 10 | 11 | int sum_dlistint(dlistint_t *head) 12 | { 13 | 14 | int sum = 0; 15 | dlistint_t *i; 16 | 17 | for (i = head; i != NULL; i = i->next) 18 | { 19 | sum += i->n; 20 | } 21 | 22 | return (sum); 23 | } 24 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - inserts a new node at a given position 5 | * @h: pointer to head of list 6 | * @idx: index of the list where the new node should be added 7 | * @n: data of the new node 8 | * 9 | * Return: the address of the new node, or NULL if it failed 10 | */ 11 | 12 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 13 | { 14 | } -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x17-doubly_linked_lists/8-delete_dnodeint.c -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | #endif -------------------------------------------------------------------------------- /0x18-dynamic_libraries/.gitignore: -------------------------------------------------------------------------------- 1 | *.c 2 | *.o 3 | 100-operations.sh 4 | crack.sh -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | gcc -Wall -c -fPIC *.c 3 | gcc -shared *.o -o liball.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp/ https://raw.github.com/Pericles001/alx-low_level_programming/master/0x18-dynamic_libraries/nrandom.so 3 | export LD_PRELOAD =/tmp/nrandom.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/liball.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x18-dynamic_libraries/liball.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN__ 2 | #define __MAIN__ 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | int nrandom(); 25 | 26 | 27 | #endif /* __MAIN__ */ 28 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/nrandom.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x18-dynamic_libraries/nrandom.so -------------------------------------------------------------------------------- /0x1A-hash_tables/.gitignore: -------------------------------------------------------------------------------- 1 | tests/ -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Creates a hash table. 5 | * @size: The size of the array. 6 | * 7 | * Return: If an error occurs - NULL. 8 | * Otherwise - a pointer to the new hash table. 9 | */ 10 | hash_table_t *hash_table_create(unsigned long int size) 11 | { 12 | hash_table_t *ht; 13 | unsigned long int i; 14 | 15 | ht = malloc(sizeof(hash_table_t)); 16 | if (ht == NULL) 17 | return (NULL); 18 | 19 | ht->size = size; 20 | ht->array = malloc(sizeof(hash_node_t *) * size); 21 | if (ht->array == NULL) 22 | return (NULL); 23 | for (i = 0; i < size; i++) 24 | ht->array[i] = NULL; 25 | 26 | return (ht); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - 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 | 2 | #include "hash_tables.h" 3 | 4 | shash_table_t *shash_table_create(unsigned long int size); 5 | int shash_table_set(shash_table_t *ht, const char *key, const char *value); 6 | char *shash_table_get(const shash_table_t *ht, const char *key); 7 | void shash_table_print(const shash_table_t *ht); 8 | void shash_table_print_rev(const shash_table_t *ht); 9 | void shash_table_delete(shash_table_t *ht); 10 | 11 | /** 12 | * shash_table_create - Creates a sorted hash table. 13 | * @size: The size of new sorted hash table. 14 | * 15 | * Return: If an error occurs - NULL. 16 | * Otherwise - a pointer to the new sorted hash table. 17 | */ 18 | shash_table_t *shash_table_create(unsigned long int size) 19 | { 20 | shash_table_t *ht; 21 | unsigned long int i; 22 | 23 | ht = malloc(sizeof(shash_table_t)); 24 | if (ht == NULL) 25 | return (NULL); 26 | 27 | ht->size = size; 28 | ht->array = malloc(sizeof(shash_node_t *) * size); 29 | if (ht->array == NULL) 30 | return (NULL); 31 | for (i = 0; i < size; i++) 32 | ht->array[i] = NULL; 33 | ht->shead = NULL; 34 | ht->stail = NULL; 35 | 36 | return (ht); 37 | } 38 | 39 | /** 40 | * shash_table_set - Adds an element to a sorted hash table. 41 | * @ht: A pointer to the sorted hash table. 42 | * @key: The key to add - cannot be an empty string. 43 | * @value: The value associated with key. 44 | * 45 | * Return: Upon failure - 0. 46 | * Otherwise - 1. 47 | */ 48 | int shash_table_set(shash_table_t *ht, const char *key, const char *value) 49 | { 50 | shash_node_t *new, *tmp; 51 | char *value_copy; 52 | unsigned long int index; 53 | 54 | if (ht == NULL || key == NULL || *key == '\0' || value == NULL) 55 | return (0); 56 | 57 | value_copy = strdup(value); 58 | if (value_copy == NULL) 59 | return (0); 60 | 61 | index = key_index((const unsigned char *)key, ht->size); 62 | tmp = ht->shead; 63 | while (tmp) 64 | { 65 | if (strcmp(tmp->key, key) == 0) 66 | { 67 | free(tmp->value); 68 | tmp->value = value_copy; 69 | return (1); 70 | } 71 | tmp = tmp->snext; 72 | } 73 | 74 | new = malloc(sizeof(shash_node_t)); 75 | if (new == NULL) 76 | { 77 | free(value_copy); 78 | return (0); 79 | } 80 | new->key = strdup(key); 81 | if (new->key == NULL) 82 | { 83 | free(value_copy); 84 | free(new); 85 | return (0); 86 | } 87 | new->value = value_copy; 88 | new->next = ht->array[index]; 89 | ht->array[index] = new; 90 | 91 | if (ht->shead == NULL) 92 | { 93 | new->sprev = NULL; 94 | new->snext = NULL; 95 | ht->shead = new; 96 | ht->stail = new; 97 | } 98 | else if (strcmp(ht->shead->key, key) > 0) 99 | { 100 | new->sprev = NULL; 101 | new->snext = ht->shead; 102 | ht->shead->sprev = new; 103 | ht->shead = new; 104 | } 105 | else 106 | { 107 | tmp = ht->shead; 108 | while (tmp->snext != NULL && strcmp(tmp->snext->key, key) < 0) 109 | tmp = tmp->snext; 110 | new->sprev = tmp; 111 | new->snext = tmp->snext; 112 | if (tmp->snext == NULL) 113 | ht->stail = new; 114 | else 115 | tmp->snext->sprev = new; 116 | tmp->snext = new; 117 | } 118 | 119 | return (1); 120 | } 121 | 122 | /** 123 | * shash_table_get - Retrieve the value associated with 124 | * a key in a sorted hash table. 125 | * @ht: A pointer to the sorted hash table. 126 | * @key: The key to get the value of. 127 | * 128 | * Return: If the key cannot be matched - NULL. 129 | * Otherwise - the value associated with key in ht. 130 | */ 131 | char *shash_table_get(const shash_table_t *ht, const char *key) 132 | { 133 | shash_node_t *node; 134 | unsigned long int index; 135 | 136 | if (ht == NULL || key == NULL || *key == '\0') 137 | return (NULL); 138 | 139 | index = key_index((const unsigned char *)key, ht->size); 140 | if (index >= ht->size) 141 | return (NULL); 142 | 143 | node = ht->shead; 144 | while (node != NULL && strcmp(node->key, key) != 0) 145 | node = node->snext; 146 | 147 | return ((node == NULL) ? NULL : node->value); 148 | } 149 | 150 | /** 151 | * shash_table_print - Prints a sorted hash table in order. 152 | * @ht: A pointer to the sorted hash table. 153 | */ 154 | void shash_table_print(const shash_table_t *ht) 155 | { 156 | shash_node_t *node; 157 | 158 | if (ht == NULL) 159 | return; 160 | 161 | node = ht->shead; 162 | printf("{"); 163 | while (node != NULL) 164 | { 165 | printf("'%s': '%s'", node->key, node->value); 166 | node = node->snext; 167 | if (node != NULL) 168 | printf(", "); 169 | } 170 | printf("}\n"); 171 | } 172 | 173 | /** 174 | * shash_table_print_rev - Prints a sorted hash table in reverse order. 175 | * @ht: A pointer to the sorted hash table to print. 176 | */ 177 | void shash_table_print_rev(const shash_table_t *ht) 178 | { 179 | shash_node_t *node; 180 | 181 | if (ht == NULL) 182 | return; 183 | 184 | node = ht->stail; 185 | printf("{"); 186 | while (node != NULL) 187 | { 188 | printf("'%s': '%s'", node->key, node->value); 189 | node = node->sprev; 190 | if (node != NULL) 191 | printf(", "); 192 | } 193 | printf("}\n"); 194 | } 195 | 196 | /** 197 | * shash_table_delete - Deletes a sorted hash table. 198 | * @ht: A pointer to the sorted hash table. 199 | */ 200 | void shash_table_delete(shash_table_t *ht) 201 | { 202 | shash_table_t *head = ht; 203 | shash_node_t *node, *tmp; 204 | 205 | if (ht == NULL) 206 | return; 207 | 208 | node = ht->shead; 209 | while (node) 210 | { 211 | tmp = node->snext; 212 | free(node->key); 213 | free(node->value); 214 | free(node); 215 | node = tmp; 216 | } 217 | 218 | free(head->array); 219 | free(head); 220 | } 221 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - Get the index at which a key/value pair should 5 | * be stored in array of a hash table. 6 | * @key: The key to get the index of. 7 | * @size: The size of the array of the hash table. 8 | * 9 | * Return: The index of the key. 10 | * 11 | * Description: Uses the djb2 algorithm. 12 | */ 13 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 14 | { 15 | return (hash_djb2(key) % size); 16 | } 17 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | 2 | #include "hash_tables.h" 3 | 4 | /** 5 | * hash_table_set - Add or update an element in a hash table. 6 | * @ht: A pointer to the hash table. 7 | * @key: The key to add - cannot be an empty string. 8 | * @value: The value associated with key. 9 | * 10 | * Return: Upon failure - 0. 11 | * Otherwise - 1. 12 | */ 13 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 14 | { 15 | hash_node_t *new; 16 | char *value_copy; 17 | unsigned long int index, i; 18 | 19 | if (ht == NULL || key == NULL || *key == '\0' || value == NULL) 20 | return (0); 21 | 22 | value_copy = strdup(value); 23 | if (value_copy == NULL) 24 | return (0); 25 | 26 | index = key_index((const unsigned char *)key, ht->size); 27 | for (i = index; ht->array[i]; i++) 28 | { 29 | if (strcmp(ht->array[i]->key, key) == 0) 30 | { 31 | free(ht->array[i]->value); 32 | ht->array[i]->value = value_copy; 33 | return (1); 34 | } 35 | } 36 | 37 | new = malloc(sizeof(hash_node_t)); 38 | if (new == NULL) 39 | { 40 | free(value_copy); 41 | return (0); 42 | } 43 | new->key = strdup(key); 44 | if (new->key == NULL) 45 | { 46 | free(new); 47 | return (0); 48 | } 49 | new->value = value_copy; 50 | new->next = ht->array[index]; 51 | ht->array[index] = new; 52 | 53 | return (1); 54 | } 55 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | 2 | #include "hash_tables.h" 3 | 4 | /** 5 | * hash_table_get - Retrieve the value associated with 6 | * a key in a hash table. 7 | * @ht: A pointer to the hash table. 8 | * @key: The key to get the value of. 9 | * 10 | * Return: If the key cannot be matched - NULL. 11 | * Otherwise - the value associated with key in ht. 12 | */ 13 | char *hash_table_get(const hash_table_t *ht, const char *key) 14 | { 15 | hash_node_t *node; 16 | unsigned long int index; 17 | 18 | if (ht == NULL || key == NULL || *key == '\0') 19 | return (NULL); 20 | 21 | index = key_index((const unsigned char *)key, ht->size); 22 | if (index >= ht->size) 23 | return (NULL); 24 | 25 | node = ht->array[index]; 26 | while (node && strcmp(node->key, key) != 0) 27 | node = node->next; 28 | 29 | return ((node == NULL) ? NULL : node->value); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | 2 | #include "hash_tables.h" 3 | 4 | /** 5 | * hash_table_print - Prints a hash table. 6 | * @ht: A pointer to the hash table to print. 7 | * 8 | * Description: Key/value pairs are printed in the order 9 | * they appear in the array of the hash table. 10 | */ 11 | void hash_table_print(const hash_table_t *ht) 12 | { 13 | hash_node_t *node; 14 | unsigned long int i; 15 | unsigned char comma_flag = 0; 16 | 17 | if (ht == NULL) 18 | return; 19 | 20 | printf("{"); 21 | for (i = 0; i < ht->size; i++) 22 | { 23 | if (ht->array[i] != NULL) 24 | { 25 | if (comma_flag == 1) 26 | printf(", "); 27 | 28 | node = ht->array[i]; 29 | while (node != NULL) 30 | { 31 | printf("'%s': '%s'", node->key, node->value); 32 | node = node->next; 33 | if (node != NULL) 34 | printf(", "); 35 | } 36 | comma_flag = 1; 37 | } 38 | } 39 | printf("}\n"); 40 | } 41 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | 2 | #include "hash_tables.h" 3 | 4 | /** 5 | * hash_table_delete - Deletes a hash table. 6 | * @ht: A pointer to a hash table. 7 | */ 8 | void hash_table_delete(hash_table_t *ht) 9 | { 10 | hash_table_t *head = ht; 11 | hash_node_t *node, *tmp; 12 | unsigned long int i; 13 | 14 | for (i = 0; i < ht->size; i++) 15 | { 16 | if (ht->array[i] != NULL) 17 | { 18 | node = ht->array[i]; 19 | while (node != NULL) 20 | { 21 | tmp = node->next; 22 | free(node->key); 23 | free(node->value); 24 | free(node); 25 | node = tmp; 26 | } 27 | } 28 | } 29 | free(head->array); 30 | free(head); 31 | } 32 | -------------------------------------------------------------------------------- /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 */ -------------------------------------------------------------------------------- /0x1C-makefiles/.gitignore: -------------------------------------------------------------------------------- 1 | 0x1B.c 2 | 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 4 | all: 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | .PHONY:all clean oclean fclean re 7 | 8 | all: m.h $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: oclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | @echo "Compiling..." 8 | @$(CC) $(OBJ) -o $(NAME) 9 | @echo "Done." 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm 6 | 7 | all:$(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | .PHONY: clean oclean fclean re 11 | 12 | clean: 13 | $(RM) -rf *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) -rf $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: oclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC= gcc 2 | SRC= main.c school.c 3 | OBJ= $(SRC:.c=.o) 4 | NAME= school 5 | RM= rm 6 | CFLAGS= -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(CFLAGS) $(OBJ) -o $(NAME) 10 | 11 | .PHONY: clean oclean fclean re 12 | 13 | clean: 14 | $(RM) -rf *~ $(NAME) 15 | 16 | oclean: 17 | $(RM) -rf $(OBJ) 18 | 19 | fclean:clean oclean 20 | 21 | re: oclean all 22 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Module to define a function island_perimeter""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Function to calculate an island described in a grid perimeter 7 | 8 | grid is a list of list of integers: 9 | 0 represents a water zone 10 | 1 represents a land zone 11 | One cell is a square with side length 1 12 | """ 13 | width = len(grid[0]) 14 | height = len(grid) 15 | edges = 0 16 | size = 0 17 | 18 | for i in range(height): 19 | for j in range(width): 20 | if grid[i][j] == 1: 21 | size += 1 22 | if (j > 0 and grid[i][j - 1] == 1): 23 | edges += 1 24 | if (i > 0 and grid[i - 1][j] == 1): 25 | edges += 1 26 | return size * 4 - edges * 2 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in a sorted array of integers 5 | * @array: array of integers 6 | * @size: size of array 7 | * @value: value to search for 8 | * Return: index of value or -1 if not found 9 | */ 10 | 11 | 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | size_t inc; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | for (inc = 0; inc < size; inc++) 19 | { 20 | printf("Value checked array[%ld] = [%d]\n", inc, array[inc]); 21 | if (array[inc] == value) 22 | return (inc); 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - searches for a value in a sorted array of integers 5 | * using the binary search algorithm 6 | * @array: array of integers 7 | * @size: size of array 8 | * @value: value to search for 9 | * Return: index of value or -1 if not found 10 | */ 11 | 12 | int binary_search(int *array, size_t size, int value) 13 | { 14 | 15 | size_t i, left, right; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | for (left = 0, right = size - 1; right >= left;) 21 | { 22 | printf("Searching in array: "); 23 | for (i = left; i < right; i++) 24 | printf("%d, ", array[i]); 25 | printf("%d\n", array[i]); 26 | 27 | i = left + (right - left) / 2; 28 | if (array[i] == value) 29 | return (i); 30 | if (array[i] > value) 31 | right = i - 1; 32 | else 33 | left = i + 1; 34 | } 35 | 36 | return (-1); 37 | } 38 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary_recursive.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pericles001/alx-low_level_programming/29c3c687100130842ea1fa2a96312de480863d40/0x1E-search_algorithms/1-binary_recursive.c -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * jump_search - function that searches for a value 5 | * in a sorted array of integers 6 | * 7 | * @array: a pointer to the first element of the array to search in 8 | * @size: the number of elements in array 9 | * @value: is the value to search for 10 | * Return: the first index where value is located 11 | */ 12 | 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | size_t i, step, jump; 16 | 17 | if (array == NULL || size == 0) 18 | return (-1); 19 | 20 | step = sqrt(size); 21 | 22 | for (i = jump = 0; jump < size && array[jump] < value;) 23 | { 24 | printf("Value checked array [%ld] = [%d]\n", jump, array[jump]); 25 | i = jump; 26 | jump += step; 27 | } 28 | 29 | printf("Value found between indexes [%ld] and [%ld]\n", i, jump); 30 | 31 | jump = jump > size - 1 ? jump : size - 1; 32 | for (; i < jump && array[i] < value; i++) 33 | printf("Value checked array [%ld] = [%d]\n", i, array[i]); 34 | printf("Value checked array [%ld] = [%d]\n", i, array[i]); 35 | 36 | return (array[i] == value ? (int)i : -1); 37 | } 38 | -------------------------------------------------------------------------------- /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 - function that searches 5 | *for a value in a sorted array of integers 6 | *@array: pointer to the first element of the array to search in 7 | *@size: number of elements in array 8 | *@value: the value to search for 9 | *Return: first index where value is located 10 | */ 11 | 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t i, l, r; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (l = 0, (r = size - 1); r >= l;) 20 | { 21 | i = l + (((double)(r - l) / (array[r] - array[l])) * (value - array[l])); 22 | if (i < size) 23 | { 24 | printf("Value checked array [%ld] = [%d]\n", i, array[i]); 25 | } 26 | else 27 | { 28 | printf("Value checked array [%ld] is out of range\n", i); 29 | break; 30 | } 31 | 32 | if (array[i] == value) 33 | return (i); 34 | if (array[i] > value) 35 | r = i - 1; 36 | else 37 | l = i + 1; 38 | } 39 | return (-1); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | 4 | 5 | /** 6 | * binary_search - searches for a value in a sorted array of integers 7 | * using the binary search algorithm 8 | * @array: array of integers 9 | * @left: Left element 10 | * @right: Right element 11 | * @value: value to search for 12 | * Return: index of value or -1 if not found 13 | */ 14 | 15 | int _binary_search(int *array, size_t left, size_t right, int value) 16 | { 17 | 18 | size_t i; 19 | 20 | if (array == NULL) 21 | return (-1); 22 | 23 | while (right > left) 24 | { 25 | printf("Searching in array: "); 26 | for (i = left; i < right; i++) 27 | printf("%d, ", array[i]); 28 | printf("%d\n", array[i]); 29 | 30 | i = left + (right - left) / 2; 31 | if (array[i] == value) 32 | return (i); 33 | if (array[i] > value) 34 | right = i - 1; 35 | else 36 | left = i + 1; 37 | } 38 | 39 | return (-1); 40 | } 41 | 42 | 43 | 44 | /** 45 | * exponential_search - a function that searches for 46 | * a value in a sorted array of integers 47 | * @array: pointer to the first element of the array 48 | * @size: size of the array to search in 49 | * @value: value to search for 50 | *Return: the index of the searched element 51 | */ 52 | 53 | int exponential_search(int *array, size_t size, int value) 54 | { 55 | size_t i = 0, right; 56 | 57 | if (array == NULL) 58 | return (-1); 59 | 60 | if (array[0] != value) 61 | { 62 | for (i = 1; i < size && array[i] <= value; i *= 2) 63 | printf("Value checked array [%ld] = [%d]\n", i, array[i]); 64 | } 65 | 66 | right = i < size ? i : size - 1; 67 | printf("Value found between indexes [%ld] and [%ld]\n", i / 2, right); 68 | return (_binary_search(array, i / 2, right, value)); 69 | } 70 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | 4 | /** 5 | * advanced_binary_recursive - Searches recursively for a value in a sorted 6 | * array of integers using binary search. 7 | * @array: A pointer to the first element of the [sub]array to search. 8 | * @left: The starting index of the [sub]array to search. 9 | * @right: The ending index of the [sub]array to search. 10 | * @value: The value to search for. 11 | * 12 | * Return: If the value is not present, -1. 13 | * Otherwise, the index where the value is located. 14 | * 15 | * Description: Prints the [sub]array being searched after each change. 16 | */ 17 | int advanced_binary_recursive(int *array, size_t left, size_t right, int value) 18 | { 19 | size_t i; 20 | 21 | if (right < left) 22 | return (-1); 23 | 24 | printf("Searching in array: "); 25 | for (i = left; i < right; i++) 26 | printf("%d, ", array[i]); 27 | printf("%d\n", array[i]); 28 | 29 | i = left + (right - left) / 2; 30 | if (array[i] == value && (i == left || array[i - 1] != value)) 31 | return (i); 32 | if (array[i] >= value) 33 | return (advanced_binary_recursive(array, left, i, value)); 34 | return (advanced_binary_recursive(array, i + 1, right, value)); 35 | } 36 | 37 | 38 | 39 | /** 40 | *advanced_binary - a function that searches for a 41 | * value in a sorted array of integers. 42 | * 43 | * @array: pointer to the first element in the array 44 | * @size: Size of the array to search in 45 | * @value: Element to be searched 46 | * Return: return index of searched element 47 | */ 48 | 49 | int advanced_binary(int *array, size_t size, int value) 50 | { 51 | if (array == NULL || size == 0) 52 | return (-1); 53 | 54 | return (advanced_binary_recursive(array, 0, size - 1, value)); 55 | } 56 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * jump_list - Searches for an algorithm in a sorted singly 5 | * linked list of integers using jump search. 6 | * @list: A pointer to the head of the linked list to search. 7 | * @size: The number of nodes in the list. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the head of the list is NULL, NULL. 11 | * Otherwise, a pointer to the first node where the value is located. 12 | * 13 | * Description: Prints a value every time it is compared in the list. 14 | * Uses the square root of the list size as the jump step. 15 | */ 16 | listint_t *jump_list(listint_t *list, size_t size, int value) 17 | { 18 | size_t step, step_size; 19 | listint_t *node, *jump; 20 | 21 | if (list == NULL || size == 0) 22 | return (NULL); 23 | 24 | step = 0; 25 | step_size = sqrt(size); 26 | for (node = jump = list; jump->index + 1 < size && jump->n < value;) 27 | { 28 | node = jump; 29 | for (step += step_size; jump->index < step; jump = jump->next) 30 | { 31 | if (jump->index + 1 == size) 32 | break; 33 | } 34 | printf("Value checked at index [%ld] = [%d]\n", jump->index, jump->n); 35 | } 36 | 37 | printf("Value found between indexes [%ld] and [%ld]\n", 38 | node->index, jump->index); 39 | 40 | for (; node->index < jump->index && node->n < value; node = node->next) 41 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 42 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 43 | 44 | return (node->n == value ? node : NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - Searches for an algorithm in a sorted singly 5 | * linked list of integers using linear skip. 6 | * @list: A pointer to the head of the linked list to search. 7 | * @value: The value to search for. 8 | * 9 | * Return: If the value is not present or the head of the list is NULL, NULL. 10 | * Otherwise, a pointer to the first node where the value is located. 11 | * 12 | * Description: Prints a value every time it is compared in the list. 13 | * Uses the square root of the list size as the jump step. 14 | */ 15 | skiplist_t *linear_skip(skiplist_t *list, int value) 16 | { 17 | skiplist_t *node, *jump; 18 | 19 | if (list == NULL) 20 | return (NULL); 21 | 22 | for (node = jump = list; jump->next != NULL && jump->n < value;) 23 | { 24 | node = jump; 25 | if (jump->express != NULL) 26 | { 27 | jump = jump->express; 28 | printf("Value checked at index [%ld] = [%d]\n", 29 | jump->index, jump->n); 30 | } 31 | else 32 | { 33 | while (jump->next != NULL) 34 | jump = jump->next; 35 | } 36 | } 37 | 38 | printf("Value found between indexes [%ld] and [%ld]\n", 39 | node->index, jump->index); 40 | 41 | for (; node->index < jump->index && node->n < value; node = node->next) 42 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 43 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 44 | 45 | return (node->n == value ? node : NULL); 46 | } 47 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS_H 2 | #define SEARCH_ALGOS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | /** 10 | * struct listint_s - singly linked list 11 | * 12 | * @n: Integer 13 | * @index: Index of the node in the list 14 | * @next: Pointer to the next node 15 | * 16 | * Description: singly linked list node structure 17 | * for Holberton project 18 | */ 19 | typedef struct listint_s 20 | { 21 | int n; 22 | size_t index; 23 | struct listint_s *next; 24 | } listint_t; 25 | 26 | 27 | 28 | /** 29 | * struct skiplist_s - Singly linked list with an express lane 30 | * 31 | * @n: Integer 32 | * @index: Index of the node in the list 33 | * @next: Pointer to the next node 34 | * @express: Pointer to the next node in the express lane 35 | * 36 | * Description: singly linked list node structure with an express lane 37 | * for Holberton project 38 | */ 39 | typedef struct skiplist_s 40 | { 41 | int n; 42 | size_t index; 43 | struct skiplist_s *next; 44 | struct skiplist_s *express; 45 | } skiplist_t; 46 | 47 | 48 | int linear_search(int *array, size_t size, int value); 49 | int binary_search(int *array, size_t size, int value); 50 | int _binary_search(int *array, size_t left, size_t right, int value); 51 | int jump_search(int *array, size_t size, int value); 52 | int interpolation_search(int *array, size_t size, int value); 53 | int exponential_search(int *array, size_t size, int value); 54 | int advanced_binary_recursive(int *array, size_t left, size_t right, int value); 55 | int advanced_binary(int *array, size_t size, int value); 56 | listint_t *jump_list(listint_t *list, size_t size, int value); 57 | skiplist_t *linear_skip(skiplist_t *list, int value); 58 | 59 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Low-Level Programming - Data Structures, Algorithms, Linux/Unix System Programming 2 | 3 | ## Description 4 | This repository contains programs written for the low-level programming and 5 | algorithm section . 6 | 7 | 8 | ## Structure 9 | 10 | 11 | * [0x00. C - Hello, World](./0x00-hello_world) 12 | * [0x01. C - Variables, if, else, while](./0x01-variables_if_else_while) 13 | * [0x02. C -Functions_nested_loops](./0x02-functions_nested_loops) 14 | * [0x03. C-Debugging](./0x03-debugging) 15 | * [0x04. C - More functions, more nested loops](./0x04-more_functions_nested_loops) 16 | * [0x05. C - Pointers, arrays and strings](./0x05-pointers_arrays_strings) 17 | * [0x06. C - Pointers, arrays and strings](./0x06-pointers_arrays_strings) 18 | * [0x07. C - Even more pointers, arrays and strings](./0x07-pointers_arrays_strings) 19 | * [0x08. C - Recursion](./0x08-recursion) 20 | * [0x09. C - Static libraries](./0x09-static_libraries) 21 | * [0x10. C - Variadic functions](./0x10-variadic_functions) 22 | * [0x11. C - Printf](./0x11-printf) 23 | * [0x12. C - Singly linked lists](./0x12-singly_linked_lists) 24 | * [0x13. C - More singly linked lists](./0x13-more_singly_linked_lists) 25 | * [0x14. C - Bit manipulation](./0x14-bit_manipulation) 26 | * [0x16. C - Simple Shell](./0x16-simple_shell) 27 | * [0x15. C - File input output](./0x15-file_io) 28 | * [0x16. C - Simple Shell](./0x16-simple_shell) 29 | * [0x17. C - Doubly linked lists](./0x17-doubly_linked_lists) 30 | * [0x18. C - Dynamic libraries](./0x18-dynamic_libraries) 31 | * [0x19. C - Stacks,Queues LIFO-FIFO](./0x19-stacks_queues_lifo_fifo) 32 | * [0x0A. C - argc, argv](./0x0A-argc_argv) 33 | * [0x0B. C - malloc, free](./0x0B-malloc_free) 34 | * [0x0C. C - more malloc, free](./0x0C-more_malloc_free) 35 | * [0x0D. C - preprocessor](./0x0D-preprocessor) 36 | * [0x0E. C - Structures, typedef](./0x0E-structures_typedef) 37 | * [0x0F. C - Variadic functions](./0x0F-variadic_functions) 38 | * [0x1A. C - Hash tables](./0x1A-hash_tables) 39 | * [0x1B. C - Sorting algorithms & Big O ](./0x1B-sorting_algorithms) 40 | * [0x1C. C - Makefiles](./0x1C-makefiles) 41 | * [0x1D. C - Binary trees](./0x1D-binary_trees) 42 | * [0x1E. C - Search Algorithms](./0x1E-search_algorithms) 43 | 44 | 45 | ## Requirements 46 | 47 | Working with this project require you to have some tools installed: 48 | * GCC : [GNU Compiler](https://gcc.gnu.org/) 49 | * Betty : [C code checker](https://github.com/holbertonschool/Betty.git) 50 | 51 | 52 | ## Navigation Guide 53 | 54 | To access any project you are willing to view , please proceed like this: 55 | 56 | * clone the repository 57 | 58 | 59 | 60 | - git clone https://github.com/Pericles001/alx-low_level_programming 61 | - cd alx-low_level_programming 62 | 63 | 64 | * open your desired project (we take Hello world project as example) 65 | 66 | 67 | 68 | - cd [0x00. C - Hello, World](./0x00-hello_world) 69 | 70 | 71 | * test your desired file 72 | 73 | 74 | 75 | - gcc -Wall -Werror -Wextra -pedantic -std=gnu89 4-puts.c 4-puts 76 | - ./4-puts 77 | 78 | * Repeat with other files or projects 79 | 80 | 81 | ## Author 82 | 83 | * **Pericles Adjovi** <[Pericles001](https://github.com/Pericles001)> 84 | 85 | ## Aknowledgements 86 | 87 | All work contained in this project was completed as part of the curriculum for ALX Africa Software Engineering program built upon Holberton School projects and materials. Holberton School is a campus-based full-stack software engineering program that prepares students for careers in the tech industry using project-based peer learning. 88 | 89 | 90 | ## Credits 91 | For more information, visit these links. 92 | 93 | * [ALX ](https://www.alxafrica.com/) 94 | * [Holberton School](https://www.holbertonschool.com/) 95 | --------------------------------------------------------------------------------