├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── .#0-positive_or_negative.c ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md ├── holberton.h └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isuper ├── 0-isupper.c ├── 0-main.c ├── 1-isdigit ├── 1-isdigit.c ├── 1-main.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-main.c ├── 2-mul ├── 2-mul.c ├── 3-main.c ├── 3-print_numbers.c ├── 4-main.c ├── 4-print_most_numbers ├── 4-print_most_numbers.c ├── 5-main.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 ├── holberton.h └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md ├── _putchar.c └── main.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-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── create_static_lib.sh.save ├── holberton.h ├── liball.a └── libholberton.a ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-mul.exe ├── 100-realloc.c ├── 101-mul.c ├── 101-mul.exe ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── _putchar.c └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-main.c ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── 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 ├── 101-hello_holberton.o ├── 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 ├── e ├── first ├── hello └── lists.h ├── 0x13-more_singly_linked_lists ├── .vscode │ └── settings.json ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.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 ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md ├── _putchar.c ├── lists.h ├── m └── n ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md ├── main.h └── tests │ ├── 0-main.c │ ├── 1-main.c │ ├── 2-main.c │ ├── Requiescat │ └── incitatous ├── 0x17-doubly_linked_lists ├── 0-main.c ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── 0-main.c ├── 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-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py └── README.md ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -E -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - write to stdout 5 | * Return: Always 0 6 | */ 7 | int main(void) 8 | { 9 | write(1, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 10 | return (1); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | printf("with proper grammar, but the outcome is a piece of art,\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | long int b; 12 | long long int c; 13 | char d; 14 | float f; 15 | 16 | printf("Size of a char: %lu byte(s)\n", (unsigned long)sizeof(d)); 17 | printf("Size of an int: %lu byte(s)\n", (unsigned long)sizeof(a)); 18 | printf("Size of a long int: %lu byte(s)\n", (unsigned long)sizeof(b)); 19 | printf("Size of a long long int: %lu byte(s)\n", (unsigned long)sizeof(c)); 20 | printf("Size of a float: %lu byte(s)\n", (unsigned long)sizeof(f)); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | hello world 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/.#0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | vagrant@vagrant-ubuntu-trusty-64.2163:1568887427 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | 17 | if (n == 0) 18 | printf("%d is zero\n", n); 19 | else if (n < 0) 20 | printf("%d is negative\n", n); 21 | else 22 | printf("%d is positive\n", n); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | *main - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | 16 | if ((n % 10) == 0) 17 | printf("Last digit of %d is %d and is 0\n", n, n % 10); 18 | else if ((n % 10) > 5) 19 | printf("Last digit of %d is %d and is greater than 5\n", n, n % 10); 20 | else 21 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, n % 10); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: Write a program that prints all possible 6 | * different combinations of two digits 7 | * Numbers must be separated by ,, followed by a space 8 | * The two digits must be different 9 | * 01 and 10 are considered the same combination of the two digits 0 and 1 10 | * Print only the smallest combination of two digits 11 | * Numbers should be printed in ascending order, with two digits 12 | * You can only use the putchar function 13 | * (every other function (printf, puts, etc…) is forbidden) 14 | * You can only use putchar five times maximum in your code 15 | * You are not allowed to use any variable of type char 16 | * All your code should be in the main function 17 | * Return: 0 18 | */ 19 | int main(void) 20 | { 21 | int c; 22 | int d = 0; 23 | 24 | while (d < 10) 25 | { 26 | c = 0; 27 | while (c < 10) 28 | { 29 | if (d != c && d < c) 30 | { 31 | putchar('0' + d); 32 | putchar('0' + c); 33 | 34 | if (c + d != 17) 35 | { 36 | putchar(','); 37 | putchar(' '); 38 | } 39 | } 40 | 41 | c++; 42 | } 43 | d++; 44 | } 45 | putchar('\n'); 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: Write a program that prints all possible 6 | * different combinations of two digits 7 | * Numbers must be separated by ,, followed by a space 8 | * The two digits must be different 9 | * 01 and 10 are considered the same combination of the two digits 0 and 1 10 | * Print only the smallest combination of two digits 11 | * Numbers should be printed in ascending order, with two digits 12 | * You can only use the putchar function 13 | * (every other function (printf, puts, etc…) is forbidden) 14 | * You can only use putchar five times maximum in your code 15 | * You are not allowed to use any variable of type char 16 | * All your code should be in the main function 17 | * Return: 0 18 | */ 19 | int main(void) 20 | { 21 | int c; 22 | int d; 23 | int e = 0; 24 | 25 | while (e < 10) 26 | { 27 | d = 0; 28 | while (d < 10) 29 | { 30 | c = 0; 31 | while (c < 10) 32 | { 33 | if (c != d && d != e && e < d && d < c) 34 | { 35 | putchar('0' + e); 36 | putchar('0' + d); 37 | putchar('0' + c); 38 | 39 | if (c + d + e != 9 + 8 + 7) 40 | { 41 | putchar(','); 42 | putchar(' '); 43 | } 44 | } 45 | 46 | c++; 47 | } 48 | d++; 49 | } 50 | e++; 51 | } 52 | putchar('\n'); 53 | return (0); 54 | } 55 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - create 2 pairs of number 4 | * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | int i, j; 9 | 10 | for (i = 0; i <= 99; i++) 11 | { 12 | for (j = i; j <= 99; j++) 13 | { 14 | if (j != i) 15 | { 16 | putchar(i / 10 + 48); 17 | putchar(i % 10 + 48); 18 | putchar(' '); 19 | putchar(j / 10 + 48); 20 | putchar(j % 10 + 48); 21 | 22 | if (i * 100 + j != 9899) 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 | #include 3 | /** 4 | * main - main block 5 | * Description: Get a random number and check its last digit, compare it with 5 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | putchar(c); 15 | c++; 16 | } 17 | 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: prints the alphabet in lowercase, 5 | * and then in uppercase, followed by a new line. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | putchar(c); 15 | c++; 16 | } 17 | 18 | c = 'A'; 19 | 20 | while (c <= 'Z') 21 | { 22 | putchar(c); 23 | c++; 24 | } 25 | 26 | putchar('\n'); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print the alphabet in lowercase letters, except for e and q 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | { 14 | if (letter != 'e' && letter != 'q') 15 | putchar(letter); 16 | } 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print all single digit numbers 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int digit; 11 | 12 | for (digit = 0; digit < 10; digit++) 13 | printf("%i", digit); 14 | putchar('\n'); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print all single digit numbers using only putchar 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int digit; 11 | 12 | for (digit = 0; digit < 10; digit++) 13 | putchar(digit + '0'); 14 | 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print the alphabet in lowercase letters in reverse 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char letter; 11 | 12 | for (letter = 'z'; letter >= 'a'; letter--) 13 | putchar(letter); 14 | 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print the numbers of base 16 in lowercase 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char digit; 11 | 12 | for (digit = '0'; digit <= '9'; digit++) 13 | putchar(digit); 14 | 15 | for (digit = 'a'; digit <= 'f'; digit++) 16 | putchar(digit); 17 | 18 | putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block 5 | * Description: prints all single digit numbers of base 10 6 | * starting from 0, followed by a new line. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int c = 0; 12 | 13 | while (c < 10) 14 | { 15 | putchar(48 + c); 16 | if (c != 9) 17 | { 18 | putchar(','); 19 | putchar(' '); 20 | } 21 | c++; 22 | } 23 | putchar('\n'); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What are the arithmetic operators and how to use them 4 | What are the logical operators (sometimes called boolean operators) and how to use them 5 | What the the relational operators and how to use them 6 | What values are considered TRUE and FALSE in C 7 | What are the boolean operators and how to use them 8 | How to use the if, if ... else statements 9 | How to use comments 10 | How to declare variables of types char, int, unsigned int 11 | How to assign values to variables 12 | How to print the values of variables of type char, int, unsigned int with printf 13 | How to use the while loop 14 | How to use variables with the while loop 15 | How to print variables using printf 16 | What is the ASCII character set 17 | What are the purpose of the gcc flags -m32 and -m64 18 | 19 | Requirements 20 | General 21 | 22 | Allowed editors: vi, vim, emacs 23 | All your files will be compiled on Ubuntu 14.04 LTS 24 | Your programs and functions will compiled with gcc 4.8.4 using the flags -Wall -Werror -Wextra and -pedantic 25 | All your files should end with a new line 26 | A README.md file, at the root of the folder of the project 27 | There should be no errors and no warnings during compilation 28 | You are not allowed to use system 29 | Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - prints _putchar, fllowd by a new linees 4 | * Return: Alwys 0 (Success) 5 | */ 6 | int main(void) 7 | { 8 | char abe[] = "_putchar"; 9 | 10 | int c; 11 | 12 | for (c = 0; c < 8; c++) 13 | { 14 | _putchar(abe[c]); 15 | } 16 | _putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - check the code for ALX School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | 9 | void print_alphabet(void) 10 | { 11 | char alp = 'a'; 12 | 13 | while (alp <= 'z') 14 | { 15 | _putchar (alp); 16 | alp++; 17 | 18 | } 19 | _putchar ('\n'); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - adds two integers and returns the result 5 | * @a: int to be added to b 6 | * @b: int to be added to a 7 | * Return: sum of a and b 8 | */ 9 | int add(int a, int b) 10 | { 11 | int c; 12 | c = a + b; 13 | return (c); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the n times table, starting with 0 5 | * @n: number of the times table 6 | */ 7 | void print_times_table(int n) 8 | { 9 | int i, j, k; 10 | if (n >= 0 && n <= 15) 11 | { 12 | for (i = 0; i <= n; i++) 13 | { 14 | for (j = 0; j <= n; j++) 15 | { 16 | k = j * i; 17 | if (j == 0) 18 | { 19 | _putchar(k + '0'); 20 | } 21 | else if (k < 10 && j != 0) 22 | { 23 | _putchar(','); 24 | _putchar(' '); 25 | _putchar(' '); 26 | _putchar(' '); 27 | _putchar(k + '0'); 28 | } 29 | else if (k >= 10 && k < 100) 30 | { 31 | _putchar(','); 32 | _putchar(' '); 33 | _putchar(' '); 34 | _putchar((k / 10) + '0'); 35 | _putchar((k % 10) + '0'); 36 | } 37 | else if (k >= 100) 38 | { 39 | _putchar(','); 40 | _putchar(' '); 41 | _putchar((k / 100) + '0'); 42 | _putchar(((k / 10) % 10) + '0'); 43 | _putchar((k % 10) + '0'); 44 | } 45 | } 46 | _putchar('\n'); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the sum of all the multiples of 3 or 5 below 1024 4 | * @void: void 5 | * Return: 0 Success 6 | */ 7 | int main(void) 8 | { 9 | int n; 10 | int sum = 0; 11 | 12 | for (n = 0; n < 1024; n++) 13 | { 14 | if (n % 3 == 0 || n % 5 == 0) 15 | { 16 | sum += n; 17 | } 18 | } 19 | printf("%d\n", sum); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the first 50 Fibonacci numbers, starting with 1 and 2 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | long int i, j, k, next; 11 | j = 1; 12 | k = 2; 13 | for (i = 1; i <= 50; ++i) 14 | { 15 | if (j != 20365011074) 16 | { 17 | printf("%ld, ", j); 18 | } 19 | else 20 | { 21 | printf("%ld\n", j); 22 | } 23 | next = j + k; 24 | j = k; 25 | k = next; 26 | } 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the sum of the even-valued terms 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | unsigned long int j, k, next, sum; 12 | j = 1; 13 | k = 2; 14 | sum = 0; 15 | for (i = 1; i <= 33; ++i) 16 | { 17 | if (j < 4000000 && (j % 2) == 0) 18 | { 19 | sum = sum + j; 20 | } 21 | next = j + k; 22 | j = k; 23 | k = next; 24 | } 25 | printf("%lu\n", sum); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the first 98 Fibonacci numbers, 5 | * starting with 1 and 2 6 | * followed by a new line 7 | * Return: ALways 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int i, j, k, j1, j2, k1, k2; 12 | j = 1; 13 | k = 2; 14 | printf("%lu", j); 15 | for (i = 1; i < 91; i++) 16 | { 17 | printf(", %lu", k); 18 | k = k + j; 19 | j = k - j; 20 | } 21 | j1 = j / 1000000000; 22 | j2 = j % 1000000000; 23 | k1 = k / 1000000000; 24 | k2 = k % 1000000000; 25 | for (i = 92; i < 99; ++i) 26 | { 27 | printf(", %lu", k1 + (k2 / 1000000000)); 28 | printf("%lu", k2 % 1000000000); 29 | k1 = k1 + j1; 30 | j1 = k1 - j1; 31 | k2 = k2 + j2; 32 | j2 = k2 - j2; 33 | } 34 | printf("\n"); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - prints all natural numbers from n to 98, 6 | * followed by a new line 7 | * @n: print from this number 8 | */ 9 | void print_to_98(int n) 10 | { 11 | int i, j; 12 | if (n <= 98) 13 | { 14 | for (i = n; i <= 98; i++) 15 | { 16 | if (i != 98) 17 | printf("%d, ", i); 18 | else if (i == 98) 19 | printf("%d\n", i); 20 | } 21 | } 22 | else if (n >= 98) 23 | { 24 | for (j = n; j >= 98; j--) 25 | { 26 | if (j != 98) 27 | printf("%d, ", j); 28 | else if (j == 98) 29 | printf("%d\n", j); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - func 5 | * 6 | * Return - 0 7 | */ 8 | void print_alphabet_x10(void) 9 | { 10 | int b = 0; 11 | while (b < 10) 12 | { 13 | char a = 'a'; 14 | while (a <= 'z') 15 | { 16 | _putchar(a); 17 | a++; 18 | } 19 | _putchar('\n'); 20 | b++; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_islower - checks for a lowercase character 5 | *@c: letter being tested 6 | * Return: Always 0 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | 12 | if (c >= 97 && c <= 122) 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - check the code for ALX School students. 5 | * 6 | * @c: is c an integer argument 7 | * 8 | * Return: Always 0. 9 | */ 10 | 11 | int _isalpha(int c) 12 | { 13 | 14 | if (c >= 97 && c <= 122) 15 | { 16 | return (1); 17 | } 18 | else if (c >= 65 && c <= 90) 19 | { 20 | return (1); 21 | } 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - prints the sign of a number 5 | *@n: number being tested 6 | * Return: Always 0. 7 | */ 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 if (n < 0) 22 | { 23 | _putchar('-'); 24 | return (-1); 25 | } 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - returns absolute value of an int 4 | * @n: integer from which to get an absolute value 5 | * Return: Void. 6 | */ 7 | 8 | int _abs(int n) 9 | { 10 | 11 | if (n < 0) 12 | { 13 | return (n * -1); 14 | } 15 | return (n); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - prints the last digit of a number 5 | * @n: the int to extract the last digit from 6 | * Return: value of the last digit 7 | */ 8 | int print_last_digit(int n) 9 | { 10 | int a; 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 | /** 4 | * jack_bauer - prints every minute of the day of Jack Bauer 5 | * starting from 00:00 to 23:59 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int i, j; 10 | i = 0; 11 | while (i < 24) 12 | { 13 | j = 0; 14 | while (j < 60) 15 | { 16 | _putchar((i / 10) + '0'); 17 | _putchar((i % 10) + '0'); 18 | _putchar(':'); 19 | _putchar((j / 10) + '0'); 20 | _putchar((j % 10) + '0'); 21 | _putchar('\n'); 22 | j++; 23 | } 24 | i++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - prints the 9 times table, starting with 0 5 | */ 6 | void time_table(void) 7 | { 8 | int i, j, k; 9 | for (i = 0; i < 10; i++) 10 | { 11 | for (j = 0; j < 10; j++) 12 | { 13 | k = j * i; 14 | if (j == 0) 15 | { 16 | _putchar(k + '0'); 17 | } 18 | if (k < 10 && j != 0) 19 | { 20 | _putchar(','); 21 | _putchar(' '); 22 | _putchar(' '); 23 | _putchar(k + '0'); 24 | } 25 | else if (k >= 10) 26 | { 27 | _putchar(','); 28 | _putchar(' '); 29 | _putchar((k / 10) + '0'); 30 | _putchar((k % 10) + '0'); 31 | } 32 | } 33 | _putchar('\n'); 34 | } 35 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | low level programming 2 | # alx-low_level_programming 3 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /* 5 | * File: main.h 6 | * 7 | * Desc: Header file containing declartions for all function 8 | */ 9 | 10 | int _putchar(char c); 11 | void print_alphabet(void); 12 | void print_alphabet_x10(void); 13 | int _islower(int c); 14 | int _isalpha(int c); 15 | int print_sign(int n); 16 | int _abs(int); 17 | int print_last_digit(int); 18 | void jack_bauer(void); 19 | void time_teble(void); 20 | int add(int, int); 21 | void print_to_98(int n); 22 | void print_time_table(int n); 23 | 24 | #endif -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Test function for positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /* 17 | * while (i < 10) 18 | * { 19 | * putchar(i); 20 | * } 21 | */ 22 | printf("Infinite loop avoided! \\o/\n"); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a > b && a > c) 16 | { 17 | largest = a; 18 | } 19 | else if (a > b && c > a) 20 | { 21 | largest = c; 22 | } 23 | else if (b > c) 24 | { 25 | largest = b; 26 | } 27 | else 28 | { 29 | largest = c; 30 | } 31 | 32 | return (largest); 33 | } 34 | -------------------------------------------------------------------------------- /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 % 100 == 0 && year % 400 == 0) || (year % 4 == 0)) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | printf("Day of the year: %d\n", day); 22 | printf("Remaining days: %d\n", 366 - day); 23 | } 24 | else 25 | { 26 | if (month == 2 && day == 60) 27 | { 28 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 29 | } 30 | else 31 | { 32 | printf("Day of the year: %d\n", day); 33 | printf("Remaining days: %d\n", 365 - day); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | Debugging is the process of finding and resolving bugs (Defects or probelms that prevent correct operation) within computer program,software or system. 2 | Debugging tactics can involve interactive debugging, control flow analysis, unit testing, integration testing, log file analysis, monitoring at the application or system level, memory dumps, and profiling. -------------------------------------------------------------------------------- /0x03-debugging/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | void positive_or_negative(int i); 5 | int largest_number(int a, int b, int c); 6 | void print_remaining_days(int month, int day, int year); 7 | int convert_day(int month, int day); 8 | #endif 9 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | void positive_or_negative(int i); 5 | int largest_number(int a, int b, int c); 6 | void print_remaining_days(int month, int day, int year); 7 | int convert_day(int month, int day); 8 | #endif 9 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isuper: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x04-more_functions_nested_loops/0-isuper -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - Check if paramter c represents an uppercase letter. 6 | * @c: Int representing a character 7 | * Return: Always 0. 8 | */ 9 | int _isupper(int c) 10 | { 11 | if (c >= 65 && c <= 90) 12 | { 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int _isupper(int c); 10 | int main(void) 11 | { 12 | char c; 13 | 14 | c = 'A'; 15 | printf("%c: %d\n", c, _isupper(c)); 16 | c = 'a'; 17 | printf("%c: %d\n", c, _isupper(c)); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x04-more_functions_nested_loops/1-isdigit -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isdigit - Check for a digit (0-9) 4 | * @c: Integer holding the character 5 | * 6 | * Return: 1- Digit 0- Not 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= 48 && c <= 57) 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "mains.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = '0'; 14 | printf("%c: %d\n", c, _isdigit(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isdigit(c)); 17 | return (0); 18 | } -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle, followed by a new line 5 | * @size: size of the triangle 6 | */ 7 | void print_triangle(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 1; i <= size; i++) 17 | { 18 | for (j = i; j < size; j++) 19 | { 20 | _putchar(' '); 21 | } 22 | 23 | for (j = 1; j <= i; j++) 24 | { 25 | _putchar('#'); 26 | } 27 | 28 | _putchar('\n'); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - calculate largest prime of 612852475143 5 | * 6 | * Return: Success Always 7 | */ 8 | 9 | 10 | int main(void) 11 | { 12 | long int x = 612852475143; 13 | long int py; 14 | 15 | for (py = 2; py < x; py++) 16 | { 17 | if (x % py == 0) 18 | { 19 | x = x / py; 20 | } 21 | } 22 | printf("%ld\n", py); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | int _putchar(char c); 6 | 7 | /** 8 | * print_number - like a hello world 9 | * 10 | * @n: params an integer 11 | * 12 | * No return 13 | */ 14 | 15 | 16 | void print_number(int n) 17 | { 18 | 19 | unsigned int n1 = 0; 20 | 21 | if (n < 0) 22 | { 23 | n1 = -n; 24 | _putchar('-'); 25 | } 26 | 27 | else 28 | { 29 | n1 = n; 30 | } 31 | 32 | if (n1 / 10) 33 | { 34 | print_number(n1 / 10); 35 | } 36 | 37 | _putchar((n1 % 10) + '0'); 38 | } 39 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", mul(98, 1024)); 12 | printf("%d\n", mul(-402, 4096)); 13 | return (0); 14 | } -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x04-more_functions_nested_loops/2-mul -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * mul - Multiply 2 integers. 6 | * @a: An integer to be multiplied with b 7 | * @b : An integer to be multipled with a 8 | * Return: Return the result of the multiplication 9 | */ 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_numbers - Print numbers between 0 to 9 incl. 6 | * 7 | * Return: Void. 8 | */ 9 | void print_numbers(void) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < 10; i++) 14 | { 15 | _putchar(i + '0'); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | void print_most_numbers(void); 9 | int main(void) 10 | { 11 | print_most_numbers(); 12 | return (0); 13 | } -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x04-more_functions_nested_loops/4-print_most_numbers -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_most_numbers - Print numbers between 0 to 9 incl. 6 | * 7 | * Return: Void. 8 | */ 9 | void print_most_numbers(void) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < 10; i++) 14 | { 15 | if (i != 2 && i != 4) 16 | { 17 | _putchar(i + '0'); 18 | } 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | more_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * more_numbers - Print numbers between 0 to 14 incl. 6 | * 7 | * Return: Void. 8 | */ 9 | void more_numbers(void) 10 | { 11 | int i; 12 | int x; 13 | 14 | for (x = 0; x < 10; x++) 15 | { 16 | i = 0; 17 | while (i <= 14) 18 | { 19 | if (i >= 10) 20 | { 21 | _putchar(i / 10 + '0'); 22 | } 23 | _putchar(i % 10 + '0'); 24 | i++; 25 | } 26 | _putchar('\n'); 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_line - Print line dependent on the integer n. 6 | * @n : The number of '_' characters to use 7 | * Return: Void. 8 | */ 9 | void print_line(int n) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < n; i++) 14 | { 15 | _putchar('_'); 16 | } 17 | _putchar('\n'); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_diagonal - Print diagonal line dependent on the integer n. 6 | * @n : The number of lines using '\' characters to use 7 | * Return: Void. 8 | */ 9 | void print_diagonal(int n) 10 | { 11 | int i; 12 | int spaces; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | for (spaces = 0; spaces < i; spaces++) 17 | { 18 | _putchar(' '); 19 | } 20 | _putchar('\\'); 21 | _putchar('\n'); 22 | } 23 | 24 | if (n <= 0) 25 | { 26 | _putchar('\n'); 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_square - Print square line dependent on the integer n. 6 | * @n : The number of lines using '#' characters to use per row and column 7 | * Return: Void. 8 | */ 9 | void print_square(int n) 10 | { 11 | int x; 12 | int y; 13 | 14 | for (y = 0; y < n; y++) 15 | { 16 | for (x = 0; x < n; x++) 17 | { 18 | _putchar('#'); 19 | } 20 | _putchar('\n'); 21 | } 22 | if (n <= 0) 23 | { 24 | _putchar('\n'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - Fizz for multiples of 3 and Buzz for multiples of 5 FizzBuzz for both 6 | * 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int x = 1; 13 | 14 | while (x < 101) 15 | { 16 | if (x % 3 == 0 && x % 5 == 0) 17 | { 18 | printf("%s", "FizzBuzz"); 19 | } 20 | else if (x % 3 == 0) 21 | { 22 | printf("%s", "Fizz"); 23 | } 24 | else if (x % 5 == 0) 25 | { 26 | printf("%s", "Buzz"); 27 | } 28 | else 29 | { 30 | printf("%d", x); 31 | } 32 | 33 | if (x != 100) 34 | { 35 | printf(" "); 36 | } 37 | x++; 38 | } 39 | printf("\n"); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | gcc - GNU compiler collection, mostly used to compile c,c++ object-c go and d programs. 2 | 3 | options;- 4 | -Wall :- his enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros 5 | -Werror:-Make all warnings into errors. 6 | -pendantic :-its warnings are also disabled in the expression that follows __extension__. However, only system header files should use these escape routes; application programs should avoid them 7 | -Wextra :- This enables some extra warning flags that are not enabled by -Wall. (This option used to be called -W. The older name is still supported, but the newer name is more descriptive.).Its also used to prints warning messages for the following cases: 8 | 9 | A pointer is compared against integer zero with <, <=, >, or >=. 10 | (C++ only) An enumerator and a non-enumerator both appear in a conditional expression. 11 | (C++ only) Ambiguous virtual bases. 12 | (C++ only) Subscripting an array that has been declared register. 13 | (C++ only) Taking the address of a variable that has been declared register. 14 | (C++ only) A base class is not initialized in the copy constructor of a derived class. -------------------------------------------------------------------------------- /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 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN 2 | #define MAIN 3 | extern int _putchar(char c); 4 | extern int _isupper(int c); 5 | extern int _isdigit(int c); 6 | extern int mul(int a, int b); 7 | extern void print_numbers(void); 8 | extern void print_most_numbers(void); 9 | extern void more_numbers(void); 10 | extern void print_line(int n); 11 | extern void print_diagonal(int n); 12 | extern void print_square(int size); 13 | extern void print_triangle(int size); 14 | extern void print_number(int n); 15 | #endif 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN 2 | #define MAIN 3 | extern int _putchar(char c); 4 | extern int _isupper(int c); 5 | extern int _isdigit(int c); 6 | extern int mul(int a, int b); 7 | extern void print_numbers(void); 8 | extern void print_most_numbers(void); 9 | extern void more_numbers(void); 10 | extern void print_line(int n); 11 | extern void print_diagonal(int n); 12 | extern void print_square(int size); 13 | extern void print_triangle(int size); 14 | extern void print_number(int n); 15 | #endif 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value *n points to. 5 | * @n: the number to modify. 6 | */ 7 | 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swap the value of two integers. 5 | * 6 | * @a: first integer. 7 | * @b: second integer. 8 | * 9 | */ 10 | 11 | void swap_int(int *a, int *b) 12 | { 13 | int c; 14 | 15 | c = 0; 16 | c = *a; 17 | *a = *b; 18 | *b = c; 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string into an integer. 5 | * 6 | * @s: the string to use. 7 | * 8 | * Return: integer. 9 | */ 10 | 11 | int _atoi(char *s) 12 | { 13 | int sign = 1, i = 0; 14 | unsigned int res = 0; 15 | 16 | while (!(s[i] <= '9' && s[i] >= '0') && s[i] != '\0') 17 | { 18 | if (s[i] == '-') 19 | sign *= -1; 20 | i++; 21 | } 22 | while (s[i] <= '9' && (s[i] >= '0' && s[i] != '\0')) 23 | { 24 | res = (res * 10) + (s[i] - '0'); 25 | i++; 26 | } 27 | res *= sign; 28 | return (res); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - print password. 8 | * 9 | * Return: 0. 10 | */ 11 | 12 | int main(void) 13 | { 14 | int ascii = 2772, i = 0, j, random; 15 | char password[100]; 16 | time_t t; 17 | 18 | srand((int) time(&t)); 19 | while (ascii > 126) 20 | { 21 | random = rand() % 126; 22 | password[i] = random; 23 | ascii -= random; 24 | i++; 25 | } 26 | if (ascii > 0) 27 | password[i] = ascii; 28 | else 29 | { 30 | i--; 31 | } 32 | 33 | 34 | for (j = 0; j <= i; j++) 35 | { 36 | printf("%c", password[j]); 37 | } 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - calculate the length of a string. 5 | * 6 | * @s: the string to calculate it's length. 7 | * Return: length of a string. 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int i; 13 | int length = 0; 14 | 15 | for (i = 0; s[i] != '\0'; i++) 16 | { 17 | length++; 18 | } 19 | return (length); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string followed by a new line.. 5 | * 6 | *@str: the string to print. 7 | * 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i; 13 | 14 | for (i = 0; str[i] != '\0'; i++) 15 | { 16 | _putchar(str[i]); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - print a string in reverse. 5 | * 6 | *@s: the string to print in reverse. 7 | * 8 | */ 9 | 10 | void print_rev(char *s) 11 | { 12 | int i = 0; 13 | int length; 14 | 15 | for (length = 0; s[length] != '\0'; length++) 16 | { 17 | } 18 | 19 | for (i = length - 1; i >= 0; i--) 20 | { 21 | _putchar(s[i]); 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverse a string. 5 | * 6 | *@s: the string to be reversed. 7 | */ 8 | 9 | void rev_string(char *s) 10 | { 11 | int length, j, i; 12 | char v1, v2; 13 | 14 | for (length = 0; s[length] != '\0'; length++) 15 | { 16 | } 17 | 18 | j = length - 1; 19 | i = 0; 20 | 21 | while (j > i) 22 | { 23 | v1 = s[i]; 24 | v2 = s[j]; 25 | s[i] = v2; 26 | s[j] = v1; 27 | j--; 28 | i++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints every other character of a string. 5 | *@str: the string to use. 6 | */ 7 | 8 | void puts2(char *str) 9 | { 10 | int i = 0; 11 | 12 | while (str[i] != '\0') 13 | { 14 | if (i % 2 == 0) 15 | { 16 | _putchar(str[i]); 17 | } 18 | i++; 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints the second half of a string. 5 | *@str: string to use. 6 | */ 7 | 8 | void puts_half(char *str) 9 | { 10 | int length, n, i; 11 | 12 | for (length = 0; str[length] != '\0'; length++) 13 | { 14 | } 15 | n = (length - 1) / 2; 16 | for (i = n + 1; str[i] != '\0'; i++) 17 | { 18 | _putchar(str[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - print an array. 6 | * @a: the array to print. 7 | * @n: array's length 8 | */ 9 | 10 | void print_array(int *a, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | printf("%d", a[i]); 17 | if (i < n - 1) 18 | { 19 | printf(", "); 20 | } 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, including \0. 5 | *@src: pointer. 6 | *@dest: pointer. 7 | *Return: the pointer to dest. 8 | */ 9 | 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i, length; 13 | 14 | for (length = 0; src[length] != '\0'; length++) 15 | { 16 | } 17 | 18 | for (i = 0; i <= length ; i++) 19 | { 20 | dest[i] = src[i]; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # Pointers, Arrays and Strings in C 2 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main 2 | 3 | int _putchar(char c); 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 | void print_rev(char *s); 9 | void rev_string(char *s); 10 | void puts2(char *str); 11 | void puts_half(char *str); 12 | void print_array(int *a, int n); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | 16 | #define main 17 | #endif 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - function that concatenates two strings. 5 | * @src: cadena a copiar 6 | * @dest: destino de la cadena. 7 | * Return: dest. 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | while (dest[i] != '\0') 17 | { 18 | i++; 19 | } 20 | j = 0; 21 | while (src[j] != '\0') 22 | { 23 | dest[i] = src[j]; 24 | i++; 25 | j++; 26 | } 27 | dest[i] = '\0'; 28 | return (dest); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strncat - function that concatenates two strings. 6 | * @src: cadena a copiar 7 | * @dest: destino de la cadena. 8 | * @n: number. 9 | * Return: dest. 10 | */ 11 | 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int index = strlen(dest); 15 | int a = 0; 16 | 17 | while (a < n && *src) 18 | { 19 | dest[index + a] = *src; 20 | src++; 21 | a++; 22 | } 23 | dest[index + a] = '\0'; 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - a function that encodes a string using rot13. 5 | * @s: An input string to encode using rot13 6 | * Return: An encode string 7 | */ 8 | char *rot13(char *s) 9 | { 10 | int i = 0; 11 | 12 | while (s[i] != '\0') 13 | { 14 | while ((s[i] >= 'a' && s[i] <= 'z') || 15 | (s[i] >= 'A' && s[i] <= 'Z')) 16 | { 17 | if ((s[i] >= 'a' && s[i] <= 'm') || 18 | (s[i] >= 'A' && s[i] <= 'M')) 19 | s[i] += 13; 20 | else 21 | s[i] -= 13; 22 | i++; 23 | } 24 | i++; 25 | } 26 | return (s); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * base10 - power in 10 base 5 | * @n: an exponent 6 | * Return: returns 10 to power exponent 7 | */ 8 | int base10(int n) 9 | { 10 | int base = 10; 11 | 12 | while (n > 0) 13 | { 14 | base *= 10; 15 | n--; 16 | } 17 | return (base); 18 | } 19 | 20 | /** 21 | * print_number - prints integers enters as parameters using putchar 22 | * @n: integer to print 23 | * Return: void 24 | */ 25 | void print_number(int n) 26 | { 27 | int power; 28 | 29 | power = base10(8); 30 | 31 | if (n < 0) 32 | { 33 | _putchar('-'); 34 | n *= -1; 35 | } 36 | 37 | if (n == 0) 38 | _putchar('0'); 39 | 40 | else 41 | { 42 | while (n / power == 0) 43 | power /= 10; 44 | 45 | while (power >= 1) 46 | { 47 | _putchar((n / power) + '0'); 48 | n %= power; 49 | power /= 10; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /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 | * rot13 - encodes a string in rot13 5 | * @s: string to be encoded 6 | * Return: the resulting strring 7 | */ 8 | char *rot13(char *s) 9 | { 10 | int i, j; 11 | 12 | char a[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 13 | char b[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 14 | 15 | for (i = 0; s[i] != '\0'; i++) 16 | { 17 | for (j = 0; a[j] != '\0'; j++) 18 | { 19 | if (s[i] == a[j]) 20 | { 21 | s[i] = b[j]; 22 | break; 23 | } 24 | } 25 | } 26 | return (s); 27 | } -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - Prints a buffer 10 bytes at a time, starting with 6 | * the byte position, then showing the hex content, 7 | * then displaying printable charcaters. 8 | * @b: The buffer to be printed. 9 | * @size: The number of bytes to be printed from the buffer. 10 | */ 11 | void print_buffer(char *b, int size) 12 | { 13 | int byte, index; 14 | 15 | for (byte = 0; byte < size; byte += 10) 16 | { 17 | printf("%08x: ", byte); 18 | 19 | for (index = 0; index < 10; index++) 20 | { 21 | if ((index + byte) >= size) 22 | printf(" "); 23 | 24 | else 25 | printf("%02x", *(b + index + byte)); 26 | 27 | if ((index % 2) != 0 && index != 0) 28 | printf(" "); 29 | } 30 | 31 | for (index = 0; index < 10; index++) 32 | { 33 | if ((index + byte) >= size) 34 | break; 35 | 36 | else if (*(b + index + byte) >= 31 && 37 | *(b + index + byte) <= 126) 38 | printf("%c", *(b + index + byte)); 39 | 40 | else 41 | printf("."); 42 | } 43 | 44 | if (byte >= size) 45 | continue; 46 | 47 | printf("\n"); 48 | } 49 | 50 | if (size <= 0) 51 | printf("\n"); 52 | } 53 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - function that copies a string.. 5 | * @src: cadena a copiar 6 | * @dest: destino de la cadena. 7 | * @n: number. 8 | * Return: void 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | i = 0; 16 | 17 | while (src[i] != '\0' && i < n) 18 | { 19 | dest[i] = src[i]; 20 | i++; 21 | } 22 | 23 | while (i < n) 24 | { 25 | dest[i] = '\0'; 26 | i++; 27 | } 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - function that compares two strings. 5 | * @s1: primera cadena. 6 | * @s2: segunda cadena. 7 | * Return: 0. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int res = 0; 13 | 14 | while (*s1 == *s2 && *s1 != '\0') 15 | { 16 | s1++; 17 | s2++; 18 | } 19 | if (s1 != s2) 20 | res = *s1 - *s2; 21 | 22 | return (res); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - function that reverses the content of an array of integers. 5 | * @a: puntero del array. 6 | * @n: numero de elementos del array. 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int i; 12 | int b; 13 | 14 | i = 0; 15 | while (i < n) 16 | { 17 | n--; 18 | b = a[i]; 19 | a[i] = a[n]; 20 | a[n] = b; 21 | i++; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - function that changes all lowercase letter 5 | * of a string to uppercase. 6 | *@n: cadena de caracteres. 7 | * Return: n. 8 | */ 9 | 10 | char *string_toupper(char *n) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (n[i] != '\0') 16 | { 17 | if (n[i] >= 'a' && n[i] <= 'z') 18 | n[i] = n[i] - 32; 19 | i++; 20 | } 21 | return (n); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cap_string - capitalizes all words of a string. 3 | * @s: string. 4 | * Return: string. 5 | */ 6 | char *cap_string(char *s) 7 | { 8 | int i; 9 | 10 | i = 0; 11 | while (s[i] != '\0') 12 | { 13 | if ((s[i - 1] == ' ' || s[i - 1] == '\n' 14 | || s[i - 1] == '\t' || s[i - 1] == ',' 15 | || s[i - 1] == ';' || s[i - 1] == '!' 16 | || s[i - 1] == '?' || s[i - 1] == '"' 17 | || s[i - 1] == '(' || s[i - 1] == ')' 18 | || s[i - 1] == '{' || s[i - 1] == '}' 19 | || s[i - 1] == '.') 20 | && (s[i] >= 'a' && s[i] <= 'z')) 21 | { 22 | s[i] = s[i] - 32; 23 | } 24 | else if ((s[0] >= 97 && s[0] <= 122)) 25 | { 26 | s[0] = s[0] - 32; 27 | } 28 | else 29 | { 30 | s[i] = s[i]; 31 | } 32 | i++; 33 | } 34 | return (s); 35 | } 36 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encodes a string into 1337. 4 | * @s: string to code 5 | * Return: the enconde string. 6 | */ 7 | char *leet(char *s) 8 | { 9 | int i; 10 | int j; 11 | int k; 12 | 13 | char l[] = "oOlLeEaAtT"; 14 | char e[] = "0011334477"; 15 | 16 | i = 0; 17 | while (s[i] != '\0') 18 | { 19 | j = 0; 20 | k = 0; 21 | while (l[j] != '\0') 22 | { 23 | if (s[i] == l[j]) 24 | { 25 | k = j; 26 | s[i] = e[k]; 27 | } 28 | j++; 29 | } 30 | i++; 31 | } 32 | return (s); 33 | } 34 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | My read me file for 0x06-pointers_arrays_strings 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _main_h_ 2 | #define _main_h_ 3 | 4 | int _putchar(char c); 5 | char *_strcat(char *dest, char *src); 6 | char *_strncat(char *dest, char *src, int n); 7 | char *_strncpy(char *dest, char *src, int n); 8 | int _strcmp(char *s1, char *s2); 9 | void reverse_array(int *a, int n); 10 | char *string_toupper(char *); 11 | char *cap_string(char *); 12 | char *leet(char *); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /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 | * set_string - sets pointer val to char 4 | * @s: pointer 5 | * @to: char 6 | */ 7 | void set_string(char **s, char *to) 8 | { 9 | *s = to; 10 | } 11 | -------------------------------------------------------------------------------- /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 | /** 2 | * _strspn - Gets the length of a prefix substring. 3 | * @s: String where substring will look. 4 | * @accept: Substring of accepted chars. 5 | * Return: Length of occurrence. 6 | */ 7 | unsigned int _strspn(char *s, char *accept) 8 | { 9 | unsigned int c = 0; 10 | char *t = accept; 11 | 12 | while (*s++) 13 | { 14 | while (*accept++) 15 | if (*(s - 1) == *(accept - 1)) 16 | { 17 | c++; 18 | break; 19 | } 20 | if (!(*--accept)) 21 | break; 22 | accept = t; 23 | } 24 | return (c); 25 | } 26 | -------------------------------------------------------------------------------- /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 | void print_diagsums(int *a, int size) 10 | { 11 | int i, sum1 = 0, sum2 = 0; 12 | 13 | for (i = 0; i < size; i++) 14 | { 15 | sum1 += *(a + (size * i + i)); 16 | sum2 += *(a + (size * i + size - 1 - i)); 17 | } 18 | printf("%d, ", sum1); 19 | printf("%d\n", sum2); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | More on Pointers, Arrays and Strings 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: 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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_HOLBERTON 2 | #define FILE_HOLBERTON 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 | 9 | void _print_rev_recursion(char *s) 10 | { 11 | if (*s) 12 | { 13 | _print_rev_recursion(s + 1); 14 | _putchar(*s); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - 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 | 32 | if (*s == *(s + l)) 33 | { 34 | return (p1(s + 1, l - 2)); 35 | } 36 | return (0); 37 | } 38 | 39 | /** 40 | * is_palindrome - palindrome 41 | * @s: pointer to string 42 | * Return: recursion 43 | */ 44 | 45 | int is_palindrome(char *s) 46 | { 47 | int len = _strlen_recursion(s); 48 | 49 | return (p1(s, len - 1)); 50 | } 51 | -------------------------------------------------------------------------------- /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 | 21 | if (*s2 == '*') 22 | { 23 | return (wildcmp(s1 + 1, s2) || wildcmp(s1, s2 + 1)); 24 | } 25 | else if (*s1 == *s2) 26 | { 27 | return (wildcmp(s1 + 1, s2 + 1)); 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /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 | 11 | int _pow_recursion(int x, int y) 12 | { 13 | if (y < 0) 14 | { 15 | return (-1); 16 | } 17 | if (y == 0) 18 | { 19 | return (1); 20 | } 21 | return (x * _pow_recursion(x, y - 1)); 22 | } 23 | -------------------------------------------------------------------------------- /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 | int _sqrt_recursion(int n) 9 | { 10 | return (_sqrt(n, 1)); 11 | } 12 | 13 | /** 14 | * _sqrt - _sqrt_recursion 15 | * @n: integer paramtr 16 | * @i: integer parameter 17 | * Return: sqrt 18 | */ 19 | int _sqrt(int n, int i) 20 | { 21 | if (n < 0) 22 | return (-1); 23 | if ((i * i) > n) 24 | return (-1); 25 | if (i * i == n) 26 | return (i); 27 | return (_sqrt(n, i + 1)); 28 | } 29 | -------------------------------------------------------------------------------- /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 | I am working on my c program :- project 0x08 - recursion 2 | 3 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_HOLBERTON 2 | #define FILE_HOLBERTON 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 "holberton.h" 2 | 3 | /** 4 | * _isupper - check the code for Holberton School students. 5 | * 6 | * @c: is an integer param 7 | * 8 | * Return: Always 0. 9 | */ 10 | 11 | 12 | int _isupper(int c) 13 | { 14 | 15 | return (c >= 65 && c <= 90); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * *_strcat - function commute srtings 5 | * @dest: param pointer to a char 6 | * @src: param pointer to a char 7 | * Return: return value of dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | 23 | while (src[j] != '\0') 24 | { 25 | dest[i] = src[j]; 26 | j++; 27 | i++; 28 | } 29 | 30 | dest[i] = '\0'; 31 | return (dest); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isdigit - check the code for Holberton School students. 5 | * 6 | * @c: is an integer param 7 | * 8 | * Return: Always 0. 9 | */ 10 | 11 | 12 | int _isdigit(int c) 13 | { 14 | 15 | return (c >= 48 && c <= 57); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strncat - two words 5 | * @dest : pointer to char param 6 | * @src : pointer to char param 7 | * @n : int parameter 8 | * Return: *dest 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int m; 14 | int i; 15 | 16 | m = 0; 17 | 18 | for (i = 0; i < 1000; i++) 19 | { 20 | if (dest[i] == '\0') 21 | { 22 | break; 23 | } 24 | m++; 25 | } 26 | 27 | for (i = 0; src[i] != '\0' && i < n; i++) 28 | { 29 | dest[m + i] = src[i]; 30 | } 31 | dest[m + i] = '\0'; 32 | return (dest); 33 | } 34 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _atoi - int 4 | * @s: pointer 5 | * Return: int. 6 | */ 7 | int _atoi(char *s) 8 | { 9 | int i; 10 | int res = 0; 11 | int sig = -1; 12 | int brk = 0; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | { 16 | if (s[i] == '-') 17 | sig = sig * -1; 18 | if (s[i] >= '0' && s[i] <= '9') 19 | { 20 | res = res * 10; 21 | res -= (s[i] - '0'); 22 | brk = 1; 23 | } 24 | else if (brk == 1) 25 | break; 26 | } 27 | res = sig * res; 28 | return (res); 29 | } 30 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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 | int i; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | { 16 | 17 | if (s[i] == c) 18 | { 19 | return (s + i); 20 | } 21 | } 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - length of a string 5 | * @s: input char 6 | * Return: length of a string 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int l = 0; 12 | 13 | while (*s != '\0') 14 | { 15 | s++; 16 | l++; 17 | } 18 | return (l); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strncpy - two words 5 | * @dest : pointer to char params 6 | * @src : pointer to char params 7 | * @n : int params 8 | * Return: *dest 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; src[i] != '\0' && i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | 20 | for ( ; i < n; i++) 21 | { 22 | dest[i] = '\0'; 23 | } 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _islower - check the code for Holberton School students. 5 | * 6 | * @c: is a parameter 7 | * 8 | * Return: Always 0. 9 | */ 10 | 11 | int _islower(int c) 12 | { 13 | if (c >= 97 && c <= 122) 14 | { 15 | return (1); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts - prints string 5 | * @str: input string 6 | * Return: no return 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | while (*str != '\0') 12 | { 13 | _putchar(*str); 14 | str++; 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcmp - compare 5 | * @s1 : pointerto char params 6 | * @s2 : pointer to char params 7 | * Return: *dest 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i; 13 | int R; 14 | 15 | i = 0; 16 | 17 | while (s1[i] == s2[i] && (s1[i] != '\0' || s2[i] != '\0')) 18 | { 19 | i++; 20 | } 21 | R = s1[i] - s2[i]; 22 | return (R); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _strspn - Gets the length of a prefix substring. 3 | * @s: String where substring will look. 4 | * @accept: Substring of accepted chars. 5 | * Return: Length of occurrence. 6 | */ 7 | unsigned int _strspn(char *s, char *accept) 8 | { 9 | unsigned int c = 0; 10 | char *t = accept; 11 | 12 | while (*s++) 13 | { 14 | while (*accept++) 15 | if (*(s - 1) == *(accept - 1)) 16 | { 17 | c++; 18 | break; 19 | } 20 | if (!(*--accept)) 21 | break; 22 | accept = t; 23 | } 24 | return (c); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isalpha - check the code for Holberton School students. 5 | * 6 | * @c: is c an integer argument 7 | * 8 | * Return: Always 0. 9 | */ 10 | 11 | int _isalpha(int c) 12 | { 13 | 14 | if (c >= 97 && c <= 122) 15 | { 16 | return (1); 17 | } 18 | else if (c >= 65 && c <= 90) 19 | { 20 | return (1); 21 | } 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.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/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _abs - check the code for Holberton School students. 5 | * 6 | * @n: is an integer argument 7 | * 8 | * Return: Always 0. 9 | */ 10 | 11 | int _abs(int n) 12 | { 13 | if (n >= 0) 14 | { 15 | return (n); 16 | } 17 | return (-n); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * *_strcpy - copy strg to another string 5 | * @dest: pointer 6 | * @src: poiter 7 | * Return: numbers dest 8 | */ 9 | 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i; 13 | 14 | for (i = 0; src[i] != '\0'; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | dest[i] = '\0'; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar -rc liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh.save: -------------------------------------------------------------------------------- 1 | create_static_lib.sh 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_HOLBERTON 2 | #define FILE_HOLBERTON 3 | 4 | /*copy list from task*/ 5 | 6 | int _putchar(char c); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int _abs(int n); 10 | int _isupper(int c); 11 | int _isdigit(int c); 12 | int _strlen(char *s); 13 | void _puts(char *s); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | char *_strcat(char *dest, char *src); 17 | char *_strncat(char *dest, char *src, int n); 18 | char *_strncpy(char *dest, char *src, int n); 19 | int _strcmp(char *s1, char *s2); 20 | char *_memset(char *s, char b, unsigned int n); 21 | char *_memcpy(char *dest, char *src, unsigned int n); 22 | char *_strchr(char *s, char c); 23 | unsigned int _strspn(char *s, char *accept); 24 | char *_strpbrk(char *s, char *accept); 25 | char *_strstr(char *haystack, char *needle); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libholberton.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x09-static_libraries/libholberton.a -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the program name, followed by a new line. 5 | * @argc: The number of arguments supplied to the program. 6 | * @argv: An array of pointers to the arguments. 7 | * 8 | * Return: Always o. 9 | */ 10 | int main(int __attribute__((__unused__)) argc, char *argv[]) 11 | { 12 | printf("%s\n", argv[0]); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the number of arguments passed to it. 5 | * @argc: The number of arguments supplied to the program. 6 | * @argv: An array of pointers to the arguments. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int argc, char __attribute__((__unused__)) *argv[]) 11 | { 12 | printf("%d\n", argc - 1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Prints the minimum number of coins to 6 | * make change for an amount of money. 7 | * @argc: The number of arguments supplied to the program. 8 | * @argv: An array of pointers to the arguments. 9 | * 10 | * Return: If the number of arguments is not exactly one - 1. 11 | * Otherwise - 0. 12 | */ 13 | int main(int argc, char *argv[]) 14 | { 15 | int cents, coins = 0; 16 | 17 | if (argc != 2) 18 | { 19 | printf("Error\n"); 20 | return (1); 21 | } 22 | 23 | cents = atoi(argv[1]); 24 | 25 | while (cents > 0) 26 | { 27 | coins++; 28 | if ((cents - 25) >= 0) 29 | { 30 | cents -= 25; 31 | continue; 32 | } 33 | if ((cents - 10) >= 0) 34 | { 35 | cents -= 10; 36 | continue; 37 | } 38 | if ((cents - 5) >= 0) 39 | { 40 | cents -= 5; 41 | continue; 42 | } 43 | if ((cents - 2) >= 0) 44 | { 45 | cents -= 2; 46 | continue; 47 | } 48 | cents--; 49 | } 50 | 51 | printf("%d\n", coins); 52 | 53 | return (0); 54 | } 55 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints all arguments it receives. 5 | * @argc: The number of arguments supplied to the program. 6 | * @argv: An array of pointers to the arguments. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int arg; 13 | 14 | for (arg = 0; arg < argc; arg++) 15 | printf("%s\n", argv[arg]); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /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 arguments supplied to the program. 7 | * @argv: An array of pointers to the arguments. 8 | * 9 | * Return: If the program receives two arguments - 0. 10 | * If the program does not receive two arguments - 1. 11 | */ 12 | int main(int argc, char *argv[]) 13 | { 14 | int num1, num2, prod; 15 | 16 | if (argc != 3) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | 22 | num1 = atoi(argv[1]); 23 | num2 = atoi(argv[2]); 24 | prod = num1 * num2; 25 | 26 | printf("%d\n", prod); 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Prints the addition of positive numbers, 6 | * followed by a new line. 7 | * @argc: The number of arguments passed to the program. 8 | * @argv: An array of pointers to the arguments. 9 | * 10 | * Return: If one of the numbers contains symbols that are non-digits - 1. 11 | * Otherwise - 0. 12 | */ 13 | int main(int argc, char *argv[]) 14 | { 15 | int num, digit, sum = 0; 16 | 17 | for (num = 1; num < argc; num++) 18 | { 19 | for (digit = 0; argv[num][digit]; digit++) 20 | { 21 | if (argv[num][digit] < '0' || argv[num][digit] > '9') 22 | { 23 | printf("Error\n"); 24 | return (1); 25 | } 26 | } 27 | 28 | sum += atoi(argv[num]); 29 | } 30 | 31 | printf("%d\n", sum); 32 | 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | ALX argument count and argument value 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * create_array - creates an array of chars. 5 | * @size: size of the array. 6 | * @c: storaged char 7 | * 8 | * Return: pointer of an array of chars 9 | */ 10 | char *create_array(unsigned int size, char c) 11 | { 12 | char *cr; 13 | unsigned int i; 14 | 15 | if (size == 0) 16 | return (NULL); 17 | 18 | cr = malloc(sizeof(c) * size); 19 | 20 | if (cr == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | cr[i] = c; 25 | 26 | return (cr); 27 | } 28 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strdup - returns a pointer to a newly allocated space in memory. 5 | * @str: string. 6 | * 7 | * Return: pointer of an array of chars 8 | */ 9 | char *_strdup(char *str) 10 | { 11 | char *strout; 12 | unsigned int i, j; 13 | 14 | if (str == NULL) 15 | return (NULL); 16 | 17 | for (i = 0; str[i] != '\0'; i++) 18 | ; 19 | 20 | strout = (char *)malloc(sizeof(char) * (i + 1)); 21 | 22 | if (strout == NULL) 23 | return (NULL); 24 | 25 | for (j = 0; j <= i; j++) 26 | strout[j] = str[j]; 27 | 28 | return (strout); 29 | } 30 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments of a program. 6 | * @ac: argument count. 7 | * @av: argument vector. 8 | * 9 | * Return: pointer of an array of char 10 | */ 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *aout; 14 | int c, i, j, ia; 15 | 16 | if (ac == 0) 17 | { 18 | return (NULL); 19 | } 20 | for (c = i = 0; i < ac; i++) 21 | { 22 | if (av[i] == NULL) 23 | return (NULL); 24 | 25 | for (j = 0; av[i][j] != '\0'; j++) 26 | c++; 27 | c++; 28 | } 29 | 30 | aout = malloc((c + 1) * sizeof(char)); 31 | 32 | if (aout == NULL) 33 | { 34 | free(aout); 35 | return (NULL); 36 | } 37 | 38 | for (i = j = ia = 0; ia < c; j++, ia++) 39 | { 40 | if (av[i][j] == '\0') 41 | { 42 | aout[ia] = '\n'; 43 | i++; 44 | ia++; 45 | j = 0; 46 | } 47 | if (ia < c - 1) 48 | aout[ia] = av[i][j]; 49 | } 50 | aout[ia] = '\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: multidimensional array of char. 7 | * @height: height of the array. 8 | * 9 | * Return: no return 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 | * 26 | * Return: pointer of an array of integers 27 | */ 28 | char **strtow(char *str) 29 | { 30 | char **aout; 31 | unsigned int c, height, i, j, a1; 32 | 33 | if (str == NULL || *str == '\0') 34 | return (NULL); 35 | for (c = height = 0; str[c] != '\0'; c++) 36 | { 37 | if (str[c] != ' ' && (str[c + 1] == ' ' || str[c + 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 (i = a1 = 0; i < height; i++) 47 | { 48 | for (c = a1; str[c] != '\0'; c++) 49 | { 50 | if (str[c] == ' ') 51 | a1++; 52 | if (str[c] != ' ' && (str[c + 1] == ' ' || str[c + 1] == '\0')) 53 | { 54 | aout[i] = malloc((c - a1 + 2) * sizeof(char)); 55 | if (aout[i] == NULL) 56 | { 57 | ch_free_grid(aout, i); 58 | return (NULL); 59 | } 60 | break; 61 | } 62 | } 63 | for (j = 0; a1 <= c; a1++, j++) 64 | aout[i][j] = str[a1]; 65 | aout[i][j] = '\0'; 66 | } 67 | aout[i] = 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 | * 9 | * Return: pointer of an array of chars 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *strout; 14 | unsigned int i, j, k, limit; 15 | 16 | if (s1 == NULL) 17 | s1 = ""; 18 | if (s2 == NULL) 19 | s2 = ""; 20 | 21 | for (i = 0; s1[i] != '\0'; i++) 22 | ; 23 | 24 | for (j = 0; s2[j] != '\0'; j++) 25 | ; 26 | 27 | strout = malloc(sizeof(char) * (i + j + 1)); 28 | 29 | if (strout == NULL) 30 | { 31 | free(strout); 32 | return (NULL); 33 | } 34 | 35 | for (k = 0; k < i; k++) 36 | strout[k] = s1[k]; 37 | 38 | limit = j; 39 | for (j = 0; j <= limit; k++, j++) 40 | strout[k] = s2[j]; 41 | 42 | return (strout); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - returns a pointer to a 2 dimensional array of integers. 6 | * @width: width of the array. 7 | * @height: height of the array. 8 | * 9 | * Return: pointer of an array of integers 10 | */ 11 | int **alloc_grid(int width, int height) 12 | { 13 | int **gridout; 14 | int i, j; 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 (i = 0; i < height; i++) 27 | { 28 | gridout[i] = malloc(width * sizeof(int)); 29 | if (gridout[i] == NULL) 30 | { 31 | for (i--; i >= 0; i--) 32 | free(gridout[i]); 33 | free(gridout); 34 | return (NULL); 35 | } 36 | } 37 | 38 | for (i = 0; i < height; i++) 39 | for (j = 0; j < width; j++) 40 | gridout[i][j] = 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: multidimensional array of integers. 7 | * @height: height of the grid. 8 | * 9 | * Return: no return 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 | # Malloc, free 2 | Project done during **Full Stack Software Engineering studies** at **Holberton School**. It aims to learn about automatic and dynamic allocation in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc 4.8.4` 6 | * C files are written according to the C90 standard 7 | * Tested on Ubuntu 14.04 LTS 8 | 9 | ## Files 10 | All of the following files are programs written in C: 11 | 12 | | Filename | Description | 13 | | -------- | ----------- | 14 | | `0-create_array.c` | Creates an array of chars, and initializes it with a specific char | 15 | | `1-strdup.c` | Returns a pointer to a newly allocated space in memory, which contains a copy of the string given as a parameter | 16 | | `2-str_concat.c` | Concatenates two strings | 17 | | `3-alloc_grid.c` | Returns a pointer to a 2 dimensional array of integers | 18 | | `4-free_grid.c` | Frees a 2 dimensional grid previously allocated in the heap memory | 19 | | `5-argstostr.c` | Concatenates all the arguments of a program | 20 | | `100-strtow.c` | Splits a string into words | 21 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * malloc_checked - allocates memory using malloc. 4 | * @b: size of memory to be allocated. 5 | * 6 | * Return: pointer to allocated memory. 7 | */ 8 | void *malloc_checked(unsigned int b) 9 | { 10 | void *p = malloc(b); 11 | 12 | if (p == NULL) 13 | exit(98); 14 | 15 | return (p); 16 | } 17 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | /** 5 | * _strlen - calculate and return string length 6 | * @string: string 7 | * Return: string length 8 | */ 9 | int _strlen(char *string) 10 | { 11 | int i; 12 | 13 | for (i = 0; string[i] != '\0'; i++) 14 | ; 15 | return (i); 16 | } 17 | /** 18 | * string_nconcat - concatenate s1 and n bytes of s2; return ptr to string 19 | * @s1: string 1 20 | * @s2: string 2 21 | * @n: n bytes to concat from string 2 22 | * Return: pointer to concatenated string 23 | */ 24 | char *string_nconcat(char *s1, char *s2, unsigned int n) 25 | { 26 | char *ptr; 27 | int num, len, i, j; 28 | 29 | num = n; 30 | 31 | if (s1 == NULL) /* account for NULL strings */ 32 | s1 = ""; 33 | if (s2 == NULL) 34 | s2 = ""; 35 | if (num < 0) /* account for negative n bytes */ 36 | return (NULL); 37 | if (num >= _strlen(s2)) /* account for n too big */ 38 | num = _strlen(s2); 39 | 40 | len = _strlen(s1) + num + 1; /* +1 to account for null pointer */ 41 | 42 | ptr = malloc(sizeof(*ptr) * len); /* malloc and check for error */ 43 | if (ptr == NULL) 44 | return (NULL); 45 | 46 | for (i = 0; s1[i] != '\0'; i++) /* concat */ 47 | ptr[i] = s1[i]; 48 | for (j = 0; j < num; j++) 49 | ptr[i + j] = s2[j]; 50 | ptr[i + j] = '\0'; 51 | 52 | return (ptr); 53 | } 54 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-mul.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x0C-more_malloc_free/100-mul.exe -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * _realloc - reallocates a memory block using malloc and free. 5 | * @ptr: pointer to the memory previously allocated with a 6 | * call to ``` malloc : malloc(old_size) ```. 7 | * @old_size: size in bytes allocated for ptr. 8 | * @new_size: size in bytes of new memory block. 9 | * 10 | * Return: pointer to new mem block, NULL or ptr. 11 | */ 12 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 13 | { 14 | char *nptr; 15 | unsigned int i; 16 | 17 | if (new_size == old_size) 18 | return (ptr); 19 | if ((new_size == 0) && (ptr != NULL)) 20 | { 21 | free(ptr); 22 | return (NULL); 23 | } 24 | if (ptr == NULL) 25 | { 26 | nptr = malloc(new_size); 27 | if (nptr == NULL) 28 | return (NULL); 29 | } 30 | if (new_size > old_size && (ptr != NULL)) 31 | { 32 | nptr = malloc(new_size); 33 | if (nptr == NULL) 34 | return (nptr); 35 | for (i = 0; i < old_size; i++) 36 | nptr[i] = *((char *)ptr + 1); 37 | free(ptr); 38 | } 39 | 40 | return (nptr); 41 | } 42 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - multiplies two positive numbers 5 | * @argc: n arguments 6 | * @argv: args 7 | * Return: int 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | unsigned long mul; 12 | int i, j; 13 | if (argc != 3) 14 | { printf("Error\n"); 15 | exit(98); } 16 | for (i = 1; i < argc; i++) 17 | { 18 | for (j = 0; argv[i][j] != '\0'; j++) 19 | { 20 | if (argv[i][j] > 57 || argv[i][j] < 48) 21 | { printf("Error\n"); 22 | exit(98); } 23 | } 24 | 25 | } 26 | mul = atol(argv[1]) * atol(argv[2]); 27 | printf("%lu\n", mul); 28 | return (0); 29 | } -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x0C-more_malloc_free/101-mul.exe -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * _calloc - allocates memory of an array using malloc. 5 | * @nmemb: number of elements in array. 6 | * @size: size of elements of array. 7 | * 8 | * Return: NULL is size or nmemb == 0. 9 | * NULL if malloc fails. 10 | * Pointer to memory allocated if successful. 11 | */ 12 | void *_calloc(unsigned int nmemb, unsigned int size) 13 | { 14 | void *p; 15 | unsigned int i; 16 | 17 | if (nmemb == 0 || size == 0) 18 | return (NULL); 19 | p = malloc(nmemb * size); 20 | if (p == NULL) 21 | { 22 | return (NULL); 23 | } 24 | 25 | for (i = 0; i < (nmemb * size); i++) 26 | { 27 | *((char *)(p) + i) = 0; 28 | } 29 | 30 | return (p); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | *array_range - creates an array of integers. 5 | *@min: minimum value. 6 | *@max: maximum value. 7 | * 8 | *Return: pointer to newly created array. 9 | *NULL if malloc fails. 10 | *NULL if min > max. 11 | */ 12 | int *array_range(int min, int max) 13 | { 14 | int range, i; 15 | int *p; 16 | 17 | range = 0; 18 | 19 | if (min > max) 20 | return (NULL); 21 | 22 | range = ((max + 1) - min); 23 | 24 | p = malloc(range * sizeof(int)); 25 | 26 | if (p == NULL) 27 | return (NULL); 28 | 29 | for (i = 0; i < range; i++) 30 | { 31 | *(p + i) = min + i; 32 | } 33 | 34 | return (p); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0C-more_malloc_free 2 | ## main.h 3 | > Header contains all function prototypes of functions in this project. 4 | 5 | 6 | 7 | ``` MANDATORY``` 8 | 9 | ## 0-malloc_checked.c 10 | > A function that allocates memory using ``` malloc ```. 11 | ## 1-string_nconcat.c 12 | > A function that concatenates two strings. 13 | ## 2-callloc.c 14 | > A function that allocates memory of an array using malloc. 15 | ## 3-array_range.c 16 | > A function that creates an array of integers. 17 | 18 | ```ADVANCED``` 19 | 20 | ## 100-realloc.c 21 | > A function that reallocates a memory block using malloc and free. 22 | # 101-mul.c 23 | > A function that multiplies two positive numbers. 24 | 25 | -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - preints the name of the file 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < (0) ? -(x) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | #c preprocessor 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * init_dog - Initializes a dog structure 6 | * @d: A dog structure. 7 | * @name: The name of the dog. 8 | * @age: The age of the dog. 9 | * @owner: The owner of the dog. 10 | * 11 | * Return: Nothing 12 | */ 13 | void init_dog(struct dog *d, char *name, float age, char *owner) 14 | { 15 | if (d != NULL) 16 | { 17 | d->name = name; 18 | d->age = age; 19 | d->owner = owner; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * print_dog - Prints all the data of a dog 7 | * @d: A dog structure 8 | * 9 | * Return: Nothing 10 | */ 11 | void print_dog(struct dog *d) 12 | { 13 | if (d != NULL) 14 | { 15 | printf("Name: %s\n", (d->name) ? d->name : "(nil)"); 16 | printf("Age: %f\n", (d->age) ? d->age : 0); 17 | printf("Owner: %s\n", (d->owner) ? d->owner : "(nil)"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | int _strlen(char *s); 5 | char *_strcpy(char *dest, char *src); 6 | 7 | /** 8 | * new_dog - ... 9 | * @name: ... 10 | * @age: ... 11 | * @owner: ... 12 | * 13 | * Return: ... 14 | */ 15 | dog_t *new_dog(char *name, float age, char *owner) 16 | { 17 | dog_t *cutie_dog; 18 | int name_l = 0, own_l = 0; 19 | 20 | if (name != NULL && owner != NULL) 21 | { 22 | name_l = _strlen(name) + 1; 23 | own_l = _strlen(owner) + 1; 24 | cutie_dog = malloc(sizeof(dog_t)); 25 | 26 | if (cutie_dog == NULL) 27 | return (NULL); 28 | 29 | cutie_dog->name = malloc(sizeof(char) * name_l); 30 | 31 | if (cutie_dog->name == NULL) 32 | { 33 | free(cutie_dog); 34 | return (NULL); 35 | } 36 | 37 | cutie_dog->owner = malloc(sizeof(char) * own_l); 38 | 39 | if (cutie_dog->owner == NULL) 40 | { 41 | free(cutie_dog->name); 42 | free(cutie_dog); 43 | return (NULL); 44 | } 45 | 46 | cutie_dog->name = _strcpy(cutie_dog->name, name); 47 | cutie_dog->owner = _strcpy(cutie_dog->owner, owner); 48 | cutie_dog->age = age; 49 | } 50 | 51 | return (cutie_dog); 52 | } 53 | 54 | /** 55 | * _strlen - Returns the length of a string 56 | * @s: String to count 57 | * 58 | * Return: String length 59 | */ 60 | int _strlen(char *s) 61 | { 62 | int c = 0; 63 | 64 | for (; *s != '\0'; s++) 65 | { 66 | c++; 67 | } 68 | 69 | return (c); 70 | } 71 | 72 | /** 73 | * _strcpy - Copy a string 74 | * @dest: Destination value 75 | * @src: Source value 76 | * 77 | * Return: the pointer to dest 78 | */ 79 | char *_strcpy(char *dest, char *src) 80 | { 81 | int i; 82 | 83 | for (i = 0; src[i] != '\0'; i++) 84 | { 85 | dest[i] = src[i]; 86 | } 87 | 88 | dest[i++] = '\0'; 89 | 90 | return (dest); 91 | } 92 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * free_dog - ... 6 | * @d: ... 7 | * 8 | * Return: ... 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d != NULL) 13 | { 14 | free(d->owner); 15 | free(d->name); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # c structures and typedef 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _HEADER_ 2 | #define _HEADER_ 3 | 4 | /** 5 | * struct dog - Dog attributes 6 | * @name: The name of the dog. 7 | * @age: The age of the dog. 8 | * @owner: The owner of the dog. 9 | * 10 | * Description: The attributes of a dog. 11 | */ 12 | struct dog 13 | { 14 | char *name; 15 | float age; 16 | char *owner; 17 | }; 18 | 19 | /** 20 | * dog_t - Typedef for dog structure 21 | */ 22 | typedef struct dog dog_t; 23 | 24 | void init_dog(struct dog *d, char *name, float age, char *owner); 25 | void print_dog(struct dog *d); 26 | dog_t *new_dog(char *name, float age, char *owner); 27 | void free_dog(dog_t *d); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | #include 3 | /** 4 | * print_name - prints a name 5 | * @name: person'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 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | /** 3 | * array_iterator - execute function on each element 4 | * of an array 5 | * @array: given array 6 | * @size: size of the array 7 | * @action: pointer to function 8 | * Return: void 9 | */ 10 | void array_iterator(int *array, size_t size, void (*action)(int)) 11 | { 12 | int i; 13 | 14 | if (array == NULL || action == NULL) 15 | return; 16 | 17 | for (i = 0; i < (int) size; i++) 18 | { 19 | action(array[i]); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * print_opcodes - print the opcodes of this program 6 | * @a: address of the main function 7 | * @n: number of bytes to print 8 | * 9 | * Return: void 10 | */ 11 | void print_opcodes(char *a, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | printf("%.2hhx", a[i]); 18 | if (i < 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 | * 30 | * Return: O 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 | * int_index - searches for an integer 4 | * @array: array with data 5 | * @size: size of the array 6 | * @cmp: function pointer 7 | * Return: count of integer 8 | */ 9 | int int_index(int *array, int size, int (*cmp)(int)) 10 | { 11 | int i; 12 | 13 | if (size <= 0) 14 | return (-1); 15 | if (array == NULL || cmp == NULL) 16 | return (-1); 17 | 18 | for (i = 0; i < size; i++) 19 | { 20 | if (cmp(array[i]) == 1) 21 | return (i); 22 | } 23 | return (-1); 24 | } 25 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _HEADER_ 2 | #define _HEADER_ 3 | 4 | /** 5 | * struct op - Struct op 6 | * 7 | * @op: The operator 8 | * @f: The function associated 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 | * 9 | * Return: ... 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 i = 0; 22 | 23 | while (i < 5) 24 | { 25 | if (strcmp(s, ops[i].op) == 0) 26 | return (ops[i].f); 27 | i++; 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 | * 10 | * Return: ... 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 | * 10 | * Return: ... 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 | * 22 | * Return: ... 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 | * 34 | * Return: ... 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 | * 46 | * Return: ... 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 | * 64 | * Return: ... 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. C - Function pointers -------------------------------------------------------------------------------- /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-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | 13 | sum = sum_them_all(2, 98, 1024); 14 | printf("%d\n", sum); 15 | sum = sum_them_all(4, 98, 1024, 402, -1024); 16 | printf("%d\n", sum); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * sum_them_all - sum of all its parameters 6 | * @n: n args 7 | * Return: sum or if n == 0, return 0 8 | */ 9 | int sum_them_all(const unsigned int n, ...) 10 | { 11 | int sum = 0; 12 | unsigned int i; 13 | va_list arguments; 14 | 15 | if (n) 16 | { 17 | va_start(arguments, n); 18 | for (i = 0; i < n; i++) 19 | { 20 | sum += va_arg(arguments, int); 21 | } 22 | va_end(arguments); 23 | } 24 | return (sum); 25 | } 26 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | /** 5 | * print_numbers - prints numbers & new line at the end 6 | * @separator: string 7 | * @n: number of elements 8 | * Return: void 9 | */ 10 | void print_numbers(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list ap; 13 | unsigned int i; 14 | 15 | va_start(ap, n); 16 | 17 | for (i = 0; i < n; i++) 18 | { 19 | printf("%d", va_arg(ap, int)); 20 | 21 | if (separator != NULL && i != (n - 1)) 22 | { 23 | printf("%s", separator); 24 | } 25 | } 26 | putchar('\n'); 27 | va_end(ap); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /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: The quantity of numbers 7 | * @...: The list of numbers 8 | * 9 | * Return: Nothing 10 | */ 11 | void print_strings(const char *separator, const unsigned int n, ...) 12 | { 13 | va_list valist; 14 | unsigned int i; 15 | char *p; 16 | 17 | va_start(valist, n); 18 | 19 | for (i = 0; i < n; i++) 20 | { 21 | if (separator != NULL && i != 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-print_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "variadic_functions.h" 4 | 5 | /** 6 | * print_all - print char, integer, float and string 7 | * @format: format 8 | */ 9 | void print_all(const char * const format, ...) 10 | { 11 | va_list list; 12 | unsigned int j = 0, start = 0; 13 | char *p; 14 | 15 | va_start(list, format); 16 | while (format && format[j] != '\0') 17 | { 18 | switch (format[j]) 19 | { case 'c': 20 | switch (start) 21 | { case 1: printf(", "); } 22 | start = 1; 23 | printf("%c", va_arg(list, int)); 24 | break; 25 | case 'i': 26 | switch (start) 27 | { case 1: printf(", "); } 28 | start = 1; 29 | printf("%i", va_arg(list, int)); 30 | break; 31 | case 'f': 32 | switch (start) 33 | { case 1: printf(", "); } 34 | start = 1; 35 | printf("%f", va_arg(list, double)); 36 | break; 37 | case's': 38 | switch (start) 39 | { case 1: printf(", "); } 40 | start = 1; 41 | p = va_arg(list, char*); 42 | if (p) 43 | { printf("%s", p); 44 | break; } 45 | printf("%p", p); 46 | break; } 47 | j++; 48 | } 49 | printf("\n"); 50 | va_end(list); 51 | } 52 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | CREATED README.md FILe" 2 | -------------------------------------------------------------------------------- /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 | * struct p - checker struct 13 | * @p: possibility 14 | * @f: Function to be used 15 | */ 16 | typedef struct p 17 | { 18 | char *p; 19 | void (*f)(va_list); 20 | } checker; 21 | 22 | void print_c(va_list); 23 | void print_i(va_list); 24 | void print_f(va_list); 25 | void print_s(va_list); 26 | 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ int main(void) { 10 | list_t *head; 11 | list_t *new; 12 | list_t hello = {"World", 5, NULL}; 13 | size_t n; 14 | head = &hello; 15 | new = malloc(sizeof(list_t)); 16 | if (new == NULL) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | new->str = strdup("Hello"); 22 | new->len = 5; 23 | new->next = head; 24 | head = new; 25 | n = print_list(head); 26 | printf("-> %lu elements\n", n); 27 | printf("\n"); 28 | free(new->str); 29 | new->str = NULL; 30 | n = print_list(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (0); 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * print_list - prints all the elements of a linked list 6 | * @h: pointer to the list_t list to print 7 | * 8 | * Return: the number of nodes printed 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 | 27 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - Calculate 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 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ int main(void) { 10 | list_t *head; 11 | list_t *new; 12 | list_t hello = {"World", 5, NULL}; 13 | size_t n; 14 | head = &hello; 15 | new = malloc(sizeof(list_t)); 16 | if (new == NULL) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | new->str = strdup("Hello"); 22 | new->len = 5; 23 | new->next = head; 24 | head = new; 25 | n = list_len(head); 26 | printf("-> %lu elements\n", n); 27 | free(new->str); 28 | free(new); 29 | return (0); 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void first(void) __attribute__ ((constructor)); 4 | 5 | /** 6 | * first - prints a sentence before the main 7 | * function is executed 8 | */ 9 | void first(void) 10 | { 11 | printf("You're beat! and yet, you must allow,\n"); 12 | printf("I bore my house upon my back!\n"); 13 | } 14 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - check the code for Holberton School students. 4 | * 5 | * Return: Always 0. 6 | */ int main(void) { 7 | printf("(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.)\n"); 8 | return (0); 9 | } 10 | 11 | 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/101-hello_holberton.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x12-singly_linked_lists/101-hello_holberton.o -------------------------------------------------------------------------------- /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 at the beginning of a linked list 7 | * @head: double pointer to the list_t list 8 | * @str: new string to add in the node 9 | * 10 | * Return: the address of the new element, or NULL if it fails 11 | */ 12 | list_t *add_node(list_t **head, const char *str) 13 | { 14 | list_t *new; 15 | unsigned int len = 0; 16 | 17 | while (str[len]) 18 | len++; 19 | 20 | new = malloc(sizeof(list_t)); 21 | if (!new) 22 | return (NULL); 23 | 24 | new->str = strdup(str); 25 | new->len = len; 26 | new->next = (*head); 27 | (*head) = new; 28 | 29 | return (*head); 30 | } 31 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ int main(void) { 10 | list_t *head; 11 | head = NULL; 12 | add_node(&head, "Alexandro"); 13 | add_node(&head, "Asaia"); 14 | add_node(&head, "Augustin"); 15 | add_node(&head, "Bennett"); 16 | add_node(&head, "Bilal"); 17 | add_node(&head, "Chandler"); 18 | add_node(&head, "Damian"); 19 | add_node(&head, "Daniel"); 20 | add_node(&head, "Dora"); 21 | add_node(&head, "Electra"); 22 | add_node(&head, "Gloria"); 23 | add_node(&head, "Joe"); 24 | add_node(&head, "John"); 25 | add_node(&head, "John"); 26 | add_node(&head, "Josquin"); 27 | add_node(&head, "Kris"); 28 | add_node(&head, "Marine"); 29 | add_node(&head, "Mason"); 30 | add_node(&head, "Praylin"); 31 | add_node(&head, "Rick"); 32 | add_node(&head, "Rick"); 33 | add_node(&head, "Rona"); 34 | add_node(&head, "Siphan"); 35 | add_node(&head, "Sravanthi"); 36 | add_node(&head, "Steven"); 37 | add_node(&head, "Tasneem"); 38 | add_node(&head, "William"); 39 | add_node(&head, "Zee"); 40 | print_list(head); 41 | return (0); 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node_end - Add a new node at the end of a list. 4 | * @head: Address of the first node of a list. 5 | * @str: Address of the string to insert into the new node. 6 | * Return: Address of the new node. 7 | **/ 8 | 9 | list_t *add_node_end(list_t **head, const char *str) 10 | { 11 | list_t *temp, *temp2; 12 | unsigned int length = 0; 13 | 14 | if (str == NULL) 15 | return (NULL); 16 | 17 | temp = malloc(sizeof(list_t)); 18 | if (temp == NULL) 19 | return (NULL); 20 | 21 | temp->str = strdup(str); 22 | if (temp->str == NULL) 23 | { 24 | free(temp); 25 | return (NULL); 26 | } 27 | while (str[length]) 28 | length++; 29 | temp->len = length; 30 | temp->next = NULL; 31 | 32 | if (*head == NULL) 33 | { 34 | *head = temp; 35 | return (temp); 36 | } 37 | 38 | temp2 = *head; 39 | while (temp2->next) 40 | temp2 = temp2->next; 41 | temp2->next = temp; 42 | return (temp); 43 | } 44 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | /** 6 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ int main(void) { 10 | list_t *head; 11 | head = NULL; 12 | add_node_end(&head, "Anne"); 13 | add_node_end(&head, "Colton"); 14 | add_node_end(&head, "Corbin"); 15 | add_node_end(&head, "Daniel"); 16 | add_node_end(&head, "Danton"); 17 | add_node_end(&head, "David"); 18 | add_node_end(&head, "Gary"); 19 | add_node_end(&head, "Holden"); 20 | add_node_end(&head, "Ian"); 21 | add_node_end(&head, "Ian"); 22 | add_node_end(&head, "Jay"); 23 | add_node_end(&head, "Jennie"); 24 | add_node_end(&head, "Jimmy"); 25 | add_node_end(&head, "Justin"); 26 | add_node_end(&head, "Kalson"); 27 | add_node_end(&head, "Kina"); 28 | add_node_end(&head, "Matthew"); 29 | add_node_end(&head, "Max"); 30 | add_node_end(&head, "Michael"); 31 | add_node_end(&head, "Ntuj"); 32 | add_node_end(&head, "Philip"); 33 | add_node_end(&head, "Richard"); 34 | add_node_end(&head, "Samantha"); 35 | add_node_end(&head, "Stuart"); 36 | add_node_end(&head, "Swati"); 37 | add_node_end(&head, "Timothy"); 38 | add_node_end(&head, "Victor"); 39 | add_node_end(&head, "Walton"); 40 | print_list(head); 41 | return (0); 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * free_list - frees a linked list 6 | * @head: list_t list to be freed 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 | * main - check the code for Holberton School students. 7 | * 8 | * Return: Always 0. 9 | */ int main(void) { 10 | list_t *head; 11 | head = NULL; 12 | add_node_end(&head, "Jennie"); 13 | add_node_end(&head, "&"); 14 | add_node_end(&head, "Jay"); 15 | add_node_end(&head, "love"); 16 | add_node_end(&head, "asm"); 17 | print_list(head); 18 | free_list(head); 19 | head = NULL; 20 | return (0); 21 | } 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # ALX - Program 2 | 3 | ------------ 4 | 5 | ## C - Programming 6 | 7 | ------------ 8 | 9 | ### 0x12-SINGLY_LINKED_LISTS 10 | 11 | The code must be compiled using 12 | 13 | 14 | * gcc -Wall -Werror -Wextra -pedantic *.c** 15 | 16 | ------------ 17 | 18 | Below are the questions 19 | 20 | #### 0-print_list.c 21 | 22 | It is a function that prints all the elements of a list_t list. 23 | 24 | ------------ 25 | 26 | #### 1-list_len.c 27 | 28 | It is a a function that returns the number of elements in a linked list_t list. 29 | 30 | ------------ 31 | #### 2-add_node.c 32 | 33 | It is a function that adds a new node at the beginning of a list_t list. 34 | 35 | ------------ 36 | 37 | #### 3-add_node_end.c 38 | It is a function that adds a new node at the end of a list_t list. 39 | 40 | ------------ 41 | 42 | #### 4-free_list.c 43 | It is a function that frees a list_t list. 44 | 45 | ------------ 46 | #### 100-first.c 47 | It`s a function that prints a sentence before the main function is executed. 48 | 49 | 50 | ------------ 51 | #### 101-hello_holberton.asm 52 | It`s 64-bit program in assembly that prints "Hello, Holberton", followed by a new line. 53 | 54 | ------------ 55 | 56 | #### lists.h 57 | it`s an header file containing the definition of the list_t struct and all the prototypes of the above functions. 58 | 59 | 60 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x12-singly_linked_lists/a -------------------------------------------------------------------------------- /0x12-singly_linked_lists/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x12-singly_linked_lists/b -------------------------------------------------------------------------------- /0x12-singly_linked_lists/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x12-singly_linked_lists/c -------------------------------------------------------------------------------- /0x12-singly_linked_lists/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x12-singly_linked_lists/d -------------------------------------------------------------------------------- /0x12-singly_linked_lists/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x12-singly_linked_lists/e -------------------------------------------------------------------------------- /0x12-singly_linked_lists/first: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x12-singly_linked_lists/first -------------------------------------------------------------------------------- /0x12-singly_linked_lists/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x12-singly_linked_lists/hello -------------------------------------------------------------------------------- /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/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "stdio.h": "c" 4 | } 5 | } -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_listint - Print elements of a singly 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 *tp; 13 | unsigned int cntr = 0; 14 | 15 | tp = h; 16 | while (tp) 17 | { 18 | printf("%d\n", tp->n); 19 | cntr++; 20 | tp = tp->next; 21 | } 22 | return (cntr); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - Calculate the number of elements. 5 | * @h: Pointer to a list. 6 | * Return: Integer. 7 | **/ 8 | 9 | size_t listint_len(const listint_t *h) 10 | { 11 | const listint_t *tp; 12 | unsigned int cnr = 0; 13 | 14 | tp = h; 15 | while (tp) 16 | { 17 | cnr++; 18 | tp = tp->next; 19 | } 20 | return (cnr); 21 | } 22 | -------------------------------------------------------------------------------- /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 at a given positiion. 6 | * @head: First node address. 7 | * @index: Position of the node to delete. 8 | * Return: If success (1). 9 | **/ 10 | 11 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 12 | { 13 | unsigned int i; 14 | listint_t *current, *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 | current = *head; 26 | for (i = 0; i < index - 1; i++) 27 | { 28 | if (current->next == NULL) 29 | return (-1); 30 | current = current->next; 31 | } 32 | next = current->next; 33 | current->next = next->next; 34 | free(next); 35 | return (1); 36 | } 37 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list 5 | * @head: pointer to the first node in the list 6 | * 7 | * Return: pointer to the first node in the new list 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *prev = NULL; 12 | listint_t *next = NULL; 13 | 14 | while (*head) 15 | { 16 | next = (*head)->next; 17 | (*head)->next = prev; 18 | prev = *head; 19 | *head = next; 20 | } 21 | 22 | *head = prev; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /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 for Holberton School students. 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 - prints a linked list, safely 5 | * @head: list of type listint_t to print 6 | * 7 | * Return: number of nodes in the list 8 | */ 9 | size_t print_listint_safe(const listint_t *head) 10 | { 11 | size_t num = 0; 12 | long int diff; 13 | 14 | while (head) 15 | { 16 | diff = head - head->next; 17 | num++; 18 | printf("[%p] %d\n", (void *)head, head->n); 19 | if (diff > 0) 20 | head = head->next; 21 | else 22 | { 23 | printf("-> [%p] %d\n", (void *)head->next, head->next->n); 24 | break; 25 | } 26 | } 27 | 28 | return (num); 29 | } 30 | -------------------------------------------------------------------------------- /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 | * 7 | * Return: number of elements in the freed list 8 | */ 9 | size_t free_listint_safe(listint_t **h) 10 | { 11 | size_t len = 0; 12 | int diff; 13 | listint_t *temp; 14 | 15 | if (!h || !*h) 16 | return (0); 17 | 18 | while (*h) 19 | { 20 | diff = *h - (*h)->next; 21 | if (diff > 0) 22 | { 23 | temp = (*h)->next; 24 | *h = temp; 25 | len++; 26 | } 27 | else 28 | { 29 | *h = NULL; 30 | len++; 31 | break; 32 | } 33 | } 34 | 35 | *h = NULL; 36 | 37 | return (len); 38 | } 39 | -------------------------------------------------------------------------------- /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 for Holberton School students. 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 in a linked list 5 | * @head: linked list to search for 6 | * 7 | * Return: address of the node where the loop starts, or NULL 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *slow = head; 12 | listint_t *fast = head; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | while (slow && fast && fast->next) 18 | { 19 | fast = fast->next->next; 20 | slow = slow->next; 21 | if (fast == slow) 22 | { 23 | slow = head; 24 | while (slow != fast) 25 | { 26 | slow = slow->next; 27 | fast = fast->next; 28 | } 29 | return (fast); 30 | } 31 | } 32 | 33 | return (NULL); 34 | } 35 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_nodeint - Add a new node at the beginning of a list. 6 | * @head: Address of the first node of a list. 7 | * @n: Integer to insert into the new node. 8 | * Return: Address of the new node. 9 | **/ 10 | 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *tp; 14 | 15 | tp = malloc(sizeof(listint_t)); 16 | if (tp == NULL) 17 | return (NULL); 18 | 19 | tp->n = n; 20 | tp->next = *head; 21 | *head = tp; 22 | return (*head); 23 | } 24 | -------------------------------------------------------------------------------- /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 of a list. 6 | * @head: Address of the first node of a list. 7 | * @n: Integer to insert in 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 *temp, *temp2; 14 | 15 | temp = malloc(sizeof(listint_t)); 16 | if (temp == NULL) 17 | return (NULL); 18 | 19 | temp->n = n; 20 | temp->next = NULL; 21 | 22 | if (*head == NULL) 23 | { 24 | *head = temp; 25 | return (temp); 26 | } 27 | 28 | temp2 = *head; 29 | while (temp2->next) 30 | temp2 = temp2->next; 31 | temp2->next = temp; 32 | return (temp); 33 | } 34 | -------------------------------------------------------------------------------- /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 of a list. 7 | **/ 8 | 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *tp, *tp2; 12 | 13 | tp = head; 14 | while (tp != NULL) 15 | { 16 | tp2 = tp->next; 17 | free(tp); 18 | tp = tp2; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /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 *temp; 12 | 13 | if (head == NULL) 14 | return; 15 | while (*head != NULL) 16 | { 17 | temp = (*head)->next; 18 | free(*head); 19 | *head = temp; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /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 singly linked list. 6 | * @head: Pointer to a list. 7 | * Return: Integer if success. 8 | **/ 9 | 10 | int pop_listint(listint_t **head) 11 | { 12 | listint_t *tp; 13 | int my_data; 14 | 15 | if (*head == NULL) 16 | return (0); 17 | 18 | tp = *head; 19 | *head = tp->next; 20 | my_data = tp->n; 21 | free(tp); 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 a list. 6 | * @head: Address of the first node in a list. 7 | * @index: Position of a the node to find (starting from 0). 8 | * Return: Node address. 9 | **/ 10 | 11 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 12 | { 13 | unsigned int y = 0; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | for (y = 0; y < index; y++) 18 | { 19 | head = head->next; 20 | if (head == NULL) 21 | return (NULL); 22 | } 23 | return (head); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * sum_listint - Sum all of the data (n) in a list. 6 | * @head: Address of the first node of the list. 7 | * Return: Integer. 8 | **/ 9 | 10 | int sum_listint(listint_t *head) 11 | { 12 | int total = 0; 13 | 14 | while (head != NULL) 15 | { 16 | total += head->n; 17 | head = head->next; 18 | } 19 | return (total); 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 at a given positiion. 6 | * @head: First node address. 7 | * @idx: Position of the new node to be inserted in. 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, *temp; 15 | unsigned int i = 0; 16 | 17 | if (*head == NULL && idx != 0) 18 | return (NULL); 19 | if (idx != 0) 20 | { 21 | temp = *head; 22 | for (; i < idx - 1 && temp != NULL; i++) 23 | temp = temp->next; 24 | if (temp == 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 = temp->next; 38 | temp->next = new_node; 39 | return (new_node); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | ### C-Programming 2 | 3 | ----------------- 4 | 5 | ### MORE SINGLY LINKED LISTS 6 | 7 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /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 the next node 9 | * 10 | * Description: singly linked list node structure 11 | * for Holberton project 12 | */ 13 | typedef struct listint_s 14 | { 15 | int n; 16 | struct listint_s *next; 17 | } listint_t; 18 | 19 | size_t print_listint(const listint_t *h); 20 | size_t listint_len(const listint_t *h); 21 | listint_t *add_nodeint(listint_t **head, const int n); 22 | listint_t *add_nodeint_end(listint_t **head, const int n); 23 | void free_listint(listint_t *head); 24 | void free_listint2(listint_t **head); 25 | int pop_listint(listint_t **head); 26 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 27 | int sum_listint(listint_t *head); 28 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 29 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 30 | listint_t *reverse_listint(listint_t **head); 31 | size_t print_listint_safe(const listint_t *head); 32 | size_t free_listint_safe(listint_t **h); 33 | listint_t *find_listint_loop(listint_t *head); 34 | #endif 35 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x13-more_singly_linked_lists/m -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/n: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/0x13-more_singly_linked_lists/n -------------------------------------------------------------------------------- /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 of a number 5 | * @n: parameter 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 2 | -------------------------------------------------------------------------------- /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 | 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | if (index > sizeof(n) * 8) 14 | return (-1); 15 | *n &= ~(1 << index); 16 | return (1); 17 | } 18 | -------------------------------------------------------------------------------- /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 | # Its a 14th project of ALX-Low_level_programming in c 4 | 5 | ----------------------------------------- 6 | 7 | Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer programming tasks that require bit manipulation include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization. 8 | 9 | ------------- 10 | ## The hexadecimal number system 11 | 12 | Digit | Binary |Examples | 13 | |----------------|-------------------------------|-----------------------------| 14 | | `0` | 0000 | y | 15 | | `1` | 0001 | y | 16 | | `2` | 0010 | y | 17 | | `3` | 0011 | y | 18 | | `4` | 0100 | y | 19 | | `5` | 0101 | y | 20 | | `6` | 0110 | y | 21 | | `7` | 0111 | y | 22 | | `8` | 1000 | y | 23 | | `9` | 1001 | y | 24 | | `a` | 1010 | y | 25 | | `b` | 1011 | y | 26 | | `c` | 1100 | y | 27 | | `e` | 1101 | y | 28 | | `f` | 1111 | y | 29 | 30 | 31 | ------------ 32 | # Hexadecimal is assigned usign "0x" notation. 33 | 34 | ...... ---- 35 | #### Bitwise operation used 36 | operator | meaning | comment| 37 | `&` | The & operator is the bitwise logical and operator, which forms the logical and of pairs of bits taken from two numbers | `(1010)&(0110) = (0010) = 0x2` 38 | `|` | operator forms the bitwise or `x|y` | `(1010)|(0110) = (1110) = 0xe` 39 | `^`| operator forms the exclusive or of bits | `(1010)^(0110) = (1100) = 0xc` 40 | `~`| operator is the bitwise negation operator |`0xb = 1011, ~x evaluates to 0100` 41 | 42 | # working with individual bits 43 | 44 | ## == equal operators 45 | If we have an unsigned short variable x with the value of 0xa8 = 10101000 and we want to determine whether or not the last bit is a 0, we would use the test x&0x01 == 0, 46 | ## | or operator 47 | To set bits in a number to particular values we use either | or & depending on whether we want to set that bit to be a 1 or a 0. For example, to set the fourth bit in an eight bit variable y to be a 1 we would use y = y|0x10 48 | 49 | ## & and operator 50 | To set the fourth bit of y to 0 we would use & with the mask 0xef = 11101111: y = y&0xef 51 | 52 | ## ~ negation 53 | Another way to do this is to use the ~ operator to help construct the mask: y = y&(~0x10) 54 | -------------------------------------------------------------------------------- /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: -------------------------------------------------------------------------------- 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 | int fd; 11 | int i, y; 12 | char *buf; 13 | if (!filename) 14 | return (0); 15 | fd = open(filename, O_RDONLY); 16 | if (fd < 0) 17 | return (0); 18 | buf = malloc(sizeof(char) * letters); 19 | if (!buf) 20 | return (0); 21 | i = read(fd, buf, letters); 22 | if (i < 0) 23 | { 24 | free(buf); 25 | return (0); 26 | } 27 | buf[i] = '\0'; 28 | close(fd); 29 | y = write(STDOUT_FILENO, buf, i); 30 | if (y < 0) 31 | { 32 | free(buf); 33 | return (0); 34 | } 35 | free(buf); 36 | return (y); 37 | } 38 | -------------------------------------------------------------------------------- /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/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/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/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 -------------------------------------------------------------------------------- /0x15-file_io/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | ssize_t n; 13 | 14 | if (ac != 2) 15 | { 16 | dprintf(2, "Usage: %s filename\n", av[0]); 17 | exit(1); 18 | } 19 | n = read_textfile(av[1], 114); 20 | printf("\n(printed chars: %li)\n", n); 21 | n = read_textfile(av[1], 1024); 22 | printf("\n(printed chars: %li)\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x15-file_io/tests/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | 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/tests/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | 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/tests/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/tests/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 | -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /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/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - 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/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/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/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/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/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 | # low level programming 2 | # alx-low_level_programming 3 | -------------------------------------------------------------------------------- /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 */ -------------------------------------------------------------------------------- /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/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/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 | 5 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/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 | # low level programming 2 | # alx-low_level_programming 3 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abela12/alx-low_level_programming/ea9ac07899c202763bc47d888e24ded3d39202c8/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/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/README.md: -------------------------------------------------------------------------------- 1 | # Hash Tables 2 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all : main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | all : $(SRC) 4 | $(CC) $(SRC) -o school 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | .PHONY : all clean oclean fclean re 7 | 8 | all : m.h $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean : 12 | $(RM) *~ $(NAME) 13 | 14 | oclean : 15 | $(RM) $(OBJ) 16 | 17 | fclean : clean oclean 18 | 19 | re : oclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | all : $(OBJ) 6 | $(CC) $(OBJ) -o $(NAME) 7 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all : $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean : 11 | $(RM) *~ $(NAME) 12 | 13 | oclean : 14 | $(RM) $(OBJ) 15 | 16 | fclean : clean oclean 17 | 18 | re : oclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all : $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean : 12 | $(RM) *~ $(NAME) 13 | 14 | oclean : 15 | $(RM) $(OBJ) 16 | 17 | fclean : clean oclean 18 | 19 | re : oclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Module that calculates the perimeter of an island in a grid.""" 3 | 4 | 5 | def num_water_neighbors(grid, i, j): 6 | """Returns the number of water neighbors a cell has in a grid.""" 7 | 8 | num = 0 9 | 10 | if i <= 0 or not grid[i - 1][j]: 11 | num += 1 12 | if j <= 0 or not grid[i][j - 1]: 13 | num += 1 14 | if j >= len(grid[i]) - 1 or not grid[i][j + 1]: 15 | num += 1 16 | if i >= len(grid) - 1 or not grid[i + 1][j]: 17 | num += 1 18 | 19 | return num 20 | 21 | 22 | def island_perimeter(grid): 23 | """Returns the perimeter of the island in grid.""" 24 | 25 | perim = 0 26 | for i in range(len(grid)): 27 | for j in range(len(grid[i])): 28 | if grid[i][j]: 29 | perim += num_water_neighbors(grid, i, j) 30 | 31 | return perim 32 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # 0x1C. C - Makefiles 2 | 3 | ## Table of contents 4 | Files | Description 5 | ----- | ----------- 6 | [0-Makefile](./0-Makefile) | First Makefile 7 | [1-Makefile](./1-Makefile) | Makefile based on the previous one 8 | [2-Makefile](./2-Makefile) | Makefile based on the previous one 9 | [3-Makefile](./3-Makefile) | Makefile based on the previous one 10 | [4-Makefile](./4-Makefile) | Makefile based on the previous one 11 | [5-island_perimeter.py](./5-island_perimeter.py) | Python function def island_perimeter(grid): that returns the perimeter of the island described in the grid list 12 | [100-Makefile](./100-Makefile) | Makefile based on the previous one 13 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 5 | * integers using the Linear search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: Always EXIT_SUCCESS 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < (int)size; i++) 20 | { 21 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 22 | if (value == array[i]) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * recursive_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int recursive_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | return ((int)half); 33 | 34 | if (value < array[half]) 35 | return (recursive_search(array, half, value)); 36 | 37 | half++; 38 | 39 | return (recursive_search(array + half, size - half, value) + half); 40 | } 41 | 42 | /** 43 | * binary_search - calls to binary_search to return 44 | * the index of the number 45 | * 46 | * @array: input array 47 | * @size: size of the array 48 | * @value: value to search in 49 | * Return: index of the number 50 | */ 51 | int binary_search(int *array, size_t size, int value) 52 | { 53 | int index; 54 | 55 | index = recursive_search(array, size, value); 56 | 57 | if (index >= 0 && array[index] != value) 58 | return (-1); 59 | 60 | return (index); 61 | } 62 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | int index, m, k, prev; 16 | 17 | if (array == NULL || size == 0) 18 | return (-1); 19 | 20 | m = (int)sqrt((double)size); 21 | k = 0; 22 | prev = index = 0; 23 | 24 | do { 25 | printf("Value checked array[%d] = [%d]\n", index, array[index]); 26 | 27 | if (array[index] == value) 28 | return (index); 29 | k++; 30 | prev = index; 31 | index = k * m; 32 | } while (index < (int)size && array[index] < value); 33 | 34 | printf("Value found between indexes [%d] and [%d]\n", prev, index); 35 | 36 | for (; prev <= index && prev < (int)size; prev++) 37 | { 38 | printf("Value checked array[%d] = [%d]\n", prev, array[prev]); 39 | if (array[prev] == value) 40 | return (prev); 41 | } 42 | 43 | return (-1); 44 | } 45 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in an array of 5 | * integers using the Interpolation search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: index of the number 11 | */ 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t pos, low, high; 15 | double f; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | low = 0; 21 | high = size - 1; 22 | 23 | while (size) 24 | { 25 | f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]); 26 | pos = (size_t)(low + f); 27 | printf("Value checked array[%d]", (int)pos); 28 | 29 | if (pos >= size) 30 | { 31 | printf(" is out of range\n"); 32 | break; 33 | } 34 | else 35 | { 36 | printf(" = [%d]\n", array[pos]); 37 | } 38 | 39 | if (array[pos] == value) 40 | return ((int)pos); 41 | 42 | if (array[pos] < value) 43 | low = pos + 1; 44 | else 45 | high = pos - 1; 46 | 47 | if (low == high) 48 | break; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * recursive_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int recursive_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | return ((int)half); 33 | 34 | if (value < array[half]) 35 | return (recursive_search(array, half, value)); 36 | 37 | half++; 38 | 39 | return (recursive_search(array + half, size - half, value) + half); 40 | } 41 | 42 | /** 43 | * binary_search - calls to binary_search to return 44 | * the index of the number 45 | * 46 | * @array: input array 47 | * @size: size of the array 48 | * @value: value to search in 49 | * Return: index of the number 50 | */ 51 | int binary_search(int *array, size_t size, int value) 52 | { 53 | int index; 54 | 55 | index = recursive_search(array, size, value); 56 | 57 | if (index >= 0 && array[index] != value) 58 | return (-1); 59 | 60 | return (index); 61 | } 62 | 63 | /** 64 | * exponential_search - searches for a value in an array of 65 | * integers using the Exponential search algorithm 66 | * 67 | * @array: input array 68 | * @size: size of the array 69 | * @value: value to search in 70 | * Return: index of the number 71 | */ 72 | int exponential_search(int *array, size_t size, int value) 73 | { 74 | size_t index, next; 75 | int result; 76 | 77 | if (array == NULL) 78 | return (-1); 79 | 80 | if (array[0] == value) 81 | return (0); 82 | 83 | index = 1; 84 | 85 | while (array[index] < value && index < size) 86 | { 87 | printf("Value checked array[%d] = [%d]\n", (int)index, array[index]); 88 | index *= 2; 89 | } 90 | 91 | next = (index >= size) ? (size - 1) : index; 92 | 93 | index /= 2; 94 | 95 | printf("Value found between indexes [%d] and [%d]\n", (int)index, (int)next); 96 | 97 | result = binary_search(array + index, (next + 1) - index, value); 98 | 99 | if (result >= 0) 100 | result += index; 101 | 102 | return (result); 103 | } 104 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * rec_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int rec_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | { 33 | if (half > 0) 34 | return (rec_search(array, half + 1, value)); 35 | return ((int)half); 36 | } 37 | 38 | if (value < array[half]) 39 | return (rec_search(array, half + 1, value)); 40 | 41 | half++; 42 | return (rec_search(array + half, size - half, value) + half); 43 | } 44 | 45 | /** 46 | * advanced_binary - calls to rec_search to return 47 | * the index of the number 48 | * 49 | * @array: input array 50 | * @size: size of the array 51 | * @value: value to search in 52 | * Return: index of the number 53 | */ 54 | int advanced_binary(int *array, size_t size, int value) 55 | { 56 | int index; 57 | 58 | index = rec_search(array, size, value); 59 | 60 | if (index >= 0 && array[index] != value) 61 | return (-1); 62 | 63 | return (index); 64 | } 65 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @list: input list 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | listint_t *jump_list(listint_t *list, size_t size, int value) 14 | { 15 | size_t index, k, m; 16 | listint_t *prev; 17 | 18 | if (list == NULL || size == 0) 19 | return (NULL); 20 | 21 | m = (size_t)sqrt((double)size); 22 | index = 0; 23 | k = 0; 24 | 25 | do { 26 | prev = list; 27 | k++; 28 | index = k * m; 29 | 30 | while (list->next && list->index < index) 31 | list = list->next; 32 | 33 | if (list->next == NULL && index != list->index) 34 | index = list->index; 35 | 36 | printf("Value checked at index [%d] = [%d]\n", (int)index, list->n); 37 | 38 | } while (index < size && list->next && list->n < value); 39 | 40 | printf("Value found between indexes "); 41 | printf("[%d] and [%d]\n", (int)prev->index, (int)list->index); 42 | 43 | for (; prev && prev->index <= list->index; prev = prev->next) 44 | { 45 | printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n); 46 | if (prev->n == value) 47 | return (prev); 48 | } 49 | 50 | return (NULL); 51 | } 52 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - searches for a value in a skip list 5 | * 6 | * @list: input list 7 | * @value: value to search in 8 | * Return: index of the number 9 | */ 10 | skiplist_t *linear_skip(skiplist_t *list, int value) 11 | { 12 | skiplist_t *go; 13 | 14 | if (list == NULL) 15 | return (NULL); 16 | 17 | go = list; 18 | 19 | do { 20 | list = go; 21 | go = go->express; 22 | printf("Value checked at index "); 23 | printf("[%d] = [%d]\n", (int)go->index, go->n); 24 | } while (go->express && go->n < value); 25 | 26 | if (go->express == NULL) 27 | { 28 | list = go; 29 | while (go->next) 30 | go = go->next; 31 | } 32 | 33 | printf("Value found between indexes "); 34 | printf("[%d] and [%d]\n", (int)list->index, (int)go->index); 35 | 36 | while (list != go->next) 37 | { 38 | printf("Value checked at index [%d] = [%d]\n", (int)list->index, list->n); 39 | if (list->n == value) 40 | return (list); 41 | list = list->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Search Algorithms -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SORTING_ALGOS_H_ 2 | #define _SORTING_ALGOS_H_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * 10 | * @n: Integer 11 | * @index: Index of the node in the list 12 | * @next: Pointer to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct listint_s 18 | { 19 | int n; 20 | size_t index; 21 | struct listint_s *next; 22 | } listint_t; 23 | 24 | /** 25 | * struct skiplist_s - Singly linked list with an express lane 26 | * 27 | * @n: Integer 28 | * @index: Index of the node in the list 29 | * @next: Pointer to the next node 30 | * @express: Pointer to the next node in the express lane 31 | * 32 | * Description: singly linked list node structure with an express lane 33 | * for Holberton project 34 | */ 35 | typedef struct skiplist_s 36 | { 37 | int n; 38 | size_t index; 39 | struct skiplist_s *next; 40 | struct skiplist_s *express; 41 | } skiplist_t; 42 | 43 | int linear_search(int *array, size_t size, int value); 44 | int binary_search(int *array, size_t size, int value); 45 | int jump_search(int *array, size_t size, int value); 46 | int interpolation_search(int *array, size_t size, int value); 47 | int exponential_search(int *array, size_t size, int value); 48 | int advanced_binary(int *array, size_t size, int value); 49 | listint_t *jump_list(listint_t *list, size_t size, int value); 50 | skiplist_t *linear_skip(skiplist_t *list, int value); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # alx-low_level_programming 2 | # C programming 3 | --------------------------------------------------------------------------------