├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts ├── 4-puts.c ├── 5-printf ├── 5-printf.c ├── 6-size.c ├── README.md ├── size32 └── size64 ├── 0x01-variables_if_else_while ├── 0-positive_or_negative ├── 0-positive_or_negative.c ├── 1-last_digit ├── 1-last_digit.c ├── 100-print_comb3 ├── 100-print_comb3.c ├── 101-print_comb4 ├── 101-print_comb4.c ├── 102-print_comb5 ├── 102-print_comb5.c ├── 2-print_alphabet ├── 2-print_alphabet.c ├── 3-print_alphabets ├── 3-print_alphabets.c ├── 4-print_alphabt ├── 4-print_alphabt.c ├── 5-print_numbers ├── 5-print_numbers.c ├── 6-print_numberz ├── 6-print_numberz.c ├── 7-print_tebahpla ├── 7-print_tebahpla.c ├── 8-print_base16 ├── 8-print_base16.c ├── 9-print_comb ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── .100-times_table.c.swp ├── 0-putchar ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural ├── 101-natural.c ├── 102-fibonacci ├── 102-fibonacci.c ├── 103-fibonacci ├── 103-fibonacci.c ├── 104-fibonacci ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md ├── main.c └── 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-main.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 ├── _putchar.c └── 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 ├── .libmy.a.swp ├── 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 ├── _putchar.c └── main.h ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-main.c ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md ├── a └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-main.c ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md ├── a └── lists.h ├── 0x13-more_singly_linked_lists ├── .101-print_listint_safe.c.swp ├── 0-main.c ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 10-main.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 103-find_loop.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-main.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md ├── a ├── g ├── k └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md ├── crackme3 └── main.h ├── 0x15-file_io ├── .100-elf_header.c.swp ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── .103-keygen.c.swp ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 7-main.c ├── 8-delete_dnodeint.c ├── README.md ├── lists.h └── result.py ├── 0x18-dynamic_libraries ├── .libdynamic.so.swp ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-create_dynamic_lib.sh ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 100-operations.so ├── 100-tests.py ├── 101-make_me_win.sh ├── 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 ├── libdynamic.so ├── main.h ├── putshack.c ├── putshack.o └── putshack.so ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 0-main.c ├── 1-djb2.c ├── 1-main.c ├── 100-main.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 2-main.c ├── 3-hash_table_set.c ├── 3-main.c ├── 4-hash_table_get.c ├── 4-main.c ├── 5-hash_table_print.c ├── 5-main.c ├── 6-hash_table_delete.c ├── 6-main.c ├── README.md ├── a ├── b ├── c ├── e ├── f ├── g ├── hash_tables.h └── sht ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py └── README.md ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x00-hello_world/101-quote -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 10 | return (1); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x00-hello_world/4-puts -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | puts("\"Programming is like building a multilingual puzzle"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x00-hello_world/5-printf -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | printf("with proper grammar, but the outcome is a piece of art,\n"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | printf("Size of a char: %lu byte(s)\n", sizeof(char)); 10 | printf("Size of an int: %lu byte(s)\n", sizeof(int)); 11 | printf("Size of a long int: %lu byte(s)\n", sizeof(long int)); 12 | printf("Size of a long long int: %lu byte(s)\n", sizeof(long long int)); 13 | printf("Size of a float: %lu byte(s)\n", sizeof(float)); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | 0x00-hello_world 2 | -------------------------------------------------------------------------------- /0x00-hello_world/size32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x00-hello_world/size32 -------------------------------------------------------------------------------- /0x00-hello_world/size64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x00-hello_world/size64 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/0-positive_or_negative -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | *main - entry point of programs 6 | * 7 | *Return: return 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | if (n < 0) 16 | { 17 | printf("%d is negative\n", n); 18 | } 19 | else if (n > 0) 20 | { 21 | printf("%d is positive\n", n); 22 | } 23 | else 24 | printf("%d is zero\n", n); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/1-last_digit -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | *main - Prints the last digit of random number 6 | * 7 | *Return: Return 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | int last; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | last = n % 10; 17 | 18 | if (last == 0) 19 | { 20 | printf("Last digit of %d is %d and is 0\n", n, last); 21 | } 22 | else 23 | { 24 | if (last > 5) 25 | { 26 | printf("Last digit of %d is %d and is greater than 5\n", n, last); 27 | } 28 | else if (last < 6) 29 | { 30 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, last); 31 | } 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/100-print_comb3 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - Prints all combinations of two digits with, 4 | * and space followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit1, digit2; 11 | 12 | for (digit1 = 0; digit1 < 9; digit1++) 13 | { 14 | for (digit2 = digit1 + 1; digit2 < 10; digit2++) 15 | { 16 | 17 | putchar((digit1 % 10) + '0'); 18 | putchar((digit2 % 10) + '0'); 19 | 20 | if (digit1 == 8 && digit2 == 9) 21 | continue; 22 | 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/101-print_comb4 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - Prints all combinations of three digits with, 4 | * and space followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit1, digit2, digit3; 11 | 12 | for (digit1 = 0; digit1 < 9; digit1++) 13 | { 14 | for (digit2 = digit1 + 1; digit2 < 9; digit2++) 15 | { 16 | for (digit3 = digit2 + 1; digit3 < 10; digit3++) 17 | { 18 | 19 | putchar((digit1 % 10) + '0'); 20 | putchar((digit2 % 10) + '0'); 21 | putchar((digit3 % 10) + '0'); 22 | 23 | if (digit1 == 7 && digit2 == 8 && digit3 == 9) 24 | continue; 25 | 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/102-print_comb5 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - Prints all combinations of two two digits with, 4 | * and space followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit1, digit2; 11 | 12 | for (digit1 = 0; digit1 <= 98; digit1++) 13 | { 14 | for (digit2 = digit1 + 1; digit2 <= 99; digit2++) 15 | { 16 | putchar((digit1 / 10) + '0'); 17 | putchar((digit1 % 10) + '0'); 18 | putchar(32); 19 | putchar((digit2 / 10) + '0'); 20 | putchar((digit2 % 10) + '0'); 21 | 22 | if (digit1 / 10 != 9 || digit1 % 10 != 8) 23 | { 24 | putchar(44); 25 | putchar(32); 26 | } 27 | } 28 | } 29 | putchar(10); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/2-print_alphabet -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the alphabet. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char alp[26] = "abcdefghijklmnopqrstuvwxyz"; 11 | int i; 12 | 13 | for (i = 0; i < 26; i++) 14 | { 15 | putchar(alp[i]); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/3-print_alphabets -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Program to print alphabet letters in all cases followed by new line 5 | * 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | int letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | { 14 | letter = tolower(letter); 15 | putchar(letter); 16 | if (letter == 'z') 17 | { 18 | letter = 'A'; 19 | for (; letter <= 'Z'; letter++) 20 | { 21 | putchar(letter); 22 | } 23 | break; 24 | } 25 | } 26 | putchar('\n'); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/4-print_alphabt -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Program to print alphabet letters except q and e followed by new line 5 | * 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | int letter; 11 | 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | { 14 | if (letter == 'q' || letter == 'e') 15 | { 16 | continue; 17 | } 18 | putchar(letter); 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/5-print_numbers -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Prints all single digits of base 10 followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit; 11 | 12 | for (digit = '0'; digit <= '9'; digit++) 13 | { 14 | putchar(digit); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/6-print_numberz -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers from 0 to 9. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = 48; i < 58; i++) 13 | { 14 | putchar(i); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/7-print_tebahpla -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Program to print alphabet letters in reverse followed by new line 5 | * 6 | *Return: return 0 7 | */ 8 | int main(void) 9 | { 10 | int letter; 11 | 12 | for (letter = 'z'; letter >= 'a'; letter--) 13 | { 14 | putchar(letter); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/8-print_base16 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Prints all single digits of base 16 followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit; 11 | 12 | for (digit = '0'; digit <= '9'; digit++) 13 | { 14 | putchar(digit); 15 | if (digit == '9') 16 | { 17 | digit = 'a'; 18 | for (; digit <= 'f'; digit++) 19 | { 20 | putchar(digit); 21 | } 22 | break; 23 | } 24 | } 25 | putchar('\n'); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x01-variables_if_else_while/9-print_comb -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - Prints all single digits with , and space followed by new line 5 | * 6 | *Return: returns 0 7 | */ 8 | int main(void) 9 | { 10 | int digit; 11 | 12 | for (digit = '0'; digit <= '9'; digit++) 13 | { 14 | putchar(digit); 15 | if (digit == '9') 16 | continue; 17 | 18 | putchar(','); 19 | putchar(' '); 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | 0x01-variables_if_else_while 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.100-times_table.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x02-functions_nested_loops/.100-times_table.c.swp -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x02-functions_nested_loops/0-putchar -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 0 (success) 8 | */ 9 | 10 | int main(void) 11 | { 12 | 13 | printf("_putchar\n"); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_alphabet - prints all alphabets in lowercase. 4 | * Return: On success 1. 5 | */ 6 | void print_alphabet(void) 7 | { 8 | char i; 9 | 10 | for (i = 'a'; i <= 'z'; i++) 11 | { 12 | _putchar(i); 13 | } 14 | _putchar('\n'); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * add - adds two integers and returnsthe result 4 | * 5 | * @x: number being added 6 | * 7 | * @y: number being added 8 | * 9 | * Return: Always 0. 10 | * 11 | */ 12 | int add(int x, int y) 13 | { 14 | return (x + y); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_times_table - prints the n times table, starting with 0 4 | * @n: number of the times table 5 | */ 6 | void print_times_table(int n) 7 | { 8 | int i, j, k; 9 | 10 | if (n >= 0 && n <= 15) 11 | { 12 | for (i = 0; i <= n; i++) 13 | { 14 | for (j = 0; j <= n; j++) 15 | { 16 | k = j * i; 17 | if (j == 0) 18 | { 19 | _putchar(k + '0'); 20 | } else if (k < 10 && j != 0) 21 | { 22 | _putchar(','); 23 | _putchar(' '); 24 | _putchar(' '); 25 | _putchar(' '); 26 | _putchar(k + '0'); 27 | } else if (k >= 10 && k < 100) 28 | { 29 | _putchar(','); 30 | _putchar(' '); 31 | _putchar(' '); 32 | _putchar((k / 10) + '0'); 33 | _putchar((k % 10) + '0'); 34 | } else if (k >= 100) 35 | { 36 | _putchar(','); 37 | _putchar(' '); 38 | _putchar((k / 100) + '0'); 39 | _putchar(((k / 10) % 10) + '0'); 40 | _putchar((k % 10) + '0'); 41 | } 42 | } 43 | _putchar('\n'); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x02-functions_nested_loops/101-natural -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - computes and prints the sum of all the multiples 5 | * of 3 or 5 below 1024 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int sum3, sum5, sum; 11 | int i; 12 | 13 | sum3 = 0; 14 | sum5 = 0; 15 | sum = 0; 16 | 17 | for (i = 0; i < 1024; ++i) 18 | { 19 | if ((i % 3) == 0) 20 | { 21 | sum3 = sum3 + i; 22 | } else if ((i % 5) == 0) 23 | { 24 | sum5 = sum5 + i; 25 | } 26 | } 27 | sum = sum3 + sum5; 28 | printf("%lu\n", sum); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x02-functions_nested_loops/102-fibonacci -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the first 50 Fibonacci numbers, starting with 1 and 2 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | long int i, j, k, next; 11 | 12 | j = 1; 13 | 14 | k = 2; 15 | 16 | for (i = 1; i <= 50; ++i) 17 | { 18 | if (j != 20365011074) 19 | { 20 | printf("%ld, ", j); 21 | } else 22 | { 23 | printf("%ld\n", j); 24 | } 25 | next = j + k; 26 | j = k; 27 | k = next; 28 | } 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x02-functions_nested_loops/103-fibonacci -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the sum of the even-valued terms 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | unsigned long int j, k, next, sum; 12 | 13 | j = 1; 14 | k = 2; 15 | sum = 0; 16 | 17 | for (i = 1; i <= 33; ++i) 18 | { 19 | if (j < 4000000 && (j % 2) == 0) 20 | { 21 | sum = sum + j; 22 | } 23 | next = j + k; 24 | j = k; 25 | k = next; 26 | } 27 | 28 | printf("%lu\n", sum); 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x02-functions_nested_loops/104-fibonacci -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | /** 5 | * numLength - returns the lenth of string 6 | * @num : operand number 7 | * Return: number of digits 8 | */ 9 | 10 | int numLength(int num) 11 | { 12 | int length = 0; 13 | 14 | if (!num) 15 | { 16 | return (1); 17 | } 18 | 19 | while (num) 20 | { 21 | num = num / 10; 22 | length += 1; 23 | } 24 | 25 | return (length); 26 | } 27 | /** 28 | * main - prints the first 98 fibonaci sequences 29 | * Return: 0 30 | */ 31 | 32 | int main(void) 33 | { 34 | unsigned long f1 = 1, f2 = 2, tmp, mx = 100000000, f1o = 0, f2o = 0, tmpo = 0; 35 | short int i = 1, initial0s; 36 | 37 | while (i <= 98) 38 | { 39 | if (f1o > 0) 40 | printf("%lu", f1o); 41 | initial0s = numLength(mx) - 1 - numLength(f1); 42 | while (f1o > 0 && initial0s > 0) 43 | { 44 | printf("%i", 0); 45 | initial0s--; 46 | } 47 | printf("%lu", f1); 48 | 49 | tmp = (f1 + f2) % mx; 50 | tmpo = f1o + f2o + (f1 + f2) / mx; 51 | f1 = f2; 52 | f1o = f2o; 53 | f2 = tmp; 54 | f2o = tmpo; 55 | 56 | if (i != 98) 57 | printf(", "); 58 | else 59 | printf("\n"); 60 | i++; 61 | } 62 | return (0); 63 | } 64 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_to_98 - prints all natural numbers from n to 98 5 | * 6 | * @n: starting point 7 | * 8 | * Return: returns nothing 9 | * 10 | */ 11 | void print_to_98(int n) 12 | { 13 | if (n <= 98) 14 | { 15 | for (n = n; n <= 97; n++) 16 | printf("%d, ", n); 17 | } 18 | else 19 | for (n = n; n > 98; n--) 20 | printf("%d, ", n); 21 | printf("98\n"); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Print lowercase alphabet 10 times 5 | */ 6 | 7 | void print_alphabet_x10(void) 8 | { 9 | char num, letter; 10 | 11 | for (num = 0; num <= 9; num++) 12 | { 13 | for (letter = 'a'; letter <= 'z'; letter++) 14 | { 15 | _putchar(letter); 16 | } 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Checks for lowercase alphabet 5 | * @c: Character to be checked 6 | * Return: if character is lowercase, otherwise 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 | * _isalpha - Checks for alphabetic order 4 | * 5 | * @c: alpabet to check for 6 | * 7 | * Return: 1 if c is a letter, 0 otherwise 8 | */ 9 | int _isalpha(int c) 10 | { 11 | return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | # include "main.h" 2 | /** 3 | * print_sign-check the sign of a number. 4 | * 5 | * @n:Number whose sign is to be checked. 6 | * Return:1-if n is greater than 0: 0- if n equals 0:-1-if n is less than 0 7 | */ 8 | 9 | 10 | 11 | int print_sign(int n) 12 | { 13 | if (n > 0) 14 | { 15 | _putchar('+'); 16 | return (1); 17 | } 18 | else if (n < 0) 19 | { 20 | _putchar ('-'); 21 | return (-1); 22 | } 23 | else 24 | { 25 | _putchar('0'); 26 | return (0); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - computes the absolute value of an integrer 4 | * 5 | * @n: the int to check 6 | * 7 | * Return: the absolute value of int 8 | * 9 | */ 10 | int _abs(int n) 11 | { 12 | if (n >= 0) 13 | { 14 | return (n); 15 | } 16 | return (-n); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_last_digit-prints the last digit of an interger. 4 | * @a:number to compute last digit. 5 | * Return:last digit. 6 | */ 7 | 8 | 9 | 10 | int print_last_digit(int a) 11 | { 12 | int last_digit; 13 | 14 | last_digit = a % 10; 15 | if (last_digit < 0) 16 | { 17 | last_digit = last_digit * -1; 18 | } 19 | _putchar(last_digit + '0'); 20 | return (last_digit); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * jack_bauer - prints every minute of the day of Jack Bauer 4 | * 5 | * starting from 00:00 to 23:59 6 | * 7 | */ 8 | void jack_bauer(void) 9 | { 10 | int i, j; 11 | 12 | i = 0; 13 | 14 | while (i < 24) 15 | { 16 | j = 0; 17 | 18 | while (j < 60) 19 | { 20 | _putchar((i / 10) + '0'); 21 | _putchar((i % 10) + '0'); 22 | _putchar(':'); 23 | _putchar((j / 10) + '0'); 24 | _putchar((j % 10) + '0'); 25 | _putchar('\n'); 26 | j++; 27 | } 28 | i++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - check description 5 | * Description: It prints 9 times table starting with 0 6 | * Return: Nothing. 7 | */ 8 | void times_table(void) 9 | { 10 | int i, j, n; 11 | 12 | for (i = 0; i <= 9; i++) 13 | { 14 | for (j = 0; j <= 9; j++) 15 | { 16 | n = i * j; 17 | 18 | if ((n / 10) == 0) 19 | { 20 | if (j != 0) 21 | _putchar(' '); 22 | _putchar(n + '0'); 23 | 24 | if (j == 9) 25 | continue; 26 | _putchar(','); 27 | _putchar(' '); 28 | } 29 | else 30 | { 31 | _putchar((n / 10) + '0'); 32 | _putchar((n % 10) + '0'); 33 | if (j == 9) 34 | continue; 35 | _putchar(','); 36 | _putchar(' '); 37 | } 38 | } 39 | _putchar('\n'); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | 0x02-functions_nested_loops 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main_h 2 | #define main_h 3 | extern int _putchar(char c); 4 | extern void print_alphabet(void); 5 | extern void print_alphabet_x10(void); 6 | extern int _islower(int c); 7 | extern int _isalpha(int c); 8 | extern int print_sign(int n); 9 | extern int _abs(int); 10 | extern int print_last_digit(int); 11 | extern void jack_bauer(void); 12 | extern void times_table(void); 13 | extern int add(int, int); 14 | extern void print_to_98(int n); 15 | extern void print_times_table(int n); 16 | #endif 17 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x03-debugging/1-main -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | /* 16 | *while (i < 10) 17 | *{ 18 | *putchar(i); 19 | *} 20 | */ 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | if (a >= b && a >= c) 15 | { 16 | largest = a; 17 | } 18 | else if (b >= a && b >= c) 19 | { 20 | largest = b; 21 | } 22 | else 23 | { 24 | largest = c; 25 | } 26 | return (largest); 27 | } 28 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * print_remaining_days - takes a date and prints how many days are 5 | * left in the year, taking leap years into account 6 | * @month: month in number format 7 | * @day: day of month 8 | * @year: year 9 | * Return: void 10 | */ 11 | void print_remaining_days(int month, int day, int year) 12 | { 13 | if ((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0)) 14 | { 15 | if (month > 2 && day >= 60) 16 | { 17 | day++; 18 | } 19 | printf("Day of the year: %d\n", day); 20 | printf("Remaining days: %d\n", 366 - day); 21 | } 22 | else 23 | { 24 | if (month == 2 && day == 60) 25 | { 26 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 27 | } 28 | else 29 | { 30 | printf("Day of the year: %d\n", day); 31 | printf("Remaining days: %d\n", 365 - day); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | 0x03-debugging 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 98; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | void positive_or_negative(int i); 5 | int largest_number(int a, int b, int c); 6 | void print_remaining_days(int month, int day, int year); 7 | int convert_day(int month, int day); 8 | #endif 9 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isupper - function that verifies if a character is uppercase or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isupper(int c) 9 | { 10 | if ((c >= 'A') && (c <= 'Z')) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isdigit - function that verifies if a character is a digit or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isdigit(int c) 9 | { 10 | if ((c >= 48) && (c <= 57)) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_triangle - prints a triangle 4 | *@size:size parameter of triangle 5 | *Return:returns nothing 6 | */ 7 | void print_triangle(int size) 8 | { 9 | int inc1, inc2; 10 | 11 | if (size > 0) 12 | { 13 | for (inc1 = 1; inc1 <= size; inc1++) 14 | { 15 | for ((inc2 = size - inc1); inc2 > 0; inc2--) 16 | _putchar(' '); 17 | 18 | for (inc2 = 0; inc2 < inc1; inc2++) 19 | _putchar('#'); 20 | 21 | if (inc1 == size) 22 | continue; 23 | 24 | _putchar('\n'); 25 | } 26 | } 27 | _putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main- prints the largest prime factor 5 | *of a number 6 | * 7 | *Return: returns 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | long number = 612852475143; 13 | int inc; 14 | while (inc++ < number / 2) 15 | { 16 | if (number % inc == 0) 17 | { 18 | number /= 2; 19 | continue; 20 | } 21 | 22 | for (inc = 3; inc < number / 2; inc += 2) 23 | { 24 | if (number % inc == 0) 25 | number /= inc; 26 | 27 | } 28 | } 29 | printf("%ld\n", number); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_number - Prints a number 4 | * @n: The number to print 5 | * 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int num = n; 10 | if (n < 0) 11 | { 12 | _putchar('-'); 13 | num = -num; 14 | } 15 | if (num > 9) 16 | { 17 | print_number(num / 10); 18 | } 19 | _putchar(num % 10 + '0'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *mul - function that multipliestwo numbers 4 | *@a: first number 5 | *@b: second number 6 | *Return: returns result 7 | */ 8 | 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_numbers - function that print all numbers from 4 | *0 to 9 5 | * 6 | *Return: returns nothing 7 | */ 8 | void print_numbers(void) 9 | { 10 | int number = 48; 11 | for (number = 48; number < 58; number++) 12 | { 13 | _putchar(number); 14 | } 15 | _putchar(10); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_most_numbers - prints numbers except 2 and 4 4 | * 5 | *Return: returns nothing 6 | */ 7 | void print_most_numbers(void) 8 | { 9 | int number; 10 | for (number = 48; number < 58; number++) 11 | { 12 | if ((number == 50) || (number == 52)) 13 | { 14 | continue; 15 | } 16 | _putchar(number); 17 | } 18 | _putchar(10); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *more_numbers - prints numbers to 14 4 | * 5 | *Return: returns nothing 6 | */ 7 | void more_numbers(void) 8 | { 9 | int number1, number2; 10 | 11 | for (number1 = 0; number1 < 10; number1++) 12 | { 13 | for (number2 = 0; number2 <= 14; number2++) 14 | 15 | { 16 | if (number2 > 9) 17 | { 18 | _putchar((number2 / 10) + '0'); 19 | } 20 | _putchar((number2 % 10) + '0'); 21 | } 22 | _putchar(10); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_line - prints a straight line 4 | *@n:parameter 5 | *Return:returns nothing 6 | */ 7 | void print_line(int n) 8 | { 9 | while (n-- > 0) 10 | { 11 | _putchar('_'); 12 | } 13 | _putchar('\n'); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_diagonal - prints a diagonal 4 | *@n:parameter 5 | *Return:returns nothing 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | int len, space; 10 | if (n > 0) 11 | { 12 | for (len = 0; len < n; len++) 13 | { 14 | for (space = 0; space < len; space++) 15 | _putchar(' '); 16 | 17 | _putchar('\\'); 18 | 19 | if (len == (n - 1)) 20 | continue; 21 | _putchar('\n'); 22 | } 23 | } 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_square - prints squares 4 | *@size: parameter 5 | *Return:returns nothing 6 | */ 7 | void print_square(int size) 8 | { 9 | int inc1, inc2; 10 | if (size > 0) 11 | { 12 | for (inc1 = 0; inc1 < size; inc1++) 13 | { 14 | for (inc2 = 0; inc2 < (size - 1); inc2++) 15 | _putchar('#'); 16 | 17 | _putchar('#'); 18 | _putchar('\n'); 19 | } 20 | } 21 | else 22 | { 23 | _putchar('\n'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main - program that prints either number 4 | *or fizz or buzz or fizzBuzz 5 | * 6 | * 7 | *Return: returns 0 8 | */ 9 | int main(void) 10 | { 11 | int num; 12 | while (num++ < 100) 13 | 14 | if ((num % 3 == 0) && (num % 5 == 0)) 15 | printf("FizzBuzz "); 16 | 17 | else if ((num % 3) == 0) 18 | printf("Fizz "); 19 | 20 | else if ((num % 5) == 0) 21 | { 22 | if (num != 100) 23 | printf("Buzz "); 24 | 25 | else 26 | printf("Buzz"); 27 | } 28 | 29 | else 30 | printf("%d ", num); 31 | 32 | printf("\n"); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | 0x04-more_functions_nested_loops 2 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | /** 4 | *main - header file for protoypes 5 | *void protoypes (void) 6 | *int protoypes (int) 7 | */ 8 | void print_numbers(void); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | int _putchar(char); 17 | int _isupper(int); 18 | int _isdigit(int); 19 | int mul(int, int); 20 | 21 | #endif /* MAIN_H */ 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *reset_to_98 - reset value of an integer to 98 4 | *@n: parameter targeted 5 | *Return : nothing 6 | */ 7 | 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *swap_int - swap two integers 4 | *@a: first integer 5 | *@b: second integer 6 | *Return: nothing 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | int tmp = *a; 12 | *a = *b; 13 | *b = tmp; 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _atoi - changes a string to an int 3 | * @s: the string to be changed 4 | * 5 | * Return: the converted int 6 | */ 7 | int _atoi(char *s) 8 | { 9 | int i = 1; 10 | unsigned int num = 0; 11 | do { 12 | if (*s == '-') 13 | i *= -1; 14 | else if (*s >= '0' && *s <= '9') 15 | num = num * 10 + (*s - '0'); 16 | else if (num > 0) 17 | break; 18 | } while (*s++); 19 | return (num *i); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - entry point generates a random password 6 | * 7 | * Return: the generated password 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | int x; 13 | 14 | srand(time(0)); 15 | while (x <= 2645) 16 | { 17 | c = rand() % 128; 18 | x += c; 19 | putchar(c); 20 | } 21 | putchar(2772 - x); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strlen - returns the length of a string 4 | * @s: string 5 | *Return: returns lenght; 6 | */ 7 | int _strlen(char *s) 8 | { 9 | int count, inc; 10 | inc = 0; 11 | for (count = 0; s[count] != '\0'; count++) 12 | inc++; 13 | 14 | return (inc); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_puts - prints a string 4 | * @str: string to print 5 | * 6 | * Description: prints a string 7 | * On success: return the number of characters printed 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | while (*str) 13 | _putchar(*str++); 14 | 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_rev - prints a string in reverse 4 | * @s: string to print 5 | */ 6 | 7 | void print_rev(char *s) 8 | { 9 | int len = 0, index = 0; 10 | len = _strlen(s); 11 | 12 | for (index = len - 1; index >= 0; index--) 13 | _putchar(s[index]); 14 | 15 | _putchar('\n'); 16 | } 17 | 18 | /** 19 | *_strlen - returns the length of a string 20 | * @s: string 21 | *Return: returns lenght; 22 | */ 23 | int _strlen(char *s) 24 | { 25 | int count, inc; 26 | inc = 0; 27 | for (count = 0; s[count] != '\0'; count++) 28 | inc++; 29 | 30 | return (inc); 31 | } 32 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *rev_string - Reverse a string 4 | *@s: String to reverse 5 | *Return: Nothing 6 | */ 7 | void rev_string(char *s) 8 | { 9 | int i = 0, length; 10 | length = _strlen(s) - 1; 11 | while (length > i) 12 | { 13 | swap_char(s + length, s + i); 14 | i++; 15 | length--; 16 | } 17 | } 18 | 19 | /** 20 | *_strlen - returns the length of a string 21 | * @s: string 22 | *Return: returns lenght; 23 | */ 24 | int _strlen(char *s) 25 | { 26 | int count, inc; 27 | inc = 0; 28 | for (count = 0; s[count] != '\0'; count++) 29 | inc++; 30 | 31 | return (inc); 32 | } 33 | 34 | /** 35 | *swap_char - swap two characters 36 | *@a: first character 37 | *@b: second character 38 | *Return: nothing 39 | */ 40 | 41 | void swap_char(char *a, char *b) 42 | { 43 | char tmp = *a; 44 | *a = *b; 45 | *b = tmp; 46 | } 47 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints part of a string 5 | * @str: string to be printed 6 | * 7 | * Return: void 8 | **/ 9 | void puts2(char *str) 10 | { 11 | int i = 0; 12 | while (str[i] != '\0') 13 | { 14 | if (i % 2 == 0) 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *puts_half - puts half of a string 4 | *@str: string 5 | *Return:void 6 | */ 7 | void puts_half(char *str) 8 | { 9 | int len = _strlen(str); 10 | 11 | if (len % 2 == 0) 12 | len = len / 2; 13 | 14 | else 15 | len = (len + 1) / 2; 16 | 17 | while (str[len] != '\0') 18 | { 19 | _putchar(str[len]); 20 | len++; 21 | } 22 | _putchar('\n'); 23 | } 24 | 25 | 26 | /** 27 | *_strlen - returns the length of a string 28 | * @s: string 29 | *Return: returns lenght; 30 | */ 31 | int _strlen(char *s) 32 | { 33 | int count, inc; 34 | inc = 0; 35 | for (count = 0; s[count] != '\0'; count++) 36 | inc++; 37 | 38 | return (inc); 39 | } 40 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_array - print n element of array 4 | *@a: array 5 | *@n: number of elements 6 | *Return: nothing 7 | */ 8 | void print_array(int *a, int n) 9 | { 10 | int inc; 11 | for (inc = 0; inc < n ; inc++) 12 | { 13 | if (inc != n - 1) 14 | printf("%d, ", a[inc]); 15 | else 16 | printf("%d", a[inc]); 17 | 18 | } 19 | printf("\n"); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - Copy paste string 4 | *@dest: destination 5 | *@src: source 6 | *Return: dest 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int inc = 0; 11 | while (*(src + inc) != '\0') 12 | { 13 | *(dest + inc) = *(src + inc); 14 | inc++; 15 | } 16 | *(dest + inc) = '\0'; 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x05-pointers_arrays_strings 2 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | 5 | /** 6 | * void prototypes() 7 | *char prototypes() 8 | * int prototypes() 9 | */ 10 | void reset_to_98(int *n); 11 | void _puts(char *str); 12 | void print_rev(char *s); 13 | void rev_string(char *s); 14 | void swap_int(int *a, int *b); 15 | void swap_char(char *a, char *b); 16 | void puts2(char *str); 17 | void puts_half(char *str); 18 | void print_array(int *a, int n); 19 | char *_strcpy(char *dest, char *src); 20 | int _putchar(char c); 21 | int _strlen(char *s); 22 | int _atoi(char *s); 23 | #endif /*MAIN_H*/ 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strcat - concatenates the string pointed to by @src to 5 | * the end of the string pointed to by @dest 6 | *@dest: String that will be appended 7 | *@src: String to be concatenated upon 8 | * 9 | * Return: returns poiner to @dest 10 | */ 11 | 12 | char *_strcat(char *dest, char *src) 13 | { 14 | 15 | int index = 0, dest_len = 0; 16 | 17 | while (dest[index++]) 18 | dest_len++; 19 | 20 | for (index = 0; src[index]; index++) 21 | dest[dest_len++] = src[index]; 22 | 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strncat - concatenate two strings but add inputted number of bytes 4 | *@dest: string to be appended upon 5 | *@src: string to be completed at end of dest 6 | *@n:integer parameter to compare index to 7 | *Return: returns new concatenated string 8 | */ 9 | 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | 13 | int index = 0, dest_len = 0; 14 | 15 | while (dest[index++]) 16 | dest_len++; 17 | 18 | for (index = 0; src[index] && index < n; index++) 19 | dest[dest_len++] = src[index]; 20 | 21 | return (dest); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * rot13 - encoder rot13 6 | * @s: pointer to string params 7 | * 8 | * Return: *s 9 | */ 10 | 11 | char *rot13(char *s) 12 | { 13 | int i; 14 | int j; 15 | char data1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 16 | char datarot[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 17 | 18 | for (i = 0; s[i] != '\0'; i++) 19 | { 20 | for (j = 0; j < 52; j++) 21 | { 22 | if (s[i] == data1[j]) 23 | { 24 | s[i] = datarot[j]; 25 | break; 26 | } 27 | } 28 | } 29 | return (s); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - check the code 4 | * 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | print_number(98); 10 | _putchar('\n'); 11 | print_number(402); 12 | _putchar('\n'); 13 | print_number(1024); 14 | _putchar('\n'); 15 | print_number(0); 16 | _putchar('\n'); 17 | print_number(-98); 18 | _putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | /** 5 | * print_number - function: prints an integer 6 | * 7 | * @n: integer to be printed in program 8 | */ 9 | void print_number(int n) 10 | { 11 | unsigned int number = n; 12 | 13 | if (n < 0) 14 | { 15 | _putchar('-'); 16 | number *= -1; 17 | } 18 | if (number >= 10) 19 | { 20 | print_number(number / 10); 21 | } 22 | _putchar('0' + (number % 10)); 23 | } 24 | -------------------------------------------------------------------------------- /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/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - prints buffer 6 | * @b: buffer 7 | * @size: size 8 | * Return: void 9 | */ 10 | 11 | void print_buffer(char *b, int size) 12 | { 13 | int o, j, i; 14 | 15 | o = 0; 16 | 17 | if (size <= 0) 18 | { 19 | printf("\n"); 20 | return; 21 | } 22 | while (o < size) 23 | { 24 | j = size - o < 10 ? size - o : 10; 25 | printf("%08x: ", o); 26 | for (i = 0; i < 10; i++) 27 | { 28 | if (i < j) 29 | printf("%02x", *(b + o + i)); 30 | else 31 | printf(" "); 32 | if (i % 2) 33 | { 34 | printf(" "); 35 | } 36 | } 37 | for (i = 0; i < j; i++) 38 | { 39 | int c = *(b + o + i); 40 | 41 | if (c < 32 || c > 132) 42 | { 43 | c = '.'; 44 | } 45 | printf("%c", c); 46 | } 47 | printf("\n"); 48 | o += 10; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - C function that copies a string, including the 4 | * terminating null byte, using at most an inputted number of bytes. 5 | * If the length of the source string is less than the maximum byte number, 6 | * the remainder of the destination string is filled with null bytes. 7 | * Works identically to the standard library function `strncpy`. 8 | *@dest: buffer storing the string copy 9 | *@src:the source string 10 | *@n:max nummber of byte copied 11 | *Return: returns 12 | */ 13 | 14 | char *_strncpy(char *dest, char *src, int n) 15 | { 16 | int i; 17 | 18 | for (i = 0; i < n && src[i] != '\0'; i++) 19 | dest[i] = src[i]; 20 | 21 | for ( ; i < n; i++) 22 | dest[i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Function which compare two strings and 5 | *@s1: first string 6 | *@s2:second string 7 | *Return: 8 | * returns zero if s1 == s2 9 | * returns negative number if s1 < s2 10 | * returns positive number if s1 > s2 11 | */ 12 | 13 | int _strcmp(char *s1, char *s2) 14 | { 15 | int i = 0, diff = 0; 16 | 17 | while (1) 18 | { 19 | if (s1[i] == '\0' && s2[i] == '\0') 20 | break; 21 | else if (s1[i] == '\0') 22 | { 23 | diff = s2[i]; 24 | break; 25 | } 26 | else if (s2[i] == '\0') 27 | { 28 | diff = s1[i]; 29 | break; 30 | } 31 | else if (s1[i] != s2[i]) 32 | { 33 | diff = s1[i] - s2[i]; 34 | break; 35 | } 36 | else 37 | i++; 38 | 39 | } 40 | return (diff); 41 | } 42 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - function that reverse content of array 5 | * @a: array 6 | * @n: number of elements 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int tmp, index; 12 | for (index = n - 1; index > n / 2; index--) 13 | { 14 | tmp = a[n - 1 - index]; 15 | a[n - 1 - index] = a[index]; 16 | a[index] = tmp; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *string_toupper - changes all lowercases letters of a string to uppercase 5 | *@str: parameter 6 | *Return: returns a character 7 | */ 8 | 9 | char *string_toupper(char *str) 10 | { 11 | int index = 0; 12 | 13 | while (str[index++]) 14 | { 15 | if (str[index] >= 'a' && str[index] <= 'z') 16 | str[index] -= 32; 17 | } 18 | 19 | return (str); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - Capitalizes all words of a string. 5 | * @str: The string to be capitalized. 6 | * 7 | * Return: A pointer to the changed string. 8 | */ 9 | char *cap_string(char *str) 10 | { 11 | int index = 0; 12 | 13 | while (str[index]) 14 | { 15 | while (!(str[index] >= 'a' && str[index] <= 'z')) 16 | index++; 17 | 18 | if (str[index - 1] == ' ' || 19 | str[index - 1] == '\t' || 20 | str[index - 1] == '\n' || 21 | str[index - 1] == ',' || 22 | str[index - 1] == ';' || 23 | str[index - 1] == '.' || 24 | str[index - 1] == '!' || 25 | str[index - 1] == '?' || 26 | str[index - 1] == '"' || 27 | str[index - 1] == '(' || 28 | str[index - 1] == ')' || 29 | str[index - 1] == '{' || 30 | str[index - 1] == '}' || 31 | index == 0) 32 | str[index] -= 32; 33 | 34 | index++; 35 | } 36 | 37 | return (str); 38 | } 39 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encode into 1337speak 4 | * @n: input value 5 | * Return: n value 6 | */ 7 | char *leet(char *n) 8 | { 9 | int i, j; 10 | char s1[] = "aAeEoOtTlL"; 11 | char s2[] = "4433007711"; 12 | 13 | for (i = 0; n[i] != '\0'; i++) 14 | { 15 | for (j = 0; j < 10; j++) 16 | { 17 | if (n[i] == s1[j]) 18 | { 19 | n[i] = s2[j]; 20 | } 21 | } 22 | } 23 | return (n); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x06-pointers_arrays_strings 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | /** 5 | *void prototypes() 6 | *int prototypes() 7 | *char prototypes() 8 | */ 9 | void reverse_array(int *a, int n); 10 | void print_number(int n); 11 | void print_buffer(char *b, int size); 12 | int _strcmp(char *s1, char *s2); 13 | char *_strcat(char *dest, char *src); 14 | char *_strncat(char *dest, char *src, int n); 15 | char *_strncpy(char *dest, char *src, int n); 16 | char *string_toupper(char *); 17 | char *cap_string(char *); 18 | char *leet(char *); 19 | char *rot13(char *); 20 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 21 | char *add_strings(char *n1, char *n2, char *r, int r_index); 22 | 23 | #endif /*MAIN_H*/ 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memset - The _memset() function fills 4 | * the first n bytes of the memory area 5 | * pointed to by s with the constant byte b 6 | *@s:target 7 | *@b: constant byte 8 | *@n:number of byte 9 | *Return: returns new value of target 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | while (n) 15 | { 16 | s[n - 1] = b; 17 | n--; 18 | } 19 | return (s); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memcpy - The _memcpy() function copies n bytes 4 | * from memory area src to memory area dest 5 | *@dest:area where bytes are copied to 6 | *@src:area where bytes are copied from 7 | *@n:number of bytes to copy 8 | *Return: returns a pointer to n 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i = 0; 13 | 14 | for (; i < n; i++) 15 | dest[i] = src[i]; 16 | 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * set_string - a function that sets the value of a pointer to a char. 4 | *@s: pointer value 5 | *@to: value pointed to 6 | * 7 | */ 8 | void set_string(char **s, char *to) 9 | { 10 | *s = to; 11 | } 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strchr - Returns a pointer to the first occurrence 4 | *of the character c in the string s, or NULL if the 5 | *character is not found 6 | * 7 | *@s:string targeted 8 | *@c:character targeted 9 | * 10 | *Return: returns pointer to first occcurence of c 11 | */ 12 | char *_strchr(char *s, char c) 13 | { 14 | int i; 15 | 16 | for (i = 0; (s[i] != c) && (s[i] != '\0'); i++) 17 | ; 18 | if (s[i] == c) 19 | return (s + i); 20 | else 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strspn - search the number of bytes in the initial 4 | * segment of s which consist only of bytes from accept 5 | *@s:segment targeted 6 | *@accept:reference bytes container 7 | * 8 | *Return:returns the number of bytes in the initial 9 | * segment of s which consist only of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int bytes = 0; 14 | int i; 15 | 16 | while (*s) 17 | { 18 | for (i = 0; accept[i]; i++) 19 | { 20 | if (accept[i] == *s) 21 | { 22 | bytes++; 23 | break; 24 | } 25 | else if ((accept[i + 1]) == '\0') 26 | return (bytes); 27 | } 28 | s++; 29 | } 30 | return (bytes); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strpbrk - The _strpbrk() function locates the first 4 | * occurrence in the string s of any of the bytes in 5 | *the string accept 6 | * 7 | *@s: string where search is made 8 | *@accept: string where searched bytes are located 9 | * 10 | *Return:Returns a pointer to the byte in s that matches 11 | * one of the bytes in accept, or NULL if no such byte is found 12 | */ 13 | 14 | char *_strpbrk(char *s, char *accept) 15 | { 16 | int i; 17 | 18 | while (*s) 19 | { 20 | for (i = 0; accept[i]; i++) 21 | { 22 | if (accept[i] == *s) 23 | return (s); 24 | } 25 | s++; 26 | } 27 | 28 | return ('\0'); 29 | } 30 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strstr - The _strstr() function finds the first occurrence 4 | * of the substring needle in the string haystack. 5 | * The terminating null bytes (\0) are not compared 6 | *@haystack: string where the search is made 7 | *@needle: string whose occurence is searched in haystack 8 | *Return:Returns a pointer to the beginning of the located 9 | * substring, or NULL if the substring is not found. 10 | */ 11 | 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | int i; 15 | 16 | if (*needle == 0) 17 | return (haystack); 18 | 19 | while (*haystack) 20 | { 21 | i = 0; 22 | 23 | if (haystack[i] == needle[i]) 24 | { 25 | do { 26 | if (needle[i + 1] == '\0') 27 | return (haystack); 28 | i++; 29 | } while (haystack[i] == needle[i]); 30 | } 31 | haystack++; 32 | } 33 | return ('\0'); 34 | } 35 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_chessboard - a function that prints the chessboard. 4 | *@a: array containing board elements 5 | *returns nothing 6 | */ 7 | 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; a[i][7]; i++) 13 | { 14 | for (j = 0; j < 8; j++) 15 | _putchar(a[i][j]); 16 | _putchar('\n'); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_diagsums - a function that prints the sum of 4 | * the two diagonals of a square matrix of integers. 5 | *@a:matrix to be summed 6 | *@size:size of the matrix 7 | */ 8 | void print_diagsums(int *a, int size) 9 | { 10 | int i, sum1 = 0, sum2 = 0; 11 | 12 | for (i = 0; i < size; i++) 13 | { 14 | sum1 += a[i]; 15 | a += size; 16 | } 17 | 18 | a -= size; 19 | 20 | for (i = 0; i < size; i++) 21 | { 22 | sum2 += a[i]; 23 | a -= size; 24 | } 25 | 26 | printf("%d, %d\n", sum1, sum2); 27 | } 28 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x07-pointers_arrays_strings 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | #include 5 | /** 6 | *void prototypes(void) 7 | *int prototypes(int) 8 | *char prototypes(char) 9 | */ 10 | void print_chessboard(char (*a)[8]); 11 | void print_diagsums(int *a, int size); 12 | void set_string(char **s, char *to); 13 | unsigned int _strspn(char *s, char *accept); 14 | int _putchar(char c); 15 | char *_memset(char *s, char b, unsigned int n); 16 | char *_memcpy(char *dest, char *src, unsigned int n); 17 | char *_strchr(char *s, char c); 18 | char *_strpbrk(char *s, char *accept); 19 | char *_strstr(char *haystack, char *needle); 20 | #endif /* MAIN_H */ 21 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts_recursion - prints a string followed by a new line 4 | *@s:character to be printed 5 | *Return:returns void 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | _putchar('\n'); 11 | else 12 | { 13 | _putchar(s[0]); 14 | _puts_recursion(s + 1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _print_rev_recursion - function that prints a string in reverse. 4 | *@s: parameter string 5 | * 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s != '\0') 10 | { 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * is_palindrome - returns true if the given string is a palindrome 5 | *@s: string to check 6 | *Return: true if the given string is a palindrome 7 | */ 8 | 9 | int is_palindrome(char *s) 10 | { 11 | int index = 0; 12 | int len = find_strlen(s); 13 | 14 | if (!(*s)) 15 | return (1); 16 | 17 | return (check_palindrome(s, len, index)); 18 | } 19 | 20 | /** 21 | * find_strlen - Returns the length of a string. 22 | * @s: The string to be measured. 23 | * 24 | * Return: The length of the string. 25 | */ 26 | int find_strlen(char *s) 27 | { 28 | int len = 0; 29 | 30 | if (*(s + len)) 31 | { 32 | len++; 33 | len += find_strlen(s + len); 34 | } 35 | 36 | return (len); 37 | } 38 | 39 | /** 40 | * check_palindrome - Checks if a string is a palindrome. 41 | * @s: The string to be checked. 42 | * @len: The length of s. 43 | * @index: The index of the string to be checked. 44 | * 45 | * Return: If the string is a palindrome - 1. 46 | * If the string is not a palindrome - 0. 47 | */ 48 | int check_palindrome(char *s, int len, int index) 49 | { 50 | if (s[index] == s[len / 2]) 51 | return (1); 52 | 53 | if (s[index] == s[len - index - 1]) 54 | return (check_palindrome(s, len, index + 1)); 55 | 56 | return (0); 57 | } 58 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two strings and returns 1 if identical 5 | * @s1: string to be checked 6 | * @s2: pattern to be used 7 | * 8 | * Return: 1 if identical, 0 otherwise 9 | */ 10 | 11 | int wildcmp(char *s1, char *s2) 12 | { 13 | if (*s2 == '\0') 14 | return (*s1 == '\0'); 15 | if (*s2 == *s1) 16 | return (*s1 != '\0' && wildcmp(s1 + 1, s2 + 1)); 17 | if (*s2 == '*') 18 | return (wildcmp(s1, s2 + 1) || (*s1 != '\0' && wildcmp(s1 + 1, s2))); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen_recursion - gives length of a string 4 | * @s: string to be measured 5 | *Return: length of the string 6 | */ 7 | 8 | int _strlen_recursion(char *s) 9 | { 10 | int len = 0; 11 | 12 | if (*s != '\0') 13 | { 14 | len++; 15 | len += _strlen_recursion(s + 1); 16 | } 17 | return (len); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - searchs for the factorial of a number 5 | * @n: the number to find the factorial of 6 | * 7 | * Return: the factorial of the number 8 | */ 9 | 10 | int factorial(int n) 11 | { 12 | int next; 13 | 14 | if (n == 0) 15 | return (1); 16 | else if (n < 0) 17 | return (-1); 18 | 19 | next = factorial(n - 1); 20 | return (n * next); 21 | } 22 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _pow_recursion - function that returns the 4 | * value of x raised to the power of y 5 | *@x: the base 6 | *@y: the exponent 7 | *Return: the value of x raised to the power of y 8 | */ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | return (-1); 13 | else if (y == 0) 14 | return (1); 15 | return (_pow_recursion(x, y - 1) * x); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _sqrt_recursion - returns the natural square root of a number 5 | * @n: number to be used 6 | * 7 | * Return: the square root of n 8 | */ 9 | 10 | int _sqrt_recursion(int n) 11 | { 12 | if (n == 0 || n == 1) 13 | return (n); 14 | return (_sqrt(0, n)); 15 | } 16 | 17 | /** 18 | * _sqrt - returns the square root of a number 19 | * @n: test number 20 | * @x: squared number 21 | * 22 | * Return: the square root of n 23 | */ 24 | 25 | int _sqrt(int n, int x) 26 | { 27 | if (n > x / 2) 28 | return (-1); 29 | else if (n * n == x) 30 | return (n); 31 | return (_sqrt(n + 1, x)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * is_prime_number - returns true if the number is prime 4 | *@n: the number to check 5 | * 6 | *Return: true if the number is prime 7 | */ 8 | int is_prime_number(int n) 9 | { 10 | int start = n / 2; 11 | 12 | if (n <= 1) 13 | return (0); 14 | return (is_prime(n, start)); 15 | } 16 | 17 | /** 18 | * is_prime - returns the 1 if n is prime 19 | * @n: number to be checked 20 | * @start: number to start checking from 21 | * 22 | * Return: 1 if n is prime, 0 otherwise 23 | */ 24 | 25 | int is_prime(int n, int start) 26 | { 27 | if (start <= 1) 28 | return (1); 29 | else if (n % start == 0) 30 | return (0); 31 | return (is_prime(n, start - 1)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | 0x08-recursion 2 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | /** 6 | *void prototypes(void) 7 | *int prototypes(int) 8 | *char prototypes(char) 9 | */ 10 | void _puts_recursion(char *s); 11 | void _print_rev_recursion(char *s); 12 | int _putchar(char c); 13 | int _strlen_recursion(char *s); 14 | int factorial(int n); 15 | int _pow_recursion(int x, int y); 16 | int _sqrt_recursion(int n); 17 | int is_prime_number(int n); 18 | int is_palindrome(char *s); 19 | int wildcmp(char *s1, char *s2); 20 | int _sqrt(int n, int x); 21 | int is_prime(int n, int start); 22 | int find_strlen(char *s); 23 | int check_palindrome(char *s, int len, int index); 24 | 25 | #endif /* MAIN_H */ 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/.libmy.a.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/.libmy.a.swp -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isupper - function that verifies if a character is uppercase or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isupper(int c) 9 | { 10 | if ((c >= 'A') && (c <= 'Z')) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memset - The _memset() function fills 4 | * the first n bytes of the memory area 5 | * pointed to by s with the constant byte b 6 | *@s:target 7 | *@b: constant byte 8 | *@n:number of byte 9 | *Return: returns new value of target 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | while (n) 15 | { 16 | s[n - 1] = b; 17 | n--; 18 | } 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strcat - concatenates the string pointed to by @src to 5 | * the end of the string pointed to by @dest 6 | *@dest: String that will be appended 7 | *@src: String to be concatenated upon 8 | * 9 | * Return: returns poiner to @dest 10 | */ 11 | 12 | char *_strcat(char *dest, char *src) 13 | { 14 | 15 | int index = 0, dest_len = 0; 16 | 17 | while (dest[index++]) 18 | dest_len++; 19 | 20 | for (index = 0; src[index]; index++) 21 | dest[dest_len++] = src[index]; 22 | 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isdigit - function that verifies if a character is a digit or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isdigit(int c) 9 | { 10 | if ((c >= 48) && (c <= 57)) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memcpy - The _memcpy() function copies n bytes 4 | * from memory area src to memory area dest 5 | *@dest:area where bytes are copied to 6 | *@src:area where bytes are copied from 7 | *@n:number of bytes to copy 8 | *Return: returns a pointer to n 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i = 0; 13 | 14 | for (; i < n; i++) 15 | dest[i] = src[i]; 16 | 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strncat - concatenate two strings but add inputted number of bytes 4 | *@dest: string to be appended upon 5 | *@src: string to be completed at end of dest 6 | *@n:integer parameter to compare index to 7 | *Return: returns new concatenated string 8 | */ 9 | 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | 13 | int index = 0, dest_len = 0; 14 | 15 | while (dest[index++]) 16 | dest_len++; 17 | 18 | for (index = 0; src[index] && index < n; index++) 19 | dest[dest_len++] = src[index]; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _atoi - changes a string to an int 4 | * @s: the string to be changed 5 | * 6 | * Return: the converted int 7 | */ 8 | int _atoi(char *s) 9 | { 10 | int i = 1; 11 | unsigned int num = 0; 12 | do { 13 | if (*s == '-') 14 | i *= -1; 15 | else if (*s >= '0' && *s <= '9') 16 | num = num * 10 + (*s - '0'); 17 | else if (num > 0) 18 | break; 19 | } while (*s++); 20 | return (num *i); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include   3 | /** 4 | *_strchr - Returns a pointer to the first occurrence 5 | *of the character c in the string s, or NULL if the 6 | *character is not found 7 | * 8 | *@s:string targeted 9 | *@c:character targeted 10 | * 11 | *Return: returns pointer to first occcurence of c 12 | */ 13 | char *_strchr(char *s, char c) 14 | { 15 | int i; 16 | 17 | for (i = 0; (s[i] != c) && (s[i] != '\0'); i++) 18 | ; 19 | if (s[i] == c) 20 | return (s + i); 21 | else 22 | return (NULL); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strlen - returns the length of a string 4 | * @s: string 5 | *Return: returns lenght; 6 | */ 7 | int _strlen(char *s) 8 | { 9 | int count, inc; 10 | inc = 0; 11 | for (count = 0; s[count] != '\0'; count++) 12 | inc++; 13 | 14 | return (inc); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - C function that copies a string, including the 4 | * terminating null byte, using at most an inputted number of bytes. 5 | * If the length of the source string is less than the maximum byte number, 6 | * the remainder of the destination string is filled with null bytes. 7 | * Works identically to the standard library function `strncpy`. 8 | *@dest: buffer storing the string copy 9 | *@src:the source string 10 | *@n:max nummber of byte copied 11 | *Return: returns 12 | */ 13 | 14 | char *_strncpy(char *dest, char *src, int n) 15 | { 16 | int i; 17 | 18 | for (i = 0; i < n && src[i] != '\0'; i++) 19 | dest[i] = src[i]; 20 | 21 | for ( ; i < n; i++) 22 | dest[i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_islower - controls if a character is in lowercase 4 | *@c: character to be verified 5 | *Return: return 0 or 1 6 | */ 7 | int _islower(int c) 8 | { 9 | if (c >= 'a' && c <= 'z') 10 | return (1); 11 | 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_puts - prints a string 4 | * @str: string to print 5 | * 6 | * Description: prints a string 7 | * On success: return the number of characters printed 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | while (*str) 13 | _putchar(*str++); 14 | 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Function which compare two strings and 5 | *@s1: first string 6 | *@s2:second string 7 | *Return: 8 | * returns zero if s1 == s2 9 | * returns negative number if s1 < s2 10 | * returns positive number if s1 > s2 11 | */ 12 | 13 | int _strcmp(char *s1, char *s2) 14 | { 15 | int i = 0, diff = 0; 16 | 17 | while (1) 18 | { 19 | if (s1[i] == '\0' && s2[i] == '\0') 20 | break; 21 | else if (s1[i] == '\0') 22 | { 23 | diff = s2[i]; 24 | break; 25 | } 26 | else if (s2[i] == '\0') 27 | { 28 | diff = s1[i]; 29 | break; 30 | } 31 | else if (s1[i] != s2[i]) 32 | { 33 | diff = s1[i] - s2[i]; 34 | break; 35 | } 36 | else 37 | i++; 38 | 39 | } 40 | return (diff); 41 | } 42 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strspn - search the number of bytes in the initial 4 | * segment of s which consist only of bytes from accept 5 | *@s:segment targeted 6 | *@accept:reference bytes container 7 | * 8 | *Return:returns the number of bytes in the initial 9 | * segment of s which consist only of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int bytes = 0; 14 | int i; 15 | 16 | while (*s) 17 | { 18 | for (i = 0; accept[i]; i++) 19 | { 20 | if (accept[i] == *s) 21 | { 22 | bytes++; 23 | break; 24 | } 25 | else if ((accept[i + 1]) == '\0') 26 | return (bytes); 27 | } 28 | s++; 29 | } 30 | return (bytes); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isalpha - controls if a character is alphabetical 4 | *@c: character to be verified 5 | *Return: return 0 or 1 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 10 | return (1); 11 | 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strpbrk - The _strpbrk() function locates the first 4 | * occurrence in the string s of any of the bytes in 5 | *the string accept 6 | * 7 | *@s: string where search is made 8 | *@accept: string where searched bytes are located 9 | * 10 | *Return:Returns a pointer to the byte in s that matches 11 | * one of the bytes in accept, or NULL if no such byte is found 12 | */ 13 | 14 | char *_strpbrk(char *s, char *accept) 15 | { 16 | int i; 17 | 18 | while (*s) 19 | { 20 | for (i = 0; accept[i]; i++) 21 | { 22 | if (accept[i] == *s) 23 | return (s); 24 | } 25 | s++; 26 | } 27 | 28 | return ('\0'); 29 | } 30 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strstr - The _strstr() function finds the first occurrence 4 | * of the substring needle in the string haystack. 5 | * The terminating null bytes (\0) are not compared 6 | *@haystack: string where the search is made 7 | *@needle: string whose occurence is searched in haystack 8 | *Return:Returns a pointer to the beginning of the located 9 | * substring, or NULL if the substring is not found. 10 | */ 11 | 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | int i; 15 | 16 | if (*needle == 0) 17 | return (haystack); 18 | 19 | while (*haystack) 20 | { 21 | i = 0; 22 | 23 | if (haystack[i] == needle[i]) 24 | { 25 | do { 26 | if (needle[i + 1] == '\0') 27 | return (haystack); 28 | i++; 29 | } while (haystack[i] == needle[i]); 30 | } 31 | haystack++; 32 | } 33 | return ('\0'); 34 | } 35 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - prints the absolute value of a number 4 | *@i: number targeted 5 | *Return: returns 0 6 | */ 7 | int _abs(int i) 8 | { 9 | if (i > 0) 10 | return (i); 11 | else if (i < 0) 12 | return (-i); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - Copy paste string 4 | *@dest: destination 5 | *@src: source 6 | *Return: dest 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int inc = 0; 11 | while (*(src + inc) != '\0') 12 | { 13 | *(dest + inc) = *(src + inc); 14 | inc++; 15 | } 16 | *(dest + inc) = '\0'; 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | 0x09-static_libraries 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c ./*.c 3 | ar -rc liball.a ./*.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | 5 | int _putchar(char c); 6 | int _islower(int c); 7 | int _isalpha(int c); 8 | int _abs(int n); 9 | int _isupper(int c); 10 | int _isdigit(int c); 11 | int _strlen(char *s); 12 | void _puts(char *s); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | char *_strcat(char *dest, char *src); 16 | char *_strncat(char *dest, char *src, int n); 17 | char *_strncpy(char *dest, char *src, int n); 18 | int _strcmp(char *s1, char *s2); 19 | char *_memset(char *s, char b, unsigned int n); 20 | char *_memcpy(char *dest, char *src, unsigned int n); 21 | char *_strchr(char *s, char c); 22 | unsigned int _strspn(char *s, char *accept); 23 | char *_strpbrk(char *s, char *accept); 24 | char *_strstr(char *haystack, char *needle); 25 | 26 | #endif /* MAIN_H */ 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints program name 5 | *@argc: number of arguments 6 | *@argv: array of arguments 7 | *Return: 0 8 | */ 9 | int main(int __attribute__((__unused__)) argc, char *argv[]) 10 | { 11 | printf("%s\n", argv[0]); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the number of arguments 5 | *@argc: number of arguments 6 | *@argv:array of arguments 7 | *Return: the number of arguments 8 | */ 9 | 10 | int main(int argc, char __attribute__((__unused__)) *argv[]) 11 | { 12 | printf("%d\n", argc - 1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - prints the minimum number of coins to make change 6 | * for an amount of money 7 | *@argc: number of arguments 8 | *@argv: array of arguments 9 | *Return: returns 1 if there is an error; else returns 0 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int cents, coins = 0; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | cents = atoi(argv[1]); 22 | while (cents > 0) 23 | { 24 | coins++; 25 | if ((cents - 25) >= 0) 26 | { 27 | cents -= 25; 28 | continue; 29 | } 30 | if ((cents - 10) >= 0) 31 | { 32 | cents -= 10; 33 | continue; 34 | } 35 | if ((cents - 5) >= 0) 36 | { 37 | cents -= 5; 38 | continue; 39 | } 40 | if ((cents - 2) >= 0) 41 | { 42 | cents -= 2; 43 | continue; 44 | } 45 | cents--; 46 | } 47 | printf("%d\n", coins); 48 | return (0); 49 | } 50 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all arguments it received 5 | *@argc: argument count 6 | *@argv: argument vector 7 | *Return: returns 0 8 | */ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < argc; i++) 15 | printf("%s\n", argv[i]); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - prints multiplication 5 | *of two numbers 6 | *@argc: number of arguments 7 | *@argv: array of arguments 8 | *Return: returns 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, j, mul; 14 | 15 | if (argc <= 2) 16 | { 17 | printf("Error\n"); 18 | return (1); 19 | } 20 | 21 | i = atoi(argv[1]); 22 | j = atoi(argv[2]); 23 | mul = i * j; 24 | 25 | printf("%d\n", mul); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - adds positive numbers 6 | *@argc: number of arguments 7 | *@argv: array of arguments 8 | *Return: 0 on success, 1 on failure 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, j, sum = 0; 14 | 15 | for (i = 1; i < argc; i++) 16 | { 17 | for (j = 0; argv[i][j] != '\0'; j++) 18 | { 19 | if (argv[i][j] < '0' || argv[i][j] > '9') 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | } 25 | 26 | sum += atoi(argv[i]); 27 | } 28 | 29 | printf("%d\n", sum); 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | 0x0A-argc_argv 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _putchar - writes the character c to stdout 6 | * @c:Ther character to print 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned and errno is set appropriately. 10 | */ 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _atoi(char *s); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_array - creates an array of chars, and 5 | * initializes it with a specific char. 6 | *@size: Size of the array 7 | *@c: Character to insert 8 | *Return: NULL if size is zero or if it fails, 9 | *pointer to array if everything is normal. 10 | */ 11 | 12 | char *create_array(unsigned int size, char c) 13 | { 14 | char *array; 15 | unsigned int index; 16 | 17 | if (size == 0) 18 | return (NULL); 19 | array = malloc(sizeof(char) * size); 20 | 21 | if (array == NULL) 22 | return (NULL); 23 | 24 | for (index = 0; index < size; index++) 25 | array[index] = c; 26 | 27 | return (array); 28 | } 29 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strdup - returns a pointer to a newly allocated 5 | *space in memory, which contains a copy of the 6 | *string given as a parameter. 7 | *@str:String to be copied 8 | * 9 | *Return: NULL in case of error, pointer to allocated 10 | *space 11 | */ 12 | 13 | char *_strdup(char *str) 14 | { 15 | char *cpy; 16 | int index, len; 17 | 18 | if (str == NULL) 19 | return (NULL); 20 | 21 | for (index = 0; str[index]; index++) 22 | len++; 23 | cpy = malloc(sizeof(char) * (len + 1)); 24 | 25 | if (cpy == NULL) 26 | return (NULL); 27 | 28 | for (index = 0; str[index]; index++) 29 | { 30 | cpy[index] = str[index]; 31 | } 32 | 33 | cpy[len] = '\0'; 34 | 35 | return (cpy); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * argstostr - a function that concatenates all the arguments of your program 4 | *@ac: count of args passed to the function 5 | *@av:array of arguments 6 | * 7 | *Return: pointer to the new string 8 | */ 9 | 10 | char *argstostr(int ac, char **av) 11 | { 12 | char *new_string = NULL; 13 | int k = 0, i = ac, j, sum = 0, temp = 0; 14 | 15 | if (ac == 0 || av == NULL) 16 | return (NULL); 17 | 18 | while (ac--) 19 | sum += (len(av[ac]) + 1); 20 | new_string = (char *) malloc(sum + 1); 21 | 22 | if (new_string != NULL) 23 | { 24 | while (k < i) 25 | { 26 | for (j = 0; av[k][j] != '\0'; j++) 27 | new_string[j + temp] = av[k][j]; 28 | new_string[temp + j] = '\n'; 29 | temp += (j + 1); 30 | k++; 31 | } 32 | new_string[temp] = '\0'; 33 | } 34 | else 35 | { 36 | return (NULL); 37 | } 38 | return (new_string); 39 | } 40 | 41 | /** 42 | *len - returns length of str 43 | *@str: string counted 44 | *Return: returns the length 45 | */ 46 | int len(char *str) 47 | { 48 | int len = 0; 49 | 50 | if (str != NULL) 51 | { 52 | while (str[len]) 53 | len++; 54 | } 55 | return (len); 56 | } 57 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * str_concat - a function that concatenates two strings. 5 | *@s1:First string 6 | *@s2:Second string 7 | * 8 | *Return: NULL in case of failure , but pointer to new string in 9 | *case of success 10 | */ 11 | 12 | char *str_concat(char *s1, char *s2) 13 | { 14 | char *concat_str; 15 | int index, concat_index = 0, len = 0; 16 | 17 | if (s1 == NULL) 18 | s1 = ""; 19 | 20 | if (s2 == NULL) 21 | s2 = ""; 22 | 23 | for (index = 0; s1[index] || s2[index]; index++) 24 | len++; 25 | 26 | concat_str = malloc(sizeof(char) * len); 27 | 28 | if (concat_str == NULL) 29 | return (NULL); 30 | 31 | for (index = 0; s1[index]; index++) 32 | concat_str[concat_index++] = s1[index]; 33 | 34 | for (index = 0; s2[index]; index++) 35 | concat_str[concat_index++] = s2[index]; 36 | 37 | return (concat_str); 38 | } 39 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | /** 6 | * alloc_grid - returns a pointer to a 7 | *2 dimensional array of integers. 8 | *@width: width oàf array 9 | *@height: height of array 10 | * 11 | *Return: pointer to the array 12 | */ 13 | 14 | int **alloc_grid(int width, int height) 15 | { 16 | int **array; 17 | int i = 0, j; 18 | 19 | if (width == 0 || height == 0) 20 | return (NULL); 21 | array = (int **) malloc(sizeof(int *) * height); 22 | if (array != NULL) 23 | { 24 | for (; i < height; i++) 25 | { 26 | array[i] = (int *) malloc(sizeof(int) * width); 27 | if (array[i] != NULL) 28 | { 29 | for (j = 0; j < width; j++) 30 | array[i][j] = 0; 31 | } 32 | else 33 | { 34 | while (i >= 0) 35 | { 36 | free(array[i]); 37 | i--; 38 | } 39 | free(array); 40 | return (NULL); 41 | } 42 | } 43 | return (array); 44 | } 45 | else 46 | { 47 | return (NULL); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * free_grid - frees a 2d array 5 | *@grid:memory block to be freed 6 | *@height:height of the array 7 | *Return:returns void 8 | */ 9 | 10 | void free_grid(int **grid, int height) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < height; i++) 15 | free(grid[i]); 16 | 17 | free(grid); 18 | } 19 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0B-malloc_free 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void free_grid(int **grid, int height); 7 | char *create_array(unsigned int size, char c); 8 | char *_strdup(char *str); 9 | char *str_concat(char *s1, char *s2); 10 | char *argstostr(int ac, char **av); 11 | char **strtow(char *str); 12 | int **alloc_grid(int width, int height); 13 | int len(char *str); 14 | int num_words(char *str); 15 | #endif /* MAIN_H */ 16 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * malloc_checked - allocates memory using malloc, exit(98) if it fails 5 | * @b: size of the memory block to be allocated 6 | * 7 | * Return: pointer to the address of the memory block 8 | */ 9 | void *malloc_checked(unsigned int b) 10 | { 11 | void *block; 12 | 13 | block = malloc(b); 14 | if (block == NULL) 15 | exit(98); 16 | return (block); 17 | } 18 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *string_nconcat - Concatenates two strings using at 5 | *most an inputted number of bytes. 6 | *@s1: The first string. 7 | *@s2: The second string. 8 | *@n: The maximum number of bytes of s2 to concatenate to s1. 9 | * 10 | *Return: If the function fails - NULL. 11 | *Otherwise - a pointer to the concatenated space in memory. 12 | */ 13 | 14 | char *string_nconcat(char *s1, char *s2, unsigned int n) 15 | { 16 | char *concat; 17 | unsigned int len = n, index; 18 | 19 | if (s1 == NULL) 20 | s1 = ""; 21 | 22 | if (s2 == NULL) 23 | s2 = ""; 24 | 25 | for (index = 0; s1[index]; index++) 26 | len++; 27 | 28 | concat = malloc(sizeof(char) * (len + 1)); 29 | 30 | if (concat == NULL) 31 | return (NULL); 32 | 33 | len = 0; 34 | 35 | for (index = 0; s1[index]; index++) 36 | concat[len++] = s1[index]; 37 | 38 | for (index = 0; s2[index] && index < n; index++) 39 | concat[len++] = s2[index]; 40 | 41 | concat[len] = '\0'; 42 | 43 | return (concat); 44 | } 45 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | /** 5 | * _realloc - reallocates a memory block 6 | * @ptr: pointer to the memory previously allocated with a call to malloc 7 | * @old_size: size of ptr 8 | * @new_size: size of the new memory to be allocated 9 | * 10 | * Return: pointer to the address of the new memory block 11 | */ 12 | 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 14 | { 15 | void *temp_block; 16 | unsigned int i; 17 | 18 | if (ptr == NULL) 19 | { 20 | temp_block = malloc(new_size); 21 | return (temp_block); 22 | } 23 | else if (new_size == old_size) 24 | return (ptr); 25 | 26 | else if (new_size == 0 && ptr != NULL) 27 | { 28 | free(ptr); 29 | return (NULL); 30 | } 31 | else 32 | { 33 | temp_block = malloc(new_size); 34 | if (temp_block != NULL) 35 | { 36 | for (i = 0; i < min(old_size, new_size); i++) 37 | *((char *)temp_block + i) = *((char *) ptr + i); 38 | free(ptr); 39 | return (temp_block); 40 | } 41 | else 42 | return (NULL); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _calloc - allocate memory using malloc and initialize it to zero 5 | *@nmemb: number of elements 6 | *@size: size of the memory block to be allocated 7 | * 8 | *Return: poiner to the address of the memory block 9 | */ 10 | void *_calloc(unsigned int nmemb, unsigned int size) 11 | { 12 | char *block; 13 | unsigned int i; 14 | 15 | if (nmemb == 0 || size == 0) 16 | return (NULL); 17 | block = malloc(nmemb * size); 18 | if (block != NULL) 19 | { 20 | for (i = 0; i < (nmemb * size); i++) 21 | block[i] = 0; 22 | return (block); 23 | } 24 | else 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * array_range - creates an array of integers 5 | * @min: smallest number in the array 6 | * @max: lagrest value in the array 7 | * 8 | * Return: pointer to the address of the memory block 9 | */ 10 | 11 | int *array_range(int min, int max) 12 | { 13 | int *block; 14 | int i, j = 0; 15 | 16 | if (min > max) 17 | return (NULL); 18 | block = malloc(sizeof(*block) * ((max - min) + 1)); 19 | if (block != NULL) 20 | { 21 | for (i = min; i <= max; i++) 22 | { 23 | block[j] = i; 24 | j++; 25 | } 26 | return (block); 27 | } 28 | else 29 | return (NULL); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | 0x0C-more_malloc_free 2 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | #define min(x, y) (((x) < (y)) ? (x) : (y)) 8 | 9 | int _putchar(char c); 10 | void *malloc_checked(unsigned int b); 11 | char *string_nconcat(char *s1, char *s2, unsigned int n); 12 | int len(char *str); 13 | void *_calloc(unsigned int nmemb, unsigned int size); 14 | int *array_range(int min, int max); 15 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 16 | int find_len(char *str); 17 | char *create_xarray(int size); 18 | char *iterate_zeroes(char *str); 19 | void get_prod(char *prod, char *mult, int digit, int zeroes); 20 | void add_nums(char *final_prod, char *next_prod, int next_len); 21 | 22 | #endif /*MAIN_H*/ 23 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _OBJECT_LIKE_MACRO_ 2 | #define _OBJECT_LIKE_MACRO_ 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | 8 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - prints the name of 5 | *the file it was compiled from 6 | * 7 | *Return: nothing to be returned 8 | */ 9 | 10 | int main(void) 11 | { 12 | printf("%s\n", __FILE__); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) (((x) < (0)) ? ((x) * (-1)) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | 0x0D-preprocesso 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - inits a variable of 5 | *type dog 6 | *@d: dog identification 7 | *@name: name of dog 8 | *@age: age of dog 9 | *@owner: owner's name 10 | */ 11 | 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d != NULL) 15 | { 16 | (*d).name = name; 17 | (*d).age = age; 18 | (*d).owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * print_dog - prints a struct dog 7 | * @d: struct dog to print 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | if (d == NULL) 12 | return; 13 | 14 | if (d->name == NULL) 15 | d->name = "(nil)"; 16 | if (d->owner == NULL) 17 | d->owner = "(nil)"; 18 | 19 | printf("Name: %s\nAge: %f\nOwner: %s\n", d->name, d->age, d->owner); 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | *free_dog - Frees dog 5 | *@d: dog to be freed 6 | * 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d == NULL) 11 | return; 12 | 13 | free(d->name); 14 | free(d->owner); 15 | free(d); 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | 0x0E-structures_typedef 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | *struct dog - a new type of data 9 | *representing a dog 10 | *@name: name of dog 11 | *@age: age of dog 12 | *@owner: owner's name 13 | */ 14 | struct dog 15 | { 16 | char *name; 17 | float age; 18 | char *owner; 19 | }; 20 | 21 | 22 | /** 23 | *dog_t - Typedef for struct dog 24 | */ 25 | typedef struct dog dog_t; 26 | 27 | 28 | void init_dog(struct dog *d, char *name, float age, char *owner); 29 | void print_dog(struct dog *d); 30 | dog_t *new_dog(char *name, float age, char *owner); 31 | void free_dog(dog_t *d); 32 | int _strlen(char *str); 33 | char *_strcopy(char *dest, char *src); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | *print_name - prints a name 5 | *@name:name to print 6 | *@f: function which print name 7 | */ 8 | 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | 12 | if (name == NULL || f == NULL) 13 | return; 14 | 15 | f(name); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | *array_iterator - executes a function on each element of an array 5 | *@array: array to be targeted 6 | *@size: size of array 7 | *@action: function to be executed 8 | * 9 | */ 10 | 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | 14 | if (array == NULL || action == NULL) 15 | return; 16 | 17 | while (size-- > 0) 18 | { 19 | action(*array); 20 | array++; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - program that prints the opcodes of its own main function. 6 | *@argc: number of arguments passed to the program 7 | *@argv: array of arguments 8 | * 9 | *Return: on success, 1 or 2 in case of failure 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | 15 | int index, bytes; 16 | int (*address)(int, char **) = main; 17 | unsigned char opcode; 18 | 19 | if (argc != 2) 20 | { 21 | printf("Error\n"); 22 | exit(1); 23 | } 24 | 25 | bytes = atoi(argv[1]); 26 | 27 | if (bytes < 0) 28 | { 29 | printf("Error\n"); 30 | exit(2); 31 | } 32 | 33 | for (index = 0; index < bytes; index++) 34 | { 35 | opcode = *(unsigned char *)address; 36 | printf("%.2x", opcode); 37 | 38 | if (index == bytes - 1) 39 | continue; 40 | printf(" "); 41 | address++; 42 | } 43 | printf("\n"); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | *int_index - searches for an integer 5 | *@array: Array containing elements 6 | *@size: number of elements in array 7 | *@cmp: function which compare values 8 | * 9 | *Return: returns -1 if no element match 10 | * or when size is less than zero 11 | *return pointer to the first corresponding element 12 | * 13 | */ 14 | 15 | int int_index(int *array, int size, int (*cmp)(int)) 16 | { 17 | int index = 0; 18 | 19 | if (array == NULL || cmp == NULL) 20 | return (-1); 21 | 22 | for (; index < size; index++) 23 | { 24 | if (cmp(array[index]) != 0) 25 | return (index); 26 | } 27 | 28 | return (-1); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | 4 | /** 5 | *struct op - Struct op 6 | *@op: The operator 7 | *@f: The function associated 8 | */ 9 | typedef struct op 10 | { 11 | char *op; 12 | int (*f)(int a, int b); 13 | } op_t; 14 | 15 | int op_add(int a, int b); 16 | int op_sub(int a, int b); 17 | int op_mul(int a, int b); 18 | int op_div(int a, int b); 19 | int op_mod(int a, int b); 20 | int (*get_op_func(char *s))(int, int); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | *get_op_func - selects the correct function to perform 6 | *the operation asked by the user. You’re not allowed 7 | *to declare any other function. 8 | *@s: operator passed as argument 9 | * 10 | *Return: A pointer to the function corresponding to 11 | *the operator given in parameter 12 | */ 13 | int (*get_op_func(char *s))(int, int) 14 | { 15 | 16 | op_t ops[] = { 17 | {"+", op_add}, 18 | {"-", op_sub}, 19 | {"*", op_mul}, 20 | {"/", op_div}, 21 | {"%", op_mod}, 22 | {NULL, NULL} 23 | }; 24 | int i = 0; 25 | 26 | while (ops[i].op != NULL && *(ops[i].op) != *s) 27 | i++; 28 | 29 | return (ops[i].f); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints the result of simple operations. 7 | * @argc: The number of arguments supplied to the program. 8 | * @argv: An array of pointers to the arguments. 9 | * 10 | * Return: Always 0. 11 | */ 12 | 13 | int main(int __attribute__((__unused__)) argc, char *argv[]) 14 | { 15 | int num1, num2; 16 | char *op; 17 | 18 | if (argc != 4) 19 | { 20 | printf("Error\n"); 21 | exit(98); 22 | } 23 | 24 | num1 = atoi(argv[1]); 25 | op = argv[2]; 26 | num2 = atoi(argv[3]); 27 | 28 | if (get_op_func(op) == NULL || op[1] != '\0') 29 | { 30 | printf("Error\n"); 31 | exit(99); 32 | } 33 | 34 | if ((*op == '/' && num2 == 0) || (*op == '%' && num2 == 0)) 35 | { 36 | printf("Error\n"); 37 | exit(100); 38 | } 39 | printf("%d\n", get_op_func(op)(num1, num2)); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | *op_add - Returns the sum of two numbers 5 | *@a:the first number 6 | *@b:the second number 7 | * 8 | *Return:The sum of a and b 9 | */ 10 | 11 | int op_add(int a, int b) 12 | { 13 | return (a + b); 14 | } 15 | 16 | 17 | /** 18 | *op_sub - Returns the substraction of two numbers 19 | *@a:The first number 20 | *@b:The second number 21 | * 22 | *Return: The Difference of a and b 23 | */ 24 | int op_sub(int a, int b) 25 | { 26 | return (a - b); 27 | } 28 | 29 | /** 30 | *op_mul - Returns the multiplication of two numbers 31 | *@a:The first number 32 | *@b: The second number 33 | * 34 | *Return: The multiplication of a and b 35 | */ 36 | int op_mul(int a, int b) 37 | { 38 | return (a * b); 39 | } 40 | 41 | 42 | /** 43 | *op_div - Returns the division of two numbers. 44 | *@a: The first number. 45 | *@b: The second number. 46 | * 47 | *Return: The quotient of a and b. 48 | */ 49 | int op_div(int a, int b) 50 | { 51 | return (a / b); 52 | } 53 | 54 | /** 55 | *op_mod - Returns the remainder of the division of two numbers. 56 | *@a: The first number. 57 | *@b: The second number. 58 | * 59 | *Return: The remainder of the division of a by b. 60 | */ 61 | int op_mod(int a, int b) 62 | { 63 | return (a % b); 64 | } 65 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | 0x0F-function_pointers 2 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | 4 | #include 5 | #include 6 | 7 | void print_name(char *name, void (*f)(char *)); 8 | void array_iterator(int *array, size_t size, void (*action)(int)); 9 | int int_index(int *array, int size, int (*cmp)(int)); 10 | 11 | #endif /*MAIN_H*/ 12 | -------------------------------------------------------------------------------- /0x10-variadic_functions/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 | /** 4 | * sum_them_all - Returns the sum of all its paramters. 5 | * @n: The number of paramters passed to the function. 6 | * @...: A variable number of paramters to calculate the sum of. 7 | * 8 | * Return: If n == 0 - 0. 9 | * Otherwise - the sum of all parameters. 10 | */ 11 | int sum_them_all(const unsigned int n, ...) 12 | { 13 | va_list nums; 14 | unsigned int index, sum = 0; 15 | 16 | va_start(nums, n); 17 | 18 | for (index = 0; index < n; index++) 19 | sum += va_arg(nums, int); 20 | 21 | va_end(nums); 22 | 23 | return (sum); 24 | } 25 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | *print_numbers - prints numbers followed by 5 | *new line 6 | *@separator: string to be printed between numbers 7 | *@n: number of integer to be passed 8 | * 9 | *Return: nothing 10 | */ 11 | 12 | void print_numbers(const char *separator, const unsigned int n, ...) 13 | { 14 | 15 | va_list li; 16 | unsigned int i; 17 | 18 | va_start(li, n); 19 | 20 | for (i = 0; i < n; i++) 21 | { 22 | 23 | 24 | printf("%d", va_arg(li, int)); 25 | 26 | if (i != (n - 1) && separator != NULL) 27 | printf("%s", separator); 28 | } 29 | printf("\n"); 30 | va_end(li); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - prints strings 5 | *@separator: separator 6 | *@n: number of strings 7 | * 8 | *Return: nothing 9 | */ 10 | void print_strings(const char *separator, const unsigned int n, ...) 11 | { 12 | 13 | va_list li; 14 | unsigned int i; 15 | char *str; 16 | 17 | va_start(li, n); 18 | 19 | for (i = 0; i < n; i++) 20 | { 21 | str = va_arg(li, char *); 22 | if (str == NULL) 23 | printf("(nil)"); 24 | else 25 | printf("%s", str); 26 | 27 | if (i != (n - 1) && separator != NULL) 28 | printf("%s", separator); 29 | 30 | } 31 | 32 | printf("\n"); 33 | va_end(li); 34 | } 35 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | 0x10-variadic_functions 2 | -------------------------------------------------------------------------------- /0x10-variadic_functions/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x10-variadic_functions/a -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct printer - A new struct type defining a printer. 9 | * @symbol: A symbol representing a data type. 10 | * @print: A function pointer to a function that prints 11 | * a data type corresponding to symbol. 12 | */ 13 | typedef struct printer 14 | { 15 | char *symbol; 16 | void (*print)(va_list arg); 17 | } printer_t; 18 | 19 | 20 | int sum_them_all(const unsigned int n, ...); 21 | void print_numbers(const char *separator, const unsigned int n, ...); 22 | void print_strings(const char *separator, const unsigned int n, ...); 23 | void print_char(va_list arg); 24 | void print_int(va_list arg); 25 | void print_float(va_list arg); 26 | void print_string(va_list arg); 27 | void print_all(const char * const format, ...); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 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 2 | #include "lists.h" 3 | 4 | /** 5 | * print_list - prints all the elements of a linked list 6 | * @h: pointer to the list_t list to print 7 | * 8 | * Return: the number of nodes printed 9 | */ 10 | size_t print_list(const list_t *h) 11 | { 12 | size_t s = 0; 13 | 14 | while (h) 15 | { 16 | if (!h->str) 17 | printf("[0] (nil)\n"); 18 | else 19 | printf("[%u] %s\n", h->len, h->str); 20 | h = h->next; 21 | s++; 22 | } 23 | 24 | return (s); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - Locate the No of elements linked-list 5 | * @h: pointer - shows to the list 6 | * 7 | * Return: returns No of elements count 8 | * 9 | * ALX PROJECTS 10 | */ 11 | 12 | size_t list_len(const list_t *h) 13 | { 14 | const list_t *pointer = h; 15 | size_t count_nod = 0; 16 | 17 | for (; pointer != NULL; pointer = pointer->next) 18 | { 19 | count_nod++; 20 | } 21 | return (count_nod); 22 | /**return the number of nodes**/ 23 | } 24 | /******STOP********/ 25 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void __attribute__((constructor)) hare(void); 4 | 5 | /** 6 | * hare - print. 7 | * 8 | *Description: print. 9 | */ 10 | void hare(void) 11 | { 12 | printf("You're beat! and yet, you must allow,\n" 13 | "I bore my house upon my back!\n"); 14 | } 15 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | extern printf 2 | 3 | section .text 4 | global main 5 | 6 | main: 7 | push rbp 8 | 9 | mov rdi, fmt 10 | mov rsi, msg 11 | mov rax, 0 12 | call printf 13 | 14 | pop rbp 15 | 16 | mov rax, 0 17 | ret 18 | 19 | section .data 20 | msg: db "Hello, Holberton", 0 21 | fmt: db "%s", 10, 0 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * add_node - adds a new node at the beginning of a linked list 7 | * @head: double pointer to the list_t list 8 | * @str: new string to add in the node 9 | * 10 | * Return: the address of the new element, or NULL if it fails 11 | */ 12 | list_t *add_node(list_t **head, const char *str) 13 | { 14 | list_t *new; 15 | unsigned int len = 0; 16 | 17 | while (str[len]) 18 | len++; 19 | 20 | new = malloc(sizeof(list_t)); 21 | if (!new) 22 | return (NULL); 23 | 24 | new->str = strdup(str); 25 | new->len = len; 26 | new->next = (*head); 27 | (*head) = new; 28 | 29 | return (*head); 30 | } 31 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * add_node_end - adds a new node at the end of a linked list 7 | * @head: double pointer to the list_t list 8 | * @str: string to put in the new node 9 | * 10 | * Return: address of the new element, or NULL if it failed 11 | */ 12 | list_t *add_node_end(list_t **head, const char *str) 13 | { 14 | list_t *new; 15 | list_t *temp = *head; 16 | unsigned int len = 0; 17 | 18 | while (str[len]) 19 | len++; 20 | 21 | new = malloc(sizeof(list_t)); 22 | if (!new) 23 | return (NULL); 24 | 25 | new->str = strdup(str); 26 | new->len = len; 27 | new->next = NULL; 28 | 29 | if (*head == NULL) 30 | { 31 | *head = new; 32 | return (new); 33 | } 34 | 35 | while (temp->next) 36 | temp = temp->next; 37 | 38 | temp->next = new; 39 | 40 | return (new); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * free_list - frees a linked list 6 | * @head: list_t list to be freed 7 | */ 8 | void free_list(list_t *head) 9 | { 10 | list_t *temp; 11 | 12 | while (head) 13 | { 14 | temp = head->next; 15 | free(head->str); 16 | free(head); 17 | head = temp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x12-singly_linked_lists 2 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x12-singly_linked_lists/a -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef H_LISTS 2 | #define H_LISTS 3 | 4 | /*********************************HEADER*******************************/ 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | /** 11 | * struct list_s - singly linked list 12 | * @str: string - (malloc'ed string) 13 | * @len: length of the string 14 | * @next: points to the next node 15 | * 16 | * Description: singly linked list node structure 17 | * 18 | * ALX Projects 19 | */ 20 | typedef struct list_s 21 | { 22 | char *str; 23 | unsigned int len; 24 | struct list_s *next; 25 | } list_t; 26 | 27 | list_t *add_node(list_t **head, const char *str); 28 | list_t *add_node_end(list_t **head, const char *str); 29 | size_t list_len(const list_t *h); 30 | void free_list(list_t *head); 31 | size_t print_list(const list_t *h); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/.101-print_listint_safe.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x13-more_singly_linked_lists/.101-print_listint_safe.c.swp -------------------------------------------------------------------------------- /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 - prints all the elements of a linked list 5 | * @h: head of the list 6 | * 7 | * Return: the number of nodes 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | const listint_t *cursor = h; 12 | size_t count = 0; 13 | 14 | while (cursor != NULL) 15 | { 16 | printf("%d\n", cursor->n); 17 | count += 1; 18 | cursor = cursor->next; 19 | } 20 | return (count); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | *listint_len - function returns No. of elements 6 | * in a linked-list 7 | *@h:pointer: shows to the head of the list 8 | * 9 | * 10 | *Return: No. of elements in the linked list 11 | * 12 | * ALX PROJECTS 13 | */ 14 | 15 | size_t listint_len(const listint_t *h) 16 | { 17 | const listint_t *pointer = h; 18 | size_t nod_s = 0; 19 | 20 | while (pointer) 21 | { 22 | nod_s++; 23 | pointer = pointer->next; 24 | } 25 | return (nod_s); 26 | } 27 | /***********************STOP********************/ 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * delete_nodeint_at_index - Deletes the node at a given 4 | * index of a listint_t list. 5 | * @head: A pointer to the address of the 6 | * head of the listint_t list. 7 | * @index: The index of the node to be deleted - indices start at 0. 8 | * 9 | * Return: On success - 1. 10 | * On failure - -1. 11 | */ 12 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 13 | { 14 | listint_t *tmp, *copy = *head; 15 | unsigned int node; 16 | 17 | if (copy == NULL) 18 | return (-1); 19 | 20 | if (index == 0) 21 | { 22 | *head = (*head)->next; 23 | free(copy); 24 | return (1); 25 | } 26 | 27 | for (node = 0; node < (index - 1); node++) 28 | { 29 | if (copy->next == NULL) 30 | return (-1); 31 | 32 | copy = copy->next; 33 | } 34 | 35 | tmp = copy->next; 36 | copy->next = tmp->next; 37 | free(tmp); 38 | return (1); 39 | } 40 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | 4 | /***********START************/ 5 | /** 6 | * reverse_listint - Reversing a linked list. 7 | * @head:pointer: shows the address of 8 | * the head of the list_t list. 9 | * 10 | * Return:pointer-shows the first node of reversed list. 11 | */ 12 | listint_t *reverse_listint(listint_t **head) 13 | { 14 | listint_t *n_node, *prevv; 15 | 16 | if (head == NULL || *head == NULL) 17 | return (NULL); 18 | 19 | prevv = NULL;/**Initialize previous node to (0)**/ 20 | 21 | while ((*head)->next != NULL) 22 | { 23 | n_node = (*head)->next; 24 | (*head)->next = prevv; 25 | prevv = *head; 26 | *head = n_node; 27 | } 28 | 29 | (*head)->next = prevv; 30 | 31 | return (*head); 32 | } 33 | /***************STOP**************/ 34 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * find_listint_loop - Finds the loop contained in 4 | * a listint_t linked list. 5 | * @head: A pointer to the head of the listint_t list. 6 | * 7 | * Return: If there is no loop - NULL. 8 | * Otherwise - the address of the node where the loop starts. 9 | */ 10 | listint_t *find_listint_loop(listint_t *head) 11 | { 12 | listint_t *tortoise, *hare; 13 | 14 | if (head == NULL || head->next == NULL) 15 | return (NULL); 16 | 17 | tortoise = head->next; 18 | hare = (head->next)->next; 19 | 20 | while (hare) 21 | { 22 | if (tortoise == hare) 23 | { 24 | tortoise = head; 25 | 26 | while (tortoise != hare) 27 | { 28 | tortoise = tortoise->next; 29 | hare = hare->next; 30 | } 31 | 32 | return (tortoise); 33 | } 34 | 35 | tortoise = tortoise->next; 36 | hare = (hare->next)->next; 37 | } 38 | 39 | return (NULL); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /**********START*********/ 4 | /** 5 | *add_nodeint - funstion puts a new_node at the start 6 | * of a linked list. 7 | *@head:pointer: shows the address for the head of the linked-list 8 | *@n:integer: shows new node 9 | * 10 | *Return:retturns empty list/NULL 11 | * Or address of element in list 12 | * 13 | * ALX PROJECTS 14 | */ 15 | 16 | listint_t *add_nodeint(listint_t **head, const int n) 17 | { 18 | listint_t *nod_n; 19 | 20 | nod_n = malloc(sizeof(listint_t)); 21 | /**Return NULL**/ 22 | if (nod_n == NULL) 23 | return (NULL); 24 | 25 | nod_n->n = n; 26 | /**next new node**/ 27 | nod_n->next = *head; 28 | 29 | *head = nod_n; 30 | 31 | return (nod_n); 32 | } 33 | /**********STOP*********/ 34 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *add_nodeint_end - adds a node to the end of a linked list 5 | *@head: pointer to the head of the list 6 | *@n: number to be used as content 7 | * 8 | * 9 | *Return: address of the newly added node 10 | */ 11 | 12 | listint_t *add_nodeint_end(listint_t **head, const int n) 13 | { 14 | listint_t *new_node; 15 | listint_t *cursor = *head; 16 | 17 | new_node = malloc(sizeof(listint_t)); 18 | if (new_node != NULL) 19 | { 20 | new_node->n = n; 21 | new_node->next = NULL; 22 | } 23 | else 24 | return (NULL); 25 | if (cursor != NULL) 26 | { 27 | while (cursor->next != NULL) 28 | cursor = cursor->next; 29 | 30 | cursor->next = new_node; 31 | } 32 | else 33 | *head = new_node; 34 | return (new_node); 35 | } 36 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | *free_listint - frees a linked list 5 | *@head: head of the list 6 | * 7 | *Return: void 8 | */ 9 | 10 | void free_listint(listint_t *head) 11 | { 12 | listint_t *temp; 13 | 14 | while (head != NULL) 15 | { 16 | temp = head; 17 | head = head->next; 18 | free(temp); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /***********START**********/ 5 | /** 6 | * free_listint2 - Free list. 7 | * @head:pointer:-show to the address of the 8 | * head of the linked-list. 9 | * 10 | * Return: Sets the head to NULL. 11 | * 12 | * ALX PROJECTS 13 | */ 14 | 15 | void free_listint2(listint_t **head) 16 | { 17 | listint_t *pointer; 18 | listint_t *nod_c; 19 | 20 | if (head != NULL) 21 | { 22 | pointer = *head; 23 | while (pointer != NULL) 24 | { 25 | nod_c = pointer->next; 26 | free(pointer); 27 | pointer = nod_c; 28 | } 29 | 30 | *head = NULL; 31 | } 32 | } 33 | /**********STOP***********/ 34 | -------------------------------------------------------------------------------- /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 | * pop_listint - Deletes the head node of a listint_t list. 4 | * @head: A pointer to the address of the 5 | * head of the listint_t list. 6 | * 7 | * Return: If the linked list is empty - 0. 8 | * Otherwise - The head node's data (n). 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | listint_t *tmp; 13 | int ret; 14 | 15 | if (*head == NULL) 16 | return (0); 17 | 18 | tmp = *head; 19 | ret = (*head)->n; 20 | *head = (*head)->next; 21 | 22 | free(tmp); 23 | 24 | return (ret); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /************START************/ 4 | /** 5 | * get_nodeint_at_index - Finds node at *index_nth from-list 6 | * 7 | * @head:pointer: shows to the first node in list 8 | * @index:Number of nodes to access 9 | * 10 | * Return: node at nth_index/NULL with no node. 11 | * 12 | * ALX PROJECTS 13 | */ 14 | 15 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 16 | { 17 | unsigned int nod_c = 0; 18 | listint_t *nod_n = head; 19 | 20 | /****Loop****/ 21 | while (nod_n != NULL) 22 | { 23 | if (nod_c == index) 24 | return (nod_n); 25 | nod_n = nod_n->next; 26 | nod_c++; 27 | } 28 | /**Check on index value**/ 29 | return (NULL); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /*******START******/ 4 | /** 5 | *sum_listint - adds up all the data in linked-list 6 | *@head: pointer: shows head of the list 7 | * 8 | *Return: returns sum of No. 9 | * 10 | * ALX PROJECTS 11 | */ 12 | int sum_listint(listint_t *head) 13 | { 14 | int sum_no = 0; 15 | listint_t *nod_c = head; 16 | 17 | /**Loop**/ 18 | while (nod_c != NULL) 19 | { 20 | sum_no += nod_c->n; 21 | nod_c = nod_c->next; 22 | } 23 | 24 | /**return sum**/ 25 | return (sum_no); 26 | } 27 | /********STOP*******/ 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x13-more_singly_linked_lists 2 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x13-more_singly_linked_lists/a -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/g: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x13-more_singly_linked_lists/g -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/k: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x13-more_singly_linked_lists/k -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef H_LISTS 2 | #define H_LISTS 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * @n: integer 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | * 14 | */ 15 | typedef struct listint_s 16 | { 17 | int n; 18 | struct listint_s *next; 19 | } listint_t; 20 | 21 | size_t print_listint(const listint_t *h); 22 | size_t listint_len(const listint_t *h); 23 | listint_t *add_nodeint(listint_t **head, const int n); 24 | listint_t *add_nodeint_end(listint_t **head, const int n); 25 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 26 | int sum_listint(listint_t *head); 27 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 28 | void free_listint(listint_t *head); 29 | void free_listint2(listint_t **head); 30 | int pop_listint(listint_t **head); 31 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 32 | listint_t *reverse_listint(listint_t **head); 33 | size_t print_listint_safe(const listint_t *head); 34 | size_t free_listint_safe(listint_t **h); 35 | listint_t *find_listint_loop(listint_t *head); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | #include 5 | 6 | /******************START****************/ 7 | /** 8 | * binary_to_uint - func;converts binary No. to an unsigned int. 9 | * @b:pointer: shows to a string of 0 and 1 chars(characters). 10 | * 11 | * Return: If b = NULL/has chars not 0/ one - zero. 12 | * Else show converted No. 13 | * 14 | * ALX PROJECTS 15 | */ 16 | 17 | unsigned int binary_to_uint(const char *b) 18 | { 19 | const char *bit = b; 20 | int y; 21 | unsigned int re_sult = 0; 22 | 23 | if (!bit) 24 | return (0); 25 | 26 | for (y = 0; bit[y]; y++) 27 | { 28 | if (bit[y] < '0' || bit[y] > '1') 29 | return (0); 30 | re_sult = 2 * re_sult + (bit[y] - '0'); 31 | } 32 | 33 | return (re_sult); 34 | } 35 | /*****************STOP*******************/ 36 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | /*************START***********/ 5 | /** 6 | * print_binary - function,prints binary_rep of a No. 7 | * 8 | *@n: printable number. 9 | * 10 | * 11 | * ALX PROJECTS 12 | */ 13 | 14 | 15 | 16 | void print_binary(unsigned long int n) 17 | { 18 | const unsigned long int number = n; 19 | /**num to take place of 'n'**/ 20 | 21 | /**Check if grater than 1**/ 22 | if (number > 1) 23 | print_binary(number >> 1); 24 | 25 | _putchar((number & 1) + '0'); 26 | } 27 | /*************STOP*************/ 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | #include 5 | 6 | /********************START******************/ 7 | /** 8 | * get_endianness -function: Looks the endian. 9 | * 10 | * Return: For big_endian - 0. 11 | * For little_endian - 1. 12 | * 13 | * 14 | * 15 | * ALX PROJECTS 16 | */ 17 | 18 | 19 | int get_endianness(void) 20 | { 21 | unsigned int tst_end = 1; 22 | /*return endiannes*/ 23 | return (tst_end & 1); 24 | } 25 | /**********************STOP*********************/ 26 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | /******************START*****************/ 6 | /** 7 | * get_bit - func:finds value of bit at an index. 8 | * @n:bit: the bit itself. 9 | * @index: index, all indices start at zero(0). 10 | * 11 | * Return: for if an error occurs 12 | * Elsewhre bit at index. 13 | * 14 | * 15 | * ALX PROJECTS 16 | */ 17 | 18 | 19 | 20 | int get_bit(unsigned long int n, unsigned int index) 21 | { 22 | /**set new constants**/ 23 | unsigned long int number = n; 24 | unsigned int tag = index; 25 | 26 | /******Check if index is out of range*****/ 27 | if (tag >= (sizeof(unsigned long int) * 8)) 28 | return (-1); 29 | /*Error, Index out of range*/ 30 | 31 | if ((number & (1 << tag)) == 0) 32 | return (0); 33 | return (1); 34 | } 35 | /******************STOP*******************/ 36 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | 6 | /*******************START*******************/ 7 | /** 8 | * set_bit - function: sets bit in position to one (1). 9 | * @n:pointer: shows at the bit. 10 | * @index:shows the index to be set. 11 | * 12 | * Return: On success, 1 is returned 13 | * Elsewhere we get -1 when on failure. 14 | * 15 | * 16 | * 17 | * ALX PROJECTS 18 | */ 19 | 20 | 21 | 22 | int set_bit(unsigned long int *n, unsigned int index) 23 | { 24 | /*set new constants to work with*/ 25 | /** 26 | * number = n. 27 | * tag = index. 28 | */ 29 | unsigned long int *number = n; 30 | unsigned int tag = index; 31 | 32 | if (number == NULL || (tag > (sizeof(unsigned long int) * 8) - 1)) 33 | return (-1); 34 | 35 | /*Check*/ 36 | *number |= (1 << tag); 37 | 38 | /*Return Success*/ 39 | return (1); 40 | } 41 | /******************STOP**********************/ 42 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /*****************START*****************/ 9 | /** 10 | * clear_bit - function: sets value of bit to a position 0. 11 | * @n:pointer: shows to the bit. 12 | * @index:position: starts at 0 and is potions to be cleared. 13 | * 14 | * Return: On an error (-1) is returned. 15 | * Elsewhere the program should return - 1(one). 16 | * 17 | * 18 | * ALX PROJECTS 19 | */ 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | int clear_bit(unsigned long int *n, unsigned int index) 28 | { 29 | /** 30 | * Set new variables to work with 31 | * 32 | * *number = n. 33 | * tag = index. 34 | */ 35 | 36 | unsigned long int *number = n; 37 | unsigned int tag = index; 38 | 39 | /**Back to program 40 | * Check for error 41 | */ 42 | if (tag >= (sizeof(unsigned long int) * 8)) 43 | return (-1); 44 | 45 | *number &= ~(1 << tag); 46 | 47 | return (1); 48 | } 49 | /**********************STOP*************************/ 50 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /******************START********************/ 4 | /** 5 | * flip_bits - function: counts No of Bits 6 | * flipped from No1 to No2 or another. 7 | * @n:pointer: number to be flipped ny the function 8 | * @m:flipping number or constant. 9 | * 10 | * Return:flipped bits n ^ m 11 | * 12 | * 13 | * ALX PROJECTS 14 | */ 15 | 16 | 17 | 18 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 19 | { 20 | unsigned long int no_flp = n ^ m; 21 | int num_ct = 0; 22 | 23 | /*loop*/ 24 | for (; no_flp; no_flp >>= 1) 25 | { 26 | if (no_flp & 1) 27 | num_ct++; 28 | } 29 | 30 | return (num_ct); 31 | } 32 | /**********************STOP******************/ 33 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | 0x14-bit_manipulation 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/crackme3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x14-bit_manipulation/crackme3 -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _H_MAIN_ 2 | #define _H_MAIN_ 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | /*****Functions******/ 11 | 12 | unsigned int binary_to_uint(const char *b); 13 | int check_valid_string(const char *b); 14 | int _putchar(char c); 15 | void print_binary(unsigned long int n); 16 | int get_bit(unsigned long int n, unsigned int index); 17 | int set_bit(unsigned long int *n, unsigned int index); 18 | int clear_bit(unsigned long int *n, unsigned int index); 19 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 20 | int get_endianness(void); 21 | 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /0x15-file_io/.100-elf_header.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x15-file_io/.100-elf_header.c.swp -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | #include 5 | 6 | /*******************START*******************/ 7 | /** 8 | * read_textfile - functions; reads text_file & prints to_stdout. 9 | * @filename:pointer: shows to the name of the file. 10 | * @letters: Shows the No. of letters the 11 | * function (read_textfile) should read & print. 12 | * 13 | * Return:Returns 0 if filename is NULL. 14 | * 15 | * 16 | * ALX PROJECTS 17 | */ 18 | 19 | 20 | ssize_t read_textfile(const char *filename, size_t letters) 21 | { 22 | /****Declarations******/ 23 | int f_d; 24 | ssize_t w_cnt; 25 | ssize_t w_r; 26 | char *buffer; 27 | 28 | if (filename == NULL) 29 | return (0); 30 | f_d = open(filename, O_RDONLY); 31 | if (f_d == -1) 32 | return (0); 33 | buffer = malloc(sizeof(char) * letters); 34 | if (buffer == NULL) 35 | { 36 | free(buffer); 37 | return (0); 38 | } 39 | w_r = read(f_d, buffer, letters); 40 | if (w_r == -1) 41 | return (0); 42 | w_cnt = write(STDOUT_FILENO, buffer, w_r); 43 | if (w_cnt == -1 || w_r != w_cnt) 44 | return (0); 45 | 46 | free(buffer); 47 | close(f_d); 48 | return (w_cnt); 49 | } 50 | /**************************STOP******************************/ 51 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | #include 6 | #include 7 | 8 | /****************START****************/ 9 | /** 10 | * create_file - function; this creates a file 11 | * 12 | * @filename:pointer: shows to the name of file to be created 13 | * 14 | * @text_content:pointer: shows to a string to write to file 15 | * 16 | * Return: gives a (-ve) -1 when function fails 17 | * Elsewhere returns - 1. 18 | * 19 | * 20 | * ALX PROJECTS 21 | */ 22 | 23 | 24 | 25 | int create_file(const char *filename, char *text_content) 26 | { 27 | /***sets constants***/ 28 | int f_d; 29 | ssize_t len = 0; 30 | ssize_t b_w; 31 | 32 | if (filename == NULL) 33 | return (-1); 34 | f_d = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 35 | if (f_d == -1) 36 | return (-1); 37 | if (text_content != NULL) 38 | { 39 | while (text_content[len] != '\0') 40 | len++; 41 | b_w = write(f_d, text_content, len); 42 | close(f_d); 43 | 44 | if (b_w == -1) 45 | return (-1); 46 | } 47 | else 48 | { 49 | close(f_d); 50 | } 51 | return (1); 52 | } 53 | /**********************START********************/ 54 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | #include 6 | #include 7 | 8 | /******************START****************/ 9 | /** 10 | * append_text_to_file - function; appends_text at end of the file 11 | * 12 | * @filename:pointer:shows to the name of the file. 13 | * 14 | * @text_content: represent the string to be added at end of_file 15 | * 16 | * Return: filename is NULL -1 when the function fails. 17 | * The user to lack permision on missing file -1 18 | * Elsewhere return 1. 19 | * 20 | * ALX PROJECTS 21 | */ 22 | 23 | 24 | int append_text_to_file(const char *filename, char *text_content) 25 | { 26 | /**Set Constants**/ 27 | const char *f_name = filename; 28 | char *t_c = text_content; 29 | int i; 30 | int j; 31 | int len = 0; 32 | 33 | if (f_name == NULL) 34 | return (-1); 35 | 36 | if (t_c != NULL) 37 | { 38 | for (len = 0; t_c[len];) 39 | len++; 40 | } 41 | 42 | i = open(f_name, O_WRONLY | O_APPEND); 43 | j = write(i, t_c, len); 44 | 45 | if (i == -1 || j == -1) 46 | return (-1); 47 | 48 | close(i); 49 | 50 | return (1); 51 | } 52 | /***************************START**************************/ 53 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | 0x15-file_io 2 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef H_MAIN_ 2 | #define H_MAIN_ 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | ssize_t read_textfile(const char *filename, size_t letters); 15 | int create_file(const char *filename, char *text_content); 16 | int append_text_to_file(const char *filename, char *text_content); 17 | 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/.103-keygen.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x17-doubly_linked_lists/.103-keygen.c.swp -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - function: Print all the elements of dlistint_t list 5 | * 6 | * @h: Pointer- shows to the head of the list 7 | * 8 | * Return: Returns the No. of nodes 9 | */ 10 | 11 | size_t print_dlistint(const dlistint_t *h) 12 | { 13 | 14 | int c_t = 0; 15 | 16 | if (h == NULL) 17 | return (c_t); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | printf("%d\n", h->n); 25 | h = h->next; 26 | c_t++; 27 | } 28 | 29 | return (c_t); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | /** 5 | * dlistint_len - function: Returns No. of elmnts in dlistint_t list 6 | * 7 | * @h: Pointer: shows to the head of the linked list 8 | * 9 | * Return: Returns the number of nodes 10 | */ 11 | 12 | size_t dlistint_len(const dlistint_t *h) 13 | { 14 | int node_c = 0; 15 | 16 | if (h == NULL) 17 | return (node_c); 18 | 19 | while (h != NULL) 20 | { 21 | node_c++; 22 | h = h->next; 23 | } 24 | 25 | return (node_c); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | /** 7 | * main - function: prints passwords for the crackme5 8 | * 9 | * @argc: No. of arguments applied 10 | * @argv: Pointer; points to the argumnets 11 | * 12 | * Return: Always 0. 13 | */ 14 | int main(int __attribute__((__unused__)) argc, char *argv[]) 15 | { 16 | char pwd[7], *c; 17 | int l = strlen(argv[1]), j, p; 18 | 19 | c = "A-CHRDw87lNS0E9B2TibgpnMVys5XzvtOGJcYLU+4mjW6fxqZeF3Qa1rPhdKIouk"; 20 | 21 | p = (l ^ 59) & 63; 22 | pwd[0] = c[p]; 23 | 24 | p = 0; 25 | for (j = 0; j < l; j++) 26 | p += argv[1][j]; 27 | pwd[1] = c[(p ^ 79) & 63]; 28 | 29 | p = 1; 30 | for (j = 0; j < l; j++) 31 | p *= argv[1][j]; 32 | pwd[2] = c[(p ^ 85) & 63]; 33 | 34 | p = 0; 35 | for (j = 0; j < l; j++) 36 | { 37 | if (argv[1][j] > p) 38 | p = argv[1][j]; 39 | } 40 | srand(p ^ 14); 41 | pwd[3] = c[rand() & 63]; 42 | 43 | p = 0; 44 | for (j = 0; j < l; j++) 45 | p += (argv[1][j] * argv[1][j]); 46 | pwd[4] = c[(p ^ 239) & 63]; 47 | 48 | for (j = 0; j < argv[1][0]; j++) 49 | p = rand(); 50 | pwd[5] = c[(p ^ 229) & 63]; 51 | 52 | pwd[6] = '\0'; 53 | printf("%s", pwd); 54 | return (0); 55 | } 56 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * add_dnodeint - functions: adds new node at start of dlistint_t list 7 | * 8 | * @head: Double pointer to the head of the list 9 | * @n: This is the node to be added 10 | * 11 | * Return: NULL if it failed 12 | */ 13 | 14 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 15 | { 16 | dlistint_t *n_node; 17 | 18 | n_node = malloc(sizeof(dlistint_t)); 19 | if (n_node == NULL) 20 | return (NULL); 21 | 22 | n_node->n = n; 23 | n_node->next = *head; 24 | n_node->prev = NULL; 25 | if (*head != NULL) 26 | (*head)->prev = n_node; 27 | *head = n_node; 28 | return (n_node); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * add_dnodeint_end -function: add new node to end of list 7 | * 8 | * @head: Double pointer to head of list 9 | * @n: Integer to add to new node 10 | * 11 | * Return: returns NULL if it failed 12 | */ 13 | 14 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 15 | { 16 | dlistint_t *n_node; 17 | 18 | n_node = malloc(sizeof(dlistint_t)); 19 | 20 | if (n_node == NULL) 21 | return (NULL); 22 | 23 | n_node->n = n; 24 | n_node->next = NULL; 25 | 26 | if (*head == NULL) 27 | { 28 | n_node->prev = NULL; 29 | *head = n_node; 30 | return (n_node); 31 | } 32 | 33 | while ((*head)->next != NULL) 34 | *head = (*head)->next; 35 | 36 | (*head)->next = n_node; 37 | n_node->prev = *head; 38 | 39 | return (n_node); 40 | } 41 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * free_dlistint - function that frees a list 7 | * 8 | * @head: head of the linked list 9 | */ 10 | void free_dlistint(dlistint_t *head) 11 | { 12 | dlistint_t *p; 13 | 14 | while (head) 15 | { 16 | p = head->next; 17 | free(head); 18 | head = p; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /*******************************START*******************************/ 6 | /** 7 | * get_dnodeint_at_index - function to locate a node in a list 8 | * 9 | * @head: Pointer: head of the list 10 | * @index: Pointer: the node 11 | * 12 | * Return: Node not exist - NULL. 13 | * Elsewhere - Address of identified node 14 | */ 15 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 16 | { 17 | for (; index != 0; index--) 18 | { 19 | if (head == NULL) 20 | return (NULL); 21 | head = head->next; 22 | } 23 | 24 | return (head); 25 | } 26 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | 6 | /** 7 | * sum_dlistint - Function: sums components of a list 8 | * 9 | * @head: Pointer, shows head of the list 10 | * 11 | * Return: Sum of components 12 | */ 13 | int sum_dlistint(dlistint_t *head) 14 | { 15 | int add = 0; 16 | 17 | while (head) 18 | { 19 | add += head->n; 20 | head = head->next; 21 | } 22 | 23 | return (add); 24 | } 25 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | /** 5 | * insert_dnodeint_at_index - function: insert new node in given position 6 | * 7 | * @h: Pointer to the node 8 | * @idx: index of the list 9 | * @n: Data of new node 10 | * 11 | * Return: NULL if it failed 12 | */ 13 | 14 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 15 | { 16 | dlistint_t *p = *h, *nw; 17 | 18 | if (idx == 0) 19 | return (add_dnodeint(h, n)); 20 | 21 | for (; idx != 1; idx--) 22 | { 23 | p = p->next; 24 | if (p == NULL) 25 | return (NULL); 26 | } 27 | 28 | if (p->next == NULL) 29 | return (add_dnodeint_end(h, n)); 30 | 31 | nw = malloc(sizeof(dlistint_t)); 32 | if (nw == NULL) 33 | return (NULL); 34 | 35 | nw->n = n; 36 | nw->prev = p; 37 | nw->next = p->next; 38 | p->next->prev = nw; 39 | p->next = nw; 40 | 41 | return (nw); 42 | } 43 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | dlistint_t *head; 14 | 15 | head = NULL; 16 | add_dnodeint_end(&head, 0); 17 | add_dnodeint_end(&head, 1); 18 | add_dnodeint_end(&head, 2); 19 | add_dnodeint_end(&head, 3); 20 | add_dnodeint_end(&head, 4); 21 | add_dnodeint_end(&head, 98); 22 | add_dnodeint_end(&head, 402); 23 | add_dnodeint_end(&head, 1024); 24 | print_dlistint(head); 25 | printf("-----------------\n"); 26 | insert_dnodeint_at_index(&head, 5, 4096); 27 | print_dlistint(head); 28 | free_dlistint(head); 29 | head = NULL; 30 | return (EXIT_SUCCESS); 31 | } 32 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - delete node from list 5 | * 6 | * @head: Pointer, to the head of the list 7 | * @index: index of the node to delete 8 | * 9 | * Return: On success - 1. 10 | * Elsewhere - -1. 11 | */ 12 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 13 | { 14 | dlistint_t *p = *head; 15 | 16 | if (*head == NULL) 17 | return (-1); 18 | 19 | for (; index != 0; index--) 20 | { 21 | if (p == NULL) 22 | return (-1); 23 | p = p->next; 24 | } 25 | 26 | if (p == *head) 27 | { 28 | *head = p->next; 29 | if (*head != NULL) 30 | (*head)->prev = NULL; 31 | } 32 | 33 | else 34 | { 35 | p->prev->next = p->next; 36 | if (p->next != NULL) 37 | p->next->prev = p->prev; 38 | } 39 | 40 | free(p); 41 | return (1); 42 | } 43 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x17-doubly_linked_lists 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_H_ 2 | #define _LISTS_H_ 3 | #include 4 | #include 5 | #include 6 | /*************************HEADER FILE********************************/ 7 | /** 8 | * struct dlistint_s - function: double-linked lists. 9 | * @n: This is the integer 10 | * 11 | * @prev: Pointer- points to the previous node 12 | * @next: Pointer- points to the next node 13 | * 14 | * Description: doubly linked list node structure 15 | * 16 | */ 17 | typedef struct dlistint_s 18 | { 19 | int n; 20 | struct dlistint_s *prev; 21 | struct dlistint_s *next; 22 | } dlistint_t; 23 | 24 | size_t print_dlistint(const dlistint_t *h); 25 | size_t dlistint_len(const dlistint_t *h); 26 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 27 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 28 | void free_dlistint(dlistint_t *head); 29 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 30 | int sum_dlistint(dlistint_t *head); 31 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 32 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 33 | #endif 34 | /************************HEADER FILE*******************************/ 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/result.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | def is_palindrome(num): 4 | # Check if the number is a palindrome 5 | return str(num) == str(num)[::-1] 6 | 7 | largest_palindrome = 0 8 | 9 | for i in range(100, 1000): 10 | for j in range(i, 1000): # Start from i to avoid duplicate combinations 11 | product = i * j 12 | if is_palindrome(product) and product > largest_palindrome and 99 < i < 1000 and 99 < j < 1000: 13 | largest_palindrome = product 14 | 15 | # Save the result to the file 16 | with open('102-result', 'w') as file: 17 | file.write(str(largest_palindrome)) 18 | 19 | # Print the result for verification 20 | print(largest_palindrome) 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/.libdynamic.so.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/.libdynamic.so.swp -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isupper - function that verifies if a character is uppercase or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isupper(int c) 9 | { 10 | if ((c >= 'A') && (c <= 'Z')) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memset - The _memset() function fills 4 | * the first n bytes of the memory area 5 | * pointed to by s with the constant byte b 6 | *@s:target 7 | *@b: constant byte 8 | *@n:number of byte 9 | *Return: returns new value of target 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | while (n) 15 | { 16 | s[n - 1] = b; 17 | n--; 18 | } 19 | return (s); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/0-memset.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_strcat - concatenates the string pointed to by @src to 5 | * the end of the string pointed to by @dest 6 | *@dest: String that will be appended 7 | *@src: String to be concatenated upon 8 | * 9 | * Return: returns poiner to @dest 10 | */ 11 | 12 | char *_strcat(char *dest, char *src) 13 | { 14 | 15 | int index = 0, dest_len = 0; 16 | 17 | while (dest[index++]) 18 | dest_len++; 19 | 20 | for (index = 0; src[index]; index++) 21 | dest[dest_len++] = src[index]; 22 | 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -fPIC -c *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_isdigit - function that verifies if a character is a digit or not 4 | *@c: tested character 5 | *Return: returns 1 if it is uppercase , 0 if not 6 | */ 7 | 8 | int _isdigit(int c) 9 | { 10 | if ((c >= 48) && (c <= 57)) 11 | return (1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_memcpy - The _memcpy() function copies n bytes 4 | * from memory area src to memory area dest 5 | *@dest:area where bytes are copied to 6 | *@src:area where bytes are copied from 7 | *@n:number of bytes to copy 8 | *Return: returns a pointer to n 9 | */ 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | unsigned int i = 0; 13 | 14 | for (; i < n; i++) 15 | dest[i] = src[i]; 16 | 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strncat - concatenate two strings but add inputted number of bytes 4 | *@dest: string to be appended upon 5 | *@src: string to be completed at end of dest 6 | *@n:integer parameter to compare index to 7 | *Return: returns new concatenated string 8 | */ 9 | 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | 13 | int index = 0, dest_len = 0; 14 | 15 | while (dest[index++]) 16 | dest_len++; 17 | 18 | for (index = 0; src[index] && index < n; index++) 19 | dest[dest_len++] = src[index]; 20 | 21 | return (dest); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | /** 2 | * _atoi - changes a string to an int 3 | * @s: the string to be changed 4 | * 5 | * Return: the converted int 6 | */ 7 | int _atoi(char *s) 8 | { 9 | int i = 1; 10 | unsigned int num = 0; 11 | do { 12 | if (*s == '-') 13 | i *= -1; 14 | else if (*s >= '0' && *s <= '9') 15 | num = num * 10 + (*s - '0'); 16 | else if (num > 0) 17 | break; 18 | } while (*s++); 19 | return (num *i); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-tests.py: -------------------------------------------------------------------------------- 1 | import random 2 | import ctypes 3 | 4 | cops = ctypes.CDLL('./100-operations.so') 5 | a = random.randint(-111, 111) 6 | b = random.randint(-111, 111) 7 | print("{} + {} = {}".format(a, b, cops.add(a, b))) 8 | print("{} - {} = {}".format(a, b, cops.sub(a, b))) 9 | print("{} x {} = {}".format(a, b, cops.mul(a, b))) 10 | print("{} / {} = {}".format(a, b, cops.div(a, b))) 11 | print("{} % {} = {}".format(a, b, cops.mod(a, b))) 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/victorpreston/alx-low_level_programming/raw/master/0x18-dynamic_libraries/putshack.so 3 | export LD_PRELOAD=/tmp/putshack.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strchr - Returns a pointer to the first occurrence 4 | *of the character c in the string s, or NULL if the 5 | *character is not found 6 | * 7 | *@s:string targeted 8 | *@c:character targeted 9 | * 10 | *Return: returns pointer to first occcurence of c 11 | */ 12 | char *_strchr(char *s, char c) 13 | { 14 | int i; 15 | 16 | for (i = 0; (s[i] != c) && (s[i] != '\0'); i++) 17 | ; 18 | if (s[i] == c) 19 | return (s + i); 20 | else 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strlen - returns the length of a string 4 | * @s: string 5 | *Return: returns lenght; 6 | */ 7 | int _strlen(char *s) 8 | { 9 | int count, inc; 10 | inc = 0; 11 | for (count = 0; s[count] != '\0'; count++) 12 | inc++; 13 | 14 | return (inc); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - C function that copies a string, including the 4 | * terminating null byte, using at most an inputted number of bytes. 5 | * If the length of the source string is less than the maximum byte number, 6 | * the remainder of the destination string is filled with null bytes. 7 | * Works identically to the standard library function `strncpy`. 8 | *@dest: buffer storing the string copy 9 | *@src:the source string 10 | *@n:max nummber of byte copied 11 | *Return: returns 12 | */ 13 | 14 | char *_strncpy(char *dest, char *src, int n) 15 | { 16 | int i; 17 | 18 | for (i = 0; i < n && src[i] != '\0'; i++) 19 | dest[i] = src[i]; 20 | 21 | for ( ; i < n; i++) 22 | dest[i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Checks for lowercase alphabet 5 | * @c: Character to be checked 6 | * Return: if character is lowercase, otherwise 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | if (c >= 'a' && c <= 'z') 12 | return (1); 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/3-islower.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_puts - prints a string 4 | * @str: string to print 5 | * 6 | * Description: prints a string 7 | * On success: return the number of characters printed 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | while (*str) 13 | _putchar(*str++); 14 | 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/3-puts.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Function which compare two strings and 5 | *@s1: first string 6 | *@s2:second string 7 | *Return: 8 | * returns zero if s1 == s2 9 | * returns negative number if s1 < s2 10 | * returns positive number if s1 > s2 11 | */ 12 | 13 | int _strcmp(char *s1, char *s2) 14 | { 15 | int i = 0, diff = 0; 16 | 17 | while (1) 18 | { 19 | if (s1[i] == '\0' && s2[i] == '\0') 20 | break; 21 | else if (s1[i] == '\0') 22 | { 23 | diff = s2[i]; 24 | break; 25 | } 26 | else if (s2[i] == '\0') 27 | { 28 | diff = s1[i]; 29 | break; 30 | } 31 | else if (s1[i] != s2[i]) 32 | { 33 | diff = s1[i] - s2[i]; 34 | break; 35 | } 36 | else 37 | i++; 38 | 39 | } 40 | return (diff); 41 | } 42 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strspn - search the number of bytes in the initial 4 | * segment of s which consist only of bytes from accept 5 | *@s:segment targeted 6 | *@accept:reference bytes container 7 | * 8 | *Return:returns the number of bytes in the initial 9 | * segment of s which consist only of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int bytes = 0; 14 | int i; 15 | 16 | while (*s) 17 | { 18 | for (i = 0; accept[i]; i++) 19 | { 20 | if (accept[i] == *s) 21 | { 22 | bytes++; 23 | break; 24 | } 25 | else if ((accept[i + 1]) == '\0') 26 | return (bytes); 27 | } 28 | s++; 29 | } 30 | return (bytes); 31 | } 32 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - Checks for alphabetic order 4 | * 5 | * @c: alpabet to check for 6 | * 7 | * Return: 1 if c is a letter, 0 otherwise 8 | */ 9 | int _isalpha(int c) 10 | { 11 | return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strpbrk - The _strpbrk() function locates the first 4 | * occurrence in the string s of any of the bytes in 5 | *the string accept 6 | * 7 | *@s: string where search is made 8 | *@accept: string where searched bytes are located 9 | * 10 | *Return:Returns a pointer to the byte in s that matches 11 | * one of the bytes in accept, or NULL if no such byte is found 12 | */ 13 | 14 | char *_strpbrk(char *s, char *accept) 15 | { 16 | int i; 17 | 18 | while (*s) 19 | { 20 | for (i = 0; accept[i]; i++) 21 | { 22 | if (accept[i] == *s) 23 | return (s); 24 | } 25 | s++; 26 | } 27 | 28 | return ('\0'); 29 | } 30 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strstr - The _strstr() function finds the first occurrence 4 | * of the substring needle in the string haystack. 5 | * The terminating null bytes (\0) are not compared 6 | *@haystack: string where the search is made 7 | *@needle: string whose occurence is searched in haystack 8 | *Return:Returns a pointer to the beginning of the located 9 | * substring, or NULL if the substring is not found. 10 | */ 11 | 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | int i; 15 | 16 | if (*needle == 0) 17 | return (haystack); 18 | 19 | while (*haystack) 20 | { 21 | i = 0; 22 | 23 | if (haystack[i] == needle[i]) 24 | { 25 | do { 26 | if (needle[i + 1] == '\0') 27 | return (haystack); 28 | i++; 29 | } while (haystack[i] == needle[i]); 30 | } 31 | haystack++; 32 | } 33 | return ('\0'); 34 | } 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - computes the absolute value of an integrer 4 | * 5 | * @n: the int to check 6 | * 7 | * Return: the absolute value of int 8 | * 9 | */ 10 | int _abs(int n) 11 | { 12 | if (n >= 0) 13 | { 14 | return (n); 15 | } 16 | return (-n); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/6-abs.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcpy - Copy paste string 4 | *@dest: destination 5 | *@src: source 6 | *Return: dest 7 | */ 8 | char *_strcpy(char *dest, char *src) 9 | { 10 | int inc = 0; 11 | while (*(src + inc) != '\0') 12 | { 13 | *(dest + inc) = *(src + inc); 14 | inc++; 15 | } 16 | *(dest + inc) = '\0'; 17 | return (dest); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | 0x18-dynamic_libraries 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | #include 4 | #include 5 | 6 | int _putchar(char c); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int _abs(int n); 10 | int _isupper(int c); 11 | int _isdigit(int c); 12 | int _strlen(char *s); 13 | void _puts(char *s); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | char *_strcat(char *dest, char *src); 17 | char *_strncat(char *dest, char *src, int n); 18 | char *_strncpy(char *dest, char *src, int n); 19 | int _strcmp(char *s1, char *s2); 20 | char *_memset(char *s, char b, unsigned int n); 21 | char *_memcpy(char *dest, char *src, unsigned int n); 22 | char *_strchr(char *s, char c); 23 | unsigned int _strspn(char *s, char *accept); 24 | char *_strpbrk(char *s, char *accept); 25 | char *_strstr(char *haystack, char *needle); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/putshack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int rand() 5 | { 6 | static int ct = -1; 7 | 8 | ct++; 9 | if (ct == 0) 10 | return 8; 11 | if (ct == 1) 12 | return 8; 13 | if (ct == 2) 14 | return 7; 15 | if (ct == 3) 16 | return 9; 17 | if (ct == 4) 18 | return 23; 19 | if (ct == 5) 20 | return 74; 21 | return ct * ct % 30000; 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/putshack.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/putshack.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/putshack.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x18-dynamic_libraries/putshack.so -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Creates a hash table. 5 | * @size: The size of the array. 6 | * 7 | * Return: If an error occurs - NULL. 8 | * Otherwise - a pointer to the new hash table. 9 | */ 10 | hash_table_t *hash_table_create(unsigned long int size) 11 | { 12 | hash_table_t *ht; 13 | unsigned long int i; 14 | 15 | ht = malloc(sizeof(hash_table_t)); 16 | if (ht == NULL) 17 | return (NULL); 18 | 19 | ht->size = size; 20 | ht->array = malloc(sizeof(hash_node_t *) * size); 21 | if (ht->array == NULL) 22 | return (NULL); 23 | for (i = 0; i < size; i++) 24 | ht->array[i] = NULL; 25 | 26 | return (ht); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | 6 | /** 7 | * main - check the code for 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%p\n", (void *)ht); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - function: implementing the djb2 algorithm. 5 | * @str: Hashed String. 6 | * 7 | * Return: Hash Value. 8 | */ 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hash; 12 | int c; 13 | 14 | hash = 5381; 15 | while ((c = *str++)) 16 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 17 | 18 | return (hash); 19 | } 20 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-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 | char *s; 14 | 15 | s = "cisfun"; 16 | printf("%lu\n", hash_djb2((unsigned char *)s)); 17 | s = "Don't forget to tweet today"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | s = "98"; 20 | printf("%lu\n", hash_djb2((unsigned char *)s)); 21 | return (EXIT_SUCCESS); 22 | } 23 | -------------------------------------------------------------------------------- /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 | * key_index - function to get key index & value 4 | * @key: value to index search. 5 | * @size: Size of hash table. 6 | * 7 | * Return: The index of the key. 8 | * Description: Uses the djb2 algorithm. 9 | */ 10 | 11 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 12 | { 13 | return (hash_djb2(key) % size); 14 | } 15 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-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 | char *s; 14 | unsigned long int hash_table_array_size; 15 | 16 | hash_table_array_size = 1024; 17 | s = "cisfun"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 20 | s = "Don't forget to tweet today"; 21 | printf("%lu\n", hash_djb2((unsigned char *)s)); 22 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 23 | s = "98"; 24 | printf("%lu\n", hash_djb2((unsigned char *)s)); 25 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 26 | return (EXIT_SUCCESS); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_set - function, add to hash table. 4 | * @ht: A pointer to the hash table. 5 | * @key: The key to add - cannot be an empty string. 6 | * @value: The value associated with key. 7 | * 8 | * Return: return 0 or 1. 9 | */ 10 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 11 | { 12 | hash_node_t *new; 13 | char *value_copy; 14 | unsigned long int index, i; 15 | 16 | if (ht == NULL || key == NULL || *key == '\0' || value == NULL) 17 | return (0); 18 | 19 | value_copy = strdup(value); 20 | if (value_copy == NULL) 21 | return (0); 22 | 23 | index = key_index((const unsigned char *)key, ht->size); 24 | for (i = index; ht->array[i]; i++) 25 | { 26 | if (strcmp(ht->array[i]->key, key) == 0) 27 | { 28 | free(ht->array[i]->value); 29 | ht->array[i]->value = value_copy; 30 | return (1); 31 | } 32 | } 33 | 34 | new = malloc(sizeof(hash_node_t)); 35 | if (new == NULL) 36 | { 37 | free(value_copy); 38 | return (0); 39 | } 40 | new->key = strdup(key); 41 | if (new->key == NULL) 42 | { 43 | free(new); 44 | return (0); 45 | } 46 | new->value = value_copy; 47 | new->next = ht->array[index]; 48 | ht->array[index] = new; 49 | 50 | return (1); 51 | } 52 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "hash_tables.h" 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always EXIT_SUCCESS. 9 | */ 10 | int main(void) 11 | { 12 | hash_table_t *ht; 13 | 14 | ht = hash_table_create(1024); 15 | hash_table_set(ht, "betty", "cool"); 16 | return (EXIT_SUCCESS); 17 | } 18 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_table_get - function to retrieve value. 4 | * @ht: Pointer to the hash table. 5 | * @key: Key to get value 6 | * 7 | * Return: NULL or node value. 8 | */ 9 | char *hash_table_get(const hash_table_t *ht, const char *key) 10 | { 11 | hash_node_t *node; 12 | unsigned long int index; 13 | 14 | if (ht == NULL || key == NULL || *key == '\0') 15 | return (NULL); 16 | 17 | index = key_index((const unsigned char *)key, ht->size); 18 | if (index >= ht->size) 19 | return (NULL); 20 | 21 | node = ht->array[index]; 22 | while (node && strcmp(node->key, key) != 0) 23 | node = node->next; 24 | 25 | return ((node == NULL) ? NULL : node->value); 26 | } 27 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - Prints a hash table. 5 | * @ht: A pointer to the hash table to print. 6 | * 7 | * Description: Key/value pairs are printed in the order 8 | * they appear in the array of the hash table. 9 | */ 10 | void hash_table_print(const hash_table_t *ht) 11 | { 12 | hash_node_t *node; 13 | unsigned long int i; 14 | unsigned char comma_flag = 0; 15 | 16 | if (ht == NULL) 17 | return; 18 | 19 | printf("{"); 20 | for (i = 0; i < ht->size; i++) 21 | { 22 | if (ht->array[i] != NULL) 23 | { 24 | if (comma_flag == 1) 25 | printf(", "); 26 | 27 | node = ht->array[i]; 28 | while (node != NULL) 29 | { 30 | printf("'%s': '%s'", node->key, node->value); 31 | node = node->next; 32 | if (node != NULL) 33 | printf(", "); 34 | } 35 | comma_flag = 1; 36 | } 37 | } 38 | printf("}\n"); 39 | } 40 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-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 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_print(ht); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Bob", "and Kris love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Cool"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_print(ht); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - Deletes a hash table. 5 | * @ht: A pointer to a hash table. 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | hash_table_t *head = ht; 10 | hash_node_t *node, *tmp; 11 | unsigned long int i; 12 | 13 | for (i = 0; i < ht->size; i++) 14 | { 15 | if (ht->array[i] != NULL) 16 | { 17 | node = ht->array[i]; 18 | while (node != NULL) 19 | { 20 | tmp = node->next; 21 | free(node->key); 22 | free(node->value); 23 | free(node); 24 | node = tmp; 25 | } 26 | } 27 | } 28 | free(head->array); 29 | free(head); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-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 | hash_table_t *ht; 14 | char *key; 15 | char *value; 16 | 17 | ht = hash_table_create(1024); 18 | hash_table_set(ht, "c", "fun"); 19 | hash_table_set(ht, "python", "awesome"); 20 | hash_table_set(ht, "Bob", "and Kris love asm"); 21 | hash_table_set(ht, "N", "queens"); 22 | hash_table_set(ht, "Asterix", "Obelix"); 23 | hash_table_set(ht, "Betty", "Cool"); 24 | hash_table_set(ht, "98", "Battery Streetz"); 25 | key = strdup("Tim"); 26 | value = strdup("Britton"); 27 | hash_table_set(ht, key, value); 28 | key[0] = '\0'; 29 | value[0] = '\0'; 30 | free(key); 31 | free(value); 32 | hash_table_set(ht, "98", "Battery Street"); 33 | hash_table_set(ht, "hetairas", "Bob"); 34 | hash_table_set(ht, "hetairas", "Bob Z"); 35 | hash_table_set(ht, "mentioner", "Bob"); 36 | hash_table_set(ht, "hetairas", "Bob Z Chu"); 37 | hash_table_print(ht); 38 | hash_table_delete(ht); 39 | return (EXIT_SUCCESS); 40 | } 41 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | 0x1A-hash_tables 2 | -------------------------------------------------------------------------------- /0x1A-hash_tables/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x1A-hash_tables/a -------------------------------------------------------------------------------- /0x1A-hash_tables/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x1A-hash_tables/b -------------------------------------------------------------------------------- /0x1A-hash_tables/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x1A-hash_tables/c -------------------------------------------------------------------------------- /0x1A-hash_tables/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x1A-hash_tables/e -------------------------------------------------------------------------------- /0x1A-hash_tables/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x1A-hash_tables/f -------------------------------------------------------------------------------- /0x1A-hash_tables/g: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x1A-hash_tables/g -------------------------------------------------------------------------------- /0x1A-hash_tables/sht: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorpreston/alx-low_level_programming/0007cb574899ea42e57c9c85d72164a5c4f41d1c/0x1A-hash_tables/sht -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 4 | all: $(SRC) 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean: 11 | $(RM) *~ $(NAME) 12 | 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: fclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(CLFAGS) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Return the perimiter of an island. 7 | The grid represents water by 0 and land by 1. 8 | Args: 9 | grid (list): A list of list of integers representing an island. 10 | Returns: 11 | The perimeter of the island defined in grid. 12 | """ 13 | width = len(grid[0]) 14 | height = len(grid) 15 | edges = 0 16 | size = 0 17 | 18 | for i in range(height): 19 | for j in range(width): 20 | if grid[i][j] == 1: 21 | size += 1 22 | if (j > 0 and grid[i][j - 1] == 1): 23 | edges += 1 24 | if (i > 0 and grid[i - 1][j] == 1): 25 | edges += 1 26 | return size * 4 - edges * 2 27 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | 0x1C-makefiles 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 5 | * integers using the Linear search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: Always EXIT_SUCCESS 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < (int)size; i++) 20 | { 21 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 22 | if (value == array[i]) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | int index, m, k, prev; 16 | 17 | if (array == NULL || size == 0) 18 | return (-1); 19 | 20 | m = (int)sqrt((double)size); 21 | k = 0; 22 | prev = index = 0; 23 | 24 | do { 25 | printf("Value checked array[%d] = [%d]\n", index, array[index]); 26 | 27 | if (array[index] == value) 28 | return (index); 29 | k++; 30 | prev = index; 31 | index = k * m; 32 | } while (index < (int)size && array[index] < value); 33 | 34 | printf("Value found between indexes [%d] and [%d]\n", prev, index); 35 | 36 | for (; prev <= index && prev < (int)size; prev++) 37 | { 38 | printf("Value checked array[%d] = [%d]\n", prev, array[prev]); 39 | if (array[prev] == value) 40 | return (prev); 41 | } 42 | 43 | return (-1); 44 | } 45 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in an array of 5 | * integers using the Interpolation search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: index of the number 11 | */ 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t pos, low, high; 15 | double f; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | low = 0; 21 | high = size - 1; 22 | 23 | while (size) 24 | { 25 | f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]); 26 | pos = (size_t)(low + f); 27 | printf("Value checked array[%d]", (int)pos); 28 | 29 | if (pos >= size) 30 | { 31 | printf(" is out of range\n"); 32 | break; 33 | } 34 | else 35 | { 36 | printf(" = [%d]\n", array[pos]); 37 | } 38 | 39 | if (array[pos] == value) 40 | return ((int)pos); 41 | 42 | if (array[pos] < value) 43 | low = pos + 1; 44 | else 45 | high = pos - 1; 46 | 47 | if (low == high) 48 | break; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @list: input list 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | listint_t *jump_list(listint_t *list, size_t size, int value) 14 | { 15 | size_t index, k, m; 16 | listint_t *prev; 17 | 18 | if (list == NULL || size == 0) 19 | return (NULL); 20 | 21 | m = (size_t)sqrt((double)size); 22 | index = 0; 23 | k = 0; 24 | 25 | do { 26 | prev = list; 27 | k++; 28 | index = k * m; 29 | 30 | while (list->next && list->index < index) 31 | list = list->next; 32 | 33 | if (list->next == NULL && index != list->index) 34 | index = list->index; 35 | 36 | printf("Value checked at index [%d] = [%d]\n", (int)index, list->n); 37 | 38 | } while (index < size && list->next && list->n < value); 39 | 40 | printf("Value found between indexes "); 41 | printf("[%d] and [%d]\n", (int)prev->index, (int)list->index); 42 | 43 | for (; prev && prev->index <= list->index; prev = prev->next) 44 | { 45 | printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n); 46 | if (prev->n == value) 47 | return (prev); 48 | } 49 | 50 | return (NULL); 51 | } 52 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - searches for a value in a skip list 5 | * 6 | * @list: input list 7 | * @value: value to search in 8 | * Return: index of the number 9 | */ 10 | skiplist_t *linear_skip(skiplist_t *list, int value) 11 | { 12 | skiplist_t *go; 13 | 14 | if (list == NULL) 15 | return (NULL); 16 | 17 | go = list; 18 | 19 | do { 20 | list = go; 21 | go = go->express; 22 | printf("Value checked at index "); 23 | printf("[%d] = [%d]\n", (int)go->index, go->n); 24 | } while (go->express && go->n < value); 25 | 26 | if (go->express == NULL) 27 | { 28 | list = go; 29 | while (go->next) 30 | go = go->next; 31 | } 32 | 33 | printf("Value found between indexes "); 34 | printf("[%d] and [%d]\n", (int)list->index, (int)go->index); 35 | 36 | while (list != go->next) 37 | { 38 | printf("Value checked at index [%d] = [%d]\n", (int)list->index, list->n); 39 | if (list->n == value) 40 | return (list); 41 | list = list->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | 0x1E-search_algorithms 2 | --------------------------------------------------------------------------------