├── 0x00-hello_world ├── .5-printf.c.swp ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c ├── README.md ├── a.out ├── c ├── cisfun ├── main.c ├── main.o ├── main.s ├── mak ├── size32 └── size64 ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── .100-times_table.c.swo ├── .100-times_table.c.swp ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md ├── _putchar.c └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main ├── 1-main.c ├── 2-largest_number.c ├── 2-main.c ├── 3-print_remaining_days.c ├── README.md ├── main.h └── positive_or_negative.c ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md ├── _putchar.c └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── .101-crackme_password.swp ├── 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 ├── 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-memset.c ├── 0-strcat.c ├── 1-isdigit.c ├── 1-memcpy.c ├── 1-strncat.c ├── 100-atoi.c ├── 2-strchr.c ├── 2-strlen.c ├── 2-strncpy.c ├── 3-islower.c ├── 3-puts.c ├── 3-strcmp.c ├── 3-strspn.c ├── 4-isalpha.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 6-abs.c ├── 9-strcpy.c ├── README.md └── _putchar.c ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── args ├── change ├── mul └── nargs ├── 0x0B-malloc_free ├── .100-argstostr.c.swp ├── 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 ├── .101-mul.c.swo ├── .101-mul.c.swp ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-main.c ├── 100-realloc.c ├── 101-mul ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── _putchar.c ├── main.h └── test │ ├── 100-realloc │ └── 101-mul ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md ├── calc └── function_pointers.h ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-main.c ├── 3-print_all.c ├── README.md ├── main.c └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-main.c ├── 0-print_list.c ├── 1-list_len.c ├── 1-main.c ├── 100-first.c ├── 100-main.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 2-main.c ├── 3-add_node_end.c ├── 3-main.c ├── 4-free_list.c ├── 4-main.c ├── README.md ├── a ├── b ├── c ├── d ├── first └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-main.c ├── 0-print_listint.c ├── 1-listint_len.c ├── 1-main.c ├── 10-delete_nodeint.c ├── 10-main.c ├── 100-main.c ├── 100-reverse_listint.c ├── 101-main.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 102-main.c ├── 103-find_loop.c ├── 103-main.c ├── 2-add_nodeint.c ├── 2-main.c ├── 3-add_nodeint_end.c ├── 3-main.c ├── 4-free_listint.c ├── 4-main.c ├── 5-free_listint2.c ├── 5-main.c ├── 6-main.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 7-main.c ├── 8-main.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── 9-main.c ├── README.md ├── a ├── b ├── c ├── d ├── e ├── f ├── g ├── h ├── i ├── j ├── k ├── l ├── lists.h ├── m ├── n └── o ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md ├── crackme3 └── main.h ├── 0x15-file_io ├── .0-read_textfile.c.swp ├── 0-main.c ├── 0-read_textfile.c ├── 1-create_file.c ├── 1-main.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 2-main.c ├── 3-cp.c ├── Incitatous ├── README.md ├── Requiescat ├── a ├── b ├── c ├── cp ├── elf_header ├── hello ├── incitatous └── main.h ├── 0x17-doubly_linked_lists ├── 0-main.c ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 1-main.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 2-main.c ├── 3-add_dnodeint_end.c ├── 3-main.c ├── 4-free_dlistint.c ├── 4-main.c ├── 5-get_dnodeint.c ├── 5-main.c ├── 6-main.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 7-main.c ├── 8-delete_dnodeint.c ├── 8-main.c ├── README.md ├── a ├── b ├── c ├── d ├── e ├── i └── lists.h ├── 0x18-dynamic_libraries ├── 0-main.c ├── 0-main.o ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_win.sh ├── README.md ├── libdynamic.so └── main.h ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-main.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── 5-main.py ├── README.md └── school.c ├── README.md └── main.h /0x00-hello_world/.5-printf.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x00-hello_world/.5-printf.c.swp -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE >> c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 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 -Entry 5 | * Return: 1 6 | */ 7 | int main(void) 8 | { 9 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 10 | return (1); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -o cisfun $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - rain block 4 | * Return: Always 0 5 | */ 6 | int main(void) 7 | { 8 | puts("\"Programming is like building a multilingual puzzle"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Enter 4 | * Return: Always 0 5 | */ 6 | int main(void) 7 | { 8 | printf("with proper grammar, but the outcome is a piece of art,\n"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry 4 | * Return: Always 0 5 | */ 6 | int main(void) 7 | { 8 | printf("Size of a char: %i byte(s)\n", sizeof(char)); 9 | printf("Size of an int: %i byte(s)\n", sizeof(int)); 10 | printf("Size of a long int: %i byte(s)\n", sizeof(long int)); 11 | printf("Size of a long long int: %i byte(s)\n", sizeof(long long int)); 12 | printf("Size of a float: %i byte(s)\n", sizeof(float)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | hello world 2 | -------------------------------------------------------------------------------- /0x00-hello_world/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x00-hello_world/a.out -------------------------------------------------------------------------------- /0x00-hello_world/cisfun: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x00-hello_world/cisfun -------------------------------------------------------------------------------- /0x00-hello_world/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | return (0); 5 | } 6 | -------------------------------------------------------------------------------- /0x00-hello_world/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x00-hello_world/main.o -------------------------------------------------------------------------------- /0x00-hello_world/main.s: -------------------------------------------------------------------------------- 1 | .file "main.c" 2 | .intel_syntax noprefix 3 | .text 4 | .globl main 5 | .type main, @function 6 | main: 7 | .LFB0: 8 | .cfi_startproc 9 | endbr64 10 | push rbp 11 | .cfi_def_cfa_offset 16 12 | .cfi_offset 6, -16 13 | mov rbp, rsp 14 | .cfi_def_cfa_register 6 15 | mov eax, 0 16 | pop rbp 17 | .cfi_def_cfa 7, 8 18 | ret 19 | .cfi_endproc 20 | .LFE0: 21 | .size main, .-main 22 | .ident "GCC: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0" 23 | .section .note.GNU-stack,"",@progbits 24 | .section .note.gnu.property,"a" 25 | .align 8 26 | .long 1f - 0f 27 | .long 4f - 1f 28 | .long 5 29 | 0: 30 | .string "GNU" 31 | 1: 32 | .align 8 33 | .long 0xc0000002 34 | .long 3f - 2f 35 | 2: 36 | .long 0x3 37 | 3: 38 | .align 8 39 | 4: 40 | -------------------------------------------------------------------------------- /0x00-hello_world/mak: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x00-hello_world/mak -------------------------------------------------------------------------------- /0x00-hello_world/size32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x00-hello_world/size32 -------------------------------------------------------------------------------- /0x00-hello_world/size64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x00-hello_world/size64 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - print if the number is postive, negative or 0 6 | * Return: always 0 7 | */ 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 zero\n", n); 18 | } 19 | else if (n < 0) 20 | { 21 | printf("%d is negative\n", n); 22 | } 23 | else 24 | { 25 | printf("%d is positive\n", n); 26 | } 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main -Entry 7 | * Return: always 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | int a; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | a = n % 10; 17 | 18 | if (a > 5) 19 | { 20 | printf("Last digit of %d is %d and is greater than 5\n", n, a); 21 | } 22 | else if (a == 0) 23 | { 24 | printf("Last digit of %d is %d and is 0\n", n, a); 25 | } 26 | else 27 | { 28 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, a); 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all possible different combinations of digits 4 | * Return: Alway 0 5 | */ 6 | int main(void) 7 | { 8 | int ones = '0'; 9 | int tens = '0'; 10 | 11 | for (tens = '0'; tens <= '9'; tens++) 12 | { 13 | for (ones = '0'; ones <= '9'; ones++) 14 | { 15 | if (!((ones == tens) || (tens > ones))) 16 | { 17 | putchar(tens); 18 | putchar(ones); 19 | if (!(ones == '9' && tens == '8')) 20 | { 21 | putchar(','); 22 | putchar(' '); 23 | } 24 | } 25 | } 26 | } 27 | putchar('\n'); 28 | 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all possible different combinations of digits 4 | * Return: Alway 0 5 | */ 6 | int main(void) 7 | { 8 | int c; 9 | int d; 10 | int e = 0; 11 | 12 | while (e < 10) 13 | { 14 | d = 0; 15 | while (d < 10) 16 | { 17 | c = 0; 18 | while (c < 10) 19 | { 20 | if (c != d && d != e && e < d && d < c) 21 | { 22 | putchar('0' + e); 23 | putchar('0' + d); 24 | putchar('0' + c); 25 | 26 | if (c + d + e != 9 + 8 + 7) 27 | { 28 | putchar(','); 29 | putchar(' '); 30 | } 31 | } 32 | 33 | c++; 34 | } 35 | d++; 36 | } 37 | e++; 38 | } 39 | putchar('\n'); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - print all comination of single digits 5 | * Return: Always 0 6 | */ 7 | int main(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 100; i++) 12 | { 13 | for (j = 0; j < 100; j++) 14 | { 15 | if (i < j) 16 | { 17 | putchar((i / 10) + 48); 18 | putchar((i % 10) + 48); 19 | putchar(' '); 20 | putchar((j / 10) + 48); 21 | putchar((j % 10) + 48); 22 | if (i != 98 || j != 99) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | putchar('\n'); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main -Entry 5 | * Return: Always 0 6 | */ 7 | int main(void) 8 | { 9 | char lc; 10 | 11 | for (lc = 'a'; lc <= 'z'; lc++) 12 | { 13 | putchar(lc); 14 | } 15 | putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints alpha in lowe and upper case 5 | * Return: Always 0 6 | */ 7 | int main(void) 8 | { 9 | char ch; 10 | 11 | for (ch = 'a'; ch <= 'z'; ch++) 12 | putchar(ch); 13 | for (ch = 'A'; ch <= 'Z'; ch++) 14 | putchar(ch); 15 | putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the alphabet is lowercase 4 | * followed by a new line, except q and e 5 | * Return: Always 0 6 | */ 7 | int main(void) 8 | { 9 | char la; 10 | 11 | for (la = 'a'; la <= 'z'; la++) 12 | { 13 | if (la != 'e' && la != 'q') 14 | putchar(la); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all strings from 0 to 9 4 | * Return: Always 0 5 | */ 6 | int main(void) 7 | { 8 | int num; 9 | 10 | for (num = 0; num < 10; num++) 11 | printf("%d", num); 12 | 13 | printf("\n"); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints single digits in base 10 4 | * Return: Always 0 5 | */ 6 | int main(void) 7 | { 8 | int num; 9 | 10 | for (num = 0; num < 10; num++) 11 | putchar((num % 10) + '0'); 12 | 13 | putchar('\n'); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main -print the alphabets in the reverse 4 | * Return: Always 0 5 | */ 6 | int main(void) 7 | { 8 | char le; 9 | 10 | for (le = 'z'; le >= 'a'; le--) 11 | putchar (le); 12 | 13 | putchar('\n'); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the numbers in base 16 4 | * Return: Always 0 5 | */ 6 | int main(void) 7 | { 8 | int num; 9 | char la; 10 | 11 | for (num = 0; num < 10; num++) 12 | putchar((num % 10) + '0'); 13 | 14 | for (la = 'a'; la <= 'f'; la++) 15 | putchar(la); 16 | 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print all comination of single digits 4 | * Return: Always 0 5 | */ 6 | int main(void) 7 | { 8 | int n; 9 | 10 | for (n = 0; n < 10; n++) 11 | { 12 | putchar(n + '0'); 13 | if (n < 9) 14 | { 15 | putchar(','); 16 | putchar(' '); 17 | } 18 | } 19 | putchar('\n'); 20 | return (0); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | my project on c if els -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.100-times_table.c.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x02-functions_nested_loops/.100-times_table.c.swo -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.100-times_table.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x02-functions_nested_loops/.100-times_table.c.swp -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - Entry point _putchar 4 | * description: print out 5 | * Return: 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | _putchar('_'); 10 | _putchar('p'); 11 | _putchar('u'); 12 | _putchar('t'); 13 | _putchar('c'); 14 | _putchar('h'); 15 | _putchar('a'); 16 | _putchar('r'); 17 | _putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_alphabet - prints the alpha in lower 4 | */ 5 | void print_alphabet(void) 6 | { 7 | char la; 8 | 9 | for (la = 'a'; la <= 'z'; la++) 10 | _putchar(la); 11 | _putchar('\n'); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * add - add two numbers 4 | * @a: int 5 | * @b: int 6 | * Return: sum 7 | */ 8 | int add(int a, int b) 9 | { 10 | return (a + b); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_times_table - prints the n times table, starting with 0 4 | * 5 | * @n: number times table (0 < n <= 15) 6 | * 7 | * Returns- void 8 | */ 9 | void print_times_table(int n) 10 | { 11 | int i, j, k; 12 | 13 | if (n >= 0 && n <= 15) 14 | { 15 | for (i = 0; i <= n; i++) 16 | { 17 | for (j = 0; j <= n; j++) 18 | { 19 | k = j * i; 20 | if (j == 0) 21 | _putchar(k + '0'); 22 | else if (k < 10) 23 | { 24 | _putchar(','); 25 | _putchar(' '); 26 | _putchar(' '); 27 | _putchar(' '); 28 | _putchar(k + '0'); 29 | } 30 | else if (k < 100) 31 | { 32 | _putchar(','); 33 | _putchar(' '); 34 | _putchar(' '); 35 | _putchar((k / 10) + '0'); 36 | _putchar((k % 10) + '0'); 37 | } 38 | else 39 | { 40 | _putchar(','); 41 | _putchar(' '); 42 | _putchar((k / 100) + '0'); 43 | _putchar(((k % 100) / 10) + '0'); 44 | _putchar(((k % 100) % 10) + '0'); 45 | } 46 | } 47 | _putchar('\n'); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main -main block 5 | * Description: computer and printes the sum of all the multiples of 5 or 6 | * 5 below 1024 (exclude), followed by a new line 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int c = 0; 12 | int sum = 0; 13 | 14 | while (c < 1024) 15 | { 16 | if (c % 3 == 0 || c % 5 == 0) 17 | { 18 | sum += c; 19 | } 20 | 21 | c++; 22 | } 23 | printf("%i\n", sum); 24 | return (0); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints the first 50 Fibonacci numbers, starting with 1 and 2, 4 | * 5 | * Return: Always (0). 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | unsigned long fib, prevFib, prev2Fib; 12 | 13 | prevFib = 1, prev2Fib = 0; 14 | 15 | for (i = 0; i < 50; i++) 16 | { 17 | fib = prevFib + prev2Fib; 18 | printf("%lu", fib); 19 | 20 | prev2Fib = prevFib; 21 | prevFib = fib; 22 | if (i == 49) 23 | printf("\n"); 24 | else 25 | printf(", "); 26 | } 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the sum of even valued 5 | * 6 | * Return: Always (0). 7 | */ 8 | 9 | int main(void) 10 | { 11 | int a = 0, b = 1, next = 0; 12 | int sum = 0; 13 | 14 | while (next < 4000000) 15 | { 16 | next = a + b; 17 | a = b; 18 | b = next; 19 | if (next % 2 == 0) 20 | sum += next; 21 | } 22 | printf("%i\n", sum); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints the first 98 Fibonacci numbers, starting with 4 | * 1 and 2, separated by a comma followed by a space. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int count; 11 | unsigned long fib1 = 0, fib2 = 1, sum; 12 | unsigned long fib1_half1, fib1_half2, fib2_half1, fib2_half2; 13 | unsigned long half1, half2; 14 | 15 | for (count = 0; count < 92; count++) 16 | { 17 | sum = fib1 + fib2; 18 | printf("%lu, ", sum); 19 | 20 | fib1 = fib2; 21 | fib2 = sum; 22 | } 23 | fib1_half1 = fib1 / 10000000000; 24 | fib2_half1 = fib2 / 10000000000; 25 | fib1_half2 = fib1 % 10000000000; 26 | fib2_half2 = fib2 % 10000000000; 27 | 28 | for (count = 93; count < 99; count++) 29 | { 30 | half1 = fib1_half1 + fib2_half1; 31 | half2 = fib1_half2 + fib2_half2; 32 | if (fib1_half2 + fib2_half2 > 9999999999) 33 | { 34 | half1 += 1; 35 | half2 %= 10000000000; 36 | } 37 | printf("%lu%lu", half1, half2); 38 | if (count != 98) 39 | printf(", "); 40 | fib1_half1 = fib2_half1; 41 | fib1_half2 = fib2_half2; 42 | fib2_half1 = half1; 43 | fib2_half2 = half2; 44 | } 45 | printf("\n"); 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * print_to_98 - print all numbers from input to 98 5 | * @n: the starting number 6 | */ 7 | void print_to_98(int n) 8 | { 9 | if (n >= 98) 10 | { 11 | while (n > 98) 12 | printf("%d, ", n--); 13 | printf("%d\n", n); 14 | } 15 | else 16 | { 17 | while (n < 98) 18 | printf("%d, ", n++); 19 | printf("%d\n", n); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_alphabet_x10 - Entry point 4 | *description: print 10x 5 | *Return: 0 (success) 6 | */ 7 | 8 | void print_alphabet_x10(void) 9 | { 10 | int ten; 11 | char la; 12 | 13 | for (ten = 0; ten <= 9; ten++) 14 | { 15 | for (la = 'a'; la <= 'z'; la++) 16 | _putchar(la); 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _islower - checks if a charactor is lowercase 4 | * @c: the character 5 | * Return: 1 if letter is lowercase. 0 if not 6 | */ 7 | int _islower(int c) 8 | { 9 | if (c >= 'a' && c <= 'z') 10 | return (1); 11 | else 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _isalpha -checks for alphabets 3 | * @c: character to be cheked 4 | * 5 | * Return: 1 if character is a letter, 0 otherwise 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 10 | return (1); 11 | else 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_sign - prints the sing of a number 4 | * @n: the number of thr sign to be printed 5 | * Return: 1 if number is greater than zero 6 | * 0 of number is zero 7 | * -1 if munber is less less than the 8 | */ 9 | int print_sign(int n) 10 | { 11 | if (n > 0) 12 | { 13 | _putchar('+'); 14 | return (1); 15 | } 16 | else if (n == 0) 17 | { 18 | _putchar('0'); 19 | return (0); 20 | } 21 | else 22 | { 23 | _putchar('-'); 24 | return (-1); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - Compuyer the absolute value of an integer 4 | * @t: Ther integer to be computed 5 | * Return: The absolute value 6 | */ 7 | int _abs(int t) 8 | { 9 | if (t >= 0) 10 | return (t); 11 | else 12 | return (-t); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_last_digit - prints the last digits 4 | * @n: the number 5 | * Return: value of the last digit 6 | */ 7 | int print_last_digit(int n) 8 | { 9 | int a; 10 | 11 | if (n < 0) 12 | n = -n; 13 | a = n % 10; 14 | if (a < 0) 15 | a = -a; 16 | _putchar(a + '0'); 17 | return (a); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * jack_bauer - prints every minute of the da 4 | * Return: always 0 5 | * it will work 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int hour; 10 | int minute; 11 | 12 | for (hour = 0; hour <= 23; hour++) 13 | { 14 | for (minute = 0; minute <= 59; minute++) 15 | { 16 | _putchar((hour / 10) + '0'); 17 | _putchar((hour % 10) + '0'); 18 | _putchar(':'); 19 | _putchar((minute / 10) + '0'); 20 | _putchar((minute % 10) + '0'); 21 | _putchar('\n'); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * times_table - print the 9 times table 4 | * Return: always 0 5 | */ 6 | void times_table(void) 7 | { 8 | int num; 9 | int mult; 10 | int prod; 11 | 12 | for (num = 0; num <= 9; num++) 13 | { 14 | _putchar('0'); 15 | 16 | for (mult = 1; mult <= 9; mult++) 17 | { 18 | _putchar(','); 19 | _putchar(' '); 20 | 21 | prod = num * mult; 22 | 23 | if (prod <= 9) 24 | _putchar(' '); 25 | else 26 | _putchar((prod / 10) + '0'); 27 | _putchar((prod % 10) + '0'); 28 | } 29 | _putchar('\n'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | functions and nested loops -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _putchar - used to write char "d" to stdot 5 | * @c: The character to be print 6 | * Return: 1 in success & error on -1 7 | */ 8 | int _putchar(char c) 9 | { 10 | return (write(1, &c, 1)); 11 | } 12 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | int _putchar(char); 4 | void print_alphabet(void); 5 | void print_alphabet_x10(void); 6 | int _islower(int c); 7 | int _isalpha(int c); 8 | int print_sign(int n); 9 | int _abs(int); 10 | int print_last_digit(int); 11 | void jack_bauer(void); 12 | void times_table(void); 13 | int add(int, int); 14 | void print_to_98(int n); 15 | void print_times_table(int n); 16 | #endif 17 | -------------------------------------------------------------------------------- /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: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x03-debugging/1-main -------------------------------------------------------------------------------- /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 | * largest_number - returns the largest of 3 numbers 4 | * @a: first integer 5 | * @b: second integer 6 | * @c: third integer 7 | * Return: largest number 8 | */ 9 | 10 | int largest_number(int a, int b, int c) 11 | { 12 | int largest; 13 | 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 | 27 | return (largest); 28 | } 29 | -------------------------------------------------------------------------------- /0x03-debugging/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - prints the largest of 3 integers 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int a, b, c, largest; 11 | 12 | a = 972; 13 | b = -98; 14 | c = 0; 15 | largest = largest_number(a, b, c); 16 | printf("%d is the largest number\n", largest); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /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 > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | debugging -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x03-debugging/positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** checks positive or negative numbers 4 | * @i: the number to be checked 5 | * return 0 6 | */ 7 | void positive_or_negative(int i); 8 | { 9 | int i; 10 | 11 | if i < 0 12 | printf("%d is negative", i); 13 | else if i > 0 14 | printf("%d is positive", i); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - checks for uppercase character 6 | * @c: Variable text 7 | * Return: Always 0. 8 | */ 9 | int _isupper(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - function that check if it's a digit 5 | * @c: takes in a character/digit 6 | * Return: 1 if digit and 0 if anything else 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= '0' && c <= '9') 11 | return (1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle 5 | * @size: takes in a integer for size of triangle hhhhhhhhhhhhhhh 6 | */ 7 | 8 | void print_triangle(int size) 9 | { 10 | int row, column; 11 | 12 | if (size <= 0) 13 | _putchar('\n'); 14 | for (row = 0; row < size; row++) 15 | { 16 | for (column = 0; column < size; column++) 17 | { 18 | if (column + 1 >= size - row) 19 | _putchar('#'); 20 | else 21 | _putchar(' '); 22 | } 23 | _putchar('\n'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry block hhhhhhhhhhhhhhhhhhhhhhhhhhhh 5 | * @void: no argument 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | long i, number = 612852475143; 12 | 13 | for (i = 2; i <= number; i++) 14 | { 15 | if (number % i == 0) 16 | { 17 | number = number / i; 18 | i--; 19 | } 20 | } 21 | printf("%lu\n", i); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - Prints an integer. hhhhhhhhhhhhh 5 | * @n: integer to be printed. 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int num = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | num = -num; 16 | } 17 | if ((num / 10) > 0) 18 | print_number(num / 10); 19 | _putchar((num % 10) + '0'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * mul - function that multiplies two integers 6 | * @a: First integer 7 | * @b: Second integer 8 | * Return: mul. 9 | */ 10 | 11 | int mul(int a, int b) 12 | { 13 | return (a * b); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - function that prints the numbers, from 0 to 9. 5 | */ 6 | 7 | void print_numbers(void) 8 | { 9 | char c; 10 | 11 | for (c = '0'; c <= '9'; c++) 12 | _putchar(c); 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints the numbers except 2 and 4. 5 | */ 6 | 7 | void print_most_numbers(void) 8 | { 9 | char c; 10 | 11 | for (c = '0'; c <= '9'; c++) 12 | { 13 | if (c != '2' && c != '4') 14 | _putchar(c); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 10 times the numbers, from 0 to 14. 5 | */ 6 | 7 | void more_numbers(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | for (j = 0; j <= 14; j++) 14 | { 15 | if (j > 9) 16 | _putchar((j / 10) + '0'); 17 | _putchar((j % 10) + '0'); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - draws a straight line in the terminal 5 | * @n: number of times the character _ should be printed 6 | */ 7 | 8 | void print_line(int n) 9 | { 10 | if (n <= 0) 11 | { 12 | _putchar('\n'); 13 | } 14 | else 15 | { 16 | int i; 17 | 18 | for (i = 0; i < n; i++) 19 | { 20 | _putchar('_'); 21 | } 22 | _putchar('\n'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - draws a diagonal line 5 | * @n: takes in an integer 6 | */ 7 | 8 | void print_diagonal(int n) 9 | { 10 | int i, j; 11 | 12 | if (n <= 0) 13 | _putchar('\n'); 14 | for (i = 0; i < n; i++) 15 | { 16 | for (j = 0; j <= i; j++) 17 | { 18 | if (i == j) 19 | { 20 | _putchar('\\'); 21 | _putchar('\n'); 22 | } 23 | else 24 | _putchar(' '); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - function to print a square 5 | * @size: takes in the size of the square 6 | */ 7 | 8 | void print_square(int size) 9 | { 10 | int i, j; 11 | 12 | if (size <= 0) 13 | _putchar('\n'); 14 | 15 | for (i = 0; i < size; i++) 16 | { 17 | for (j = 0; j < size; j++) 18 | _putchar('#'); 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry block hhhhhhhhhhhhhhhhhhhhhhhhhhh 5 | * @void: no argument 6 | * Return: 0 Success 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 1; i <= 100; i++) 14 | { 15 | if (i % 15 == 0) 16 | { 17 | printf("FizzBuzz "); 18 | } 19 | else if (i % 5 == 0) 20 | { 21 | if (i == 100) 22 | printf("Buzz"); 23 | else 24 | printf("Buzz "); 25 | } 26 | else if (i % 3 == 0) 27 | { 28 | printf("Fizz "); 29 | } 30 | else 31 | { 32 | printf("%d ", i); 33 | } 34 | } 35 | printf("\n"); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | more functions nested loops -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * Return: On success 1. 7 | * On error, -1 is returned, and errno is set appropriately. 8 | */ 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _isupper(int c); 5 | int _isdigit(int c); 6 | int mul(int a, int b); 7 | void print_numbers(void); 8 | int _putchar(char c); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - takes a pointer to a parameter and updates its value. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | } 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - Swaps the values of two integers. 5 | * @a: first integer. 6 | * @b: second integer. 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 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * Return: the int converted from the string 7 | */ 8 | 9 | int _atoi(char *s) 10 | { 11 | int i, d, n, len, f, digit; 12 | 13 | i = 0; 14 | d = 0; 15 | n = 0; 16 | len = 0; 17 | f = 0; 18 | digit = 0; 19 | 20 | while (s[len] != '\0') 21 | len++; 22 | 23 | while (i < len && f == 0) 24 | { 25 | if (s[i] == '-') 26 | ++d; 27 | 28 | if (s[i] >= '0' && s[i] <= '9') 29 | { 30 | digit = s[i] - '0'; 31 | 32 | if (d % 2) 33 | digit = -digit; 34 | n = n * 10 + digit; 35 | f = 1; 36 | 37 | if (s[i + 1] < '0' || s[i + 1] > '9') 38 | break; 39 | f = 0; 40 | } 41 | i++; 42 | } 43 | if (f == 0) 44 | return (0); 45 | return (n); 46 | } 47 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - program that generates random valid 7 | * passwords for the program 101-crackme 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int pass[100]; 14 | int i, sum, n; 15 | 16 | sum = 0; 17 | 18 | srand(time(NULL)); 19 | 20 | for (i = 0; i < 100; i++) 21 | { 22 | pass[i] = rand() % 78; 23 | sum += (pass[i] + '0'); 24 | putchar(pass[i] + '0'); 25 | if ((2772 - sum) - '0' < 78) 26 | { 27 | n = 2772 - sum - '0'; 28 | sum += n; 29 | putchar(n + '0'); 30 | break; 31 | } 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string. 5 | * @s: The string to get the length of. 6 | * Return: The length of @s. 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int lenStr = 0; 12 | 13 | while (*s++) 14 | lenStr++; 15 | return (lenStr); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | * Return: no return. 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | { 15 | _putchar(str[i]); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string, in reverse, followed by a new line. 5 | * @s: input string. 6 | * Return: no return. 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int count = 0; 12 | 13 | while (count >= 0) 14 | { 15 | if (s[count] == '\0') 16 | break; 17 | count++; 18 | } 19 | for (count--; count >= 0; count--) 20 | _putchar(s[count]); 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * rev_string - update value. 6 | * @s: value to be evaluate. 7 | * Return: not. 8 | */ 9 | 10 | void rev_string(char *s) 11 | { 12 | char tmp; 13 | int i, len, len1; 14 | 15 | len = 0; 16 | len1 = 0; 17 | 18 | while (s[len] != '\0') 19 | { 20 | len++; 21 | } 22 | len1 = len - 1; 23 | 24 | for (i = 0; i < len / 2; i++) 25 | { 26 | tmp = s[i]; 27 | s[i] = s[len1]; 28 | s[len1--] = tmp; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints one char out of 2 of a string 5 | * followed by a new line 6 | * @str: string to print the chars from 7 | */ 8 | 9 | void puts2(char *str) 10 | { 11 | int len, i; 12 | 13 | len = 0; 14 | 15 | while (str[len] != '\0') 16 | { 17 | len++; 18 | } 19 | 20 | for (i = 0; i < len; i += 2) 21 | { 22 | _putchar(str[i]); 23 | } 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints half of a string. 5 | * @str: input string. 6 | * Return: no return. 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int count = 0, i; 12 | 13 | while (count >= 0) 14 | { 15 | if (str[count] == '\0') 16 | break; 17 | count++; 18 | } 19 | 20 | if (count % 2 == 1) 21 | i = count / 2; 22 | else 23 | i = (count - 1) / 2; 24 | 25 | for (i++; i < count; i++) 26 | _putchar(str[i]); 27 | _putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - prints n elements of an array of integers. 6 | * @a: input array. 7 | * @n: input n elements 8 | * Return: no return. 9 | */ 10 | 11 | void print_array(int *a, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | printf("%d", *(a + i)); 18 | 19 | if (i != (n - 1)) 20 | printf(", "); 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * Return: the pointer to dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int len, i; 15 | 16 | len = 0; 17 | 18 | while (src[len] != '\0') 19 | { 20 | len++; 21 | } 22 | 23 | for (i = 0; i < len; i++) 24 | { 25 | dest[i] = src[i]; 26 | } 27 | 28 | dest[i] = '\0'; 29 | 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x05-pointers_arrays_strings -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | void reset_to_98(int *n); 5 | void swap_int(int *a, int *b); 6 | int _strlen(char *s); 7 | void _puts(char *str); 8 | int _putchar(char); 9 | void print_rev(char *s); 10 | void rev_string(char *s); 11 | void puts2(char *str); 12 | void puts_half(char *str); 13 | void print_array(int *a, int n); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concatenates two strings, 5 | * @dest: destination. 6 | * @src: source. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int count = 0, count2 = 0; 13 | 14 | while (*(dest + count) != '\0') 15 | { 16 | count++; 17 | } 18 | while (count2 >= 0) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * Return: a pointer to the resulting string dest 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | i++; 20 | while (src[j] != '\0' && j < n) 21 | { 22 | dest[i] = src[j]; 23 | i++; 24 | j++; 25 | } 26 | dest[i] = '\0'; 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string in rot13 5 | * @s: string to be encoded 6 | * Return: the resulting string 7 | */ 8 | 9 | char *rot13(char *s) 10 | { 11 | int i, j; 12 | 13 | char a[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 14 | char b[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | for (j = 0; a[j] != '\0'; j++) 19 | { 20 | if (s[i] == a[j]) 21 | { 22 | s[i] = b[j]; 23 | break; 24 | } 25 | } 26 | } 27 | return (s); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | 9 | void print_number(int n) 10 | { 11 | unsigned int m, d, count; 12 | 13 | if (n < 0) 14 | { 15 | _putchar(45); 16 | m = n * -1; 17 | } 18 | else 19 | { 20 | m = n; 21 | } 22 | 23 | d = m; 24 | count = 1; 25 | 26 | while (d > 9) 27 | { 28 | d /= 10; 29 | count *= 10; 30 | } 31 | for (; count >= 1; count /= 10) 32 | { 33 | _putchar(((m / count) % 10) + 48); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * infinite_add - adds two numbers 5 | * @n1: first number 6 | * @n2: second number 7 | * @r: buffer for result 8 | * @size_r: buffer size 9 | * ahhh! Crazy task! Expand your knowledge 10 | * Return: address of r or 0 11 | */ 12 | 13 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 14 | { 15 | int i, j, k, l, m, n; 16 | 17 | for (i = 0; n1[i]; i++) 18 | for (j = 0; n2[j]; j++) 19 | if (i > size_r || j > size_r) 20 | return (0); 21 | m = 0; 22 | for (i -= 1, j -= 1, k = 0; k < size_r - 1; i--, j--, k++) 23 | { 24 | n = m; 25 | if (i >= 0) 26 | n += n1[i] - '0'; 27 | if (j >= 0) 28 | n += n2[j] - '0'; 29 | if (i < 0 && j < 0 && n == 0) 30 | { 31 | break; 32 | } 33 | m = n / 10; 34 | r[k] = n % 10 + '0'; 35 | } 36 | r[k] = '\0'; 37 | if (i >= 0 || j >= 0 || m) 38 | return (0); 39 | for (k -= 1, l = 0; l < k; k--, l++) 40 | { 41 | m = r[k]; 42 | r[k] = r[l]; 43 | r[l] = m; 44 | } 45 | return (r); 46 | } 47 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_line - prints a s bytes of a buffer 6 | * @c: buffer to print 7 | * @s: bytes of buffer to print 8 | * @l: line of buffer to print 9 | * Return: void 10 | */ 11 | 12 | void print_line(char *c, int s, int l) 13 | { 14 | int j, k; 15 | 16 | for (j = 0; j <= 9; j++) 17 | { 18 | if (j <= s) 19 | printf("%02x", c[l * 10 + j]); 20 | else 21 | printf(" "); 22 | if (j % 2) 23 | putchar(' '); 24 | } 25 | for (k = 0; k <= s; k++) 26 | { 27 | if (c[l * 10 + k] > 31 && c[l * 10 + k] < 127) 28 | putchar(c[l * 10 + k]); 29 | else 30 | putchar('.'); 31 | } 32 | } 33 | 34 | /** 35 | * print_buffer - prints a buffer 36 | * @b: buffer to print 37 | * @size: size of buffer 38 | * Return: void 39 | */ 40 | 41 | void print_buffer(char *b, int size) 42 | { 43 | int i; 44 | 45 | for (i = 0; i <= (size - 1) / 10 && size; i++) 46 | { 47 | printf("%08x: ", i * 10); 48 | if (i < size / 10) 49 | { 50 | print_line(b, 9, i); 51 | } 52 | else 53 | { 54 | print_line(b, size % 10 - 1, i); 55 | } 56 | putchar('\n'); 57 | } 58 | if (size == 0) 59 | putchar('\n'); 60 | } 61 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copy a string 5 | * @dest: input value 6 | * @src: input value 7 | * @n: input value 8 | * Return: dest 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int j; 14 | 15 | j = 0; 16 | while (j < n && src[j] != '\0') 17 | { 18 | dest[j] = src[j]; 19 | j++; 20 | } 21 | while (j < n) 22 | { 23 | dest[j] = '\0'; 24 | j++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare string values 5 | * @s1: input value 6 | * @s2: input value 7 | * Return: s1[i] - s2[i] 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (s1[i] != '\0' && s2[i] != '\0') 16 | { 17 | if (s1[i] != s2[i]) 18 | { 19 | return (s1[i] - s2[i]); 20 | } 21 | i++; 22 | } 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - reverses an array of integers 5 | * @a: array to be reversed 6 | * @n: number of elements in the array 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int i, j, tmp; 12 | 13 | j = n - 1; 14 | 15 | for (i = 0; i < n / 2; i++) 16 | { 17 | tmp = a[i]; 18 | a[i] = a[j]; 19 | a[j--] = tmp; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - change all lowercase to uppercase 5 | * @n: pointer 6 | * Return: n 7 | */ 8 | 9 | char *string_toupper(char *n) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (n[i] != '\0') 15 | { 16 | if (n[i] >= 'a' && n[i] <= 'z') 17 | n[i] = n[i] - 32; 18 | i++; 19 | } 20 | return (n); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes everey word of a string 5 | * @s: string to modify 6 | * Return: the resulting string 7 | */ 8 | 9 | char *cap_string(char *s) 10 | { 11 | int i, j; 12 | 13 | char spe[13] = {' ', '\t', '\n', ',', ';', '.', 14 | '!', '?', '"', '(', ')', '{', '}'}; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | if (i == 0 && s[i] >= 'a' && s[i] <= 'z') 19 | s[i] -= 32; 20 | 21 | for (j = 0; j < 13; j++) 22 | { 23 | if (s[i] == spe[j]) 24 | { 25 | if (s[i + 1] >= 'a' && s[i + 1] <= 'z') 26 | { 27 | s[i + 1] -= 32; 28 | } 29 | } 30 | } 31 | } 32 | return (s); 33 | } 34 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - encode into 1337speak 5 | * @n: input value 6 | * Return: n value 7 | */ 8 | 9 | char *leet(char *n) 10 | { 11 | int i, j; 12 | char s1[] = "aAeEoOtTlL"; 13 | char s2[] = "4433007711"; 14 | 15 | for (i = 0; n[i] != '\0'; i++) 16 | { 17 | for (j = 0; j < 10; j++) 18 | { 19 | if (n[i] == s1[j]) 20 | { 21 | n[i] = s2[j]; 22 | } 23 | } 24 | } 25 | return (n); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x06-pointers_arrays_strings -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *_strcat(char *dest, char *src); 5 | char *_strncat(char *dest, char *src, int n); 6 | char *_strncpy(char *dest, char *src, int n); 7 | int _strcmp(char *s1, char *s2); 8 | void reverse_array(int *a, int n); 9 | char *string_toupper(char *); 10 | char *cap_string(char *); 11 | char *leet(char *); 12 | int _putchar(char); 13 | char *rot13(char *); 14 | void print_number(int n); 15 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 16 | void print_buffer(char *b, int size); 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/.101-crackme_password.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x07-pointers_arrays_strings/.101-crackme_password.swp -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - ironnou 5 | * @s: pointer to char params 6 | * @b: data to change 7 | * @n: index 8 | * Return: *s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | s[i] = b; 18 | } 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - irontnoux2 5 | * @dest: pointer to char params 6 | * @src: pointer to char params 7 | * @n: size 8 | * Return: *dest 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets pointer val to char 5 | * @s: pointer 6 | * @to: char 7 | */ 8 | 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - prints found c 5 | * @s: pointer to char 6 | * @c: char params to found 7 | * Return: *S 8 | */ 9 | 10 | char *_strchr(char *s, char c) 11 | { 12 | while (*s != '\0') 13 | { 14 | if (*s == c) 15 | { 16 | return (s); 17 | } 18 | s++; 19 | } 20 | if (*s == c) 21 | { 22 | return (s); 23 | } 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - Gets the length of a prefix substring. 5 | * @s: String where substring will look. 6 | * @accept: Substring of accepted chars. 7 | * Return: Length of occurrence. 8 | */ 9 | 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int c = 0; 13 | char *t = accept; 14 | 15 | while (*s++) 16 | { 17 | while (*accept++) 18 | if (*(s - 1) == *(accept - 1)) 19 | { 20 | c++; 21 | break; 22 | } 23 | if (!(*--accept)) 24 | break; 25 | accept = t; 26 | } 27 | return (c); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strpbrk - bytes 6 | * @s: pointer to char 7 | * @accept: pointer to char 8 | * Return: NULL 9 | */ 10 | 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i; 14 | 15 | while (*s) 16 | { 17 | for (i = 0; accept[i]; i++) 18 | { 19 | if (*s == accept[i]) 20 | { 21 | return (s); 22 | } 23 | } 24 | s++; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - function locate 5 | * @haystack: pointer to char 6 | * @needle: pointer to char 7 | * Return: 0 8 | */ 9 | 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | char *result = haystack, *fneedle = needle; 13 | 14 | while (*haystack) 15 | { 16 | while (*needle) 17 | { 18 | if (*haystack++ != *needle++) 19 | { 20 | break; 21 | } 22 | } 23 | if (!*needle) 24 | { 25 | return (result); 26 | } 27 | needle = fneedle; 28 | result++; 29 | haystack = result; 30 | } 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints chessboard. 5 | * @a: matrix 6 | */ 7 | 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < 8; i++) 13 | { 14 | for (j = 0; j < 8; j++) 15 | { 16 | _putchar(*(*(i + a) + j)); 17 | } 18 | _putchar('\n'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - prints sums 6 | * @a: a pointer 7 | * @size: size 8 | */ 9 | 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i, sum1 = 0, sum2 = 0; 13 | 14 | for (i = 0; i < size; i++) 15 | { 16 | sum1 += *(a + (size * i + i)); 17 | sum2 += *(a + (size * i + size - 1 - i)); 18 | } 19 | printf("%d, ", sum1); 20 | printf("%d\n", sum2); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x07-pointers_arrays_strings -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: character to print 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *_memset(char *s, char b, unsigned int n); 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn(char *s, char *accept); 8 | char *_strpbrk(char *s, char *accept); 9 | char *_strstr(char *haystack, char *needle); 10 | void print_chessboard(char (*a)[8]); 11 | int _putchar(char c); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *to); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - print a string. 5 | * @s: pointer to char 6 | * Return: No. 7 | */ 8 | 9 | void _puts_recursion(char *s) 10 | { 11 | if (*s != '\0') 12 | { 13 | _putchar(*s); 14 | _puts_recursion(s + 1); 15 | } 16 | else 17 | { 18 | _putchar('\n'); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - reverse 5 | * @s: pointer to string params 6 | */ 7 | 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s) 11 | { 12 | _print_rev_recursion(s + 1); 13 | _putchar(*s); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - size 5 | * @s: pointer to string params 6 | * Return: recursion 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | if (!*s) 12 | { 13 | return (0); 14 | } 15 | return (1 + _strlen_recursion(++s)); 16 | } 17 | 18 | /** 19 | * p1 - palindrome 20 | * @s: pointer to string 21 | * @l: position 22 | * Return: boolena 23 | */ 24 | 25 | int p1(char *s, int l) 26 | { 27 | if (l < 1) 28 | { 29 | return (1); 30 | } 31 | if (*s == *(s + l)) 32 | { 33 | return (p1(s + 1, l - 2)); 34 | } 35 | return (0); 36 | } 37 | 38 | /** 39 | * is_palindrome - palindrome 40 | * @s: pointer to string 41 | * Return: recursion 42 | */ 43 | 44 | int is_palindrome(char *s) 45 | { 46 | int len = _strlen_recursion(s); 47 | 48 | return (p1(s, len - 1)); 49 | } 50 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - Compare strings 5 | * @s1: pointer to string params 6 | * @s2: pointer to string params 7 | * Return: 0 8 | */ 9 | 10 | int wildcmp(char *s1, char *s2) 11 | { 12 | if (*s1 == '\0') 13 | { 14 | if (*s2 != '\0' && *s2 == '*') 15 | { 16 | return (wildcmp(s1, s2 + 1)); 17 | } 18 | return (*s2 == '\0'); 19 | } 20 | if (*s2 == '*') 21 | { 22 | return (wildcmp(s1 + 1, s2) || wildcmp(s1, s2 + 1)); 23 | } 24 | else if (*s1 == *s2) 25 | { 26 | return (wildcmp(s1 + 1, s2 + 1)); 27 | } 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - size 5 | * @s: pointer to string params 6 | * Return: recursion 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | if (!*s) 12 | { 13 | return (0); 14 | } 15 | return (1 + _strlen_recursion(++s)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - factorial n 5 | * @n: integer arams 6 | * Return: recursion 7 | */ 8 | 9 | int factorial(int n) 10 | { 11 | if (n < 0) 12 | { 13 | return (-1); 14 | } 15 | if (n == 1) 16 | { 17 | return (1); 18 | } 19 | return (n * factorial(n - 1)); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - pow recursion 5 | * @x: integer params 6 | * @y: integer paramtr 7 | * Return: recursion 8 | */ 9 | 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | { 14 | return (-1); 15 | } 16 | if (y == 0) 17 | { 18 | return (1); 19 | } 20 | return (x * _pow_recursion(x, y - 1)); 21 | } 22 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _sqrt_recursion - main funct 5 | * @n: int n 6 | * Return: int 7 | */ 8 | 9 | int _sqrt_recursion(int n) 10 | { 11 | return (_sqrt(n, 1)); 12 | } 13 | 14 | /** 15 | * _sqrt - _sqrt_recursion 16 | * @n: integer paramtr 17 | * @i: integer parameter 18 | * Return: sqrt 19 | */ 20 | 21 | int _sqrt(int n, int i) 22 | { 23 | if (n < 0) 24 | return (-1); 25 | if ((i * i) > n) 26 | return (-1); 27 | if (i * i == n) 28 | return (i); 29 | return (_sqrt(n, i + 1)); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int tmp_prime(int n, int i); 4 | 5 | /** 6 | * divisors - number is prime? 7 | * @n: integer params 8 | * @m: integer params 9 | * Return: boolean 10 | */ 11 | 12 | int divisors(int n, int m) 13 | { 14 | if (m % n == 0) 15 | { 16 | return (0); 17 | } 18 | else if (m / 2 > n) 19 | { 20 | return (divisors(n + 2, m)); 21 | } 22 | else 23 | { 24 | return (1); 25 | } 26 | } 27 | 28 | /** 29 | * is_prime_number - prime 30 | * @n: integer params 31 | * Return: recursion 32 | */ 33 | 34 | int is_prime_number(int n) 35 | { 36 | if ((!(n % 2) && n != 2) || n < 2) 37 | { 38 | return (0); 39 | } 40 | else 41 | { 42 | return (divisors(3, n)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | 0x08-recursion -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | void _puts_recursion(char *s); 5 | void _print_rev_recursion(char *s); 6 | void _putchar(char c); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int x, int y); 10 | int _sqrt_recursion(int n); 11 | int _sqrt(int n, int i); 12 | int wildcmp(char *s1, char *s2); 13 | int is_prime_number(int n); 14 | int is_palindrome(char *s); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - checks for uppercase character 6 | * @c: Variable text 7 | * Return: Always 0. 8 | */ 9 | int _isupper(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - ironnou 5 | * @s: pointer to char params 6 | * @b: data to change 7 | * @n: index 8 | * Return: *s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | s[i] = b; 18 | } 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concatenates two strings, 5 | * @dest: destination. 6 | * @src: source. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int count = 0, count2 = 0; 13 | 14 | while (*(dest + count) != '\0') 15 | { 16 | count++; 17 | } 18 | while (count2 >= 0) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - function that check if it's a digit 5 | * @c: takes in a character/digit 6 | * Return: 1 if digit and 0 if anything else 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= '0' && c <= '9') 11 | return (1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - irontnoux2 5 | * @dest: pointer to char params 6 | * @src: pointer to char params 7 | * @n: size 8 | * Return: *dest 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * Return: a pointer to the resulting string dest 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | i++; 20 | while (src[j] != '\0' && j < n) 21 | { 22 | dest[i] = src[j]; 23 | i++; 24 | j++; 25 | } 26 | dest[i] = '\0'; 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * Return: the int converted from the string 7 | */ 8 | 9 | int _atoi(char *s) 10 | { 11 | int i, d, n, len, f, digit; 12 | 13 | i = 0; 14 | d = 0; 15 | n = 0; 16 | len = 0; 17 | f = 0; 18 | digit = 0; 19 | 20 | while (s[len] != '\0') 21 | len++; 22 | 23 | while (i < len && f == 0) 24 | { 25 | if (s[i] == '-') 26 | ++d; 27 | 28 | if (s[i] >= '0' && s[i] <= '9') 29 | { 30 | digit = s[i] - '0'; 31 | 32 | if (d % 2) 33 | digit = -digit; 34 | n = n * 10 + digit; 35 | f = 1; 36 | 37 | if (s[i + 1] < '0' || s[i + 1] > '9') 38 | break; 39 | f = 0; 40 | } 41 | i++; 42 | } 43 | if (f == 0) 44 | return (0); 45 | return (n); 46 | } 47 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - prints found c 5 | * @s: pointer to char 6 | * @c: char params to found 7 | * Return: *S 8 | */ 9 | 10 | char *_strchr(char *s, char c) 11 | { 12 | while (*s != '\0') 13 | { 14 | if (*s == c) 15 | { 16 | return (s); 17 | } 18 | s++; 19 | } 20 | if (*s == c) 21 | { 22 | return (s); 23 | } 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string. 5 | * @s: The string to get the length of. 6 | * Return: The length of @s. 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int lenStr = 0; 12 | 13 | while (*s++) 14 | lenStr++; 15 | return (lenStr); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copy a string 5 | * @dest: input value 6 | * @src: input value 7 | * @n: input value 8 | * Return: dest 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int j; 14 | 15 | j = 0; 16 | while (j < n && src[j] != '\0') 17 | { 18 | dest[j] = src[j]; 19 | j++; 20 | } 21 | while (j < n) 22 | { 23 | dest[j] = '\0'; 24 | j++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _islower - checks if a charactor is lowercase 4 | * @c: the character 5 | * Return: 1 if letter is lowercase. 0 if not 6 | */ 7 | int _islower(int c) 8 | { 9 | if (c >= 'a' && c <= 'z') 10 | return (1); 11 | else 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | * Return: no return. 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | { 15 | _putchar(str[i]); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare string values 5 | * @s1: input value 6 | * @s2: input value 7 | * Return: s1[i] - s2[i] 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (s1[i] != '\0' && s2[i] != '\0') 16 | { 17 | if (s1[i] != s2[i]) 18 | { 19 | return (s1[i] - s2[i]); 20 | } 21 | i++; 22 | } 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - Gets the length of a prefix substring. 5 | * @s: String where substring will look. 6 | * @accept: Substring of accepted chars. 7 | * Return: Length of occurrence. 8 | */ 9 | 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int c = 0; 13 | char *t = accept; 14 | 15 | while (*s++) 16 | { 17 | while (*accept++) 18 | if (*(s - 1) == *(accept - 1)) 19 | { 20 | c++; 21 | break; 22 | } 23 | if (!(*--accept)) 24 | break; 25 | accept = t; 26 | } 27 | return (c); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _isalpha -checks for alphabets 3 | * @c: character to be cheked 4 | * 5 | * Return: 1 if character is a letter, 0 otherwise 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 10 | return (1); 11 | else 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strpbrk - bytes 6 | * @s: pointer to char 7 | * @accept: pointer to char 8 | * Return: NULL 9 | */ 10 | 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i; 14 | 15 | while (*s) 16 | { 17 | for (i = 0; accept[i]; i++) 18 | { 19 | if (*s == accept[i]) 20 | { 21 | return (s); 22 | } 23 | } 24 | s++; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - function locate 5 | * @haystack: pointer to char 6 | * @needle: pointer to char 7 | * Return: 0 8 | */ 9 | 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | char *result = haystack, *fneedle = needle; 13 | 14 | while (*haystack) 15 | { 16 | while (*needle) 17 | { 18 | if (*haystack++ != *needle++) 19 | { 20 | break; 21 | } 22 | } 23 | if (!*needle) 24 | { 25 | return (result); 26 | } 27 | needle = fneedle; 28 | result++; 29 | haystack = result; 30 | } 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - Compuyer the absolute value of an integer 4 | * @t: Ther integer to be computed 5 | * Return: The absolute value 6 | */ 7 | int _abs(int t) 8 | { 9 | if (t >= 0) 10 | return (t); 11 | else 12 | return (-t); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * Return: the pointer to dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int len, i; 15 | 16 | len = 0; 17 | 18 | while (src[len] != '\0') 19 | { 20 | len++; 21 | } 22 | 23 | for (i = 0; i < len; i++) 24 | { 25 | dest[i] = src[i]; 26 | } 27 | 28 | dest[i] = '\0'; 29 | 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | 0x09-static_libraries -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _putchar - used to write char "d" to stdot 5 | * @c: The character to be print 6 | * Return: 1 in success & error on -1 7 | */ 8 | int _putchar(char c) 9 | { 10 | return (write(1, &c, 1)); 11 | } 12 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main- prints the program name followed by new line 5 | * @argc: the number of arguments written in the program 6 | * @argv: an array of arguments 7 | * Return: always 0 8 | */ 9 | 10 | int main(int __attribute__((__unused__)) argc, char *argv[]) 11 | { 12 | printf("%s\n", argv[0]); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the number of argumments passed followed by new line 5 | * @argc: the number of arguments in the program 6 | * @argv: an array of arguments 7 | * Return: always 0 8 | */ 9 | 10 | int main(int argc, char __attribute__((__unused__)) *argv[]) 11 | { 12 | printf("%d\n", argc - 1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Prints the minimum number of coins 6 | * @argc: The number of arguments in the program 7 | * @argv: An array of pointers to the arguments 8 | * Return: If the number of arguments is not exactly one 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int cents, coins = 0; 14 | 15 | if (argc != 2) 16 | { 17 | printf("Error\n"); 18 | return (1); 19 | } 20 | cents = atoi(argv[1]); 21 | 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 | 49 | return (0); 50 | } 51 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all arguments it receives 5 | * @argc: the number of argumments in the program 6 | * @argv: an array of arguments 7 | * Return: always 0 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < argc; i++) 14 | printf("%s\n", argv[i]); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints the multiplication of two numbers followed by a new line 6 | * @argc: the number of arrguments in the program 7 | * @argv: an array of arguments 8 | * Return: if the program receives two arguments -0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int n1, n2, prod; 14 | 15 | if (argc != 3) 16 | { 17 | printf("Error\n"); 18 | return (1); 19 | } 20 | n1 = atoi(argv[1]); 21 | n2 = atoi(argv[2]); 22 | prod = n1 * n2; 23 | 24 | printf("%d\n", prod); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - the sum of positive numbers followed by a new line 5 | * @argc: the number of arguments in a program 6 | * @argv: the array of arguments 7 | * Return: always 0 8 | */ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int num1; 13 | int num2; 14 | int sum = num1 + num2; 15 | 16 | if (argc == 0) 17 | { 18 | printf("0\n"); 19 | } 20 | 21 | else if (num1 == char || num2 == char) 22 | { 23 | printf("Error\n"); 24 | return (1); 25 | } 26 | else 27 | { 28 | printf("%d\n", sum); 29 | return (0); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | 0x0A-argc_argv -------------------------------------------------------------------------------- /0x0A-argc_argv/args: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0A-argc_argv/args -------------------------------------------------------------------------------- /0x0A-argc_argv/change: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0A-argc_argv/change -------------------------------------------------------------------------------- /0x0A-argc_argv/mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0A-argc_argv/mul -------------------------------------------------------------------------------- /0x0A-argc_argv/nargs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0A-argc_argv/nargs -------------------------------------------------------------------------------- /0x0B-malloc_free/.100-argstostr.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0B-malloc_free/.100-argstostr.c.swp -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * create_array - creates an array. 6 | * @size: size of the array. 7 | * @c: storaged char 8 | * Return: pointer of an array 9 | */ 10 | 11 | char *create_array(unsigned int size, char c) 12 | { 13 | char *ch; 14 | unsigned int n; 15 | 16 | if (size == 0) 17 | return (NULL); 18 | 19 | ch = malloc(sizeof(c) * size); 20 | 21 | if (ch == NULL) 22 | return (NULL); 23 | 24 | for (n = 0; n < size; n++) 25 | ch[n] = c; 26 | 27 | return (ch); 28 | } 29 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strdup - returns a pointer to a new allocated space. 6 | * @str: string. 7 | * Return: pointer of an array 8 | */ 9 | 10 | char *_strdup(char *str) 11 | { 12 | char *strout; 13 | unsigned int a, b; 14 | 15 | if (str == NULL) 16 | return (NULL); 17 | 18 | for (a = 0; str[a] != '\0'; a++) 19 | ; 20 | 21 | strout = (char *)malloc(sizeof(char) * (a + 1)); 22 | 23 | if (strout == NULL) 24 | return (NULL); 25 | 26 | for (b = 0; b <= a; b++) 27 | strout[b] = str[b]; 28 | 29 | return (strout); 30 | } 31 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments. 6 | * @ac: argument count. 7 | * @av: argument vector. 8 | * Return: pointer of an array 9 | */ 10 | 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *aout; 14 | int x, y, z, i; 15 | 16 | if (ac == 0) 17 | { 18 | return (NULL); 19 | } 20 | for (x = y = 0; y < ac; y++) 21 | { 22 | if (av[y] == NULL) 23 | return (NULL); 24 | 25 | for (z = 0; av[y][z] != '\0'; z++) 26 | x++; 27 | x++; 28 | } 29 | 30 | aout = malloc((x + 1) * sizeof(char)); 31 | 32 | if (aout == NULL) 33 | { 34 | free(aout); 35 | return (NULL); 36 | } 37 | 38 | for (y = z = i = 0; i < x; z++, i++) 39 | { 40 | if (av[y][z] == '\0') 41 | { 42 | aout[i] = '\n'; 43 | y++; 44 | i++; 45 | z = 0; 46 | } 47 | if (i < x - 1) 48 | aout[i] = av[y][z]; 49 | } 50 | aout[i] = '\0'; 51 | 52 | return (aout); 53 | } 54 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * ch_free_grid - frees a 2 dimensional array. 6 | * @grid: 2 dimensional array of char. 7 | * @height: height of the array. 8 | * Return: no return 9 | */ 10 | 11 | void ch_free_grid(char **grid, unsigned int height) 12 | { 13 | if (grid != NULL && height != 0) 14 | { 15 | for (; height > 0; height--) 16 | free(grid[height]); 17 | free(grid[height]); 18 | free(grid); 19 | } 20 | } 21 | 22 | /** 23 | * strtow - splits a string into words. 24 | * @str: string. 25 | * Return: pointer of an array 26 | */ 27 | 28 | char **strtow(char *str) 29 | { 30 | char **aout; 31 | unsigned int x, height, y, z, a; 32 | 33 | if (str == NULL || *str == '\0') 34 | return (NULL); 35 | for (x = height = 0; str[x] != '\0'; x++) 36 | { 37 | if (str[x] != ' ' && (str[x + 1] == ' ' || str[x + 1] == '\0')) 38 | height++; 39 | } 40 | aout = malloc((height + 1) * sizeof(char *)); 41 | if (aout == NULL || height == 0) 42 | { 43 | free(aout); 44 | return (NULL); 45 | } 46 | for (y = a = 0; y < height; y++) 47 | { 48 | for (x = a; str[x] != '\0'; x++) 49 | { 50 | if (str[x] == ' ') 51 | a++; 52 | if (str[x] != ' ' && (str[x + 1] == ' ' || str[x + 1] == '\0')) 53 | { 54 | aout[y] = malloc((x - a + 2) * sizeof(char)); 55 | if (aout[y] == NULL) 56 | { 57 | ch_free_grid(aout, y); 58 | return (NULL); 59 | } 60 | break; 61 | } 62 | } 63 | for (z = 0; a <= x; a++, z++) 64 | aout[y][z] = str[a]; 65 | aout[y][z] = '\0'; 66 | } 67 | aout[y] = NULL; 68 | return (aout); 69 | } 70 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - concatenates two strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * Return: pointer of an array. 9 | */ 10 | char *str_concat(char *s1, char *s2) 11 | { 12 | char *strout; 13 | unsigned int x, y, z, limit; 14 | 15 | if (s1 == NULL) 16 | s1 = ""; 17 | if (s2 == NULL) 18 | s2 = ""; 19 | 20 | for (x = 0; s1[x] != '\0'; x++) 21 | ; 22 | 23 | for (y = 0; s2[y] != '\0'; y++) 24 | ; 25 | 26 | strout = malloc(sizeof(char) * (x + y + 1)); 27 | 28 | if (strout == NULL) 29 | { 30 | free(strout); 31 | return (NULL); 32 | } 33 | 34 | for (z = 0; z < x; z++) 35 | strout[z] = s1[z]; 36 | 37 | limit = y; 38 | for (y = 0; y <= limit; z++, y++) 39 | strout[z] = s2[y]; 40 | 41 | return (strout); 42 | } 43 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - returns a pointer to an array of integers. 6 | * @width: width of the array. 7 | * @height: height of the array. 8 | * Return: pointer of an array. 9 | */ 10 | 11 | int **alloc_grid(int width, int height) 12 | { 13 | int **gridout; 14 | int x, y; 15 | 16 | if (width < 1 || height < 1) 17 | return (NULL); 18 | 19 | gridout = malloc(height * sizeof(int *)); 20 | if (gridout == NULL) 21 | { 22 | free(gridout); 23 | return (NULL); 24 | } 25 | 26 | for (x = 0; x < height; x++) 27 | { 28 | gridout[x] = malloc(width * sizeof(int)); 29 | if (gridout[x] == NULL) 30 | { 31 | for (x--; x >= 0; x--) 32 | free(gridout[x]); 33 | free(gridout); 34 | return (NULL); 35 | } 36 | } 37 | 38 | for (x = 0; x < height; x++) 39 | for (y = 0; y < width; y++) 40 | gridout[x][y] = 0; 41 | 42 | return (gridout); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - frees a 2 dimensional grid. 6 | * @grid: 2 dimensional array. 7 | * @height: height of the grid. 8 | * Return: no return 9 | */ 10 | 11 | void free_grid(int **grid, int height) 12 | { 13 | if (grid != NULL && height != 0) 14 | { 15 | for (; height >= 0; height--) 16 | free(grid[height]); 17 | free(grid); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0B-malloc_free -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | char *create_array(unsigned int size, char c); 4 | char *_strdup(char *str); 5 | char *str_concat(char *s1, char *s2); 6 | int **alloc_grid(int width, int height); 7 | void free_grid(int **grid, int height); 8 | char *argstostr(int ac, char **av); 9 | char **strtow(char *str); 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/.101-mul.c.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0C-more_malloc_free/.101-mul.c.swo -------------------------------------------------------------------------------- /0x0C-more_malloc_free/.101-mul.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0C-more_malloc_free/.101-mul.c.swp -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * malloc_checked - allocates memory by malloc. 5 | * @b: size of memory to be allocated. 6 | * Return: pointer to allocated memory. 7 | */ 8 | 9 | void *malloc_checked(unsigned int b) 10 | { 11 | void *x = malloc(b); 12 | 13 | if (x == NULL) 14 | exit(98); 15 | 16 | return (x); 17 | } 18 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * _strlen - calculates and returns length of string. 7 | * @string: string 8 | * Return: string length 9 | */ 10 | 11 | int _strlen(char *string) 12 | { 13 | int x; 14 | 15 | for (x = 0; string[x] != '\0'; x++) 16 | ; 17 | return (x); 18 | } 19 | 20 | /** 21 | * string_nconcat - concatenate s1 and n bytes of s2 and returns ptr to string 22 | * @s1: string 1 23 | * @s2: string 2 24 | * @n: n bytes to concat from string 2 25 | * Return: pointer to concatenated string 26 | */ 27 | 28 | char *string_nconcat(char *s1, char *s2, unsigned int n) 29 | { 30 | char *ptr; 31 | int num, len, x, y; 32 | 33 | num = n; 34 | 35 | if (s1 == NULL) /* account for NULL strings */ 36 | s1 = ""; 37 | if (s2 == NULL) 38 | s2 = ""; 39 | if (num < 0) /* account for negative n bytes */ 40 | return (NULL); 41 | if (num >= _strlen(s2)) /* account for n too big */ 42 | num = _strlen(s2); 43 | 44 | len = _strlen(s1) + num + 1; /* +1 to account for null pointer */ 45 | 46 | ptr = malloc(sizeof(*ptr) * len); /* malloc and check for error */ 47 | if (ptr == NULL) 48 | return (NULL); 49 | 50 | for (x = 0; s1[x] != '\0'; x++) /* concat */ 51 | ptr[x] = s1[x]; 52 | for (y = 0; y < num; y++) 53 | ptr[x + y] = s2[y]; 54 | ptr[x + y] = '\0'; 55 | 56 | return (ptr); 57 | } 58 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * simple_print_buffer - prints buffer in hexa 8 | * @buffer: the address of memory to print 9 | * @size: the size of the memory to print 10 | * 11 | * Return: Nothing. 12 | */ 13 | void simple_print_buffer(char *buffer, unsigned int size) 14 | { 15 | unsigned int i; 16 | 17 | i = 0; 18 | while (i < size) 19 | { 20 | if (i % 10) 21 | { 22 | printf(" "); 23 | } 24 | if (!(i % 10) && i) 25 | { 26 | printf("\n"); 27 | } 28 | printf("0x%02x", buffer[i]); 29 | i++; 30 | } 31 | printf("\n"); 32 | } 33 | 34 | /** 35 | * main - check the code for 36 | * 37 | * Return: Always 0. 38 | */ 39 | int main(void) 40 | { 41 | char *p; 42 | int i; 43 | 44 | p = malloc(sizeof(char) * 10); 45 | p = _realloc(p, sizeof(char) * 10, sizeof(char) * 98); 46 | i = 0; 47 | while (i < 98) 48 | { 49 | p[i++] = 98; 50 | } 51 | simple_print_buffer(p, 98); 52 | free(p); 53 | return (0); 54 | } 55 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _realloc - reallocates a memory block using malloc and free. 6 | * @ptr: pointer to the memory previously allocated with a 7 | * call to ``` malloc : malloc(old_size) ```. 8 | * @old_size: size in bytes allocated for ptr. 9 | * @new_size: size in bytes of new memory block. 10 | * Return: pointer to new mem block, NULL or ptr. 11 | */ 12 | 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 14 | { 15 | char *nptr; 16 | unsigned int x; 17 | 18 | if (new_size == old_size) 19 | return (ptr); 20 | if ((new_size == 0) && (ptr != NULL)) 21 | { 22 | free(ptr); 23 | return (NULL); 24 | } 25 | if (ptr == NULL) 26 | { 27 | nptr = malloc(new_size); 28 | if (nptr == NULL) 29 | return (NULL); 30 | } 31 | if (new_size > old_size && (ptr != NULL)) 32 | { 33 | nptr = malloc(new_size); 34 | if (nptr == NULL) 35 | return (nptr); 36 | for (x = 0; x < old_size; x++) 37 | nptr[x] = *((char *)ptr + 1); 38 | free(ptr); 39 | } 40 | 41 | return (nptr); 42 | } 43 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0C-more_malloc_free/101-mul -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - multiplies two positive numbers 7 | * @argc: number of arguments 8 | * @argv: An array of pointers. 9 | * Return: int 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | unsigned long mul; 15 | int num1, num2; 16 | 17 | if (argc != 3) 18 | { 19 | printf("Error\n"); 20 | exit(98); 21 | } 22 | 23 | for (num1 = 1; num1 < argc; num1++) 24 | { 25 | for (num2 = 0; argv[num1][num2] != '\0'; num2++) 26 | { 27 | if (argv[num1][num2] > 57 || argv[num1][num2] < 48) 28 | { 29 | printf("Error\n"); 30 | exit(98); 31 | } 32 | } 33 | 34 | } 35 | mul = atol(argv[0]) * atol(argv[1]); 36 | printf("%lu\n", mul); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _calloc - allocates memory of an array using malloc. 6 | * @nmemb: number of elements in an array. 7 | * @size: size of elements of an array. 8 | * Return: NULL is size or nmemb == 0. 9 | * NULL if malloc fails. 10 | * Pointer to memory allocated if successful. 11 | */ 12 | 13 | void *_calloc(unsigned int nmemb, unsigned int size) 14 | { 15 | void *n; 16 | unsigned int x; 17 | 18 | if (nmemb == 0 || size == 0) 19 | return (NULL); 20 | n = malloc(nmemb * size); 21 | if (n == NULL) 22 | { 23 | return (NULL); 24 | } 25 | 26 | for (x = 0; x < (nmemb * size); x++) 27 | { 28 | *((char *)(n) + x) = 0; 29 | } 30 | 31 | return (n); 32 | } 33 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *array_range - creates an array. 6 | *@min: minimum value. 7 | *@max: maximum value. 8 | *Return: pointer to the newly created array. 9 | *NULL if malloc fails. 10 | *NULL if min > max. 11 | */ 12 | 13 | int *array_range(int min, int max) 14 | { 15 | int range, x; 16 | int *n; 17 | 18 | range = 0; 19 | 20 | if (min > max) 21 | return (NULL); 22 | 23 | range = ((max + 1) - min); 24 | 25 | n = malloc(range * sizeof(int)); 26 | 27 | if (n == NULL) 28 | return (NULL); 29 | 30 | for (x = 0; x < range; x++) 31 | { 32 | *(n + x) = min + x; 33 | } 34 | 35 | return (n); 36 | } 37 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0C-more_malloc_free -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void _puts(char *str); 5 | void *malloc_checked(unsigned int b); 6 | char *string_nconcat(char *s1, char *s2, unsigned int n); 7 | void *_calloc(unsigned int nmemb, unsigned int size); 8 | int *array_range(int min, int max); 9 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 10 | int _putchar(char c); 11 | int _atoi(const char *s); 12 | void print_int(unsigned long int n); 13 | 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/100-realloc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0C-more_malloc_free/test/100-realloc -------------------------------------------------------------------------------- /0x0C-more_malloc_free/test/101-mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0C-more_malloc_free/test/101-mul -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | #include 3 | /** 4 | * print_name - prints a name 5 | * @name: someone's name 6 | * @f: pointer to a function 7 | * Return: void 8 | */ 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | if (f != NULL) 12 | f(name); 13 | } 14 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - execute function on each element of an array 5 | * @array: given array 6 | * @size: size of an array 7 | * @action: pointer to function 8 | * Return: void 9 | */ 10 | 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | int x; 14 | 15 | if (array == NULL || action == NULL) 16 | return; 17 | 18 | for (x = 0; x < (int) size; x++) 19 | { 20 | action(array[x]); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * print_opcodes - prints opcodes of the program 6 | * @a: address of the main function 7 | * @n: number of bytes 8 | * Return: void 9 | */ 10 | 11 | void print_opcodes(char *a, int n) 12 | { 13 | int x; 14 | 15 | for (x = 0; x < n; x++) 16 | { 17 | printf("%.2hhx", a[x]); 18 | if (x < n - 1) 19 | printf(" "); 20 | } 21 | printf("\n"); 22 | 23 | } 24 | 25 | /** 26 | * main - prints the opcodes of its own main function 27 | * @argc: argument count 28 | * @argv: argument vector 29 | * Return: O 30 | */ 31 | 32 | int main(int argc, char **argv) 33 | { 34 | int a; 35 | 36 | if (argc != 2) 37 | { 38 | printf("Error\n"); 39 | exit(1); 40 | } 41 | a = atoi(argv[1]); 42 | if (a < 0) 43 | { 44 | printf("Error\n"); 45 | exit(2); 46 | } 47 | print_opcodes((char *)&main, a); 48 | return (0); 49 | } 50 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer 5 | * @array: array 6 | * @size: size of an array 7 | * @cmp: function pointer 8 | * Return: count of integer 9 | */ 10 | 11 | int int_index(int *array, int size, int (*cmp)(int)) 12 | { 13 | int x; 14 | 15 | if (size <= 0) 16 | return (-1); 17 | if (array == NULL || cmp == NULL) 18 | return (-1); 19 | 20 | for (x = 0; x < size; x++) 21 | { 22 | if (cmp(array[x]) == 1) 23 | return (x); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _HEADER_ 2 | #define _HEADER_ 3 | 4 | /** 5 | * struct op - Struct op 6 | * @op: operator 7 | * @f: function associated 8 | */ 9 | 10 | typedef struct op 11 | { 12 | char *op; 13 | int (*f)(int a, int b); 14 | } op_t; 15 | 16 | int op_add(int a, int b); 17 | int op_sub(int a, int b); 18 | int op_mul(int a, int b); 19 | int op_div(int a, int b); 20 | int op_mod(int a, int b); 21 | int (*get_op_func(char *s))(int, int); 22 | #endif 23 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * get_op_func - ... 7 | * @s: ... 8 | * Return: ... 9 | */ 10 | 11 | int (*get_op_func(char *s))(int, int) 12 | { 13 | op_t ops[] = { 14 | { "+", op_add }, 15 | { "-", op_sub }, 16 | { "*", op_mul }, 17 | { "/", op_div }, 18 | { "%", op_mod }, 19 | { NULL, NULL } 20 | }; 21 | int x = 0; 22 | 23 | while (x < 5) 24 | { 25 | if (strcmp(s, ops[x].op) == 0) 26 | return (ops[x].f); 27 | x++; 28 | } 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - ... 7 | * @argc: ... 8 | * @argv: ... 9 | * Return: ... 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int (*oprt)(int, int); 15 | 16 | if (argc != 4) 17 | { 18 | printf("Error\n"); 19 | exit(98); 20 | } 21 | 22 | oprt = get_op_func(argv[2]); 23 | 24 | if (!oprt) 25 | { 26 | printf("Error\n"); 27 | exit(99); 28 | } 29 | 30 | printf("%d\n", oprt(atoi(argv[1]), atoi(argv[3]))); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * op_add - ... 7 | * @a: ... 8 | * @b: ... 9 | * Return: ... 10 | */ 11 | 12 | int op_add(int a, int b) 13 | { 14 | return (a + b); 15 | } 16 | 17 | /** 18 | * op_sub - ... 19 | * @a: ... 20 | * @b: ... 21 | * Return: ... 22 | */ 23 | 24 | int op_sub(int a, int b) 25 | { 26 | return (a - b); 27 | } 28 | 29 | /** 30 | * op_mul - ... 31 | * @a: ... 32 | * @b: ... 33 | * Return: ... 34 | */ 35 | 36 | int op_mul(int a, int b) 37 | { 38 | return (a * b); 39 | } 40 | 41 | /** 42 | * op_div - ... 43 | * @a: ... 44 | * @b: ... 45 | * Return: ... 46 | */ 47 | 48 | int op_div(int a, int b) 49 | { 50 | if (b == 0) 51 | { 52 | printf("Error\n"); 53 | exit(100); 54 | } 55 | 56 | return (a / b); 57 | } 58 | 59 | /** 60 | * op_mod - ... 61 | * @a: ... 62 | * @b: ... 63 | * Return: ... 64 | */ 65 | 66 | int op_mod(int a, int b) 67 | { 68 | if (b == 0) 69 | { 70 | printf("Error\n"); 71 | exit(100); 72 | } 73 | 74 | return (a % b); 75 | } 76 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | 0x0F-function_pointers -------------------------------------------------------------------------------- /0x0F-function_pointers/calc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x0F-function_pointers/calc -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_H 2 | #define _FUNCTION_POINTERS_H 3 | #include 4 | void print_name(char *name, void (*f)(char *)); 5 | void array_iterator(int *array, size_t size, void (*action)(int)); 6 | int int_index(int *array, int size, int (*cmp)(int)); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * sum_them_all - sum of all the parameters 6 | * @n: n args 7 | * Return: sum or if n == 0, return 0 8 | */ 9 | 10 | int sum_them_all(const unsigned int n, ...) 11 | { 12 | int sum = 0; 13 | unsigned int x; 14 | va_list arguments; 15 | 16 | if (n) 17 | { 18 | va_start(arguments, n); 19 | for (x = 0; x < n; x++) 20 | { 21 | sum += va_arg(arguments, int); 22 | } 23 | va_end(arguments); 24 | } 25 | return (sum); 26 | } 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_numbers - prints numbers 7 | * @separator: string 8 | * @n: number of elements 9 | * Return: void 10 | */ 11 | 12 | void print_numbers(const char *separator, const unsigned int n, ...) 13 | { 14 | va_list ap; 15 | unsigned int x; 16 | 17 | va_start(ap, n); 18 | 19 | for (x = 0; x < n; x++) 20 | { 21 | printf("%d", va_arg(ap, int)); 22 | 23 | if (separator != NULL && x != (n - 1)) 24 | { 25 | printf("%s", separator); 26 | } 27 | } 28 | putchar('\n'); 29 | va_end(ap); 30 | } 31 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - Print numbers using a separator 5 | * @separator: The string separator 6 | * @n: Quantity of numbers 7 | * @...: List of numbers 8 | * Return: Nothing 9 | */ 10 | 11 | void print_strings(const char *separator, const unsigned int n, ...) 12 | { 13 | va_list valist; 14 | unsigned int x; 15 | char *p; 16 | 17 | va_start(valist, n); 18 | 19 | for (x = 0; x < n; x++) 20 | { 21 | if (separator != NULL && x != 0) 22 | printf("%s", separator); 23 | p = va_arg(valist, char *); 24 | printf("%s", (p == NULL) ? "(nil)" : p); 25 | } 26 | printf("\n"); 27 | va_end(valist); 28 | } 29 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_all("ceis", 'B', 3, "stSchool"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "variadic_functions.h" 4 | 5 | /** 6 | * print_all - prints all 7 | * @format: format 8 | */ 9 | 10 | void print_all(const char * const format, ...) 11 | { 12 | va_list list; 13 | unsigned int x = 0, start = 0; 14 | char *p; 15 | 16 | va_start(list, format); 17 | while (format && format[x] != '\0') 18 | { 19 | switch (format[x]) 20 | { case 'c': 21 | switch (start) 22 | { case 1: printf(", "); } 23 | start = 1; 24 | printf("%c", va_arg(list, int)); 25 | break; 26 | case 'i': 27 | switch (start) 28 | { case 1: printf(", "); } 29 | start = 1; 30 | printf("%i", va_arg(list, int)); 31 | break; 32 | case 'f': 33 | switch (start) 34 | { case 1: printf(", "); } 35 | start = 1; 36 | printf("%f", va_arg(list, double)); 37 | break; 38 | case's': 39 | switch (start) 40 | { case 1: printf(", "); } 41 | start = 1; 42 | p = va_arg(list, char*); 43 | if (p) 44 | { printf("%s", p); 45 | break; } 46 | printf("%p", p); 47 | break; } 48 | x++; 49 | } 50 | printf("\n"); 51 | va_end(list); 52 | } 53 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | 0x10-variadic_functions -------------------------------------------------------------------------------- /0x10-variadic_functions/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int sum; 11 | 12 | sum = sum_them_all(2, 98, 1024); 13 | printf("%d\n", sum); 14 | sum = sum_them_all(4, 98, 1024, 402, -1024); 15 | printf("%d\n", sum); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _VARIADIC_FUNCTIONS_H_ 2 | #define _VARIADIC_FUNCTIONS_H_ 3 | 4 | #include 5 | #include 6 | 7 | int sum_them_all(const unsigned int n, ...); 8 | void print_numbers(const char *separator, const unsigned int n, ...); 9 | void print_strings(const char *separator, const unsigned int n, ...); 10 | void print_all(const char * const format, ...); 11 | 12 | /** 13 | * struct p - checker struct 14 | * @p: possibility 15 | * @f: Function to be used 16 | */ 17 | 18 | typedef struct p 19 | { 20 | char *p; 21 | void (*f)(va_list); 22 | } checker; 23 | 24 | void print_c(va_list); 25 | void print_i(va_list); 26 | void print_f(va_list); 27 | void print_s(va_list); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * Return: Always 0. 9 | */ 10 | 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = print_list(head); 30 | printf("-> %lu elements\n", n); 31 | printf("\n"); 32 | free(new->str); 33 | new->str = NULL; 34 | n = print_list(head); 35 | printf("-> %lu elements\n", n); 36 | free(new); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * print_list - prints elements of the linked list 6 | * @h: pointer to the list_t list to print 7 | * Return: the number of nodes printed 8 | */ 9 | 10 | size_t print_list(const list_t *h) 11 | { 12 | size_t n = 0; 13 | 14 | while (h) 15 | { 16 | if (!h->str) 17 | printf("[0] (nil)\n"); 18 | else 19 | printf("[%u] %s\n", h->len, h->str); 20 | h = h->next; 21 | n++; 22 | } 23 | 24 | return (n); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - Calculates the number of elements. 5 | * @h: Pointer to a list. 6 | * Return: Integer. 7 | **/ 8 | 9 | size_t list_len(const list_t *h) 10 | { 11 | const list_t *temp; 12 | unsigned int counter = 0; 13 | 14 | temp = h; 15 | while (temp) 16 | { 17 | counter++; 18 | temp = temp->next; 19 | } 20 | return (counter); 21 | } 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * Return: Always 0. 9 | */ 10 | 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = list_len(head); 30 | printf("-> %lu elements\n", n); 31 | free(new->str); 32 | free(new); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void first(void) __attribute__ ((constructor)); 4 | 5 | /** 6 | * first - prints a sentence 7 | * function is executed 8 | */ 9 | 10 | void first(void) 11 | { 12 | printf("You're beat! and yet, you must allow,\n"); 13 | printf("I bore my house upon my back!\n"); 14 | } 15 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.)\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | global main 2 | extern printf 3 | main: 4 | mov edi, format 5 | xor eax, eax 6 | call printf 7 | mov eax, 0 8 | ret 9 | format: db `Hello, Holberton\n`,0 10 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * add_node - adds a new node 7 | * @head: double pointer to list_t 8 | * @str: new string to add 9 | * Return: the address of the new element, or NULL if it fails 10 | */ 11 | list_t *add_node(list_t **head, const char *str) 12 | { 13 | list_t *new; 14 | unsigned int len = 0; 15 | 16 | while (str[len]) 17 | len++; 18 | 19 | new = malloc(sizeof(list_t)); 20 | if (!new) 21 | return (NULL); 22 | 23 | new->str = strdup(str); 24 | new->len = len; 25 | new->next = (*head); 26 | (*head) = new; 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node(&head, "Alexandro"); 17 | add_node(&head, "Asaia"); 18 | add_node(&head, "Augustin"); 19 | add_node(&head, "Bennett"); 20 | add_node(&head, "Bilal"); 21 | add_node(&head, "Chandler"); 22 | add_node(&head, "Damian"); 23 | add_node(&head, "Daniel"); 24 | add_node(&head, "Dora"); 25 | add_node(&head, "Electra"); 26 | add_node(&head, "Gloria"); 27 | add_node(&head, "Joe"); 28 | add_node(&head, "John"); 29 | add_node(&head, "John"); 30 | add_node(&head, "Josquin"); 31 | add_node(&head, "Kris"); 32 | add_node(&head, "Marine"); 33 | add_node(&head, "Mason"); 34 | add_node(&head, "Praylin"); 35 | add_node(&head, "Rick"); 36 | add_node(&head, "Rick"); 37 | add_node(&head, "Rona"); 38 | add_node(&head, "Siphan"); 39 | add_node(&head, "Sravanthi"); 40 | add_node(&head, "Steven"); 41 | add_node(&head, "Tasneem"); 42 | add_node(&head, "William"); 43 | add_node(&head, "Zee"); 44 | print_list(head); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - Adds new node at the end of the list 5 | * @head: Address of the first node 6 | * @str: Address of the string 7 | * Return: Address of a new node 8 | **/ 9 | 10 | list_t *add_node_end(list_t **head, const char *str) 11 | { 12 | list_t *temp, *temp2; 13 | unsigned int length = 0; 14 | 15 | if (str == NULL) 16 | return (NULL); 17 | 18 | temp = malloc(sizeof(list_t)); 19 | if (temp == NULL) 20 | return (NULL); 21 | 22 | temp->str = strdup(str); 23 | if (temp->str == NULL) 24 | { 25 | free(temp); 26 | return (NULL); 27 | } 28 | while (str[length]) 29 | length++; 30 | temp->len = length; 31 | temp->next = NULL; 32 | 33 | if (*head == NULL) 34 | { 35 | *head = temp; 36 | return (temp); 37 | } 38 | 39 | temp2 = *head; 40 | while (temp2->next) 41 | temp2 = temp2->next; 42 | temp2->next = temp; 43 | return (temp); 44 | } 45 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Anne"); 17 | add_node_end(&head, "Colton"); 18 | add_node_end(&head, "Corbin"); 19 | add_node_end(&head, "Daniel"); 20 | add_node_end(&head, "Danton"); 21 | add_node_end(&head, "David"); 22 | add_node_end(&head, "Gary"); 23 | add_node_end(&head, "Holden"); 24 | add_node_end(&head, "Ian"); 25 | add_node_end(&head, "Ian"); 26 | add_node_end(&head, "Jay"); 27 | add_node_end(&head, "Jennie"); 28 | add_node_end(&head, "Jimmy"); 29 | add_node_end(&head, "Justin"); 30 | add_node_end(&head, "Kalson"); 31 | add_node_end(&head, "Kina"); 32 | add_node_end(&head, "Matthew"); 33 | add_node_end(&head, "Max"); 34 | add_node_end(&head, "Michael"); 35 | add_node_end(&head, "Ntuj"); 36 | add_node_end(&head, "Philip"); 37 | add_node_end(&head, "Richard"); 38 | add_node_end(&head, "Samantha"); 39 | add_node_end(&head, "Stuart"); 40 | add_node_end(&head, "Swati"); 41 | add_node_end(&head, "Timothy"); 42 | add_node_end(&head, "Victor"); 43 | add_node_end(&head, "Walton"); 44 | print_list(head); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * free_list - this frees the linked list 6 | * @head: list_t list there to be free 7 | */ 8 | void free_list(list_t *head) 9 | { 10 | list_t *tmp; 11 | 12 | while (head) 13 | { 14 | tmp = head->next; 15 | free(head->str); 16 | free(head); 17 | head = tmp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Bob"); 17 | add_node_end(&head, "&"); 18 | add_node_end(&head, "Kris"); 19 | add_node_end(&head, "love"); 20 | add_node_end(&head, "asm"); 21 | print_list(head); 22 | free_list(head); 23 | head = NULL; 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x12-singly_linked_lists -------------------------------------------------------------------------------- /0x12-singly_linked_lists/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x12-singly_linked_lists/a -------------------------------------------------------------------------------- /0x12-singly_linked_lists/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x12-singly_linked_lists/b -------------------------------------------------------------------------------- /0x12-singly_linked_lists/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x12-singly_linked_lists/c -------------------------------------------------------------------------------- /0x12-singly_linked_lists/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x12-singly_linked_lists/d -------------------------------------------------------------------------------- /0x12-singly_linked_lists/first: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x12-singly_linked_lists/first -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | #include 4 | #include 5 | /** 6 | * struct list_s - singly linked list 7 | * @str: string - (malloc'ed string) 8 | * @len: length of the string 9 | * @next: points to the next node 10 | * 11 | * Description: singly linked list node structure 12 | * for Holberton project 13 | */ 14 | typedef struct list_s 15 | { 16 | char *str; 17 | unsigned int len; 18 | struct list_s *next; 19 | } list_t; 20 | 21 | size_t print_list(const list_t *h); 22 | size_t list_len(const list_t *h); 23 | list_t *add_node(list_t **head, const char *str); 24 | list_t *add_node_end(list_t **head, const char *str); 25 | void free_list(list_t *head); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = print_listint(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_listint - Prints elements of the linked list 6 | * @h: Pointer to a list. 7 | * Return: Integer. 8 | **/ 9 | 10 | size_t print_listint(const listint_t *h) 11 | { 12 | const listint_t *pt; 13 | unsigned int ctr = 0; 14 | 15 | pt = h; 16 | while (pt) 17 | { 18 | printf("%d\n", pt->n); 19 | ctr++; 20 | pt = pt->next; 21 | } 22 | return (ctr); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - Calculates the number of elements 5 | * @h: Pointer 6 | * Return: Integer 7 | **/ 8 | 9 | size_t listint_len(const listint_t *h) 10 | { 11 | const listint_t *pt; 12 | unsigned int ctr = 0; 13 | 14 | pt = h; 15 | while (pt) 16 | { 17 | ctr++; 18 | pt = pt->next; 19 | } 20 | return (ctr); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = listint_len(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * delete_nodeint_at_index - Delete a node 6 | * @head: Address of first node 7 | * @index: Position of the node to delete 8 | * Return: 1 if success 9 | **/ 10 | 11 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 12 | { 13 | unsigned int x; 14 | listint_t *curnt, *next; 15 | 16 | if (head == NULL || *head == NULL) 17 | return (-1); 18 | if (index == 0) 19 | { 20 | next = (*head)->next; 21 | free(*head); 22 | *head = next; 23 | return (1); 24 | } 25 | curnt = *head; 26 | for (x = 0; x < index - 1; x++) 27 | { 28 | if (curnt->next == NULL) 29 | return (-1); 30 | curnt = curnt->next; 31 | } 32 | next = curnt->next; 33 | curnt->next = next->next; 34 | free(next); 35 | return (1); 36 | } 37 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | printf("-----------------\n"); 26 | delete_nodeint_at_index(&head, 5); 27 | print_listint(head); 28 | printf("-----------------\n"); 29 | delete_nodeint_at_index(&head, 0); 30 | print_listint(head); 31 | printf("-----------------\n"); 32 | delete_nodeint_at_index(&head, 0); 33 | print_listint(head); 34 | printf("-----------------\n"); 35 | delete_nodeint_at_index(&head, 0); 36 | print_listint(head); 37 | printf("-----------------\n"); 38 | delete_nodeint_at_index(&head, 0); 39 | print_listint(head); 40 | printf("-----------------\n"); 41 | delete_nodeint_at_index(&head, 0); 42 | print_listint(head); 43 | printf("-----------------\n"); 44 | delete_nodeint_at_index(&head, 0); 45 | print_listint(head); 46 | printf("-----------------\n"); 47 | delete_nodeint_at_index(&head, 0); 48 | printf("-----------------\n"); 49 | delete_nodeint_at_index(&head, 0); 50 | printf("-----------------\n"); 51 | delete_nodeint_at_index(&head, 0); 52 | printf("-----------------\n"); 53 | delete_nodeint_at_index(&head, 0); 54 | printf("-----------------\n"); 55 | delete_nodeint_at_index(&head, 0); 56 | printf("-----------------\n"); 57 | delete_nodeint_at_index(&head, 0); 58 | printf("-----------------\n"); 59 | delete_nodeint_at_index(&head, 0); 60 | printf("-----------------\n"); 61 | delete_nodeint_at_index(&head, 0); 62 | printf("-----------------\n"); 63 | delete_nodeint_at_index(&head, 0); 64 | printf("-----------------\n"); 65 | delete_nodeint_at_index(&head, 0); 66 | print_listint(head); 67 | return (0); 68 | } 69 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | reverse_listint(&head); 26 | print_listint(head); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list 5 | * @head: A pointer to the first node 6 | * Return: A pointer to the first node in the new list 7 | */ 8 | listint_t *reverse_listint(listint_t **head) 9 | { 10 | listint_t *prev = NULL; 11 | listint_t *next = NULL; 12 | 13 | while (*head) 14 | { 15 | next = (*head)->next; 16 | (*head)->next = prev; 17 | prev = *head; 18 | *head = next; 19 | } 20 | 21 | *head = prev; 22 | 23 | return (*head); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *head2; 15 | listint_t *node; 16 | 17 | head2 = NULL; 18 | add_nodeint(&head2, 0); 19 | add_nodeint(&head2, 1); 20 | add_nodeint(&head2, 2); 21 | add_nodeint(&head2, 3); 22 | add_nodeint(&head2, 4); 23 | add_nodeint(&head2, 98); 24 | add_nodeint(&head2, 402); 25 | add_nodeint(&head2, 1024); 26 | print_listint_safe(head2); 27 | head = NULL; 28 | node = add_nodeint(&head, 0); 29 | add_nodeint(&head, 1); 30 | add_nodeint(&head, 2); 31 | add_nodeint(&head, 3); 32 | add_nodeint(&head, 4); 33 | node->next = add_nodeint(&head, 98); 34 | add_nodeint(&head, 402); 35 | add_nodeint(&head, 1024); 36 | print_listint_safe(head); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint_safe - safely prints a linked list 5 | * @head: list of listint_t to print 6 | * Return: number of nodes 7 | */ 8 | size_t print_listint_safe(const listint_t *head) 9 | { 10 | size_t num = 0; 11 | long int dif; 12 | 13 | while (head) 14 | { 15 | dif = head - head->next; 16 | num++; 17 | printf("[%p] %d\n", (void *)head, head->n); 18 | if (dif > 0) 19 | head = head->next; 20 | else 21 | { 22 | printf("-> [%p] %d\n", (void *)head->next, head->next->n); 23 | break; 24 | } 25 | } 26 | 27 | return (num); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * free_listint_safe - frees a linked list 5 | * @h: pointer to the first node in the linked list 6 | * Return: number of elements in the freed list 7 | */ 8 | size_t free_listint_safe(listint_t **h) 9 | { 10 | size_t len = 0; 11 | int dif; 12 | listint_t *tmp; 13 | 14 | if (!h || !*h) 15 | return (0); 16 | 17 | while (*h) 18 | { 19 | dif = *h - (*h)->next; 20 | if (dif > 0) 21 | { 22 | tmp = (*h)->next; 23 | *h = tmp; 24 | len++; 25 | } 26 | else 27 | { 28 | *h = NULL; 29 | len++; 30 | break; 31 | } 32 | } 33 | 34 | *h = NULL; 35 | 36 | return (len); 37 | } 38 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *head2; 15 | listint_t *node; 16 | 17 | head2 = NULL; 18 | add_nodeint(&head2, 0); 19 | add_nodeint(&head2, 1); 20 | add_nodeint(&head2, 2); 21 | add_nodeint(&head2, 3); 22 | add_nodeint(&head2, 4); 23 | add_nodeint(&head2, 98); 24 | add_nodeint(&head2, 402); 25 | add_nodeint(&head2, 1024); 26 | print_listint_safe(head2); 27 | head = NULL; 28 | node = add_nodeint(&head, 0); 29 | add_nodeint(&head, 1); 30 | add_nodeint(&head, 2); 31 | add_nodeint(&head, 3); 32 | add_nodeint(&head, 4); 33 | node->next = add_nodeint(&head, 98); 34 | add_nodeint(&head, 402); 35 | add_nodeint(&head, 1024); 36 | print_listint_safe(head); 37 | free_listint_safe(&head2); 38 | free_listint_safe(&head); 39 | printf("%p, %p\n", (void *)head2, (void *)head); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - finds the loop 5 | * @head: linked list to be searched 6 | * Return: address of the node where the loop starts 7 | */ 8 | listint_t *find_listint_loop(listint_t *head) 9 | { 10 | listint_t *slow = head; 11 | listint_t *fast = head; 12 | 13 | if (!head) 14 | return (NULL); 15 | 16 | while (slow && fast && fast->next) 17 | { 18 | fast = fast->next->next; 19 | slow = slow->next; 20 | if (fast == slow) 21 | { 22 | slow = head; 23 | while (slow != fast) 24 | { 25 | slow = slow->next; 26 | fast = fast->next; 27 | } 28 | return (fast); 29 | } 30 | } 31 | 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *head2; 15 | listint_t *node; 16 | 17 | head2 = NULL; 18 | add_nodeint(&head2, 0); 19 | add_nodeint(&head2, 1); 20 | add_nodeint(&head2, 2); 21 | add_nodeint(&head2, 3); 22 | add_nodeint(&head2, 4); 23 | add_nodeint(&head2, 98); 24 | add_nodeint(&head2, 402); 25 | add_nodeint(&head2, 1024); 26 | print_listint_safe(head2); 27 | node = find_listint_loop(head2); 28 | if (node != NULL) 29 | { 30 | printf("Loop starts at [%p] %d\n", (void *)node, node->n); 31 | } 32 | free_listint_safe(&head2); 33 | head = NULL; 34 | node = add_nodeint(&head, 0); 35 | add_nodeint(&head, 1); 36 | add_nodeint(&head, 2); 37 | add_nodeint(&head, 3); 38 | add_nodeint(&head, 4); 39 | add_nodeint(&head, 5); 40 | add_nodeint(&head, 6); 41 | node->next = add_nodeint(&head, 7); 42 | add_nodeint(&head, 98); 43 | add_nodeint(&head, 402); 44 | add_nodeint(&head, 1024); 45 | print_listint_safe(head); 46 | node = find_listint_loop(head); 47 | if (node != NULL) 48 | { 49 | printf("Loop starts at [%p] %d\n", (void *)node, node->n); 50 | } 51 | free_listint_safe(&head); 52 | return (0); 53 | } 54 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_nodeint - Adds a new node at the beginning 6 | * @head: Address of the first node 7 | * @n: Integer to insert 8 | * Return: Address of a new node. 9 | **/ 10 | 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *pt; 14 | 15 | pt = malloc(sizeof(listint_t)); 16 | if (pt == NULL) 17 | return (NULL); 18 | 19 | pt->n = n; 20 | pt->next = *head; 21 | *head = pt; 22 | return (*head); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint(&head, 0); 17 | add_nodeint(&head, 1); 18 | add_nodeint(&head, 2); 19 | add_nodeint(&head, 3); 20 | add_nodeint(&head, 4); 21 | add_nodeint(&head, 98); 22 | add_nodeint(&head, 402); 23 | add_nodeint(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_nodeint_end - Add a new node at the end 6 | * @head: Address of the first node 7 | * @n: Integer to insert into the new node 8 | * Return: Address of the new node. 9 | **/ 10 | 11 | listint_t *add_nodeint_end(listint_t **head, const int n) 12 | { 13 | listint_t *tmp, *tmp2; 14 | 15 | tmp = malloc(sizeof(listint_t)); 16 | if (tmp == NULL) 17 | return (NULL); 18 | 19 | tmp->n = n; 20 | tmp->next = NULL; 21 | 22 | if (*head == NULL) 23 | { 24 | *head = tmp; 25 | return (tmp); 26 | } 27 | 28 | tmp2 = *head; 29 | while (tmp2->next) 30 | tmp2 = tmp2->next; 31 | tmp2->next = tmp; 32 | return (tmp); 33 | } 34 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_listint - Frees a list. 6 | * @head: Address of the first node 7 | **/ 8 | 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *pt, *pt2; 12 | 13 | pt = head; 14 | while (pt != NULL) 15 | { 16 | pt2 = pt->next; 17 | free(pt); 18 | pt = pt2; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint(head); 26 | head = NULL; 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_listint2 - Frees a list. 6 | * @head: Address of the first node of a list. 7 | **/ 8 | 9 | void free_listint2(listint_t **head) 10 | { 11 | listint_t *tmp; 12 | 13 | if (head == NULL) 14 | return; 15 | while (*head != NULL) 16 | { 17 | tmp = (*head)->next; 18 | free(*head); 19 | *head = tmp; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint2(&head); 26 | printf("%p\n", (void *)head); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | int n; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | print_listint(head); 26 | n = pop_listint(&head); 27 | printf("- %d\n", n); 28 | print_listint(head); 29 | n = pop_listint(&head); 30 | printf("- %d\n", n); 31 | print_listint(head); 32 | free_listint2(&head); 33 | printf("%p\n", (void *)head); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * pop_listint - Delete the first element of a linked list 6 | * @head: Pointer to a list 7 | * Return: Integer in success 8 | **/ 9 | 10 | int pop_listint(listint_t **head) 11 | { 12 | listint_t *pt; 13 | int my_data; 14 | 15 | if (*head == NULL) 16 | return (0); 17 | 18 | pt = *head; 19 | *head = pt->next; 20 | my_data = pt->n; 21 | free(pt); 22 | return (my_data); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * get_nodeint_at_index - Find a node in the list 6 | * @head: Address of the first node 7 | * @index: Position of a the node to find 8 | * Return: Node address. 9 | **/ 10 | 11 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 12 | { 13 | unsigned int x = 0; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | for (x = 0; x < index; x++) 18 | { 19 | head = head->next; 20 | if (head == NULL) 21 | return (NULL); 22 | } 23 | return (head); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *node; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | print_listint(head); 26 | node = get_nodeint_at_index(head, 5); 27 | printf("%d\n", node->n); 28 | print_listint(head); 29 | free_listint2(&head); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | sum = sum_listint(head); 26 | printf("sum = %d\n", sum); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * sum_listint - Sum of all the data in the list 6 | * @head: Address of the first node 7 | * Return: Integer 8 | **/ 9 | 10 | int sum_listint(listint_t *head) 11 | { 12 | int sum = 0; 13 | 14 | while (head != NULL) 15 | { 16 | sum += head->n; 17 | head = head->next; 18 | } 19 | return (sum); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * insert_nodeint_at_index - Insert a new node 6 | * @head: First node address 7 | * @idx: Position of the new node 8 | * @n: Data of the new node 9 | * Return: Address of the new node 10 | **/ 11 | 12 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 13 | { 14 | listint_t *new_node, *tmp; 15 | unsigned int x = 0; 16 | 17 | if (*head == NULL && idx != 0) 18 | return (NULL); 19 | if (idx != 0) 20 | { 21 | tmp = *head; 22 | for (; x < idx - 1 && tmp != NULL; x++) 23 | tmp = tmp->next; 24 | if (tmp == NULL) 25 | return (NULL); 26 | } 27 | new_node = malloc(sizeof(listint_t)); 28 | if (new_node == NULL) 29 | return (NULL); 30 | new_node->n = n; 31 | if (idx == 0) 32 | { 33 | new_node->next = *head; 34 | *head = new_node; 35 | return (new_node); 36 | } 37 | new_node->next = tmp->next; 38 | tmp->next = new_node; 39 | return (new_node); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | printf("-----------------\n"); 26 | insert_nodeint_at_index(&head, 5, 4096); 27 | print_listint(head); 28 | free_listint2(&head); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x13-more_singly_linked_lists -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/a -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/b -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/c -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/d -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/e -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/f -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/g: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/g -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/h -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/i: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/i -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/j: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/j -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/k: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/k -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/l: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/l -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | #include 4 | #include 5 | /** 6 | * struct listint_s - singly linked list 7 | * @n: integer 8 | * @next: points to next node 9 | * Description: singly linked list node structure 10 | */ 11 | typedef struct listint_s 12 | { 13 | int n; 14 | struct listint_s *next; 15 | } listint_t; 16 | 17 | size_t print_listint(const listint_t *h); 18 | size_t listint_len(const listint_t *h); 19 | listint_t *add_nodeint(listint_t **head, const int n); 20 | listint_t *add_nodeint_end(listint_t **head, const int n); 21 | void free_listint(listint_t *head); 22 | void free_listint2(listint_t **head); 23 | int pop_listint(listint_t **head); 24 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 25 | int sum_listint(listint_t *head); 26 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 27 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 28 | listint_t *reverse_listint(listint_t **head); 29 | size_t print_listint_safe(const listint_t *head); 30 | size_t free_listint_safe(listint_t **h); 31 | listint_t *find_listint_loop(listint_t *head); 32 | #endif 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/m -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/n: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/n -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x13-more_singly_linked_lists/o -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to unsigned int 5 | * @b: string containing the binary number 6 | * Return: the converted number 7 | */ 8 | unsigned int binary_to_uint(const char *b) 9 | { 10 | int i; 11 | unsigned int dec_val = 0; 12 | 13 | if (!b) 14 | return (0); 15 | 16 | for (i = 0; b[i]; i++) 17 | { 18 | if (b[i] < '0' || b[i] > '1') 19 | return (0); 20 | dec_val = 2 * dec_val + (b[i] - '0'); 21 | } 22 | return (dec_val); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary representation 5 | * @n: paramete 6 | */ 7 | void print_binary(unsigned long int n) 8 | { 9 | int i, count = 0; 10 | unsigned long int current; 11 | 12 | for (i = 63; i >= 0; i--) 13 | { 14 | current = n >> i; 15 | 16 | if (current & 1) 17 | { 18 | _putchar('1'); 19 | count++; 20 | } 21 | else if (count) 22 | _putchar('0'); 23 | } 24 | if (!count) 25 | _putchar('0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * Return: 0 if big endian, 1 if little endian 6 | */ 7 | int get_endianness(void) 8 | { 9 | unsigned int i = 1; 10 | char *c = (char *) &i; 11 | 12 | return (*c); 13 | } 14 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - value of a bit at a given index 5 | * @n: decimal parameter 6 | * @index: index 7 | * Return: val 8 | */ 9 | int get_bit(unsigned long int n, unsigned int index) 10 | { 11 | int bit; 12 | 13 | bit = (n >> index); 14 | if (index > 32) 15 | return (-1); 16 | return (bit & 1); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * set_bit - sets the value of a bit to 1 at a given index 6 | * @n: parameter 7 | * @index: index 8 | * Return: value 9 | */ 10 | int set_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (index > sizeof(n) * 8) 13 | return (-1); 14 | *n |= (1 << index); 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * clear_bit - sets the value of a bit to 0 at a given index 6 | * @n: parameter 7 | * @index: index 8 | * Return: 1 if works, -1 if error 9 | */ 10 | int clear_bit(unsigned long int *n, unsigned int index) 11 | { 12 | if (index > sizeof(n) * 8) 13 | return (-1); 14 | *n &= ~(1 << index); 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - number of bits you would need to flip 5 | * to get from one number to another 6 | * @n: parameter 7 | * @m: destiny 8 | * Return: nflips 9 | */ 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 11 | { 12 | unsigned int i, nflips = 0; 13 | unsigned long int j = sizeof(unsigned long int) * 8; 14 | 15 | for (i = 0; i < j; i++) 16 | { 17 | if ((m & 1) != (n & 1)) 18 | nflips += 1; 19 | n = n >> 1; 20 | m = m >> 1; 21 | } 22 | return (nflips); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # 0x14. C - Bit Manipulation 2 | 3 | ## Description 4 | 5 | This project focuses on bit manipulation operations in the C programming language. It covers concepts such as bitwise operators, bit shifting, and manipulating individual bits in variables. 6 | 7 | ## Requirements 8 | 9 | - Allowed editors: vi, vim, emacs 10 | - All files will be compiled on Ubuntu 20.04 LTS using gcc with the following options: -Wall -Werror -Wextra -pedantic -std=gnu89 11 | - All files should end with a new line 12 | - A README.md file, at the root of the project folder, is mandatory 13 | - The code should follow the Betty style (checked using betty-style.pl and betty-doc.pl) 14 | - Global variables are not allowed 15 | - Each file should contain no more than 5 functions 16 | - Only the following C standard library functions are allowed: malloc, free, and exit. Functions like printf, puts, calloc, realloc, etc. are forbidden 17 | - Allowed syscalls: read, write, open, close 18 | - You are allowed to use the provided _putchar function 19 | - _putchar.c should not be pushed to your repository 20 | - Main.c files shown in examples are provided for testing purposes but should not be pushed to your repository 21 | - Prototypes of all functions and the _putchar function should be included in the main.h header file 22 | - All header files should have include guards 23 | 24 | ## Usage 25 | 26 | Compile the C files using the provided gcc options. For example: 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/crackme3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x14-bit_manipulation/crackme3 -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /*prototypes*/ 5 | int _putchar(char c); 6 | unsigned int binary_to_uint(const char *b); 7 | void print_binary(unsigned long int n); 8 | int get_bit(unsigned long int n, unsigned int index); 9 | int set_bit(unsigned long int *n, unsigned int index); 10 | int clear_bit(unsigned long int *n, unsigned int index); 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 12 | int get_endianness(void); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x15-file_io/.0-read_textfile.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x15-file_io/.0-read_textfile.c.swp -------------------------------------------------------------------------------- /0x15-file_io/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | ssize_t n; 13 | 14 | if (ac != 2) 15 | { 16 | dprintf(2, "Usage: %s filename\n", av[0]); 17 | exit(1); 18 | } 19 | n = read_textfile(av[1], 114); 20 | printf("\n(printed chars: %li)\n", n); 21 | n = read_textfile(av[1], 1024); 22 | printf("\n(printed chars: %li)\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * read_textfile - reads a text file and prints it to the standard output 4 | * @filename: name of the file to be read 5 | * @letters: number of letters to read and print 6 | * Return: the number of letters printed, or 0 if it failed 7 | */ 8 | ssize_t read_textfile(const char *filename, size_t letters) 9 | { 10 | ssize_t fd, r, w; 11 | char *buffer; 12 | 13 | if (!filename) 14 | return (0); 15 | 16 | buffer = malloc(letters * sizeof(char)); 17 | if (!buffer) 18 | return (0); 19 | 20 | fd = open(filename, O_RDONLY); 21 | r = read(fd, buffer, letters); 22 | w = write(STDOUT_FILENO, buffer, r); 23 | 24 | if (fd == -1 || r == -1 || w == -1 || w != r) 25 | { 26 | free(buffer); 27 | return (0); 28 | } 29 | 30 | free(buffer); 31 | close(fd); 32 | 33 | return (w); 34 | } 35 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file and fills it with text 5 | * @filename: name of the file to create 6 | * @text_content: text to write in the file 7 | * 8 | * Return: 1 on success, -1 on failure 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd, x, y = 0; 13 | 14 | if (!filename) 15 | return (-1); 16 | 17 | fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600); 18 | if (fd < 0) 19 | return (-1); 20 | 21 | if (text_content) 22 | { 23 | while (text_content[y]) 24 | y++; 25 | x = write(fd, text_content, y); 26 | if (x != y) 27 | return (-1); 28 | } 29 | 30 | close(fd); 31 | 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /0x15-file_io/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = create_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text at the end of a file 5 | * @filename: file to append the text to 6 | * @text_content: content to append into the file 7 | * 8 | * Return: 1 on success and -1 on failure 9 | */ 10 | int append_text_to_file(const char *filename, char *text_content) 11 | { 12 | int fd, a, b = 0; 13 | 14 | if (!filename) 15 | return (-1); 16 | 17 | fd = open(filename, O_WRONLY | O_APPEND); 18 | if (fd < 0) 19 | return (-1); 20 | 21 | if (text_content) 22 | { 23 | while (text_content[b]) 24 | b++; 25 | a = write(fd, text_content, b); 26 | if (a != b) 27 | return (-1); 28 | } 29 | 30 | close(fd); 31 | 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /0x15-file_io/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = append_text_to_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - copies the content of a file to another file 5 | * @argc: number of arguments passed to the program 6 | * @argv: array of arguments 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int fd_r, fd_w, x, m, n; 13 | char buf[BUFSIZ]; 14 | 15 | if (argc != 3) 16 | { 17 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"); 18 | exit(97); 19 | } 20 | fd_r = open(argv[1], O_RDONLY); 21 | if (fd_r < 0) 22 | { 23 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 24 | exit(98); 25 | } 26 | fd_w = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0664); 27 | while ((x = read(fd_r, buf, BUFSIZ)) > 0) 28 | { 29 | if (fd_w < 0 || write(fd_w, buf, x) != x) 30 | { 31 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]); 32 | close(fd_r); 33 | exit(99); 34 | } 35 | } 36 | if (x < 0) 37 | { 38 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 39 | exit(98); 40 | } 41 | m = close(fd_r); 42 | n = close(fd_w); 43 | if (m < 0 || n < 0) 44 | { 45 | if (m < 0) 46 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd_r); 47 | if (n < 0) 48 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd_w); 49 | exit(100); 50 | } 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /0x15-file_io/Incitatous: -------------------------------------------------------------------------------- 1 | Why you should think twice before putting pictures on social media. 2 | (What you always wanted to know about @Incitatous) 3 | #PrivacyAware 4 | http://imgur.com/a/Mq1tc 5 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | # 0x15. C - File I/O 2 | 3 | ## Description 4 | 5 | This project focuses on file input/output operations in the C programming language. It covers concepts such as reading from and writing to files, handling errors, and using system calls. 6 | 7 | ## Requirements 8 | 9 | - Allowed editors: vi, vim, emacs 10 | - All files will be compiled on Ubuntu 20.04 LTS using gcc with the following options: -Wall -Werror -Wextra -pedantic -std=gnu89 11 | - All files should end with a new line 12 | - A README.md file, at the root of the project folder, is mandatory 13 | - The code should follow the Betty style (checked using betty-style.pl and betty-doc.pl) 14 | - Global variables are not allowed 15 | - Each file should contain no more than 5 functions 16 | - Only the following C standard library functions are allowed: malloc, free, and exit. Functions like printf, puts, calloc, realloc, etc. are forbidden 17 | - Allowed syscalls: read, write, open, close 18 | - You are allowed to use the provided _putchar function 19 | - _putchar.c should not be pushed to your repository 20 | - Main.c files shown in examples are provided for testing purposes but should not be pushed to your repository 21 | - Prototypes of all functions and the _putchar function should be included in the main.h header file 22 | - All header files should have include guards 23 | 24 | ## Usage 25 | 26 | Compile the C files using the provided gcc options. For example: 27 | -------------------------------------------------------------------------------- /0x15-file_io/Requiescat: -------------------------------------------------------------------------------- 1 | Requiescat 2 | by Oscar Wilde 3 | 4 | Tread lightly, she is near 5 | Under the snow, 6 | Speak gently, she can hear 7 | The daisies grow. 8 | 9 | All her bright golden hair 10 | Tarnished with rust, 11 | She that was young and fair 12 | Fallen to dust. 13 | 14 | Lily-like, white as snow, 15 | She hardly knew 16 | She was a woman, so 17 | Sweetly she grew. 18 | 19 | Coffin-board, heavy stone, 20 | Lie on her breast, 21 | I vex my heart alone, 22 | She is at rest. 23 | 24 | Peace, Peace, she cannot hear 25 | Lyre or sonnet, 26 | All my life's buried here, 27 | Heap earth upon it. 28 | -------------------------------------------------------------------------------- /0x15-file_io/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x15-file_io/a -------------------------------------------------------------------------------- /0x15-file_io/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x15-file_io/b -------------------------------------------------------------------------------- /0x15-file_io/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x15-file_io/c -------------------------------------------------------------------------------- /0x15-file_io/cp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x15-file_io/cp -------------------------------------------------------------------------------- /0x15-file_io/elf_header: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x15-file_io/elf_header -------------------------------------------------------------------------------- /0x15-file_io/hello: -------------------------------------------------------------------------------- 1 | Hello -------------------------------------------------------------------------------- /0x15-file_io/incitatous: -------------------------------------------------------------------------------- 1 | Why you should think twice before putting pictures on social media. 2 | (What you always wanted to know about @Incitatous) 3 | #PrivacyAware 4 | http://imgur.com/a/Mq1tc 5 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_N 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | ssize_t read_textfile(const char *filename, size_t letters); 13 | int create_file(const char *filename, char *text_content); 14 | int append_text_to_file(const char *filename, char *text_content); 15 | 16 | /** 17 | * ELF header functions 18 | */ 19 | void check_elf(unsigned char *e_ident); 20 | void print_magic(unsigned char *e_ident); 21 | void print_class(unsigned char *e_ident); 22 | void print_data(unsigned char *e_ident); 23 | void print_version(unsigned char *e_ident); 24 | void print_abi(unsigned char *e_ident); 25 | void print_osabi(unsigned char *e_ident); 26 | void print_type(unsigned int e_type, unsigned char *e_ident); 27 | void print_entry(unsigned long int e_entry, unsigned char *e_ident); 28 | void close_elf(int elf); 29 | #endif 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = new; 30 | n = print_dlistint(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_dlistint - print all the elements of a dlistint_t list 6 | * @h: pointer to the start of the linked list 7 | * 8 | * Return: number of nodes 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | size_t i; 13 | 14 | for (i = 0; h != NULL; i++) 15 | { 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | } 19 | return (i); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - count the number of nodes in the linked list 5 | * @h: pointer to the beginning of a linked list 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t i; 12 | 13 | for (i = 0; h != NULL; i++) 14 | h = h->next; 15 | return (i); 16 | } 17 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = new; 30 | n = dlistint_len(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /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: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - generate a key depending on a username for crackme5 7 | * @argc: number of arguments passed 8 | * @argv: arguments passed to main 9 | * 10 | * Return: 0 on success, 1 on error 11 | */ 12 | int main(int argc, char *argv[]) 13 | { 14 | unsigned int i, b; 15 | size_t len, add; 16 | char *l = "A-CHRDw87lNS0E9B2TibgpnMVys5XzvtOGJcYLU+4mjW6fxqZeF3Qa1rPhdKIouk"; 17 | char p[7] = " "; 18 | 19 | if (argc != 2) 20 | { 21 | printf("Correct usage: ./keygen5 username\n"); 22 | return (1); 23 | } 24 | len = strlen(argv[1]); 25 | p[0] = l[(len ^ 59) & 63]; 26 | for (i = 0, add = 0; i < len; i++) 27 | add += argv[1][i]; 28 | p[1] = l[(add ^ 79) & 63]; 29 | for (i = 0, b = 1; i < len; i++) 30 | b *= argv[1][i]; 31 | p[2] = l[(b ^ 85) & 63]; 32 | for (b = argv[1][0], i = 0; i < len; i++) 33 | if ((char)b <= argv[1][i]) 34 | b = argv[1][i]; 35 | srand(b ^ 14); 36 | p[3] = l[rand() & 63]; 37 | for (b = 0, i = 0; i < len; i++) 38 | b += argv[1][i] * argv[1][i]; 39 | p[4] = l[(b ^ 239) & 63]; 40 | for (b = 0, i = 0; (char)i < argv[1][0]; i++) 41 | b = rand(); 42 | p[5] = l[(b ^ 229) & 63]; 43 | printf("%s\n", p); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - Adds a new node at the beginning 5 | * 6 | * @head: Head 7 | * 8 | * @n: Value 9 | * 10 | * Return: dlistint_t 11 | */ 12 | 13 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 14 | { 15 | 16 | dlistint_t *temp, *node = malloc(sizeof(dlistint_t)); 17 | 18 | 19 | if (node == NULL) 20 | return (NULL); 21 | node->n = n; 22 | node->prev = NULL; 23 | node->next = NULL; 24 | 25 | if (*head == NULL) 26 | { 27 | node->next = NULL; 28 | *head = node; 29 | return (*head); 30 | } 31 | temp = *head; 32 | node->next = temp; 33 | temp->prev = node; 34 | *head = node; 35 | return (node); 36 | } 37 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint(&head, 0); 17 | add_dnodeint(&head, 1); 18 | add_dnodeint(&head, 2); 19 | add_dnodeint(&head, 3); 20 | add_dnodeint(&head, 4); 21 | add_dnodeint(&head, 98); 22 | add_dnodeint(&head, 402); 23 | add_dnodeint(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 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 5 | * 6 | * @head: struct 7 | * 8 | * @n: const int 9 | * 10 | * Return: dlistint_t 11 | */ 12 | 13 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 14 | { 15 | dlistint_t *list = NULL, *end = *head; 16 | 17 | list = malloc(sizeof(dlistint_t)); 18 | 19 | if (list == NULL) 20 | { 21 | return (NULL); 22 | } 23 | list->n = n; 24 | list->prev = NULL; 25 | list->next = NULL; 26 | 27 | if (*head == NULL) 28 | { 29 | *head = list; 30 | return (*head); 31 | } 32 | 33 | while (end->next != NULL) 34 | { 35 | end = end->next; 36 | } 37 | list->prev = end; 38 | end->next = list; 39 | 40 | return (list); 41 | } 42 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - Free list 5 | * 6 | * @head: struct 7 | * 8 | * Return: void 9 | */ 10 | 11 | void free_dlistint(dlistint_t *head) 12 | { 13 | dlistint_t *list; 14 | 15 | while (head) 16 | { 17 | list = head; 18 | head = head->next; 19 | free(list); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | free_dlistint(head); 26 | head = NULL; 27 | return (EXIT_SUCCESS); 28 | } 29 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - Nth node 5 | * 6 | * @head: pointer to head node 7 | * 8 | * @index: int 9 | * 10 | * Return: dlistint_t 11 | */ 12 | 13 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 14 | { 15 | unsigned int count = 0; 16 | 17 | if (head == NULL) 18 | { 19 | return (NULL); 20 | } 21 | 22 | while (head != NULL) 23 | { 24 | if (index == count) 25 | { 26 | return (head); 27 | } 28 | count++; 29 | head = head->next; 30 | } 31 | return (NULL); 32 | } 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *node; 15 | 16 | head = NULL; 17 | add_dnodeint_end(&head, 0); 18 | add_dnodeint_end(&head, 1); 19 | add_dnodeint_end(&head, 2); 20 | add_dnodeint_end(&head, 3); 21 | add_dnodeint_end(&head, 4); 22 | add_dnodeint_end(&head, 98); 23 | add_dnodeint_end(&head, 402); 24 | add_dnodeint_end(&head, 1024); 25 | print_dlistint(head); 26 | node = get_dnodeint_at_index(head, 5); 27 | printf("%d\n", node->n); 28 | free_dlistint(head); 29 | head = NULL; 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_dnodeint_end(&head, 0); 18 | add_dnodeint_end(&head, 1); 19 | add_dnodeint_end(&head, 2); 20 | add_dnodeint_end(&head, 3); 21 | add_dnodeint_end(&head, 4); 22 | add_dnodeint_end(&head, 98); 23 | add_dnodeint_end(&head, 402); 24 | add_dnodeint_end(&head, 1024); 25 | sum = sum_dlistint(head); 26 | printf("sum = %d\n", sum); 27 | free_dlistint(head); 28 | head = NULL; 29 | return (EXIT_SUCCESS); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - sums all of the data of a dlistint_t linked list 5 | * @head: pointer to the beginning of the linked list 6 | * 7 | * Return: sum of all data, or 0 if the list is empty 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head != NULL) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | return (sum); 19 | } 20 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - Add node at nth index 5 | * 6 | * @h: Head of node 7 | * 8 | * @idx: index 9 | * 10 | * @n: struct int 11 | * 12 | * Return: dlistint_t 13 | */ 14 | 15 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 16 | { 17 | dlistint_t *new_node = malloc(sizeof(dlistint_t)); 18 | dlistint_t *current; 19 | unsigned int count = 0; 20 | 21 | if (h == NULL || new_node == NULL) 22 | { 23 | return (NULL); 24 | } 25 | new_node->n = n; 26 | new_node->next = NULL; 27 | new_node->prev = NULL; 28 | current = *h; 29 | 30 | if (idx == 0) 31 | { 32 | new_node = add_dnodeint(h, n); 33 | return (new_node); 34 | } 35 | while (current) 36 | { 37 | if (current->next == NULL && count == idx - 1) 38 | { 39 | new_node = add_dnodeint_end(h, n); 40 | return (new_node); 41 | } 42 | else if ((idx - 1) == count) 43 | { 44 | new_node->next = current->next; 45 | new_node->prev = current; 46 | current->next->prev = new_node; 47 | current->next = new_node; 48 | return (new_node); 49 | } 50 | count++; 51 | current = current->next; 52 | } 53 | free(new_node); 54 | return (NULL); 55 | } 56 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | printf("-----------------\n"); 26 | insert_dnodeint_at_index(&head, 5, 4096); 27 | print_dlistint(head); 28 | free_dlistint(head); 29 | head = NULL; 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - Delete node at nth index 5 | * 6 | * @head: Head of node 7 | * 8 | * @index: index 9 | * 10 | * Return: 1 succeed, -1 if fail 11 | */ 12 | 13 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 14 | { 15 | dlistint_t *node; 16 | unsigned int count; 17 | 18 | if (*head == NULL) 19 | return (-1); 20 | 21 | node = *head; 22 | if (index == 0) 23 | { 24 | *head = node->next; 25 | if (node->next != NULL) 26 | { 27 | node->next->prev = NULL; 28 | } 29 | free(node); 30 | return (1); 31 | } 32 | for (count = 0; node != NULL && count < index - 1 ; count++) 33 | { 34 | node = node->next; 35 | } 36 | if (node == NULL || node->next == NULL) 37 | { 38 | return (-1); 39 | } 40 | 41 | if (node->next->next != NULL) 42 | { 43 | node->next = node->next->next; 44 | free(node->next->prev); 45 | node->next->prev = node; 46 | return (1); 47 | } 48 | else 49 | { 50 | free(node->next); 51 | node->next = NULL; 52 | return (1); 53 | } 54 | return (-1); 55 | } 56 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x17. C - Doubly Linked Lists 2 | 3 | ## Learning Objectives 4 | 5 | At the end of this project, you should be able to explain the following concepts without using external resources: 6 | 7 | ### General 8 | 9 | - What is a doubly linked list 10 | - How to use doubly linked lists 11 | - Understanding and implementing various operations (deletion, insertion, etc.) with doubly linked lists 12 | - Starting to look for the right source of information without too much help 13 | 14 | ### Copyright - Plagiarism 15 | 16 | - You are responsible for creating your solutions to meet the learning objectives. 17 | - Copying and pasting someone else’s work is not allowed. 18 | - Publishing any content of this project is prohibited. 19 | - Any form of plagiarism will result in removal from the program. 20 | 21 | ## Requirements 22 | 23 | ### General 24 | 25 | - Allowed editors: vi, vim, emacs 26 | - Interpreted/compiled on Ubuntu 20.04 LTS using python3 (version 3.8.5) 27 | - All files should end with a new line 28 | - A README.md file, at the root of the folder of the project, is mandatory 29 | - Code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl 30 | - You are not allowed to use global variables 31 | - No more than 5 functions per file 32 | - The only C standard library functions allowed are malloc, free, printf, and exit 33 | - The prototypes of all your functions should be included in your header file called lists.h 34 | - Don’t forget to push your header file 35 | - All your header files should be include guarded 36 | 37 | ### Data Structure 38 | 39 | Please use this data structure for this project: 40 | 41 | ```c 42 | /** 43 | * struct dlistint_s - doubly linked list 44 | * @n: integer 45 | * @prev: points to the previous node 46 | * @next: points to the next node 47 | * 48 | * Description: doubly linked list node structure 49 | * 50 | */ 51 | typedef struct dlistint_s 52 | { 53 | int n; 54 | struct dlistint_s *prev; 55 | struct dlistint_s *next; 56 | } dlistint_t; 57 | 58 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x17-doubly_linked_lists/a -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x17-doubly_linked_lists/b -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x17-doubly_linked_lists/c -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x17-doubly_linked_lists/d -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x17-doubly_linked_lists/e -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/i: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x17-doubly_linked_lists/i -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * for Holberton project 15 | */ 16 | 17 | typedef struct dlistint_s 18 | { 19 | int n; 20 | struct dlistint_s *prev; 21 | struct dlistint_s *next; 22 | } dlistint_t; 23 | 24 | 25 | size_t print_dlistint(const dlistint_t *h); 26 | size_t dlistint_len(const dlistint_t *h); 27 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 28 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 29 | void free_dlistint(dlistint_t *head); 30 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 31 | int sum_dlistint(dlistint_t *head); 32 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 33 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 34 | 35 | #endif /* LISTS_H */ 36 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always EXIT_SUCCESS. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", _strlen("My Dyn Lib")); 12 | return (EXIT_SUCCESS); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x18-dynamic_libraries/0-main.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c -Wall -Werror -fpic *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P .. https://raw.githubusercontent.com/monoprosito/holbertonschool-low_level_programming/master/0x18-dynamic_libraries/libgiga.so 3 | export LD_PRELOAD="$PWD/../libgiga.so" -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | 0x18-dynamic_libraries -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Makitey/alx-low_level_programming/130c114841a08a284e7872009b96d2025c5d36fc/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif /* _MAIN_H_ */ 26 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creates a hash table 5 | * @size: size of the array 6 | * 7 | * Return: pointer to the newly created hash table 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *hash_table; 12 | unsigned long int i; 13 | 14 | hash_table = malloc(sizeof(hash_table_t)); 15 | if (hash_table == NULL) 16 | return (NULL); 17 | hash_table->size = size; 18 | hash_table->array = malloc(size * sizeof(hash_node_t *)); 19 | if (hash_table->array == NULL) 20 | { 21 | free(hash_table); 22 | return (NULL); 23 | } 24 | for (i = 0; i < size; i++) 25 | hash_table->array[i] = NULL; 26 | return (hash_table); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | /** 2 | * hash_djb2 - implementation of the djb2 algorithm 3 | * @str: string used to generate hash value 4 | * 5 | * Return: hash value 6 | */ 7 | unsigned long int hash_djb2(const unsigned char *str) 8 | { 9 | unsigned long int hash; 10 | int c; 11 | 12 | hash = 5381; 13 | while ((c = *str++)) 14 | { 15 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 16 | } 17 | return (hash); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | shash_table_t *ht; 14 | 15 | ht = shash_table_create(1024); 16 | shash_table_set(ht, "y", "0"); 17 | shash_table_print(ht); 18 | shash_table_set(ht, "j", "1"); 19 | shash_table_print(ht); 20 | shash_table_set(ht, "c", "2"); 21 | shash_table_print(ht); 22 | shash_table_set(ht, "b", "3"); 23 | shash_table_print(ht); 24 | shash_table_set(ht, "z", "4"); 25 | shash_table_print(ht); 26 | shash_table_set(ht, "n", "5"); 27 | shash_table_print(ht); 28 | shash_table_set(ht, "a", "6"); 29 | shash_table_print(ht); 30 | shash_table_set(ht, "m", "7"); 31 | shash_table_print(ht); 32 | shash_table_print_rev(ht); 33 | shash_table_delete(ht); 34 | return (EXIT_SUCCESS); 35 | } 36 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - returns the index of a key 5 | * @key: key 6 | * @size: size of the array of the hash table 7 | * Return: index 8 | */ 9 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 10 | { 11 | unsigned long int index = hash_djb2(key) % size; 12 | 13 | return (index); 14 | } 15 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_set - adds an element to the hash table. 4 | * @ht: hash table 5 | * @key: is the key. key can not be an empty string 6 | * @value: value associated with the key. 7 | * value must be duplicated. value can be an empty string 8 | * Return: 1 on success, 0 on failurre 9 | */ 10 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 11 | { 12 | 13 | hash_node_t *node; 14 | hash_node_t *new_node; 15 | unsigned long int index; 16 | 17 | if (ht == NULL || *key == '\n' || *value == '\n') 18 | return (0); 19 | 20 | index = key_index((const unsigned char *)key, ht->size); 21 | node = ht->array[index]; 22 | 23 | if (node == NULL) 24 | { 25 | new_node = create_new_node(key, value); 26 | if (new_node == NULL) 27 | return (0); 28 | 29 | ht->array[index] = new_node; 30 | return (1); 31 | } 32 | 33 | /*If key exists, replace value*/ 34 | while (node != NULL) 35 | { 36 | if (strcmp(key, node->key) == 0) 37 | { 38 | free(node->value); 39 | node->value = strdup(value); 40 | return (1); 41 | } 42 | node = node->next; 43 | } 44 | /*If key doesn't exist, create new node*/ 45 | new_node = create_new_node(key, value); 46 | if (new_node == NULL) 47 | return (0); 48 | 49 | new_node->next = ht->array[index]; 50 | ht->array[index] = new_node; 51 | return (1); 52 | } 53 | 54 | /** 55 | * create_new_node - create a new node 56 | * @key: is the key. key can not be an empty string 57 | * @value: value associated with the key. 58 | * value must be duplicated. value can be an empty string 59 | * Return: 1 on success, 0 on failurre 60 | */ 61 | 62 | hash_node_t *create_new_node (const char *key, const char *value) 63 | { 64 | hash_node_t *new_node; 65 | 66 | new_node = malloc(sizeof(hash_node_t)); 67 | 68 | if (new_node == NULL) 69 | return (NULL); 70 | 71 | new_node->key = strdup(key); 72 | new_node->value = strdup(value); 73 | new_node->next = NULL; 74 | 75 | return (new_node); 76 | } 77 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_get - retrieves a value associated with a key. 4 | * @ht: the hash table you want to look into 5 | * @key: is the key you are looking for 6 | * Return: value associated with key if found, NULL if failed 7 | */ 8 | char *hash_table_get(const hash_table_t *ht, const char *key) 9 | { 10 | 11 | hash_node_t *node; 12 | unsigned long int index; 13 | 14 | if (ht == NULL) 15 | return (NULL); 16 | 17 | index = key_index((const unsigned char *) key, ht->size); 18 | node = ht->array[index]; 19 | 20 | while (node != NULL) 21 | { 22 | if (strcmp(node->key, key) == 0) 23 | return (node->value); 24 | 25 | node = node->next; 26 | } 27 | return (NULL); 28 | } 29 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_print - prints hashtable 4 | * @ht: the hash table you want to print 5 | */ 6 | void hash_table_print(const hash_table_t *ht) 7 | { 8 | 9 | unsigned long int i; 10 | hash_node_t *node; 11 | short int comma = 0; 12 | 13 | if (ht == NULL) 14 | return; 15 | 16 | putchar('{'); 17 | for (i = 0; i < ht->size; i++) 18 | { 19 | node = ht->array[i]; 20 | while (node != NULL) 21 | { 22 | if (comma) 23 | printf(", "); 24 | 25 | printf("'%s': '%s'", node->key, node->value); 26 | node = node->next; 27 | 28 | if (comma == 0) 29 | comma = 1; 30 | } 31 | } 32 | printf("}\n"); 33 | } 34 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_delete - deletes a hash table 4 | * @ht: the hash table you want to delete 5 | */ 6 | void hash_table_delete(hash_table_t *ht) 7 | { 8 | unsigned long int i; 9 | hash_node_t *node; 10 | 11 | if (ht == NULL) 12 | return; 13 | 14 | for (i = 0; i < ht->size; i++) 15 | { 16 | node = ht->array[i]; 17 | free_hash_list(node); 18 | } 19 | free(ht->array); 20 | free(ht); 21 | } 22 | 23 | 24 | /** 25 | * free_hash_list - frees a hash_node_t list 26 | * @head: head of linked list 27 | */ 28 | void free_hash_list(hash_node_t *head) 29 | { 30 | hash_node_t *current; 31 | hash_node_t *next; 32 | 33 | current = head; 34 | 35 | while (current != NULL) 36 | { 37 | next = current->next; 38 | free(current->key); 39 | free(current->value); 40 | free(current); 41 | current = next; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0x1A-hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef _HASH_TABLES_H_ 2 | #define _HASH_TABLES_H_ 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | /** 10 | * struct hash_node_s - Node of a hash table 11 | * 12 | * @key: The key, string 13 | * The key is unique in the HashTable 14 | * @value: The value corresponding to a key 15 | * @next: A pointer to the next node of the List 16 | */ 17 | typedef struct hash_node_s 18 | { 19 | char *key; 20 | char *value; 21 | struct hash_node_s *next; 22 | } hash_node_t; 23 | 24 | /** 25 | * struct hash_table_s - Hash table data structure 26 | * 27 | * @size: The size of the array 28 | * @array: An array of size @size 29 | * Each cell of this array is a pointer to the first node of a linked list, 30 | * because we want our HashTable to use a Chaining collision handling 31 | */ 32 | typedef struct hash_table_s 33 | { 34 | unsigned long int size; 35 | hash_node_t **array; 36 | } hash_table_t; 37 | 38 | hash_table_t *hash_table_create(unsigned long int size); 39 | unsigned long int hash_djb2(const unsigned char *str); 40 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 41 | hash_node_t *create_new_node (const char *key, const char *value); 42 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 43 | char *hash_table_get(const hash_table_t *ht, const char *key); 44 | void hash_table_print(const hash_table_t *ht); 45 | void hash_table_delete(hash_table_t *ht); 46 | void free_hash_list(hash_node_t *head); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | # -*- MakeFile -*- 2 | 3 | #target: dependencies 4 | # action 5 | 6 | all: school 7 | school: school.c main.c 8 | gcc main.c school.c -o school 9 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 4 | all: $(SRC) 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean: 11 | $(RM) *~ $(NAME) 12 | 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: fclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(CLFAGS) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Return the perimiter of an island. 7 | The grid represents water by 0 and land by 1. 8 | Args: 9 | grid (list): A list of list of integers representing an island. 10 | Returns: 11 | The perimeter of the island defined in grid. 12 | """ 13 | perimeter = 0 14 | for row in range(len(grid)): 15 | for cell in range(len(grid[row])): 16 | if grid[row][cell] == 1: 17 | if cell != (len(grid[row]) - 1) and not grid[row][cell + 1]: 18 | perimeter += 1 19 | if not cell or cell == len(grid[row]) - 1: 20 | perimeter += 1 21 | if row == len(grid) - 1 or not row: 22 | perimeter += 1 23 | if cell != 0 and not grid[row][cell - 1]: 24 | perimeter += 1 25 | if row and not grid[row - 1][cell]: 26 | perimeter += 1 27 | if row != (len(grid) - 1) and not grid[row + 1][cell]: 28 | perimeter += 1 29 | return perimeter 30 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [1, 0, 0, 0, 0, 0], 10 | [1, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 1, 1], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # 0x1C. C - Makefiles 2 | 3 | Welcome to the C - Makefiles project! This project focuses on understanding and utilizing Makefiles in C programming. 4 | 5 | ## Resources 6 | 7 | - [What is a makefile and how does it work](link-to-makefile) 8 | - [Installing the make utility](link-to-make-installation) 9 | - [Make official documentation](link-to-make-docs) 10 | - [More about Makefile](link-to-more-makefile-info) 11 | 12 | ## Learning Objectives 13 | 14 | By the end of this project, you should be able to: 15 | 16 | - Understand what make and Makefiles are 17 | - Know when, why, and how to use Makefiles 18 | - Understand rules and how to set and use them 19 | - Differentiate between explicit and implicit rules 20 | - Know common and useful rules 21 | - Understand variables and how to set and use them 22 | 23 | ## Requirements 24 | 25 | - Allowed editors: vi, vim, emacs 26 | - OS: Ubuntu 20.04 LTS 27 | - Version of gcc: 9.3.0 28 | - Version of make: GNU Make 4.2.1 29 | - All files should end with a new line 30 | - A README.md file at the root of the project folder is mandatory 31 | 32 | ## Files 33 | 34 | In the following tasks, we will use specific files provided for compilation purposes. 35 | 36 | ## Author 37 | 38 | [Makda Weldemichael] 39 | 40 | 41 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_school - print the logo of Holberton 5 | * 6 | * Return: Nothing 7 | * 8 | */ 9 | void print_school(void) 10 | { 11 | printf("j#0000000000000000000000000000000000000\n"); 12 | printf("j#000000000000000000@Q**g00000000000000\n"); 13 | printf("j#0000000000000000*]++]4000000000000000\n"); 14 | printf("j#000000000000000k]++]++*N#000000000000\n"); 15 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 16 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 17 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 18 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 19 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 20 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 21 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 22 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 23 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 24 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 25 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 26 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 27 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 28 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 29 | printf("j#00000000000w]++++]*0##000000000000000\n"); 30 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 31 | printf("j#00000000000000000we]+]Q00000000000000\n"); 32 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 33 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 34 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 35 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # alx-low_level_programming -------------------------------------------------------------------------------- /main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_N 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | ssize_t read_textfile(const char *filename, size_t letters); 13 | int create_file(const char *filename, char *text_content); 14 | int append_text_to_file(const char *filename, char *text_content); 15 | 16 | /** 17 | * ELF header functions 18 | */ 19 | void check_elf(unsigned char *e_ident); 20 | void print_magic(unsigned char *e_ident); 21 | void print_class(unsigned char *e_ident); 22 | void print_data(unsigned char *e_ident); 23 | void print_version(unsigned char *e_ident); 24 | void print_abi(unsigned char *e_ident); 25 | void print_osabi(unsigned char *e_ident); 26 | void print_type(unsigned int e_type, unsigned char *e_ident); 27 | void print_entry(unsigned long int e_entry, unsigned char *e_ident); 28 | void close_elf(int elf); 29 | #endif 30 | --------------------------------------------------------------------------------