├── 0-positive_or_negative.c ├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.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 ├── _putchar.c └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── 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 └── 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 ├── liball.a ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── add ├── args ├── change ├── mul ├── mynameis └── nargs ├── 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 ├── .100-realloc.c.swp ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-main.c ├── 100-realloc ├── 100-realloc.c ├── 101-mul ├── 101-mul.c ├── 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 ├── calc └── function_pointers.h ├── 0x10-variadic_functions ├── 0-main.c ├── 0-sum_them_all.c ├── 1-main.c ├── 1-print_numbers.c ├── 2-main.c ├── 2-print_strings.c ├── 3-main.c ├── 3-print_all.c ├── README.md ├── a ├── b ├── c ├── d └── variadic_functions.h ├── 0x12-singly_linked_lists ├── .0-print_list.c.swp ├── 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 ├── 0-main.c ├── 0-print_listint.c ├── 1-listint_len.c ├── 1-main.c ├── 10-delete_nodeint.c ├── 10-main.c ├── 100-main.c ├── 100-reverse_listint.c ├── 101-main.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 102-main.c ├── 103-find_loop.c ├── 103-main.c ├── 2-add_nodeint.c ├── 2-main.c ├── 3-add_nodeint_end.c ├── 3-main.c ├── 4-free_listint.c ├── 4-main.c ├── 5-free_listint2.c ├── 5-main.c ├── 6-main.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 7-main.c ├── 8-main.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── 9-main.c ├── README.md ├── a ├── b ├── c ├── d ├── e ├── f ├── g ├── h ├── i ├── j ├── k ├── l ├── lists.h ├── m ├── n └── o ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 0-main.c ├── 1-main.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 100-main.c ├── 101-password ├── 2-get_bit.c ├── 2-main.c ├── 3-main.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 4-main.c ├── 5-flip_bits.c ├── 5-main.c ├── README.md ├── _putchar.c ├── a ├── b ├── c ├── d ├── e ├── f ├── h └── main.h ├── 0x15-file_io ├── 0-main.c ├── 0-read_textfile.c ├── 1-create_file.c ├── 1-main.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 2-main.c ├── 3-cp.c ├── Incitatous ├── README.md ├── Requiescat ├── a ├── b ├── c ├── cp ├── elf_header ├── hello ├── incitatous └── main.h ├── 0x17-doubly_linked_lists ├── 0-main.c ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 1-main.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 2-main.c ├── 3-add_dnodeint_end.c ├── 3-main.c ├── 4-free_dlistint.c ├── 4-main.c ├── 5-get_dnodeint.c ├── 5-main.c ├── 6-main.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 7-main.c ├── 8-delete_dnodeint.c ├── 8-main.c ├── README.md ├── keygen5 └── lists.h ├── 0x18-dynamic_libraries ├── .libdynamic.so.swp ├── 0-main.c ├── 0-main.o ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_win.sh ├── README.md ├── c-files │ ├── 0-isupper.c │ ├── 0-memset.c │ ├── 0-strcat.c │ ├── 1-isdigit.c │ ├── 1-memcpy.c │ ├── 1-strncat.c │ ├── 100-atoi.c │ ├── 2-strchr.c │ ├── 2-strlen.c │ ├── 2-strncpy.c │ ├── 3-islower.c │ ├── 3-puts.c │ ├── 3-strcmp.c │ ├── 3-strspn.c │ ├── 4-isalpha.c │ ├── 4-strpbrk.c │ ├── 5-strstr.c │ ├── 6-abs.c │ ├── 9-strcpy.c │ └── _putchar.c ├── libdynamic.so ├── main.h └── object-files │ ├── 0-isupper.o │ ├── 0-memset.o │ ├── 0-strcat.o │ ├── 1-isdigit.o │ ├── 1-memcpy.o │ ├── 1-strncat.o │ ├── 100-atoi.o │ ├── 2-strchr.o │ ├── 2-strlen.o │ ├── 2-strncpy.o │ ├── 3-islower.o │ ├── 3-puts.o │ ├── 3-strcmp.o │ ├── 3-strspn.o │ ├── 4-isalpha.o │ ├── 4-strpbrk.o │ ├── 5-strstr.o │ ├── 6-abs.o │ ├── 9-strcpy.o │ └── _putchar.o ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-main.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py └── README.md ├── 0x1E-search_algorithms ├── 0-linear ├── 0-linear.c ├── 1-binary ├── 1-binary.c ├── 1-main.c ├── 100-jump ├── 100-jump.c ├── 100-main.c ├── 101-O ├── 102-interpolation ├── 102-interpolation.c ├── 102-main.c ├── 103-exponential ├── 103-exponential.c ├── 103-main.c ├── 104-advanced_binary ├── 104-advanced_binary.c ├── 104-main.c ├── 105-jump_list.c ├── 105-main.c ├── 106-linear_skip.c ├── 106-main.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md ├── search_algos.h └── test │ └── 0-main.c └── README.md /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 | -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE >> c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main -Entry 6 | * Return: 1 7 | */ 8 | 9 | int main(void) 10 | { 11 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 12 | return (1); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -o cisfun $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - rain block 5 | * Return: Always 0 6 | */ 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 -Enter 5 | * Return: Always 0 6 | */ 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 5 | * Return: Always 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("Size of a char: %i byte(s)\n", sizeof(char)); 11 | printf("Size of an int: %i byte(s)\n", sizeof(int)); 12 | printf("Size of a long int: %i byte(s)\n", sizeof(long int)); 13 | printf("Size of a long long int: %i byte(s)\n", sizeof(long long int)); 14 | printf("Size of a float: %i byte(s)\n", sizeof(float)); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | thi is for 0x00-hello_world alx project -------------------------------------------------------------------------------- /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 | /** 3 | * main - prints all possible different combinations of digits 4 | * Return: Alway 0 5 | */ 6 | int main(void) 7 | { 8 | int ones = '0'; 9 | int tens = '0'; 10 | 11 | for (tens = '0'; tens <= '9'; tens++) 12 | { 13 | for (ones = '0'; ones <= '9'; ones++) 14 | { 15 | if (!((ones == tens) || (tens > ones))) 16 | { 17 | putchar(tens); 18 | putchar(ones); 19 | if (!(ones == '9' && tens == '8')) 20 | { 21 | putchar(','); 22 | putchar(' '); 23 | } 24 | } 25 | } 26 | } 27 | putchar('\n'); 28 | 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all possible different combinations of digits 4 | * Return: Alway 0 5 | */ 6 | int main(void) 7 | { 8 | int c; 9 | int d; 10 | int e = 0; 11 | 12 | while (e < 10) 13 | { 14 | d = 0; 15 | while (d < 10) 16 | { 17 | c = 0; 18 | while (c < 10) 19 | { 20 | if (c != d && d != e && e < d && d < c) 21 | { 22 | putchar('0' + e); 23 | putchar('0' + d); 24 | putchar('0' + c); 25 | 26 | if (c + d + e != 9 + 8 + 7) 27 | { 28 | putchar(','); 29 | putchar(' '); 30 | } 31 | } 32 | 33 | c++; 34 | } 35 | d++; 36 | } 37 | e++; 38 | } 39 | putchar('\n'); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - print all comination of single digits 5 | * Return: Always 0 6 | */ 7 | int main(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 100; i++) 12 | { 13 | for (j = 0; j < 100; j++) 14 | { 15 | if (i < j) 16 | { 17 | putchar((i / 10) + 48); 18 | putchar((i % 10) + 48); 19 | putchar(' '); 20 | putchar((j / 10) + 48); 21 | putchar((j % 10) + 48); 22 | if (i != 98 || j != 99) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | putchar('\n'); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #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 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | char letter; 10 | 11 | for (letter = 'z'; letter >= 'a'; letter--) 12 | putchar(letter); 13 | 14 | putchar('\n'); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print the numbers of base 16 in lowercase 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | char digit; 10 | 11 | for (digit = '0'; digit <= '9'; digit++) 12 | putchar(digit); 13 | 14 | for (digit = 'a'; digit <= 'f'; digit++) 15 | putchar(digit); 16 | 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /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 | this is my project -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Entry point _putchar 5 | * description: print out 6 | * Return: 0 (success) 7 | */ 8 | 9 | int main(void) 10 | { 11 | _putchar('_'); 12 | _putchar('p'); 13 | _putchar('u'); 14 | _putchar('t'); 15 | _putchar('c'); 16 | _putchar('h'); 17 | _putchar('a'); 18 | _putchar('r'); 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - prints the alpha in lower 5 | */ 6 | 7 | void print_alphabet(void) 8 | { 9 | char la; 10 | 11 | for (la = 'a'; la <= 'z'; la++) 12 | _putchar(la); 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - a function that adds two integers 5 | * and returns the result. 6 | * 7 | * @a: number one. 8 | * @b: number two. 9 | * 10 | * Return: Add of number one and number two. 11 | */ 12 | 13 | int add(int a, int b) 14 | { 15 | return (a + b); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Lists all the natural numbers below 1024 (excluded) 4 | * that are multiples of 3 or 5. 5 | * 6 | * Return: Always (0). 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i, sum; 12 | 13 | sum = 0; 14 | for (i = 0; i < 1024; i++) 15 | { 16 | if ((i % 3) == 0 || (i % 5) == 0) 17 | sum += i; 18 | } 19 | printf("%d\n", sum); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints the first 50 Fibonacci numbers, starting with 1 and 2, 4 | * 5 | * Return: Always (0). 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | unsigned long fib, prevFib, prev2Fib; 12 | 13 | prevFib = 1, prev2Fib = 0; 14 | 15 | for (i = 0; i < 50; i++) 16 | { 17 | fib = prevFib + prev2Fib; 18 | printf("%lu", fib); 19 | 20 | prev2Fib = prevFib; 21 | prevFib = fib; 22 | if (i == 49) 23 | printf("\n"); 24 | else 25 | printf(", "); 26 | } 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the sum of even-valued Fibonacci sequence 5 | * terms not exceeding 4000000. 6 | * 7 | * Return: Always 0. 8 | */ 9 | 10 | int main(void) 11 | { 12 | unsigned long fib, prevFib, prev2Fib; 13 | float sum; 14 | 15 | prevFib = 1; 16 | prev2Fib = 0; 17 | 18 | while (1) 19 | { 20 | fib = prevFib + prev2Fib; 21 | 22 | if (fib > 4000000) 23 | break; 24 | 25 | if ((fib % 2) == 0) 26 | sum += fib; 27 | prev2Fib = prevFib; 28 | prevFib = fib; 29 | 30 | } 31 | 32 | printf("%.0f\n", sum); 33 | 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_to_98 - prints all natural numbers 5 | * from n to 98, followed by a new line 6 | * 7 | * @n: input number. 8 | * 9 | * Return: no return. 10 | */ 11 | 12 | void print_to_98(int n) 13 | { 14 | if (n > 98) 15 | { 16 | for (; n > 98; n--) 17 | { 18 | printf("%d, ", n); 19 | } 20 | } 21 | else if (n < 98) 22 | { 23 | for (; n < 98; n++) 24 | { 25 | printf("%d, ", n); 26 | } 27 | } 28 | printf("%d\n", n); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Entry point 5 | * description: print 10x 6 | * Return: 0 7 | */ 8 | 9 | void print_alphabet_x10(void) 10 | { 11 | int ten; 12 | char la; 13 | 14 | for (ten = 0; ten <= 9; ten++) 15 | { 16 | for (la = 'a'; la <= 'z'; la++) 17 | { 18 | _putchar(la); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks if a charactor is lowercase 5 | * @c: the character 6 | * Return: 1 if letter is lowercase. 0 if not 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha -checks for alphabets 5 | * @c: character to be cheked 6 | * Return: 1 if character is a letter, 0 otherwise 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - prints the sing of a number 5 | * @n: the number of thr sign to be printed 6 | * Return: 1 if number is greater than zero 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 22 | { 23 | _putchar('-'); 24 | return (-1); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Compuyer the absolute value of an integer 5 | * @t: Ther integer to be computed 6 | * Return: The absolute value 7 | */ 8 | 9 | int _abs(int t) 10 | { 11 | if (t >= 0) 12 | return (t); 13 | else 14 | return (-t); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - prints the last digits 5 | * @n: the number 6 | * Return: value of the last digit 7 | */ 8 | 9 | int print_last_digit(int n) 10 | { 11 | int a; 12 | 13 | if (n < 0) 14 | n = -n; 15 | a = n % 10; 16 | if (a < 0) 17 | a = -a; 18 | _putchar(a + '0'); 19 | return (a); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the dataq 5 | * Return: always 0 6 | */ 7 | 8 | void jack_bauer(void) 9 | { 10 | int hour; 11 | int minute; 12 | 13 | for (hour = 0; hour <= 23; hour++) 14 | { 15 | for (minute = 0; minute <= 59; minute++) 16 | { 17 | _putchar((hour / 10) + '0'); 18 | _putchar((hour % 10) + '0'); 19 | _putchar(':'); 20 | _putchar((minute / 10) + '0'); 21 | _putchar((minute % 10) + '0'); 22 | _putchar('\n'); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /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 | * Returns- void 7 | */ 8 | 9 | void times_table(void) 10 | { 11 | 12 | int i, j, k; 13 | 14 | for (i = 0; i <= 9; i++) 15 | { 16 | for (j = 0; j <= 9; j++) 17 | { 18 | k = j * i; 19 | if (j == 0) 20 | { 21 | _putchar(k + '0'); 22 | } 23 | if (k < 10 && j != 0) 24 | { 25 | _putchar(','); 26 | _putchar(' '); 27 | _putchar(' '); 28 | _putchar(k + '0'); 29 | } 30 | else if (k >= 10) 31 | { 32 | _putchar(','); 33 | _putchar(' '); 34 | _putchar((k / 10) + '0'); 35 | _putchar((k % 10) + '0'); 36 | } 37 | } 38 | _putchar('\n'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | alx project for 0x02-functions_nested_loops -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - used to write char "d" 6 | * @c: The character to be print 7 | * Return: 1 in success & error on -1 8 | */ 9 | 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char); 5 | void print_alphabet(void); 6 | void print_alphabet_x10(void); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int print_sign(int n); 10 | int _abs(int); 11 | int print_last_digit(int); 12 | void jack_bauer(void); 13 | void times_table(void); 14 | int add(int, int); 15 | void print_to_98(int n); 16 | void print_times_table(int n); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /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 | 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | alx projext for 0x03-debugging -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | void positive_or_negative(int i); 6 | int largest_number(int a, int b, int c); 7 | void print_remaining_days(int month, int day, int year); 8 | int convert_day(int month, int day); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - checks for uppercase character 6 | * @c: Variable text 7 | * Return: Always 0. 8 | */ 9 | 10 | int _isupper(int c) 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | { 14 | return (1); 15 | } 16 | else 17 | { 18 | return (0); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - function that check if it's a digit 5 | * @c: takes in a character/digit 6 | * Return: 1 if digit and 0 if anything else 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle 5 | * @size: takes in a integer for size of triangle 6 | */ 7 | 8 | void print_triangle(int size) 9 | { 10 | int row, column; 11 | 12 | if (size <= 0) 13 | _putchar('\n'); 14 | for (row = 0; row < size; row++) 15 | { 16 | for (column = 0; column < size; column++) 17 | { 18 | if (column + 1 >= size - row) 19 | _putchar('#'); 20 | else 21 | _putchar(' '); 22 | } 23 | _putchar('\n'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry block 5 | * @void: no argument 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | long i, number = 612852475143; 12 | 13 | for (i = 2; i <= number; i++) 14 | { 15 | if (number % i == 0) 16 | { 17 | number = number / i; 18 | i--; 19 | } 20 | } 21 | 22 | printf("%lu\n", i); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - Prints an integer. 5 | * @n: integer to be printed. 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int num = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | num = -num; 16 | } 17 | 18 | if ((num / 10) > 0) 19 | print_number(num / 10); 20 | 21 | _putchar((num % 10) + '0'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * mul - function that multiplies two integers 6 | * @a: First integer 7 | * @b: Second integer 8 | * Return: mul. 9 | */ 10 | 11 | int mul(int a, int b) 12 | { 13 | return (a * b); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - function that prints the numbers, from 0 to 9. 5 | */ 6 | 7 | void print_numbers(void) 8 | { 9 | char c; 10 | 11 | for (c = '0'; c <= '9'; c++) 12 | _putchar(c); 13 | 14 | _putchar('\n'); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints the numbers except 2 and 4. 5 | */ 6 | 7 | void print_most_numbers(void) 8 | { 9 | char c; 10 | 11 | for (c = '0'; c <= '9'; c++) 12 | { 13 | if (c != '2' && c != '4') 14 | _putchar(c); 15 | } 16 | 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 10 times the numbers, from 0 to 14. 5 | */ 6 | 7 | void more_numbers(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | for (j = 0; j <= 14; j++) 14 | { 15 | if (j > 9) 16 | _putchar((j / 10) + '0'); 17 | _putchar((j % 10) + '0'); 18 | } 19 | 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - draws a straight line in the terminal 5 | * @n: number of times the character _ should be printed 6 | */ 7 | 8 | void print_line(int n) 9 | { 10 | if (n <= 0) 11 | { 12 | _putchar('\n'); 13 | } 14 | else 15 | { 16 | int i; 17 | 18 | for (i = 0; i < n; i++) 19 | { 20 | _putchar('_'); 21 | } 22 | 23 | _putchar('\n'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - draws a diagonal line 5 | * @n: takes in an integer 6 | */ 7 | 8 | void print_diagonal(int n) 9 | { 10 | int i, j; 11 | 12 | if (n <= 0) 13 | _putchar('\n'); 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | for (j = 0; j <= i; j++) 18 | { 19 | if (i == j) 20 | { 21 | _putchar('\\'); 22 | _putchar('\n'); 23 | } 24 | else 25 | _putchar(' '); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - function to print a square 5 | * @size: takes in the size of the square 6 | */ 7 | 8 | void print_square(int size) 9 | { 10 | int i, j; 11 | 12 | if (size <= 0) 13 | _putchar('\n'); 14 | 15 | for (i = 0; i < size; i++) 16 | { 17 | for (j = 0; j < size; j++) 18 | _putchar('#'); 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - entry block 5 | * @void: no argument 6 | * Return: 0 Success 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 1; i <= 100; i++) 14 | { 15 | if (i % 15 == 0) 16 | { 17 | printf("FizzBuzz "); 18 | } 19 | else if (i % 5 == 0) 20 | { 21 | if (i == 100) 22 | printf("Buzz"); 23 | else 24 | printf("Buzz "); 25 | } 26 | else if (i % 3 == 0) 27 | { 28 | printf("Fizz "); 29 | } 30 | else 31 | { 32 | printf("%d ", i); 33 | } 34 | } 35 | 36 | printf("\n"); 37 | 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | alx 0x04-more_functions_nested_loops project -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _isupper(int c); 5 | int _isdigit(int c); 6 | int mul(int a, int b); 7 | void print_numbers(void); 8 | int _putchar(char c); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - takes a pointer to a parameter and updates 5 | * @n: input integer 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 - Swaps the values of two integers 5 | * @a: first int 6 | * @b: second int 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | int tmp = *a; 12 | *a = *b; 13 | *b = tmp; 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * Return: the int converted from the string 7 | */ 8 | 9 | int _atoi(char *s) 10 | { 11 | int i, d, n, len, f, digit; 12 | 13 | i = 0; 14 | d = 0; 15 | n = 0; 16 | len = 0; 17 | f = 0; 18 | digit = 0; 19 | 20 | while (s[len] != '\0') 21 | len++; 22 | 23 | while (i < len && f == 0) 24 | { 25 | if (s[i] == '-') 26 | ++d; 27 | 28 | if (s[i] >= '0' && s[i] <= '9') 29 | { 30 | digit = s[i] - '0'; 31 | 32 | if (d % 2) 33 | digit = -digit; 34 | n = n * 10 + digit; 35 | f = 1; 36 | 37 | if (s[i + 1] < '0' || s[i + 1] > '9') 38 | break; 39 | f = 0; 40 | } 41 | i++; 42 | } 43 | if (f == 0) 44 | return (0); 45 | return (n); 46 | } 47 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - program that generates random valid 7 | * passwords for the program 101-crackme 8 | * Return: Always 0 9 | */ 10 | 11 | int main(void) 12 | { 13 | int pass[100]; 14 | int i, sum, n; 15 | 16 | sum = 0; 17 | 18 | srand(time(NULL)); 19 | 20 | for (i = 0; i < 100; i++) 21 | { 22 | pass[i] = rand() % 78; 23 | sum += (pass[i] + '0'); 24 | putchar(pass[i] + '0'); 25 | if ((2772 - sum) - '0' < 78) 26 | { 27 | n = 2772 - sum - '0'; 28 | sum += n; 29 | putchar(n + '0'); 30 | break; 31 | } 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string 5 | * @s: The string to get the length 6 | * Return: The length of @s 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int lenStr = 0; 12 | 13 | while (*s++) 14 | lenStr++; 15 | 16 | return (lenStr); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | 8 | void _puts(char *str) 9 | { 10 | int i; 11 | 12 | for (i = 0; str[i] != '\0'; i++) 13 | { 14 | _putchar(str[i]); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string, in reverse, followed by a new line 5 | * @s: input string 6 | * Return: no return 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int count = 0; 12 | 13 | while (count >= 0) 14 | { 15 | if (s[count] == '\0') 16 | break; 17 | count++; 18 | } 19 | 20 | for (count--; count >= 0; count--) 21 | _putchar(s[count]); 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * rev_string - update value 6 | * @s: value to be evaluate 7 | * Return: not 8 | */ 9 | 10 | void rev_string(char *s) 11 | { 12 | char tmp; 13 | int i, len, len1; 14 | 15 | len = 0; 16 | len1 = 0; 17 | 18 | while (s[len] != '\0') 19 | { 20 | len++; 21 | } 22 | 23 | len1 = len - 1; 24 | 25 | for (i = 0; i < len / 2; i++) 26 | { 27 | tmp = s[i]; 28 | s[i] = s[len1]; 29 | s[len1--] = tmp; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * puts2 - prints one char out of 2 of a string 4 | * followed by a new line 5 | * @str: string to print the chars from 6 | */ 7 | 8 | void puts2(char *str) 9 | { 10 | int len, i; 11 | 12 | len = 0; 13 | while (str[len] != '\0') 14 | { 15 | len++; 16 | } 17 | 18 | for (i = 0; i < len; i += 2) 19 | { 20 | _putchar(str[i]); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints half of a string 5 | * @str: input string 6 | * Return: no return 7 | */ 8 | void puts_half(char *str) 9 | { 10 | int count = 0, i; 11 | 12 | while (count >= 0) 13 | { 14 | if (str[count] == '\0') 15 | break; 16 | count++; 17 | } 18 | 19 | if (count % 2 == 1) 20 | i = count / 2; 21 | else 22 | i = (count - 1) / 2; 23 | 24 | for (i++; i < count; i++) 25 | _putchar(str[i]); 26 | _putchar('\n'); 27 | } 28 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - prints n elements of an array of integers 6 | * @a: input array 7 | * @n: input n elements 8 | * Return: no return 9 | */ 10 | 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 | 18 | if (i != (n - 1)) 19 | printf(", "); 20 | } 21 | printf("\n"); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * Return: the pointer to dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int len, i; 15 | 16 | len = 0; 17 | while (src[len] != '\0') 18 | { 19 | len++; 20 | } 21 | 22 | for (i = 0; i < len; i++) 23 | { 24 | dest[i] = src[i]; 25 | } 26 | 27 | dest[i] = '\0'; 28 | return (dest); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | alx projectt got pointers and array -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | void reset_to_98(int *n); 5 | void swap_int(int *a, int *b); 6 | int _strlen(char *s); 7 | void _puts(char *str); 8 | int _putchar(char); 9 | void print_rev(char *s); 10 | void rev_string(char *s); 11 | void puts2(char *str); 12 | void puts_half(char *str); 13 | void print_array(int *a, int n); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * Return: the pointer to dest. 7 | */ 8 | char *_strcat(char *dest, char *src) 9 | { 10 | int count = 0, count2 = 0; 11 | 12 | while (*(dest + count) != '\0') 13 | { 14 | count++; 15 | } 16 | 17 | while (count2 >= 0) 18 | { 19 | *(dest + count) = *(src + count2); 20 | if (*(src + count2) == '\0') 21 | break; 22 | count++; 23 | count2++; 24 | } 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * Return: a pointer to the resulting string dest 9 | */ 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int i, j; 13 | 14 | i = 0; 15 | j = 0; 16 | 17 | while (dest[i] != '\0') 18 | i++; 19 | 20 | while (src[j] != '\0' && j < n) 21 | { 22 | dest[i] = src[j]; 23 | i++; 24 | j++; 25 | } 26 | 27 | dest[i] = '\0'; 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string in rot13 5 | * @s: string to be encoded 6 | * Return: the resulting string 7 | */ 8 | 9 | char *rot13(char *s) 10 | { 11 | int i, j; 12 | 13 | char a[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 14 | char b[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | for (j = 0; a[j] != '\0'; j++) 19 | { 20 | if (s[i] == a[j]) 21 | { 22 | s[i] = b[j]; 23 | break; 24 | } 25 | } 26 | } 27 | 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | 9 | void print_number(int n) 10 | { 11 | unsigned int m, d, count; 12 | 13 | if (n < 0) 14 | { 15 | _putchar(45); 16 | m = n * -1; 17 | } 18 | else 19 | { 20 | m = n; 21 | } 22 | 23 | d = m; 24 | count = 1; 25 | 26 | while (d > 9) 27 | { 28 | d /= 10; 29 | count *= 10; 30 | } 31 | 32 | for (; count >= 1; count /= 10) 33 | { 34 | _putchar(((m / count) % 10) + 48); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /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/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copy a string 5 | * @dest: input value 6 | * @src: input value 7 | * @n: input value 8 | * Return: dest 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int j; 13 | 14 | j = 0; 15 | while (j < n && src[j] != '\0') 16 | { 17 | dest[j] = src[j]; 18 | j++; 19 | } 20 | while (j < n) 21 | { 22 | dest[j] = '\0'; 23 | j++; 24 | } 25 | 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare string values 5 | * @s1: input value 6 | * @s2: input value 7 | * Return: s1[i] - s2[i] 8 | */ 9 | int _strcmp(char *s1, char *s2) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (s1[i] != '\0' && s2[i] != '\0') 15 | { 16 | if (s1[i] != s2[i]) 17 | { 18 | return (s1[i] - s2[i]); 19 | } 20 | i++; 21 | } 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - reverses an array of integers 5 | * @a: array to be reversed 6 | * @n: number of elements in the array 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i, j, tmp; 11 | 12 | j = n - 1; 13 | 14 | for (i = 0; i < n / 2; i++) 15 | { 16 | tmp = a[i]; 17 | a[i] = a[j]; 18 | a[j--] = tmp; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - change all lowercase to uppercase 5 | * @n: pointer 6 | * Return: n 7 | */ 8 | 9 | char *string_toupper(char *n) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (n[i] != '\0') 15 | { 16 | if (n[i] >= 'a' && n[i] <= 'z') 17 | n[i] = n[i] - 32; 18 | i++; 19 | } 20 | return (n); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes everey word of a string 5 | * @s: string to modify 6 | * Return: the resulting string 7 | */ 8 | 9 | char *cap_string(char *s) 10 | { 11 | int i, j; 12 | 13 | char spe[13] = {' ', '\t', '\n', ',', ';', '.', 14 | '!', '?', '"', '(', ')', '{', '}'}; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | if (i == 0 && s[i] >= 'a' && s[i] <= 'z') 19 | s[i] -= 32; 20 | 21 | for (j = 0; j < 13; j++) 22 | { 23 | if (s[i] == spe[j]) 24 | { 25 | if (s[i + 1] >= 'a' && s[i + 1] <= 'z') 26 | { 27 | s[i + 1] -= 32; 28 | } 29 | } 30 | } 31 | } 32 | 33 | return (s); 34 | } 35 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - encode into 1337speak 5 | * @n: input value 6 | * Return: n value 7 | */ 8 | 9 | char *leet(char *n) 10 | { 11 | int i, j; 12 | char s1[] = "aAeEoOtTlL"; 13 | char s2[] = "4433007711"; 14 | 15 | for (i = 0; n[i] != '\0'; i++) 16 | { 17 | for (j = 0; j < 10; j++) 18 | { 19 | if (n[i] == s1[j]) 20 | { 21 | n[i] = s2[j]; 22 | } 23 | } 24 | } 25 | return (n); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | this is alx project for pointers and array -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | int _putchar(char c); 7 | void *_memset(void *s, int c, size_t n); 8 | char *_memcpy(char *dest, char *src, unsigned int n); 9 | char *_strchr(char *s, char c); 10 | unsigned int _strspn(char *s, char *accept); 11 | char *_strpbrk(char *s, char *accept); 12 | char *_strstr(char *haystack, char *needle); 13 | void print_chessboard(char (*a)[8]); 14 | void print_diagsums(int *a, int size); 15 | void set_string(char **s, char *to); 16 | void print_number(int n); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /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 | this is a project for alx pointer and array -------------------------------------------------------------------------------- /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 - prints a string, followed by a new line 5 | * @s: string to print 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | { 11 | _putchar('\n'); 12 | return; 13 | } 14 | _putchar(*s); 15 | _puts_recursion(s + 1); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _print_rev_recursion - prints a string, in reverse. 4 | * @s: pointer to string 5 | * Return: nothing 6 | **/ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s != '\0') 10 | { 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two string. 5 | * @s1: 1st string to be compared 6 | * @s2: second string containing '*' or not 7 | * Return: 1 if the strings can be considered as identical, and 0 8 | **/ 9 | int wildcmp(char *s1, char *s2) 10 | { 11 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 12 | return (0); 13 | if (*s1 == '\0' && *s2 == '\0') 14 | return (1); 15 | if (*s1 == *s2) 16 | return (wildcmp(s1 + 1, s2 + 1)); 17 | if (*s2 == '*') 18 | return (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * factorial - calculates the factorial of a given number. 4 | * @n: given number 5 | * Return: factorial 6 | **/ 7 | int factorial(int n) 8 | { 9 | if (n < 0) 10 | { 11 | return (-1); 12 | } 13 | if (n == 0 || n == 1) 14 | { 15 | return (1); 16 | } 17 | return (n * factorial(n - 1)); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _pow_recursion - calculates value of x raised to the power of y 5 | * @x: given number 6 | * @y: exponent 7 | * Return: value of x raised to the power of y 8 | **/ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | { 13 | return (-1); 14 | } 15 | if (y == 0) 16 | { 17 | return (1); 18 | } 19 | return (x * _pow_recursion(x, y - 1)); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * is_prime - detects if an input number is a prime number. 5 | * @n: input number. 6 | * @c: iterator. 7 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 8 | */ 9 | int is_prime(unsigned int n, unsigned int c) 10 | { 11 | if (n % c == 0) 12 | { 13 | if (n == c) 14 | return (1); 15 | else 16 | return (0); 17 | } 18 | return (0 + is_prime(n, c + 1)); 19 | } 20 | /** 21 | * is_prime_number - detects if an input number is a prime number. 22 | * @n: input number. 23 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 24 | */ 25 | int is_prime_number(int n) 26 | { 27 | if (n == 0) 28 | return (0); 29 | if (n < 0) 30 | return (0); 31 | if (n == 1) 32 | return (0); 33 | return (is_prime(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | alx project for recursion -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | void _puts_recursion(char *s); 6 | void _print_rev_recursion(char *s); 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 is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - checks for uppercase character 6 | * @c: Variable text 7 | * Return: Always 0. 8 | */ 9 | 10 | int _isupper(int c) 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | { 14 | return (1); 15 | } 16 | else 17 | { 18 | return (0); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - ironnou 5 | * @s: pointer to char params 6 | * @b: data to change 7 | * @n: index 8 | * Return: *s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | s[i] = b; 18 | } 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * Return: the pointer to dest. 7 | */ 8 | char *_strcat(char *dest, char *src) 9 | { 10 | int count = 0, count2 = 0; 11 | 12 | while (*(dest + count) != '\0') 13 | { 14 | count++; 15 | } 16 | 17 | while (count2 >= 0) 18 | { 19 | *(dest + count) = *(src + count2); 20 | if (*(src + count2) == '\0') 21 | break; 22 | count++; 23 | count2++; 24 | } 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - function that check if it's a digit 5 | * @c: takes in a character/digit 6 | * Return: 1 if digit and 0 if anything else 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - irontnoux2 5 | * @dest: pointer to char params 6 | * @src: pointer to char params 7 | * @n: size 8 | * Return: *dest 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * Return: a pointer to the resulting string dest 9 | */ 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int i, j; 13 | 14 | i = 0; 15 | j = 0; 16 | 17 | while (dest[i] != '\0') 18 | i++; 19 | 20 | while (src[j] != '\0' && j < n) 21 | { 22 | dest[i] = src[j]; 23 | i++; 24 | j++; 25 | } 26 | 27 | dest[i] = '\0'; 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * Return: the int converted from the string 7 | */ 8 | 9 | int _atoi(char *s) 10 | { 11 | int i, d, n, len, f, digit; 12 | 13 | i = 0; 14 | d = 0; 15 | n = 0; 16 | len = 0; 17 | f = 0; 18 | digit = 0; 19 | 20 | while (s[len] != '\0') 21 | len++; 22 | 23 | while (i < len && f == 0) 24 | { 25 | if (s[i] == '-') 26 | ++d; 27 | 28 | if (s[i] >= '0' && s[i] <= '9') 29 | { 30 | digit = s[i] - '0'; 31 | 32 | if (d % 2) 33 | digit = -digit; 34 | n = n * 10 + digit; 35 | f = 1; 36 | 37 | if (s[i + 1] < '0' || s[i + 1] > '9') 38 | break; 39 | f = 0; 40 | } 41 | i++; 42 | } 43 | if (f == 0) 44 | return (0); 45 | return (n); 46 | } 47 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - prints found c 5 | * @s: pointer to char 6 | * @c: char params to found 7 | * Return: *S 8 | */ 9 | 10 | char *_strchr(char *s, char c) 11 | { 12 | while (*s != '\0') 13 | { 14 | if (*s == c) 15 | { 16 | return (s); 17 | } 18 | s++; 19 | } 20 | if (*s == c) 21 | { 22 | return (s); 23 | } 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string 5 | * @s: The string to get the length 6 | * Return: The length of @s 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int lenStr = 0; 12 | 13 | while (*s++) 14 | lenStr++; 15 | 16 | return (lenStr); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copy a string 5 | * @dest: input value 6 | * @src: input value 7 | * @n: input value 8 | * Return: dest 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int j; 13 | 14 | j = 0; 15 | while (j < n && src[j] != '\0') 16 | { 17 | dest[j] = src[j]; 18 | j++; 19 | } 20 | while (j < n) 21 | { 22 | dest[j] = '\0'; 23 | j++; 24 | } 25 | 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks if a charactor is lowercase 5 | * @c: the character 6 | * Return: 1 if letter is lowercase. 0 if not 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | 8 | void _puts(char *str) 9 | { 10 | int i; 11 | 12 | for (i = 0; str[i] != '\0'; i++) 13 | { 14 | _putchar(str[i]); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare string values 5 | * @s1: input value 6 | * @s2: input value 7 | * Return: s1[i] - s2[i] 8 | */ 9 | int _strcmp(char *s1, char *s2) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (s1[i] != '\0' && s2[i] != '\0') 15 | { 16 | if (s1[i] != s2[i]) 17 | { 18 | return (s1[i] - s2[i]); 19 | } 20 | i++; 21 | } 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/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/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha -checks for alphabets 5 | * @c: character to be cheked 6 | * Return: 1 if character is a letter, 0 otherwise 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strpbrk - bytes 6 | * @s: pointer to char 7 | * @accept: pointer to char 8 | * Return: NULL 9 | */ 10 | 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i; 14 | 15 | while (*s) 16 | { 17 | for (i = 0; accept[i]; i++) 18 | { 19 | if (*s == accept[i]) 20 | { 21 | return (s); 22 | } 23 | } 24 | s++; 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - function locate 5 | * @haystack: pointer to char 6 | * @needle: pointer to char 7 | * Return: 0 8 | */ 9 | 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | char *result = haystack, *fneedle = needle; 13 | 14 | while (*haystack) 15 | { 16 | while (*needle) 17 | { 18 | if (*haystack++ != *needle++) 19 | { 20 | break; 21 | } 22 | } 23 | if (!*needle) 24 | { 25 | return (result); 26 | } 27 | needle = fneedle; 28 | result++; 29 | haystack = result; 30 | } 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Compuyer the absolute value of an integer 5 | * @t: Ther integer to be computed 6 | * Return: The absolute value 7 | */ 8 | 9 | int _abs(int t) 10 | { 11 | if (t >= 0) 12 | return (t); 13 | else 14 | return (-t); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * Return: the pointer to dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int len, i; 15 | 16 | len = 0; 17 | while (src[len] != '\0') 18 | { 19 | len++; 20 | } 21 | 22 | for (i = 0; i < len; i++) 23 | { 24 | dest[i] = src[i]; 25 | } 26 | 27 | dest[i] = '\0'; 28 | return (dest); 29 | } 30 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | this is for alx project of static libraries -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - used to write char "d" 6 | * @c: The character to be print 7 | * Return: 1 in success & error on -1 8 | */ 9 | 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/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/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_STATIC 2 | #define FILE_STATIC 3 | 4 | 5 | int _putchar(char c); 6 | int _islower(int c); 7 | int _isalpha(int c); 8 | int _abs(int n); 9 | int _isupper(int c); 10 | int _isdigit(int c); 11 | int _strlen(char *s); 12 | void _puts(char *s); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | char *_strcat(char *dest, char *src); 16 | char *_strncat(char *dest, char *src, int n); 17 | char *_strncpy(char *dest, char *src, int n); 18 | int _strcmp(char *s1, char *s2); 19 | char *_memset(char *s, char b, unsigned int n); 20 | char *_memcpy(char *dest, char *src, unsigned int n); 21 | char *_strchr(char *s, char c); 22 | unsigned int _strspn(char *s, char *accept); 23 | char *_strpbrk(char *s, char *accept); 24 | char *_strstr(char *haystack, char *needle); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the program name. 5 | * @argc: The number of arguments supplied . 6 | * @argv: An array of pointers to the arguments. 7 | * Return: Always o. 8 | */ 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. 5 | * @argc: The number of arguments supplied. 6 | * @argv: An array of pointers to the arguments. 7 | * Return: Always 0. 8 | */ 9 | 10 | int main(int argc, char __attribute__((__unused__)) *argv[]) 11 | { 12 | printf("%d\n", argc - 1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Prints the minimum number of coins - change for an amount of money. 6 | * @argc: The number of arguments supplied. 7 | * @argv: An array of pointers to the arguments. 8 | * Return: If the number of arguments is not exactly one - 1 Otherwise - 0. 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int cents, coins = 0; 14 | 15 | if (argc != 2) 16 | { 17 | printf("Error\n"); 18 | return (1); 19 | } 20 | 21 | cents = atoi(argv[1]); 22 | 23 | while (cents > 0) 24 | { 25 | coins++; 26 | if ((cents - 25) >= 0) 27 | { 28 | cents -= 25; 29 | continue; 30 | } 31 | if ((cents - 10) >= 0) 32 | { 33 | cents -= 10; 34 | continue; 35 | } 36 | if ((cents - 5) >= 0) 37 | { 38 | cents -= 5; 39 | continue; 40 | } 41 | if ((cents - 2) >= 0) 42 | { 43 | cents -= 2; 44 | continue; 45 | } 46 | cents--; 47 | } 48 | 49 | printf("%d\n", coins); 50 | 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints all arguments. 5 | * @argc: The number of arguments. 6 | * @argv: An array of pointers to the arguments. 7 | * Return: Always 0. 8 | */ 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 multipl of two numbers, followed by a new line. 6 | * @argc: The number of arguments. 7 | * @argv: An array of pointers to the arguments. 8 | * Return: If the program receives two arguments - 0 Otherwise - 1. 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int num1, num2, prod; 14 | 15 | if (argc != 3) 16 | { 17 | printf("Error\n"); 18 | return (1); 19 | } 20 | 21 | num1 = atoi(argv[1]); 22 | num2 = atoi(argv[2]); 23 | prod = num1 * num2; 24 | 25 | printf("%d\n", prod); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Prints the addition of positive numbers,followed by a new line. 6 | * @argc: The number of arguments passed. 7 | * @argv: An array of pointers to the arguments. 8 | * Return: If one of the numbers contains symbols that are non-digits - 1. 9 | * Otherwise - 0. 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int num, digit, sum = 0; 15 | 16 | for (num = 1; num < argc; num++) 17 | { 18 | for (digit = 0; argv[num][digit]; digit++) 19 | { 20 | if (argv[num][digit] < '0' || argv[num][digit] > '9') 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | } 26 | 27 | sum += atoi(argv[num]); 28 | } 29 | 30 | printf("%d\n", sum); 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | this project is for arguments argc and argv -------------------------------------------------------------------------------- /0x0A-argc_argv/add: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0A-argc_argv/add -------------------------------------------------------------------------------- /0x0A-argc_argv/args: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0A-argc_argv/args -------------------------------------------------------------------------------- /0x0A-argc_argv/change: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0A-argc_argv/change -------------------------------------------------------------------------------- /0x0A-argc_argv/mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0A-argc_argv/mul -------------------------------------------------------------------------------- /0x0A-argc_argv/mynameis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0A-argc_argv/mynameis -------------------------------------------------------------------------------- /0x0A-argc_argv/nargs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0A-argc_argv/nargs -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * create_array - creates an array of chars for test. 5 | * @size: contains size of the array. 6 | * @c: storaged char to collect one by one 7 | * Return: pointer of an array of chars 8 | */ 9 | 10 | char *create_array(unsigned int size, char c) 11 | { 12 | char *car; 13 | unsigned int i; 14 | 15 | if (size == 0) 16 | return (NULL); 17 | 18 | car = malloc(sizeof(c) * size); 19 | 20 | if (car == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | car[i] = c; 25 | 26 | return (car); 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 function. 5 | * @str: string to be collected like an arrey. 6 | * Return: pointer of an array of chars 7 | */ 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/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - concatenates two strings that passs from main. 6 | * @s1: first string at the first place. 7 | * @s2: second string at the second place. 8 | * Return: pointer of an array of chars 9 | */ 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 . 6 | * @width: width of the array at first parameter. 7 | * @height: height of the array at second paramater. 8 | * Return: pointer of an array of integers 9 | */ 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 function. 6 | * @grid: multidimensional array of integers first parameter. 7 | * @height: height of the grid second parameter. 8 | * Return: no return 9 | */ 10 | 11 | void free_grid(int **grid, int height) 12 | { 13 | if (grid != NULL && height != 0) 14 | { 15 | for (; height >= 0; height--) 16 | free(grid[height]); 17 | free(grid); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | this is alx project of malloc free -------------------------------------------------------------------------------- /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/.100-realloc.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0C-more_malloc_free/.100-realloc.c.swp -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * malloc_checked - allocates memory to the variable. 6 | * @b: amount of bytes. 7 | * Return: pointer to the allocated memory. 8 | * if malloc fails, status value is equal to 98. 9 | */ 10 | 11 | void *malloc_checked(unsigned int b) 12 | { 13 | int *pointer; 14 | 15 | pointer = malloc(b); 16 | 17 | if (pointer == NULL) 18 | exit(98); 19 | 20 | return (pointer); 21 | } 22 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0C-more_malloc_free/100-realloc -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0C-more_malloc_free/101-mul -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _calloc - Allocates memory for an array of a certain number 6 | * @nmemb: The number of elements. 7 | * @size: The byte size of each array element. 8 | * Return: If nmemb = 0, size = 0, or the function fails - NULL. 9 | */ 10 | 11 | void *_calloc(unsigned int nmemb, unsigned int size) 12 | { 13 | unsigned int i; 14 | char *array; 15 | 16 | if (nmemb == 0 || size == 0) 17 | return (NULL); 18 | 19 | array = malloc(nmemb * size); 20 | 21 | if (array == NULL) 22 | return (NULL); 23 | 24 | for (i = 0; i < nmemb * size; i++) 25 | array[i] = 0; 26 | 27 | return (array); 28 | } 29 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * array_range - creates an array of integers with min and max value. 6 | * @min: minimum value of the array. 7 | * @max: maximum value of the array. 8 | * Return: pointer to the newly created array. 9 | * if min > max, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | 13 | int *array_range(int min, int max) 14 | { 15 | int *array; 16 | int i, j, size; 17 | 18 | if (min > max) 19 | return (NULL); 20 | 21 | size = (max - min) + 1; 22 | 23 | array = malloc(sizeof(int) * size); 24 | 25 | if (array == NULL) 26 | return (NULL); 27 | 28 | for (i = 0, j = min; j <= max; i++, j++) 29 | array[i] = j; 30 | 31 | return (array); 32 | } 33 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | alx proect for malloc free -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c: The character to print 7 | * Return: On success 1. 8 | */ 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H 2 | #define _MAIN_H 3 | 4 | int _putchar(char c); 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 | 11 | #endif 12 | -------------------------------------------------------------------------------- /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 | 8 | -------------------------------------------------------------------------------- /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 | * Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef 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 | this is preprocessor project -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * init_dog - Initializes a dog structure int dog 6 | * @d: A dog structure representation. 7 | * @name: The name of the dog just name. 8 | * @age: The age of the dog just age. 9 | * @owner: The owner of the dog. 10 | * Return: Nothing 11 | */ 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 name age and owner name 7 | * @d: A dog structure from parameter 8 | * Return: Nothing 9 | */ 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/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * free_dog - free dog from cage 6 | * @d: dog to be freed form cage 7 | * Return: nothing 8 | */ 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 | this is alx project for structure -------------------------------------------------------------------------------- /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 | * Description: The attributes of a dog. 10 | */ 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 | /** 5 | * print_name - prints a name of a function 6 | * @name: person's name 7 | * @f: pointer to a function 8 | * Return: void 9 | */ 10 | 11 | void print_name(char *name, void (*f)(char *)) 12 | { 13 | if (f != NULL) 14 | f(name); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - execute function on each element 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 | 15 | if (array == NULL || action == NULL) 16 | return; 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 | * Return: void 9 | */ 10 | void print_opcodes(char *a, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | printf("%.2hhx", a[i]); 17 | if (i < n - 1) 18 | printf(" "); 19 | } 20 | printf("\n"); 21 | 22 | } 23 | 24 | /** 25 | * main - prints the opcodes of its own main function 26 | * @argc: argument count 27 | * @argv: argument vector 28 | * Return: O 29 | */ 30 | int main(int argc, char **argv) 31 | { 32 | int a; 33 | 34 | if (argc != 2) 35 | { 36 | printf("Error\n"); 37 | exit(1); 38 | } 39 | a = atoi(argv[1]); 40 | if (a < 0) 41 | { 42 | printf("Error\n"); 43 | exit(2); 44 | } 45 | print_opcodes((char *)&main, a); 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer in the function 5 | * @array: array with data 6 | * @size: size of the array 7 | * @cmp: function pointer 8 | * Return: count of integer 9 | */ 10 | 11 | int int_index(int *array, int size, int (*cmp)(int)) 12 | { 13 | int i; 14 | 15 | if (size <= 0) 16 | return (-1); 17 | if (array == NULL || cmp == NULL) 18 | return (-1); 19 | 20 | for (i = 0; i < size; i++) 21 | { 22 | if (cmp(array[i]) == 1) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _HEADER_ 2 | #define _HEADER_ 3 | 4 | /** 5 | * struct op - Struct op 6 | * @op: The operator 7 | * @f: The function associated 8 | */ 9 | 10 | typedef struct op 11 | { 12 | char *op; 13 | int (*f)(int a, int b); 14 | } op_t; 15 | 16 | int op_add(int a, int b); 17 | int op_sub(int a, int b); 18 | int op_mul(int a, int b); 19 | int op_div(int a, int b); 20 | int op_mod(int a, int b); 21 | int (*get_op_func(char *s))(int, int); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * get_op_func - function name 7 | * @s: sting character 8 | * Return: void 9 | */ 10 | 11 | int (*get_op_func(char *s))(int, int) 12 | { 13 | op_t ops[] = { 14 | { "+", op_add }, 15 | { "-", op_sub }, 16 | { "*", op_mul }, 17 | { "/", op_div }, 18 | { "%", op_mod }, 19 | { NULL, NULL } 20 | }; 21 | int 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 - main function 7 | * @argc: argc argument from parameter 8 | * @argv: argv arguments from parameter 9 | * Return: void 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int (*oprt)(int, int); 14 | 15 | if (argc != 4) 16 | { 17 | printf("Error\n"); 18 | exit(98); 19 | } 20 | 21 | oprt = get_op_func(argv[2]); 22 | 23 | if (!oprt) 24 | { 25 | printf("Error\n"); 26 | exit(99); 27 | } 28 | 29 | printf("%d\n", oprt(atoi(argv[1]), atoi(argv[3]))); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | this is for functions and pointers project -------------------------------------------------------------------------------- /0x0F-function_pointers/calc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x0F-function_pointers/calc -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_H 2 | #define _FUNCTION_POINTERS_H 3 | #include 4 | void print_name(char *name, void (*f)(char *)); 5 | void array_iterator(int *array, size_t size, void (*action)(int)); 6 | int int_index(int *array, int size, int (*cmp)(int)); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | 13 | sum = sum_them_all(2, 98, 1024); 14 | printf("%d\n", sum); 15 | sum = sum_them_all(4, 98, 1024, 402, -1024); 16 | printf("%d\n", sum); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * sum_them_all - returns the sum of all parameters. 7 | * @n: number of the arguments. 8 | * Return: sum of its parameters. 9 | */ 10 | 11 | int sum_them_all(const unsigned int n, ...) 12 | { 13 | va_list list; 14 | unsigned int i, sum = 0; 15 | 16 | if (n == 0) 17 | return (0); 18 | 19 | va_start(list, n); 20 | 21 | for (i = 0; i < n; i++) 22 | sum += va_arg(list, int); 23 | 24 | va_end(list); 25 | 26 | return (sum); 27 | } 28 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(", ", 4, 0, 98, -1024, 402); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_numbers - Prints numbers all in new line 7 | * @separator: The string to be printed b/n numbers. 8 | * @n: The number of integers passed to the function. 9 | * Return: void 10 | */ 11 | 12 | void print_numbers(const char *separator, const unsigned int n, ...) 13 | { 14 | va_list list; 15 | unsigned int i; 16 | unsigned int num; 17 | 18 | va_start(list, n); 19 | 20 | for (i = 0; i < n; i++) 21 | { 22 | num = va_arg(list, int); 23 | printf("%d", num); 24 | 25 | if (separator && i < n - 1) 26 | printf("%s", separator); 27 | } 28 | 29 | va_end(list); 30 | printf("\n"); 31 | } 32 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_strings(", ", 2, "Jay", "Django"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_strings - Prints strings, in a new line. 7 | * @separator: The string be printed between strings. 8 | * @n: The number of strings passed to the function. 9 | * Description: If separator is NULL, it is not printed. 10 | */ 11 | 12 | void print_strings(const char *separator, const unsigned int n, ...) 13 | { 14 | va_list list; 15 | unsigned int i; 16 | char *str; 17 | 18 | va_start(list, n); 19 | 20 | for (i = 0; i < n; i++) 21 | { 22 | str = va_arg(list, char *); 23 | 24 | if (str) 25 | printf("%s", str); 26 | else 27 | printf("(nil)"); 28 | 29 | if ((i < n - 1) && separator) 30 | printf("%s", separator); 31 | } 32 | 33 | printf("\n"); 34 | va_end(list); 35 | } 36 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_all("ceis", 'B', 3, "stSchool"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | this is alx project of variadic functions -------------------------------------------------------------------------------- /0x10-variadic_functions/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x10-variadic_functions/a -------------------------------------------------------------------------------- /0x10-variadic_functions/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x10-variadic_functions/b -------------------------------------------------------------------------------- /0x10-variadic_functions/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x10-variadic_functions/c -------------------------------------------------------------------------------- /0x10-variadic_functions/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x10-variadic_functions/d -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _VARIADIC_FUNCTIONS_ 2 | #define _VARIADIC_FUNCTIONS_ 3 | 4 | int _putchar(char c); 5 | int sum_them_all(const unsigned int n, ...); 6 | void print_numbers(const char *separator, const unsigned int n, ...); 7 | void print_strings(const char *separator, const unsigned int n, ...); 8 | void print_all(const char * const format, ...); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/.0-print_list.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x12-singly_linked_lists/.0-print_list.c.swp -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = print_list(head); 30 | printf("-> %lu elements\n", n); 31 | 32 | printf("\n"); 33 | free(new->str); 34 | new->str = NULL; 35 | n = print_list(head); 36 | printf("-> %lu elements\n", n); 37 | 38 | free(new); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_list - Prints the elements of the list_t list all. 5 | * @h: list. 6 | * Return: Number Of the nodes in the list. 7 | */ 8 | size_t print_list(const list_t *h) 9 | { 10 | size_t i = 0; 11 | 12 | while (h) 13 | { 14 | if (h->str == NULL) 15 | printf("[0] (nil)\n"); 16 | else 17 | printf("[%d] %s\n", h->len, h->str); 18 | h = h->next; 19 | i++; 20 | } 21 | return (i); 22 | } 23 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - returns the number of elements in a list one by one. 5 | * @h: list of the given structure. 6 | * Return: Number Of the nodes in the list. 7 | */ 8 | 9 | size_t list_len(const list_t *h) 10 | { 11 | size_t i = 0; 12 | 13 | while (h) 14 | { 15 | h = h->next; 16 | i++; 17 | } 18 | 19 | return (i); 20 | } 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = list_len(head); 30 | printf("-> %lu elements\n", n); 31 | free(new->str); 32 | free(new); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * constructorRun - runs before main function is executed. 5 | */ 6 | 7 | void constructorRun(void) 8 | { 9 | printf("You're beat! and yet, you must allow,\n" 10 | "I bore my house upon my back!\n"); 11 | } 12 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.)\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | global main 2 | 3 | section .text 4 | 5 | main: 6 | mov rax, 1 7 | mov rdi, 1 8 | mov rsi, msg 9 | mov rdx, msglen 10 | syscall 11 | mov rax, 60 12 | mov rdi, 0 13 | syscall 14 | 15 | section .rodata 16 | msg: db "Hello, Holberton", 10 17 | msglen: equ $ - msg 18 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x12-singly_linked_lists/101-hello_holberton.o -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node - Add new node at the beginning of the list_t list. 5 | * @head: The head of the list_t list in parameter. 6 | * @str: string to be added to the list_t list. 7 | * Return: NULL on failure and the address of the new element on success. 8 | */ 9 | 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *new = NULL; 13 | size_t i = 0; 14 | 15 | new = malloc(sizeof(list_t)); 16 | if (!new) 17 | { 18 | free(new); 19 | return (NULL); 20 | } 21 | 22 | for (i = 0; str[i]; i++) 23 | ; 24 | 25 | new->str = strdup(str) ? strdup(str) : (NULL); 26 | new->len = i; 27 | new->next = *head; 28 | 29 | *head = new; 30 | 31 | return (new); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - add new node at the of end list. 5 | * @head: head of the list_t list from parameter. 6 | * @str: string to be added to the list_t list. 7 | * Return: NULL on failure and the address of the new element on success. 8 | */ 9 | 10 | list_t *add_node_end(list_t **head, const char *str) 11 | { 12 | list_t *new, *temp; 13 | size_t i = 0; 14 | 15 | new = malloc(sizeof(list_t)); 16 | if (!new) 17 | { 18 | free(new); 19 | return (NULL); 20 | } 21 | 22 | for (i = 0; str[i]; i++) 23 | ; 24 | 25 | new->str = strdup(str) ? strdup(str) : (NULL); 26 | new->len = i; 27 | new->next = NULL; 28 | 29 | if (!(*head)) 30 | *head = new; 31 | else 32 | { 33 | temp = *head; 34 | /*Use temp->next instead of just temp*/ 35 | while (temp->next != NULL) 36 | temp = temp->next; 37 | temp->next = new; 38 | } 39 | 40 | return (new); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - Frees list fress memory space. 5 | * @head: head of the list_t list from parameter. 6 | */ 7 | 8 | void free_list(list_t *head) 9 | { 10 | list_t *temp; 11 | 12 | while (head) 13 | { 14 | temp = head; 15 | free(head->str); 16 | free(head); 17 | head = temp->next; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Bob"); 17 | add_node_end(&head, "&"); 18 | add_node_end(&head, "Kris"); 19 | add_node_end(&head, "love"); 20 | add_node_end(&head, "asm"); 21 | print_list(head); 22 | free_list(head); 23 | head = NULL; 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | alx project for singliy linked list -------------------------------------------------------------------------------- /0x12-singly_linked_lists/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x12-singly_linked_lists/a -------------------------------------------------------------------------------- /0x12-singly_linked_lists/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x12-singly_linked_lists/b -------------------------------------------------------------------------------- /0x12-singly_linked_lists/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x12-singly_linked_lists/c -------------------------------------------------------------------------------- /0x12-singly_linked_lists/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x12-singly_linked_lists/d -------------------------------------------------------------------------------- /0x12-singly_linked_lists/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x12-singly_linked_lists/e -------------------------------------------------------------------------------- /0x12-singly_linked_lists/first: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x12-singly_linked_lists/first -------------------------------------------------------------------------------- /0x12-singly_linked_lists/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x12-singly_linked_lists/hello -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | */ 16 | typedef struct list_s 17 | { 18 | char *str; 19 | unsigned int len; 20 | struct list_s *next; 21 | } list_t; 22 | 23 | size_t print_list(const list_t *h); 24 | size_t list_len(const list_t *h); 25 | list_t *add_node(list_t **head, const char *str); 26 | list_t *add_node_end(list_t **head, const char *str); 27 | void free_list(list_t *head); 28 | void constructorRun(void) __attribute__ ((constructor)); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = print_listint(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - a function that Prints all elements of the list. 5 | * @h: pointer to head of the list or that pointes to the first node. 6 | * Return: The number of nodes in the list. 7 | */ 8 | 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t j = 0; 12 | 13 | while (h) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | j++; 18 | } 19 | 20 | return (j); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - Function that returns the number of elements in list. 5 | * @h: pointer to head of the list or that pointes to the first node. 6 | * Return: numbers of elements in list. 7 | */ 8 | 9 | size_t listint_len(const listint_t *h) 10 | { 11 | size_t number = 0; 12 | 13 | while (h) 14 | { 15 | h = h->next; 16 | number++; 17 | } 18 | 19 | return (number); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *new; 15 | listint_t hello = {8, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(listint_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->n = 9; 26 | new->next = head; 27 | head = new; 28 | n = listint_len(head); 29 | printf("-> %lu elements\n", n); 30 | free(new); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - Function deletes node at inde of a list. 5 | * @head: A pointer to the address of the at the head of the list. 6 | * @index: The index of list where the new node is inserted. 7 | * Return: 1 if it succeeded, -1 if it failed. 8 | */ 9 | 10 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 11 | { 12 | listint_t *currnt, *prev, *temp_p; 13 | unsigned int i = 0; 14 | 15 | currnt = *head; 16 | prev = currnt; 17 | 18 | if ((*head) && index == 0) 19 | { 20 | *head = (*head)->next; 21 | free(prev); 22 | return (1); 23 | } 24 | 25 | while (currnt) 26 | { 27 | temp_p = currnt; 28 | 29 | if (i == index) 30 | { 31 | prev->next = currnt->next; 32 | free(currnt); 33 | return (1); 34 | } 35 | currnt = currnt->next; 36 | prev = temp_p; 37 | i++; 38 | } 39 | 40 | return (-1); 41 | } 42 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | reverse_listint(&head); 26 | print_listint(head); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - Function that reverses a linked list. 5 | * @head: A pointer to the address of the at the head of the list. 6 | * Return: pointer to the first node in the reversed list. 7 | */ 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 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 - Function that prints the list. 5 | * @head: A pointer to the address of the at the head of the list. 6 | * Return: number the nodes in the list. 7 | */ 8 | 9 | size_t print_listint_safe(const listint_t *head) 10 | { 11 | size_t num1 = 0; 12 | long int diffrent; 13 | 14 | while (head) 15 | { 16 | diffrent = head - head->next; 17 | num1++; 18 | printf("[%p] %d\n", (void *)head, head->n); 19 | if (diffrent > 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 (num1); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * free_listint_safe - Function that frees a linked list. 5 | * @h: pointer to head of the list or that pointes to the first node. 6 | * Return: number of elements from the freed list. 7 | */ 8 | 9 | size_t free_listint_safe(listint_t **h) 10 | { 11 | size_t length = 0; 12 | int diffrenc; 13 | listint_t *temp; 14 | 15 | if (!h || !*h) 16 | return (0); 17 | 18 | while (*h) 19 | { 20 | diffrenc = *h - (*h)->next; 21 | if (diffrenc > 0) 22 | { 23 | temp = (*h)->next; 24 | *h = temp; 25 | length++; 26 | } 27 | else 28 | { 29 | *h = NULL; 30 | length++; 31 | break; 32 | } 33 | } 34 | 35 | *h = NULL; 36 | 37 | return (length); 38 | } 39 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - Function that finds the loop in a list. 5 | * @head: A pointer to the address of the at the head of the list. 6 | * Return: Address of the node where the loop beagn. 7 | */ 8 | 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *slow_t = head; 12 | listint_t *fast_t = head; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | while (slow_t && fast_t && fast_t->next) 18 | { 19 | fast_t = fast_t->next->next; 20 | slow_t = slow_t->next; 21 | if (fast_t == slow_t) 22 | { 23 | slow_t = head; 24 | while (slow_t != fast_t) 25 | { 26 | slow_t = slow_t->next; 27 | fast_t = fast_t->next; 28 | } 29 | return (fast_t); 30 | } 31 | } 32 | 33 | return (NULL); 34 | } 35 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - Funtion that Adds a new node at the beginning of list. 5 | * @head: A pointer to the address of the at the head of the list. 6 | * @n: The integer for the new node to contain the data of the node. 7 | * Return: The address of the new element/ Null if failed. 8 | */ 9 | 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | listint_t *new = NULL; 13 | 14 | new = (listint_t *)malloc(sizeof(listint_t)); 15 | if (!new) 16 | return (NULL); 17 | 18 | new->n = n; 19 | new->next = *head; 20 | 21 | *head = new; 22 | 23 | return (new); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint(&head, 0); 17 | add_nodeint(&head, 1); 18 | add_nodeint(&head, 2); 19 | add_nodeint(&head, 3); 20 | add_nodeint(&head, 4); 21 | add_nodeint(&head, 98); 22 | add_nodeint(&head, 402); 23 | add_nodeint(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - Function that adds new node at the end of a list. 5 | * @head: A pointer to the address of the at the head of the list. 6 | * @n: The integer for the new node to contain the data of the node. 7 | * Return: The address of the new element/ NULL if failed 8 | */ 9 | 10 | listint_t *add_nodeint_end(listint_t **head, const int n) 11 | { 12 | listint_t *temp_p; 13 | listint_t *new = NULL; 14 | 15 | new = (listint_t *)malloc(sizeof(listint_t)); 16 | if (!new) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->next = NULL; 21 | 22 | if (!(*head)) 23 | *head = new; 24 | else 25 | { 26 | temp_p = *head; 27 | while (temp_p->next != NULL) 28 | temp_p = temp_p->next; 29 | 30 | temp_p->next = new; 31 | } 32 | 33 | return (new); 34 | } 35 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - A function that frees a list. 5 | * @head: A pointer to the address of the at the head of the list. 6 | */ 7 | 8 | void free_listint(listint_t *head) 9 | { 10 | listint_t *temp_p = head; 11 | 12 | while (head) 13 | { 14 | temp_p = temp_p->next; 15 | free(head); 16 | head = temp_p; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint(head); 26 | head = NULL; 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - Function that frees a list and sets the head to NULL. 5 | * @head: A pointer to the address of the at the head of the list. 6 | */ 7 | 8 | void free_listint2(listint_t **head) 9 | { 10 | listint_t *temp_p; 11 | 12 | if (head == NULL) 13 | return; 14 | 15 | while (*head) 16 | { 17 | temp_p = (*head)->next; 18 | free(*head); 19 | *head = temp_p; 20 | } 21 | 22 | *head = NULL; 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | free_listint2(&head); 26 | printf("%p\n", (void *)head); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | int n; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | print_listint(head); 26 | n = pop_listint(&head); 27 | printf("- %d\n", n); 28 | print_listint(head); 29 | n = pop_listint(&head); 30 | printf("- %d\n", n); 31 | print_listint(head); 32 | free_listint2(&head); 33 | printf("%p\n", (void *)head); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - Function that delete the head node of a list. 5 | * @head: A pointer to the address of the at the head of the list. 6 | * Return: If the linked list is empty - 0. 7 | */ 8 | 9 | int pop_listint(listint_t **head) 10 | { 11 | listint_t *temp_p; 12 | int data_n; 13 | 14 | if (!(*head)) 15 | return (0); 16 | 17 | temp_p = *head; 18 | data_n = temp_p->n; 19 | temp_p = temp_p->next; 20 | 21 | free(*head); 22 | 23 | *head = temp_p; 24 | 25 | return (data_n); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - function that returns the nth node of a list 5 | * @head: A pointer to the address of the at the head of the list. 6 | * @index: index of the node, starting at 0 just like array. 7 | * Return: nth node. If node does not exist, returns NULL. 8 | */ 9 | 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int j = 0; 13 | 14 | while (head != NULL) 15 | { 16 | if (j == index) 17 | return (head); 18 | j++, head = head->next; 19 | } 20 | 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | listint_t *node; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | print_listint(head); 26 | node = get_nodeint_at_index(head, 5); 27 | printf("%d\n", node->n); 28 | print_listint(head); 29 | free_listint2(&head); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_nodeint_end(&head, 0); 18 | add_nodeint_end(&head, 1); 19 | add_nodeint_end(&head, 2); 20 | add_nodeint_end(&head, 3); 21 | add_nodeint_end(&head, 4); 22 | add_nodeint_end(&head, 98); 23 | add_nodeint_end(&head, 402); 24 | add_nodeint_end(&head, 1024); 25 | sum = sum_listint(head); 26 | printf("sum = %d\n", sum); 27 | free_listint2(&head); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - Function that returns the sum of all data in list. 5 | * @head: A pointer to the address of the at the head of the list. 6 | * Return: sum of all the data (n). 7 | */ 8 | 9 | int sum_listint(listint_t *head) 10 | { 11 | int sum_of_n = 0; 12 | 13 | if (!head) 14 | return (0); 15 | 16 | while (head) 17 | { 18 | sum_of_n += head->n; 19 | head = head->next; 20 | } 21 | 22 | return (sum_of_n); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | listint_t *head; 14 | 15 | head = NULL; 16 | add_nodeint_end(&head, 0); 17 | add_nodeint_end(&head, 1); 18 | add_nodeint_end(&head, 2); 19 | add_nodeint_end(&head, 3); 20 | add_nodeint_end(&head, 4); 21 | add_nodeint_end(&head, 98); 22 | add_nodeint_end(&head, 402); 23 | add_nodeint_end(&head, 1024); 24 | print_listint(head); 25 | printf("-----------------\n"); 26 | insert_nodeint_at_index(&head, 5, 4096); 27 | print_listint(head); 28 | free_listint2(&head); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | this is alx project for more singly linked list -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/a -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/b -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/c -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/d -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/e -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/f -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/g: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/g -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/h -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/i: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/i -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/j: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/j -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/k: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/k -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/l: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/l -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/m -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/n: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/n -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x13-more_singly_linked_lists/o -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - Function that converts a binary num to unsigned int. 5 | * @b: string that contain binary number from parameter. 6 | * Return: the converted number 7 | */ 8 | 9 | unsigned int binary_to_uint(const char *b) 10 | { 11 | int j; 12 | unsigned int decmal_val = 0; 13 | 14 | if (!b) 15 | return (0); 16 | 17 | for (j = 0; b[j]; j++) 18 | { 19 | if (b[j] < '0' || b[j] > '1') 20 | return (0); 21 | decmal_val = 2 * decmal_val + (b[j] - '0'); 22 | } 23 | return (decmal_val); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = binary_to_uint("1"); 14 | printf("%u\n", n); 15 | n = binary_to_uint("101"); 16 | printf("%u\n", n); 17 | n = binary_to_uint("1e01"); 18 | printf("%u\n", n); 19 | n = binary_to_uint("1100010"); 20 | printf("%u\n", n); 21 | n = binary_to_uint("0000000000000000000110010010"); 22 | printf("%u\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | print_binary(0); 12 | printf("\n"); 13 | print_binary(1); 14 | printf("\n"); 15 | print_binary(98); 16 | printf("\n"); 17 | print_binary(1024); 18 | printf("\n"); 19 | print_binary((1 << 10) + 1); 20 | printf("\n"); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - Function that prints binary representation of a number. 5 | * @n: Number that comes from parameter. 6 | */ 7 | 8 | void print_binary(unsigned long int n) 9 | { 10 | int i, counter = 0; 11 | unsigned long int current; 12 | 13 | for (i = 63; i >= 0; i--) 14 | { 15 | current = n >> i; 16 | 17 | if (current & 1) 18 | { 19 | _putchar('1'); 20 | counter++; 21 | } 22 | else if (counter) 23 | _putchar('0'); 24 | } 25 | if (!counter) 26 | _putchar('0'); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - A function that checks the endianness. 5 | * Return: 0 if big endian and 1 if little. 6 | */ 7 | 8 | int get_endianness(void) 9 | { 10 | unsigned int i = 1; 11 | char *c = (char *) &i; 12 | 13 | return (*c); 14 | } 15 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | int main(void) 5 | { 6 | int n; 7 | 8 | n = get_endianness(); 9 | if (n != 0) 10 | { 11 | printf("Little Endian\n"); 12 | } 13 | else 14 | { 15 | printf("Big Endian\n"); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - Function thast value of a bit at a given index. 5 | * @n: decimal number that cames from parameter. 6 | * @index: index number. 7 | * Return: value. 8 | */ 9 | 10 | int get_bit(unsigned long int n, unsigned int index) 11 | { 12 | int bit_man; 13 | 14 | bit_man = (n >> index); 15 | if (index > 32) 16 | return (-1); 17 | return (bit_man & 1); 18 | } 19 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = get_bit(1024, 10); 14 | printf("%d\n", n); 15 | n = get_bit(98, 1); 16 | printf("%d\n", n); 17 | n = get_bit(1024, 0); 18 | printf("%d\n", n); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | set_bit(&n, 5); 15 | printf("%lu\n", n); 16 | n = 0; 17 | set_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | set_bit(&n, 0); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * set_bit - Function that sets the value of a bit to 1 at a given index. 6 | * @n: decimal number that cames from parameter. 7 | * @index: index number. 8 | * Return: value. 9 | */ 10 | 11 | int set_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/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * clear_bit - A function that sets the value of a bit to 0 at a given index. 6 | * @n: decimal number that cames from parameter. 7 | * @index: index number. 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/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int n; 12 | 13 | n = 1024; 14 | clear_bit(&n, 10); 15 | printf("%lu\n", n); 16 | n = 0; 17 | clear_bit(&n, 10); 18 | printf("%lu\n", n); 19 | n = 98; 20 | clear_bit(&n, 1); 21 | printf("%lu\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - Afunction that number of bits you would need to flip. 5 | * @n: decimal number that cames from parameter. 6 | * @m: unsigned lonf int number. 7 | * Return: n_flips returns. 8 | */ 9 | 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 11 | { 12 | unsigned int i, n_flips = 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 | n_flips += 1; 19 | n = n >> 1; 20 | m = m >> 1; 21 | } 22 | return (n_flips); 23 | } 24 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | unsigned int n; 12 | 13 | n = flip_bits(1024, 1); 14 | printf("%u\n", n); 15 | n = flip_bits(402, 98); 16 | printf("%u\n", n); 17 | n = flip_bits(1024, 3); 18 | printf("%u\n", n); 19 | n = flip_bits(1024, 1025); 20 | printf("%u\n", n); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/_putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _putchar - A function that writes the character c to stdout. 6 | * @c: The character to print that comes from parameter. 7 | */ 8 | 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x14-bit_manipulation/a -------------------------------------------------------------------------------- /0x14-bit_manipulation/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x14-bit_manipulation/b -------------------------------------------------------------------------------- /0x14-bit_manipulation/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x14-bit_manipulation/c -------------------------------------------------------------------------------- /0x14-bit_manipulation/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x14-bit_manipulation/d -------------------------------------------------------------------------------- /0x14-bit_manipulation/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x14-bit_manipulation/e -------------------------------------------------------------------------------- /0x14-bit_manipulation/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x14-bit_manipulation/f -------------------------------------------------------------------------------- /0x14-bit_manipulation/h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x14-bit_manipulation/h -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | unsigned int binary_to_uint(const char *b); 6 | void print_binary(unsigned long int n); 7 | int get_bit(unsigned long int n, unsigned int index); 8 | int set_bit(unsigned long int *n, unsigned int index); 9 | int clear_bit(unsigned long int *n, unsigned int index); 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 11 | int get_endianness(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x15-file_io/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | ssize_t n; 13 | 14 | if (ac != 2) 15 | { 16 | dprintf(2, "Usage: %s filename\n", av[0]); 17 | exit(1); 18 | } 19 | n = read_textfile(av[1], 114); 20 | printf("\n(printed chars: %li)\n", n); 21 | n = read_textfile(av[1], 1024); 22 | printf("\n(printed chars: %li)\n", n); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * read_textfile - Reads a text file and prints the text file. 4 | * @filename: Name of file to be readed by the program. 5 | * @letters: The number of letters ithat will be read and print. 6 | * Return: 2 success, otherwise 0 7 | */ 8 | 9 | ssize_t read_textfile(const char *filename, size_t letters) 10 | { 11 | char *print_field; 12 | int file, read_file; 13 | 14 | if (!filename) 15 | return (0); 16 | print_field = malloc(letters * sizeof(char)); 17 | if (print_field == NULL) 18 | return (0); 19 | file = open(filename, O_RDONLY); 20 | if (file == -1) 21 | return (0); 22 | read_file = read(file, print_field, letters); 23 | write(STDOUT_FILENO, print_field, read_file); 24 | 25 | close(file); 26 | free(print_field); 27 | return (read_file); 28 | } 29 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * create_file - Creates a file in separet part. 4 | * @filename: Name of file to be readed by the program. 5 | * @text_content: The content of letters ithat will be read and print. 6 | * Return: 2 success, otherwise 0 7 | */ 8 | 9 | int create_file(const char *filename, char *text_content) 10 | { 11 | int file, i; 12 | 13 | if (filename == NULL) 14 | return (-1); 15 | 16 | file = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600); 17 | if (file == -1) 18 | return (-1); 19 | if (text_content == NULL) 20 | text_content = ""; 21 | for (i = 0; *(text_content + i) != '\0'; i++) 22 | ; 23 | write(file, text_content, i); 24 | close(file); 25 | return (1); 26 | } 27 | -------------------------------------------------------------------------------- /0x15-file_io/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = create_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * append_text_to_file - Append text to the end of file contiously. 4 | * @filename: Name of file to be readed by the program. 5 | * @text_content: The content of letters ithat will be read and print. 6 | * Return: 2 success, otherwise 0 7 | */ 8 | 9 | int append_text_to_file(const char *filename, char *text_content) 10 | { 11 | int file, i; 12 | 13 | if (filename == NULL) 14 | return (-1); 15 | 16 | file = open(filename, O_WRONLY | O_APPEND); 17 | if (file == -1) 18 | return (-1); 19 | 20 | if (text_content == NULL) 21 | text_content = ""; 22 | 23 | for (i = 0; *(text_content + i) != '\0'; i++) 24 | ; 25 | write(file, text_content, i); 26 | close(file); 27 | return (1); 28 | } 29 | -------------------------------------------------------------------------------- /0x15-file_io/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int ac, char **av) 11 | { 12 | int res; 13 | 14 | if (ac != 3) 15 | { 16 | dprintf(2, "Usage: %s filename text\n", av[0]); 17 | exit(1); 18 | } 19 | res = append_text_to_file(av[1], av[2]); 20 | printf("-> %i)\n", res); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x15-file_io/Incitatous: -------------------------------------------------------------------------------- 1 | Why you should think twice before putting pictures on social media. 2 | (What you always wanted to know about @Incitatous) 3 | #PrivacyAware 4 | http://imgur.com/a/Mq1tc 5 | -------------------------------------------------------------------------------- /0x15-file_io/Requiescat: -------------------------------------------------------------------------------- 1 | Requiescat 2 | by Oscar Wilde 3 | 4 | Tread lightly, she is near 5 | Under the snow, 6 | Speak gently, she can hear 7 | The daisies grow. 8 | 9 | All her bright golden hair 10 | Tarnished with rust, 11 | She that was young and fair 12 | Fallen to dust. 13 | 14 | Lily-like, white as snow, 15 | She hardly knew 16 | She was a woman, so 17 | Sweetly she grew. 18 | 19 | Coffin-board, heavy stone, 20 | Lie on her breast, 21 | I vex my heart alone, 22 | She is at rest. 23 | 24 | Peace, Peace, she cannot hear 25 | Lyre or sonnet, 26 | All my life's buried here, 27 | Heap earth upon it. 28 | -------------------------------------------------------------------------------- /0x15-file_io/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x15-file_io/a -------------------------------------------------------------------------------- /0x15-file_io/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x15-file_io/b -------------------------------------------------------------------------------- /0x15-file_io/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x15-file_io/c -------------------------------------------------------------------------------- /0x15-file_io/cp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x15-file_io/cp -------------------------------------------------------------------------------- /0x15-file_io/elf_header: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x15-file_io/elf_header -------------------------------------------------------------------------------- /0x15-file_io/hello: -------------------------------------------------------------------------------- 1 | Hello -------------------------------------------------------------------------------- /0x15-file_io/incitatous: -------------------------------------------------------------------------------- 1 | Why you should think twice before putting pictures on social media. 2 | (What you always wanted to know about @Incitatous) 3 | #PrivacyAware 4 | http://imgur.com/a/Mq1tc 5 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_N 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int _putchar(char); 13 | ssize_t read_textfile(const char *filename, size_t letters); 14 | int create_file(const char *filename, char *text_content); 15 | int append_text_to_file(const char *filename, char *text_content); 16 | 17 | void check_elf(unsigned char *e_ident); 18 | void print_magic(unsigned char *e_ident); 19 | void print_class(unsigned char *e_ident); 20 | void print_data(unsigned char *e_ident); 21 | void print_version(unsigned char *e_ident); 22 | void print_abi(unsigned char *e_ident); 23 | void print_osabi(unsigned char *e_ident); 24 | void print_type(unsigned int e_type, unsigned char *e_ident); 25 | void print_entry(unsigned long int e_entry, unsigned char *e_ident); 26 | void close_elf(int elf); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = new; 30 | n = print_dlistint(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_dlistint - print all the elements of a dlistint_t list 6 | * @h: pointer to the start of the linked list 7 | * 8 | * Return: number of nodes 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | size_t i; 13 | 14 | for (i = 0; h != NULL; i++) 15 | { 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | } 19 | return (i); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - count the number of nodes in the linked list 5 | * @h: pointer to the beginning of a linked list 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t i; 12 | 13 | for (i = 0; h != NULL; i++) 14 | h = h->next; 15 | return (i); 16 | } 17 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *new; 15 | dlistint_t hello = {8, NULL, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(dlistint_t)); 20 | if (new == NULL) 21 | { 22 | dprintf(2, "Error: Can't malloc\n"); 23 | return (EXIT_FAILURE); 24 | } 25 | new->n = 9; 26 | head->prev = new; 27 | new->next = head; 28 | new->prev = NULL; 29 | head = new; 30 | n = dlistint_len(head); 31 | printf("-> %lu elements\n", n); 32 | free(new); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 2 | 3 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - Adds a new node at the beginning 5 | * 6 | * @head: Head 7 | * 8 | * @n: Value 9 | * 10 | * Return: dlistint_t 11 | */ 12 | 13 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 14 | { 15 | 16 | dlistint_t *temp, *node = malloc(sizeof(dlistint_t)); 17 | 18 | 19 | if (node == NULL) 20 | return (NULL); 21 | node->n = n; 22 | node->prev = NULL; 23 | node->next = NULL; 24 | 25 | if (*head == NULL) 26 | { 27 | node->next = NULL; 28 | *head = node; 29 | return (*head); 30 | } 31 | temp = *head; 32 | node->next = temp; 33 | temp->prev = node; 34 | *head = node; 35 | return (node); 36 | } 37 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint(&head, 0); 17 | add_dnodeint(&head, 1); 18 | add_dnodeint(&head, 2); 19 | add_dnodeint(&head, 3); 20 | add_dnodeint(&head, 4); 21 | add_dnodeint(&head, 98); 22 | add_dnodeint(&head, 402); 23 | add_dnodeint(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - Adds a new node at the end 5 | * 6 | * @head: struct 7 | * 8 | * @n: const int 9 | * 10 | * Return: dlistint_t 11 | */ 12 | 13 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 14 | { 15 | dlistint_t *list = NULL, *end = *head; 16 | 17 | list = malloc(sizeof(dlistint_t)); 18 | 19 | if (list == NULL) 20 | { 21 | return (NULL); 22 | } 23 | list->n = n; 24 | list->prev = NULL; 25 | list->next = NULL; 26 | 27 | if (*head == NULL) 28 | { 29 | *head = list; 30 | return (*head); 31 | } 32 | 33 | while (end->next != NULL) 34 | { 35 | end = end->next; 36 | } 37 | list->prev = end; 38 | end->next = list; 39 | 40 | return (list); 41 | } 42 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - Free list 5 | * 6 | * @head: struct 7 | * 8 | * Return: void 9 | */ 10 | 11 | void free_dlistint(dlistint_t *head) 12 | { 13 | dlistint_t *list; 14 | 15 | while (head) 16 | { 17 | list = head; 18 | head = head->next; 19 | free(list); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | free_dlistint(head); 26 | head = NULL; 27 | return (EXIT_SUCCESS); 28 | } 29 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - Nth node 5 | * 6 | * @head: pointer to head node 7 | * 8 | * @index: int 9 | * 10 | * Return: dlistint_t 11 | */ 12 | 13 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 14 | { 15 | unsigned int count = 0; 16 | 17 | if (head == NULL) 18 | { 19 | return (NULL); 20 | } 21 | 22 | while (head != NULL) 23 | { 24 | if (index == count) 25 | { 26 | return (head); 27 | } 28 | count++; 29 | head = head->next; 30 | } 31 | return (NULL); 32 | } 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | dlistint_t *node; 15 | 16 | head = NULL; 17 | add_dnodeint_end(&head, 0); 18 | add_dnodeint_end(&head, 1); 19 | add_dnodeint_end(&head, 2); 20 | add_dnodeint_end(&head, 3); 21 | add_dnodeint_end(&head, 4); 22 | add_dnodeint_end(&head, 98); 23 | add_dnodeint_end(&head, 402); 24 | add_dnodeint_end(&head, 1024); 25 | print_dlistint(head); 26 | node = get_dnodeint_at_index(head, 5); 27 | printf("%d\n", node->n); 28 | free_dlistint(head); 29 | head = NULL; 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | int sum; 15 | 16 | head = NULL; 17 | add_dnodeint_end(&head, 0); 18 | add_dnodeint_end(&head, 1); 19 | add_dnodeint_end(&head, 2); 20 | add_dnodeint_end(&head, 3); 21 | add_dnodeint_end(&head, 4); 22 | add_dnodeint_end(&head, 98); 23 | add_dnodeint_end(&head, 402); 24 | add_dnodeint_end(&head, 1024); 25 | sum = sum_dlistint(head); 26 | printf("sum = %d\n", sum); 27 | free_dlistint(head); 28 | head = NULL; 29 | return (EXIT_SUCCESS); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - sums all of the data of a dlistint_t linked list 5 | * @head: pointer to the beginning of the linked list 6 | * 7 | * Return: sum of all data, or 0 if the list is empty 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head != NULL) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | return (sum); 19 | } 20 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | printf("-----------------\n"); 26 | insert_dnodeint_at_index(&head, 5, 4096); 27 | print_dlistint(head); 28 | free_dlistint(head); 29 | head = NULL; 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/keygen5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x17-doubly_linked_lists/keygen5 -------------------------------------------------------------------------------- /0x18-dynamic_libraries/.libdynamic.so.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/.libdynamic.so.swp -------------------------------------------------------------------------------- /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/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/0-main.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc *.c -c -fPIC 3 | gcc *.o -shared -o liball.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/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/c-files/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _isupper - checks for uppercase character 6 | * @c: Variable text 7 | * Return: Always 0. 8 | */ 9 | 10 | int _isupper(int c) 11 | { 12 | if (c >= 'A' && c <= 'Z') 13 | { 14 | return (1); 15 | } 16 | else 17 | { 18 | return (0); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | * 9 | * PSEUDOCODE 10 | * Loop through "s" using "n" as limit 11 | * replace s[i] with "b" 12 | * return s 13 | */ 14 | char *_memset(char *s, char b, unsigned int n) 15 | { 16 | unsigned int i; 17 | 18 | for (i = 0; i < n; i++) 19 | *(s + i) = b; 20 | 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * Return: the pointer to dest. 7 | */ 8 | char *_strcat(char *dest, char *src) 9 | { 10 | int count = 0, count2 = 0; 11 | 12 | while (*(dest + count) != '\0') 13 | { 14 | count++; 15 | } 16 | 17 | while (count2 >= 0) 18 | { 19 | *(dest + count) = *(src + count2); 20 | if (*(src + count2) == '\0') 21 | break; 22 | count++; 23 | count2++; 24 | } 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - function that check if it's a digit 5 | * @c: takes in a character/digit 6 | * Return: 1 if digit and 0 if anything else 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | * 9 | * PSEUDOCODE 10 | * Looping through the length of bytes n 11 | * dest[i] = src[i] 12 | */ 13 | char *_memcpy(char *dest, char *src, unsigned int n) 14 | { 15 | unsigned int i; 16 | 17 | for (i = 0; i < n; i++) 18 | *(dest + i) = *(src + i); 19 | 20 | return (dest); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * 9 | * Return: a pointer to the resulting string dest 10 | */ 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | i++; 20 | 21 | while (src[j] != '\0' && j < n) 22 | { 23 | dest[i] = src[j]; 24 | i++; 25 | j++; 26 | } 27 | 28 | dest[i] = '\0'; 29 | 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * 7 | * Return: the int converted from the string 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int i, d, n, len, f, digit; 13 | 14 | i = 0; 15 | d = 0; 16 | n = 0; 17 | len = 0; 18 | f = 0; 19 | digit = 0; 20 | 21 | while (s[len] != '\0') 22 | len++; 23 | 24 | while (i < len && f == 0) 25 | { 26 | if (s[i] == '-') 27 | ++d; 28 | 29 | if (s[i] >= '0' && s[i] <= '9') 30 | { 31 | digit = s[i] - '0'; 32 | 33 | if (d % 2) 34 | digit = -digit; 35 | n = n * 10 + digit; 36 | f = 1; 37 | 38 | if (s[i + 1] < '0' || s[i + 1] > '9') 39 | break; 40 | f = 0; 41 | } 42 | i++; 43 | } 44 | 45 | if (f == 0) 46 | return (0); 47 | 48 | return (n); 49 | } 50 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurance of the character c 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string. 5 | * @s: The string to get the length of. 6 | * 7 | * Return: The length of @s. 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int lenStr = 0; 13 | 14 | while (*s++) 15 | lenStr++; 16 | 17 | return (lenStr); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copy a string 5 | * @dest: input value 6 | * @src: input value 7 | * @n: input value 8 | * 9 | * Return: dest 10 | */ 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int j; 14 | 15 | j = 0; 16 | while (j < n && src[j] != '\0') 17 | { 18 | dest[j] = src[j]; 19 | j++; 20 | } 21 | while (j < n) 22 | { 23 | dest[j] = '\0'; 24 | j++; 25 | } 26 | 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Retuns 1 if char is lowercase character 5 | * else, return 0 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for lowercase. 0 for the rest. 10 | */ 11 | 12 | int _islower(int c) 13 | { 14 | if (c >= 97 && c <= 122) 15 | { 16 | return (1); 17 | } 18 | else 19 | { 20 | return (0); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | 8 | void _puts(char *str) 9 | { 10 | int i; 11 | 12 | for (i = 0; str[i] != '\0'; i++) 13 | { 14 | _putchar(str[i]); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare string values 5 | * @s1: input value 6 | * @s2: input value 7 | * 8 | * Return: s1[i] - s2[i] 9 | */ 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (s1[i] != '\0' && s2[i] != '\0') 16 | { 17 | if (s1[i] != s2[i]) 18 | { 19 | return (s1[i] - s2[i]); 20 | } 21 | i++; 22 | } 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - Gets the length of a prefix substring. 4 | * @s: The string to be searched. 5 | * @accept: The prefix to be measured. 6 | * 7 | * Return: The number of bytes in s which 8 | * consist only of bytes from accept. 9 | * 10 | * PSEUDOCODE 11 | * Loop through s and compare s[i] to values in accept 12 | * increase bytes counter when values in accept can be found 13 | * *in s 14 | * end loop and return bytes when accept is exhaused 15 | */ 16 | unsigned int _strspn(char *s, char *accept) 17 | { 18 | unsigned int bytes = 0; 19 | int index; 20 | 21 | while (*s) 22 | { 23 | for (index = 0; accept[index]; index++) 24 | { 25 | if (*s == accept[index]) 26 | { 27 | bytes++; 28 | break; 29 | } 30 | 31 | else if (accept[index + 1] == '\0') 32 | return (bytes); 33 | } 34 | 35 | s++; 36 | } 37 | 38 | return (bytes); 39 | } 40 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Retuns 1 if char c is a letter, 5 | * and a lowercase or uppercase 6 | * 7 | * @c: The character in ASCII code 8 | * 9 | * Return: 1 for lowercase. 0 for the rest. 10 | */ 11 | 12 | int _isalpha(int c) 13 | { 14 | if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)) 15 | { 16 | return (1); 17 | } 18 | else 19 | { 20 | return (0); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | * 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | unsigned int i, j; 14 | 15 | for (i = 0; *(s + i) != '\0'; i++) 16 | { 17 | for (j = 0; *(accept + j) != '\0'; j++) 18 | { 19 | if (*(s + i) == *(accept + j)) 20 | return (s + i); 21 | } 22 | } 23 | return ('\0'); 24 | } 25 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - a function that locates a substring 4 | * @haystack: locate a substring 5 | * @needle: substring to locate 6 | * 7 | * Return: pointer to the beginning of the located substring, 8 | * or NULL, if substring is not found 9 | */ 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | char *h = haystack; 13 | char *n = needle; 14 | 15 | while (*h) 16 | { 17 | n = needle; 18 | h = haystack; 19 | while (*n) 20 | { 21 | if (*h == *n) 22 | { 23 | n++; 24 | h++; 25 | } 26 | else 27 | break; 28 | } 29 | if (*n == '\0') 30 | return (haystack); 31 | haystack++; 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - gives the absolute value 5 | * of an integer. 6 | * 7 | * @n: input number as an integer. 8 | * 9 | * Return: the absolute value of int n 10 | */ 11 | 12 | int _abs(int n) 13 | { 14 | if (n < 0) 15 | { 16 | return (n * -1); 17 | } 18 | return (n); 19 | } 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * 10 | * Return: the pointer to dest 11 | */ 12 | 13 | char *_strcpy(char *dest, char *src) 14 | { 15 | int len, i; 16 | 17 | len = 0; 18 | 19 | while (src[len] != '\0') 20 | { 21 | len++; 22 | } 23 | 24 | for (i = 0; i < len; i++) 25 | { 26 | dest[i] = src[i]; 27 | } 28 | 29 | dest[i] = '\0'; 30 | 31 | return (dest); 32 | } 33 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/c-files/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | /** 6 | 7 | * _putchar - writes the character c to stdout 8 | 9 | * @c: The character to print 10 | 11 | * 12 | 13 | * Return: On success 1. 14 | 15 | * On error, -1 is returned, and errno is set appropriately. 16 | 17 | */ 18 | 19 | int _putchar(char c) 20 | 21 | { 22 | 23 | return (write(1, &c, 1)); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/0-isupper.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/0-memset.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/0-strcat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/1-isdigit.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/1-memcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/1-strncat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/100-atoi.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/2-strchr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/2-strlen.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/2-strncpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/3-islower.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/3-puts.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/3-strcmp.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/3-strspn.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/4-isalpha.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/4-strpbrk.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/5-strstr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/6-abs.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/9-strcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/object-files/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x18-dynamic_libraries/object-files/_putchar.o -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creates a hash table 5 | * @size: size of the array 6 | * 7 | * Return: pointer to the newly created hash table 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *hash_table; 12 | unsigned long int i; 13 | 14 | hash_table = malloc(sizeof(hash_table_t)); 15 | if (hash_table == NULL) 16 | return (NULL); 17 | hash_table->size = size; 18 | hash_table->array = malloc(size * sizeof(hash_node_t *)); 19 | if (hash_table->array == NULL) 20 | { 21 | free(hash_table); 22 | return (NULL); 23 | } 24 | for (i = 0; i < size; i++) 25 | hash_table->array[i] = NULL; 26 | return (hash_table); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | /** 2 | * hash_djb2 - implementation of the djb2 algorithm 3 | * @str: string used to generate hash value 4 | * 5 | * Return: hash value 6 | */ 7 | unsigned long int hash_djb2(const unsigned char *str) 8 | { 9 | unsigned long int hash; 10 | int c; 11 | 12 | hash = 5381; 13 | while ((c = *str++)) 14 | { 15 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 16 | } 17 | return (hash); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | shash_table_t *ht; 14 | 15 | ht = shash_table_create(1024); 16 | shash_table_set(ht, "y", "0"); 17 | shash_table_print(ht); 18 | shash_table_set(ht, "j", "1"); 19 | shash_table_print(ht); 20 | shash_table_set(ht, "c", "2"); 21 | shash_table_print(ht); 22 | shash_table_set(ht, "b", "3"); 23 | shash_table_print(ht); 24 | shash_table_set(ht, "z", "4"); 25 | shash_table_print(ht); 26 | shash_table_set(ht, "n", "5"); 27 | shash_table_print(ht); 28 | shash_table_set(ht, "a", "6"); 29 | shash_table_print(ht); 30 | shash_table_set(ht, "m", "7"); 31 | shash_table_print(ht); 32 | shash_table_print_rev(ht); 33 | shash_table_delete(ht); 34 | return (EXIT_SUCCESS); 35 | } 36 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - returns the index of a key 5 | * @key: key 6 | * @size: size of the array of the hash table 7 | * Return: index 8 | */ 9 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 10 | { 11 | unsigned long int index = hash_djb2(key) % size; 12 | 13 | return (index); 14 | } 15 | -------------------------------------------------------------------------------- /0x1A-hash_tables/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x1E-search_algorithms/0-linear -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of integers 5 | * using Linear search 6 | * @array: pointer to first element of the array 7 | * @size: number of elements in array 8 | * @value: value to search 9 | * 10 | * Return: 1st index where value is located or -1 on NULL 11 | * array input or if value is absent 12 | */ 13 | int linear_search(int *array, size_t size, int value) 14 | { 15 | size_t i; 16 | 17 | if (!array || size <= 0) 18 | return (-1); 19 | 20 | i = 0; 21 | while (i < size) 22 | { 23 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 24 | if (array[i] == value) 25 | return (i); 26 | i++; 27 | } 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x1E-search_algorithms/1-binary -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); 18 | printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x1E-search_algorithms/100-jump -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 6, jump_search(array, size, 6)); 18 | printf("Found %d at index: %d\n\n", 1, jump_search(array, size, 1)); 19 | printf("Found %d at index: %d\n", 999, jump_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x1E-search_algorithms/102-interpolation -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, interpolation_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 7, interpolation_search(array, size, 7)); 19 | printf("Found %d at index: %d\n", 999, interpolation_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x1E-search_algorithms/103-exponential -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 54, 61, 62, 76, 99 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 62, exponential_search(array, size, 62)); 18 | printf("Found %d at index: %d\n\n", 3, exponential_search(array, size, 3)); 19 | printf("Found %d at index: %d\n", 999, exponential_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Emakiflom/alx-low_level_programming/d1c545a4f6681ba4ebc241484c1ef3cd1ebeb710/0x1E-search_algorithms/104-advanced_binary -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 8, advanced_binary(array, size, 8)); 18 | printf("Found %d at index: %d\n\n", 5, advanced_binary(array, size, 5)); 19 | printf("Found %d at index: %d\n", 999, advanced_binary(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | listint_t *create_list(int *array, size_t size); 6 | void print_list(const listint_t *list); 7 | void free_list(listint_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | listint_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_list(array, size); 23 | print_list(list); 24 | 25 | res = jump_list(list, size, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = jump_list(list, size, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = jump_list(list, size, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_list(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | skiplist_t *create_skiplist(int *array, size_t size); 6 | void print_skiplist(const skiplist_t *list); 7 | void free_skiplist(skiplist_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | skiplist_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_skiplist(array, size); 23 | print_skiplist(list); 24 | 25 | res = linear_skip(list, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = linear_skip(list, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = linear_skip(list, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_skiplist(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /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/test/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | this is my alx project on if els 0x01-variables_if_else_while --------------------------------------------------------------------------------