├── 0x00-hello_world ├── .2-assembler.swp ├── .sbetty ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── .alx-low_level_programming.swp ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 1-last_digit.out ├── 100-print_comb3.c ├── 100-print_comb3.out ├── 101-print_comb4.c ├── 102-print_comb5 ├── 102-print_comb5.c ├── 102-print_comb5.out ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 3-print_alphabets.out ├── 4-print_alphabt.c ├── 4-print_alphabt.out ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 8-print_base16.out ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 101-natural.out ├── 102-fibonacci.c ├── 102-fibonacci.out ├── 103-fibonacci.c ├── 103-fibonacci.out ├── 104-fibonacci.c ├── 104-fibonacci.out ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 1-main.out ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 0-main.c ├── 1-isdigit ├── 1-isdigit.c ├── 1-main.c ├── 10-print_triangle.c ├── 100-prime_factor ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-main.c ├── 2-mul ├── 2-mul.c ├── 3-main.c ├── 3-print_numbers.c ├── 4-main.c ├── 4-print_most_numbers.c ├── 5-main.c ├── 5-more_numbers.c ├── 6-main.c ├── 6-print_line.c ├── 7-main.c ├── 7-print_diagonal.c ├── 8-main.c ├── 8-print_square.c ├── 9-fizz_buzz ├── 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 └── 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 ├── _putchar.c └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── liball.a ├── libmy.a ├── main.c ├── main.h ├── main.o └── quote ├── 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 ├── 0-print_name.c.swp ├── 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-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 101-hello_holberton.o ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md ├── hello └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.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-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md └── 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 └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_win.sh ├── README.md ├── libdynamic.so └── main.h ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md ├── hash_tables.h └── tests │ ├── 0-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ └── 6-main.c ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── m.h ├── main.c ├── main.o ├── school ├── school.c ├── school.o └── tests │ └── 5-main.py ├── 0x1E-search_algorithms ├── 0-linear ├── 0-linear.c ├── 0-main.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 ├── LICENSE └── README.md /0x00-hello_world/.2-assembler.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x00-hello_world/.2-assembler.swp -------------------------------------------------------------------------------- /0x00-hello_world/.sbetty: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x00-hello_world/.sbetty -------------------------------------------------------------------------------- /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE > c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE > $(echo $CFILE|cut -d"." -f1).o 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints to string 4 | * 5 | * Description: Prints "and that piece of art is useful.." without puts 6 | * 7 | * Return: 1 8 | */ 9 | int main(void) 10 | 11 | { 12 | char *s = "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n"; 13 | long l = 59; 14 | long fd = 1; 15 | long syscall = 1; 16 | long ret = 0; 17 | __asm__ ("syscall" 18 | : "=a" (ret) 19 | : "a" (syscall), 20 | "D" (fd), 21 | "S" (s), 22 | "d" (l)); 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -S > $(echo $CFILE|cut -d"." -f1).s 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Entry point 4 | * 5 | * Description: prints "\"Programming is like building a multilingual puzzle" 6 | * 7 | * Return: 0 8 | * 9 | */ 10 | int main(void) 11 | { 12 | puts("\"Programming is like building a multilingual puzzle"); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Print a string 4 | * Description: use printf 5 | * 6 | * Return: 0 7 | * 8 | */ 9 | 10 | int main(void) 11 | 12 | { 13 | 14 | printf("%s", "with proper grammar, but the outcome is a piece of art,\n"); 15 | 16 | return (0); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints sized of characters 4 | * 5 | *Return: Always 0 (success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("Size of a char: %zu byte(s)\n", sizeof(char)); 11 | 12 | printf("Size of an int: %zu byte(s)\n", sizeof(int)); 13 | 14 | printf("Size of a long int: %zu byte(s)\n", sizeof(long int)); 15 | 16 | printf("Size of a long long int: %zu byte(s)\n", sizeof(long long int)); 17 | 18 | printf("Size of a float: %zu byte(s)\n", sizeof(float)); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/.alx-low_level_programming.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x01-variables_if_else_while/.alx-low_level_programming.swp -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Description: prints if a number is positive or negative. 9 | * Return: 0 10 | */ 11 | 12 | int main(void) 13 | 14 | { 15 | int n; 16 | srand(time(0)); 17 | n = rand() - RAND_MAX / 2; 18 | if (n > 0) 19 | { 20 | printf("%d is positive\n", n); 21 | } 22 | else if (n == 0) 23 | { 24 | printf("%d is zero\n", n); 25 | } 26 | else if (n < 0) 27 | { 28 | printf("%d is negative\n", n); 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | /** 8 | *main - Entry point 9 | * 10 | * Return: Always 0 (Success) 11 | */ 12 | 13 | int main(void) 14 | 15 | { 16 | int n; 17 | srand(time(0)); 18 | n = rand() - RAND_MAX / 2; 19 | if ((n % 10) == 0) 20 | printf("Last digit of %d is %d and is 0\n", n, n % 10); 21 | else if ((n % 10) > 5) 22 | printf("Last digit of %d is %d and is greater than 5\n", n, n % 10); 23 | else 24 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, n % 10); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x01-variables_if_else_while/1-last_digit.out -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - main block 6 | * Description: Write a program that prints all possible 7 | * 8 | * different combinations of two digits 9 | * 10 | * Numbers must be separated by ,, followed by a space 11 | * 12 | * The two digits must be different 13 | * 14 | * 01 and 10 are considered the same combination of the two digits 0 and 1 15 | * 16 | * Print only the smallest combination of two digits 17 | * 18 | * Numbers should be printed in ascending order, with two digits 19 | * 20 | * You can only use the putchar function 21 | * 22 | * (every other function (printf, puts, etc…) is forbidden) 23 | * 24 | * You can only use putchar five times maximum in your code 25 | * 26 | * You are not allowed to use any variable of type char 27 | * 28 | * All your code should be in the main function 29 | * 30 | * Return: 0 31 | */ 32 | 33 | int main(void) 34 | 35 | { 36 | int c; 37 | int d = 0; 38 | while (d < 10) 39 | { 40 | c = 0; 41 | while (c < 10) 42 | { 43 | if (d != c && d < c) 44 | { 45 | putchar('0' + d); 46 | putchar('0' + c); 47 | if (c + d != 17) 48 | { 49 | putchar(','); 50 | putchar(' '); 51 | } 52 | } 53 | c++; 54 | } 55 | d++; 56 | } 57 | putchar('\n'); 58 | return (0); 59 | } 60 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x01-variables_if_else_while/100-print_comb3.out -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints 3 combination of numbers 4 | * Return: Always (Success) 5 | */ 6 | int main(void) 7 | { 8 | int c, i, k; 9 | for (c = '0'; c <= '9'; c++) 10 | { 11 | for (i = '0'; i <= '9'; i++) 12 | { 13 | for (k = '0'; k <= '9'; k++) 14 | { 15 | if (c < i && i < k) 16 | { 17 | putchar(c); 18 | putchar(i); 19 | putchar(k); 20 | if (c != '7') 21 | { 22 | putchar(','); 23 | putchar(' '); 24 | } 25 | } 26 | } 27 | } 28 | } 29 | putchar('\n'); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x01-variables_if_else_while/102-print_comb5 -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - create 2 pairs of number 5 | * 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | 11 | { 12 | int i, j; 13 | for (i = 0; i <= 99; i++) 14 | { 15 | for (j = i; j <= 99; j++) 16 | { 17 | if (j != i) 18 | { 19 | putchar(i / 10 + 48); 20 | putchar(i % 10 + 48); 21 | putchar(' '); 22 | putchar(j / 10 + 48); 23 | putchar(j % 10 + 48); 24 | if (i * 100 + j != 9899) 25 | { 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | } 31 | } 32 | putchar('\n'); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x01-variables_if_else_while/102-print_comb5.out -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: prints the alphabet in lowercase\n 7 | * 8 | * Return: Always 0 (success) 9 | */ 10 | int main(void) 11 | 12 | { 13 | int ch; 14 | for (ch = 'a'; ch <= 'z'; ch++) 15 | { 16 | putchar(ch); 17 | } 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - main block 5 | * 6 | * Description: prints the alphabet in lowercase, 7 | * 8 | * and then in uppercase, followed by a new line. 9 | * Return: 0 10 | */ 11 | 12 | int main(void) 13 | 14 | { 15 | char c = 'a'; 16 | while (c <= 'z') 17 | { 18 | putchar(c); 19 | c++; 20 | } 21 | c = 'A'; 22 | while (c <= 'Z') 23 | { 24 | putchar(c); 25 | c++; 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x01-variables_if_else_while/3-print_alphabets.out -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print the alphabet in lowercase letters, except for e and q 5 | * Return: Always 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | 10 | { 11 | char letter; 12 | for (letter = 'a'; letter <= 'z'; letter++) 13 | { 14 | if (letter != 'e' && letter != 'q') 15 | putchar(letter); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x01-variables_if_else_while/4-print_alphabt.out -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main -Entry point 5 | * 6 | * Description: prints allsingle digit numbers of 7 | * 8 | * base 10 from 0 9 | * 10 | * Return: Always 0 (success) 11 | */ 12 | int main(void) 13 | { 14 | int a; 15 | for (a = 0; a < 10; a++) 16 | { 17 | printf("%d", a); 18 | } 19 | putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: prints char below 10 7 | * 8 | * Return: Always 0 (success) 9 | */ 10 | int main(void) 11 | { 12 | int a; 13 | for (a = 0; a < 10; a++) 14 | { 15 | putchar(a + '0'); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: prints lowercase alphabets in reverse 7 | * 8 | * Return: Always 0 (success) 9 | */ 10 | int main(void) 11 | { 12 | int a; 13 | for (a = 'z'; a >= 'a'; a--) 14 | { 15 | putchar(a); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print the numbers of base 16 in lowercase 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | 9 | int main(void) 10 | 11 | { 12 | char digit; 13 | for (digit = '0'; digit <= '9'; digit++) 14 | putchar(digit); 15 | for (digit = 'a'; digit <= 'f'; digit++) 16 | putchar(digit); 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x01-variables_if_else_while/8-print_base16.out -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: prints numbers 7 | * 8 | * Return: Always 0 (success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | for (i = '0'; i <= '9' ; i++) 14 | { 15 | putchar(i); 16 | if (i != '9') 17 | { 18 | putchar(','); 19 | putchar(' '); 20 | } 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - prints _putchar, fllowd by a new linees 5 | * Return: Alwys 0 (Success) 6 | */ 7 | 8 | int main(void) 9 | 10 | { 11 | char abe[] = "_putchar"; 12 | int c; 13 | for (c = 0; c < 8; c++) 14 | { 15 | _putchar(abe[c]); 16 | } 17 | _putchar('\n'); 18 | return (0); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - check the code for ALX School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | 9 | void print_alphabet(void) 10 | 11 | { 12 | char alp = 'a'; 13 | while (alp <= 'z') 14 | { 15 | _putchar (alp); 16 | alp++; 17 | } 18 | _putchar ('\n'); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - adds two integers and returns the result 5 | * 6 | * @a: int to be added to b 7 | * 8 | * @b: int to be added to a 9 | * Return: sum of a and b 10 | */ 11 | 12 | int add(int a, int b) 13 | 14 | { 15 | int c; 16 | c = a + b; 17 | return (c); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the n times table, starting with 0 5 | * 6 | * @n: number of the times table 7 | */ 8 | 9 | void print_times_table(int n) 10 | 11 | { 12 | int i, j, k; 13 | if (n >= 0 && n <= 15) 14 | { 15 | for (i = 0; i <= n; i++) 16 | { 17 | for (j = 0; j <= n; j++) 18 | { 19 | k = j * i; 20 | if (j == 0) 21 | { 22 | _putchar(k + '0'); 23 | } 24 | else if (k < 10 && j != 0) 25 | { 26 | _putchar(','); 27 | _putchar(' '); 28 | _putchar(' '); 29 | _putchar(' '); 30 | _putchar(k + '0'); 31 | } 32 | else if (k >= 10 && k < 100) 33 | { 34 | _putchar(','); 35 | _putchar(' '); 36 | _putchar(' '); 37 | _putchar((k / 10) + '0'); 38 | _putchar((k % 10) + '0'); 39 | } 40 | else if (k >= 100) 41 | { 42 | _putchar(','); 43 | _putchar(' '); 44 | _putchar((k / 100) + '0'); 45 | _putchar(((k / 10) % 10) + '0'); 46 | _putchar((k % 10) + '0'); 47 | } 48 | } 49 | _putchar('\n'); 50 | } 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the sum of all the multiples of 3 or 5 below 1024 5 | * 6 | * * @void: void 7 | * 8 | * * Return: 0 Success 9 | */ 10 | 11 | int main(void) 12 | 13 | { 14 | int n; 15 | int sum = 0; 16 | for (n = 0; n < 1024; n++) 17 | { 18 | if (n % 3 == 0 || n % 5 == 0) 19 | { 20 | sum += n; 21 | } 22 | } 23 | printf("%d\n", sum); 24 | return (0); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x02-functions_nested_loops/101-natural.out -------------------------------------------------------------------------------- /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 | * 6 | * followed by a new line 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | 13 | { 14 | long int i, j, k, next; 15 | j = 1; 16 | k = 2; 17 | for (i = 1; i <= 50; ++i) 18 | { 19 | if (j != 20365011074) 20 | { 21 | printf("%ld, ", j); 22 | } 23 | else 24 | { 25 | printf("%ld\n", j); 26 | } 27 | next = j + k; 28 | j = k; 29 | k = next; 30 | } 31 | return (0); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x02-functions_nested_loops/102-fibonacci.out -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the sum of the even-valued terms 5 | * 6 | * followed by a new line 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | 13 | { 14 | int i; 15 | unsigned long int j, k, next, sum; 16 | j = 1; 17 | k = 2; 18 | sum = 0; 19 | for (i = 1; i <= 33; ++i) 20 | { 21 | if (j < 4000000 && (j % 2) == 0) 22 | { 23 | sum = sum + j; 24 | } 25 | next = j + k; 26 | j = k; 27 | k = next; 28 | } 29 | printf("%lu\n", sum); 30 | return (0); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x02-functions_nested_loops/103-fibonacci.out -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the first 98 Fibonacci numbers, 5 | * 6 | * starting with 1 and 2 7 | * 8 | * followed by a new line 9 | * 10 | * Return: ALways 0 (Success) 11 | */ 12 | 13 | int main(void) 14 | 15 | { 16 | unsigned long int i, j, k, j1, j2, k1, k2; 17 | j = 1; 18 | k = 2; 19 | printf("%lu", j); 20 | for (i = 1; i < 91; i++) 21 | { 22 | printf(", %lu", k); 23 | k = k + j; 24 | j = k - j; 25 | } 26 | j1 = j / 1000000000; 27 | j2 = j % 1000000000; 28 | k1 = k / 1000000000; 29 | k2 = k % 1000000000; 30 | for (i = 92; i < 99; ++i) 31 | { 32 | printf(", %lu", k1 + (k2 / 1000000000)); 33 | printf("%lu", k2 % 1000000000); 34 | k1 = k1 + j1; 35 | j1 = k1 - j1; 36 | k2 = k2 + j2; 37 | j2 = k2 - j2; 38 | } 39 | printf("\n"); 40 | return (0); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x02-functions_nested_loops/104-fibonacci.out -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - prints all natural numbers from n to 98, 6 | * 7 | * followed by a new line 8 | * 9 | * @n: print from this number 10 | */ 11 | 12 | void print_to_98(int n) 13 | 14 | { 15 | int i, j; 16 | if (n <= 98) 17 | { 18 | for (i = n; i <= 98; i++) 19 | { 20 | if (i != 98) 21 | printf("%d, ", i); 22 | else if (i == 98) 23 | printf("%d\n", i); 24 | } 25 | } 26 | else if (n >= 98) 27 | { 28 | for (j = n; j >= 98; j--) 29 | { 30 | if (j != 98) 31 | printf("%d, ", j); 32 | else if (j == 98) 33 | printf("%d\n", j); 34 | } 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *print_alphabet_x10 - func 5 | * Return - 0 6 | */ 7 | 8 | void print_alphabet_x10(void) 9 | 10 | { 11 | int b = 0; 12 | while (b < 10) 13 | { 14 | char a = 'a'; 15 | while (a <= 'z') 16 | { 17 | _putchar(a); 18 | a++; 19 | } 20 | _putchar('\n'); 21 | b++; 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *_islower - checks for a lowercase character 5 | *@c: letter being tested 6 | * Return: Always 0 7 | */ 8 | 9 | int _islower(int c) 10 | 11 | { 12 | if (c >= 97 && c <= 122) 13 | return (1); 14 | else 15 | return (0); 16 | 17 | } 18 | 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - check the code for ALX School students. 5 | * @c: is c an integer argument 6 | * 7 | * Return: Always 0. 8 | */ 9 | 10 | int _isalpha(int c) 11 | 12 | { 13 | if (c >= 97 && c <= 122) 14 | { 15 | return (1); 16 | } 17 | else if (c >= 65 && c <= 90) 18 | { 19 | return (1); 20 | } 21 | return (0); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - prints the sign of a number 5 | *@n: number being tested 6 | * Return: Always 0. 7 | */ 8 | 9 | int print_sign(int n) 10 | 11 | { 12 | if (n > 0) 13 | { 14 | _putchar('+'); 15 | return (1); 16 | } 17 | else if (n == 0) 18 | { 19 | _putchar('0'); 20 | return (0); 21 | } 22 | else if (n < 0) 23 | { 24 | _putchar('-'); 25 | return (-1); 26 | } 27 | return (0); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - returns absolute value of an int 5 | * 6 | * @n: integer from which to get an absolute value 7 | * 8 | * Return: Void. 9 | */ 10 | 11 | int _abs(int n) 12 | 13 | { 14 | if (n < 0) 15 | { 16 | return (n * -1); 17 | } 18 | return (n); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - prints the last digit of a number 5 | * 6 | * @n: the int to extract the last digit from 7 | * 8 | * Return: value of the last digit 9 | */ 10 | 11 | int print_last_digit(int n) 12 | 13 | { 14 | int a; 15 | if (n < 0) 16 | n = -n; 17 | a = n % 10; 18 | if (a < 0) 19 | a = -a; 20 | _putchar(a + '0'); 21 | return (a); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day of Jack Bauer 5 | * 6 | * * starting from 00:00 to 23:59 7 | */ 8 | 9 | void jack_bauer(void) 10 | 11 | { 12 | int i, j; 13 | i = 0; 14 | while (i < 24) 15 | { 16 | j = 0; 17 | while (j < 60) 18 | { 19 | _putchar((i / 10) + '0'); 20 | _putchar((i % 10) + '0'); 21 | _putchar(':'); 22 | _putchar((j / 10) + '0'); 23 | _putchar((j % 10) + '0'); 24 | _putchar('\n'); 25 | j++; 26 | } 27 | i++; 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - prints the 9 times table, starting with 0 5 | */ 6 | 7 | void times_table(void) 8 | 9 | { 10 | int i, j, k; 11 | for (i = 0; i < 10; i++) 12 | { 13 | for (j = 0; j < 10; j++) 14 | { 15 | k = j * i; 16 | if (j == 0) 17 | { 18 | _putchar(k + '0'); 19 | } 20 | if (k < 10 && j != 0) 21 | { 22 | _putchar(','); 23 | _putchar(' '); 24 | _putchar(' '); 25 | _putchar(k + '0'); 26 | } 27 | else if (k >= 10) 28 | { 29 | _putchar(','); 30 | _putchar(' '); 31 | _putchar((k / 10) + '0'); 32 | _putchar((k % 10) + '0'); 33 | } 34 | } 35 | _putchar('\n'); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | ### 0x02-functions_nested_loops 2 | 3 |
    4 |
  1. A program that prints _putchar, followed by a new line.
  2. 5 |
  3. A function that prints the alphabet, in lowercase, followed by a new line
  4. 6 |
  5. A function that prints 10 times the alphabet, in lowercase, followed by a new line.
  6. 7 |
  7. A function that checks for lowercase character.
  8. 8 |
  9. A function that checks for alphabetic character.
  10. 9 |
  11. A function that prints the sign of a number.
  12. 10 |
  13. A function that computes the absolute value of an integer.
  14. 11 |
  15. A function that prints the last digit of a number.
  16. 12 |
  17. A function that prints every minute of the day of Jack Bauer, starting from 00:00 to 23:59.
  18. 13 |
  19. A function that prints the 9 times table, starting with 0.
  20. 14 |
  21. A function that adds two integers and returns the result.
  22. 15 |
  23. A function that prints all natural numbers from n to 98, followed by a new line.
  24. 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /* 5 | * File: main.h 6 | * 7 | * Desc: Header file containing declartions for all function 8 | */ 9 | 10 | 11 | 12 | int _putchar(char c); 13 | 14 | void print_alphabet(void); 15 | 16 | void print_alphabet_x10(void); 17 | 18 | int _islower(int c); 19 | 20 | int _isalpha(int c); 21 | 22 | int print_sign(int n); 23 | 24 | int _abs(int); 25 | 26 | int print_last_digit(int); 27 | 28 | void jack_bauer(void); 29 | 30 | void times_table(void); 31 | 32 | int add(int, int); 33 | 34 | void print_to_98(int n); 35 | 36 | void print_times_table(int n); 37 | 38 | 39 | 40 | #endif 41 | 42 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Test function for positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /* 17 | * while (i < 10) 18 | * { 19 | * putchar(i); 20 | * } 21 | */ 22 | printf("Infinite loop avoided! \\o/\n"); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x03-debugging/1-main.out -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a > b && a > c) 16 | { 17 | largest = a; 18 | } 19 | else if (a > b && c > a) 20 | { 21 | largest = c; 22 | } 23 | else if (b > c) 24 | { 25 | largest = b; 26 | } 27 | else 28 | { 29 | largest = c; 30 | } 31 | 32 | return (largest); 33 | } 34 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if ((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0)) 16 | { 17 | if (month > 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | printf("Day of the year: %d\n", day); 22 | printf("Remaining days: %d\n", 366 - day); 23 | } 24 | else 25 | { 26 | if (month == 2 && day == 60) 27 | { 28 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 29 | } 30 | else 31 | { 32 | printf("Day of the year: %d\n", day); 33 | printf("Remaining days: %d\n", 365 - day); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x03-debugging/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 | #include 3 | 4 | /** 5 | * _isupper - Check if paramter c represents an uppercase letter. 6 | * @c: Int representing a character 7 | * Return: Always 0. 8 | */ 9 | int _isupper(int c) 10 | { 11 | if (c >= 65 && c <= 90) 12 | { 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code. 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = 'A'; 14 | printf("%c: %d\n", c, _isupper(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isupper(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x04-more_functions_nested_loops/1-isdigit -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isdigit - Check for a digit (0-9) 4 | * @c: Integer holding the character 5 | * 6 | * Return: 1- Digit 0- Not 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= 48 && c <= 57) 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | c = '0'; 14 | printf("%c: %d\n", c, _isdigit(c)); 15 | c = 'a'; 16 | printf("%c: %d\n", c, _isdigit(c)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_triangle - Print triangle dependent on the parameter size. 6 | * @size : The height of the triangle 7 | * Return: Void. 8 | */ 9 | void print_triangle(int size) 10 | { 11 | int i; 12 | int x; 13 | int spaces; 14 | ; 15 | for (i = 0; i < size; i++) 16 | { 17 | for (spaces = size - 1 - i; spaces > 0; spaces--) 18 | { 19 | _putchar(' '); 20 | } 21 | for (x = 0; x <= i; x++) 22 | { 23 | _putchar('#'); 24 | } 25 | _putchar('\n'); 26 | } 27 | if (size <= 0) 28 | { 29 | _putchar('\n'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x04-more_functions_nested_loops/100-prime_factor -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - calculate largest prime of 612852475143 5 | * 6 | * Return: Success Always 7 | */ 8 | 9 | 10 | int main(void) 11 | { 12 | long int x = 612852475143; 13 | long int py; 14 | 15 | for (py = 2; py < x; py++) 16 | { 17 | if (x % py == 0) 18 | { 19 | x = x / py; 20 | } 21 | } 22 | printf("%ld\n", py); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | int _putchar(char c); 6 | 7 | /** 8 | * print_number - like a hello world 9 | * 10 | * @n: params an integer 11 | * 12 | * No return 13 | */ 14 | 15 | 16 | void print_number(int n) 17 | { 18 | 19 | unsigned int n1 = 0; 20 | 21 | if (n < 0) 22 | { 23 | n1 = -n; 24 | _putchar('-'); 25 | } 26 | 27 | else 28 | { 29 | n1 = n; 30 | } 31 | 32 | if (n1 / 10) 33 | { 34 | print_number(n1 / 10); 35 | } 36 | 37 | _putchar((n1 % 10) + '0'); 38 | } 39 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | printf("%d\n", mul(98, 1024)); 12 | printf("%d\n", mul(-402, 4096)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x04-more_functions_nested_loops/2-mul -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * mul - Multiply 2 integers. 6 | * @a: An integer to be multiplied with b 7 | * @b : An integer to be multipled with a 8 | * Return: Return the result of the multiplication 9 | */ 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_numbers - Print numbers between 0 to 9 incl. 6 | * 7 | * Return: Void. 8 | */ 9 | void print_numbers(void) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < 10; i++) 14 | { 15 | _putchar(i + '0'); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_most_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_most_numbers - Print numbers between 0 to 9 incl. 6 | * 7 | * Return: Void. 8 | */ 9 | void print_most_numbers(void) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < 10; i++) 14 | { 15 | if (i != 2 && i != 4) 16 | { 17 | _putchar(i + '0'); 18 | } 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | more_numbers(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * more_numbers - Print numbers between 0 to 14 incl. 6 | * 7 | * Return: Void. 8 | */ 9 | void more_numbers(void) 10 | { 11 | int i; 12 | int x; 13 | 14 | for (x = 0; x < 10; x++) 15 | { 16 | i = 0; 17 | while (i <= 14) 18 | { 19 | if (i >= 10) 20 | { 21 | _putchar(i / 10 + '0'); 22 | } 23 | _putchar(i % 10 + '0'); 24 | i++; 25 | } 26 | _putchar('\n'); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_line(0); 11 | print_line(2); 12 | print_line(10); 13 | print_line(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_line - Print line dependent on the integer n. 6 | * @n : The number of '_' characters to use 7 | * Return: Void. 8 | */ 9 | void print_line(int n) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < n; i++) 14 | { 15 | _putchar('_'); 16 | } 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_diagonal(0); 11 | print_diagonal(2); 12 | print_diagonal(10); 13 | print_diagonal(-4); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagonal - Print diagonal line dependent on the integer n. 6 | * @n : The number of lines using '\' characters to use 7 | * Return: Void. 8 | */ 9 | void print_diagonal(int n) 10 | { 11 | int i; 12 | int spaces; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | for (spaces = 0; spaces < i; spaces++) 17 | { 18 | _putchar(' '); 19 | } 20 | _putchar('\\'); 21 | _putchar('\n'); 22 | } 23 | 24 | if (n <= 0) 25 | { 26 | _putchar('\n'); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_square(2); 11 | print_square(10); 12 | print_square(0); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_square - Print square line dependent on the integer n. 6 | * @n : The number of lines using '#' characters to use per row and column 7 | * Return: Void. 8 | */ 9 | void print_square(int n) 10 | { 11 | int x; 12 | int y; 13 | 14 | for (y = 0; y < n; y++) 15 | { 16 | for (x = 0; x < n; x++) 17 | { 18 | _putchar('#'); 19 | } 20 | _putchar('\n'); 21 | } 22 | if (n <= 0) 23 | { 24 | _putchar('\n'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x04-more_functions_nested_loops/9-fizz_buzz -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - Fizz for multiples of 3 and Buzz for multiples of 5 FizzBuzz for both 6 | * 7 | * 8 | * Return: Always 0 9 | */ 10 | int main(void) 11 | { 12 | int x = 1; 13 | 14 | while (x < 101) 15 | { 16 | if (x % 3 == 0 && x % 5 == 0) 17 | { 18 | printf("%s", "FizzBuzz"); 19 | } 20 | else if (x % 3 == 0) 21 | { 22 | printf("%s", "Fizz"); 23 | } 24 | else if (x % 5 == 0) 25 | { 26 | printf("%s", "Buzz"); 27 | } 28 | else 29 | { 30 | printf("%d", x); 31 | } 32 | 33 | if (x != 100) 34 | { 35 | printf(" "); 36 | } 37 | x++; 38 | } 39 | printf("\n"); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN 2 | #define MAIN 3 | extern int _putchar(char c); 4 | extern int _isupper(int c); 5 | extern int _isdigit(int c); 6 | extern int mul(int a, int b); 7 | extern void print_numbers(void); 8 | extern void print_most_numbers(void); 9 | extern void more_numbers(void); 10 | extern void print_line(int n); 11 | extern void print_diagonal(int n); 12 | extern void print_square(int size); 13 | extern void print_triangle(int size); 14 | extern void print_number(int n); 15 | #endif 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value *n points to. 5 | * @n: the number to modify. 6 | */ 7 | 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swap the value of two integers. 5 | * 6 | * @a: first integer. 7 | * @b: second integer. 8 | * 9 | */ 10 | 11 | void swap_int(int *a, int *b) 12 | { 13 | int c; 14 | 15 | c = 0; 16 | c = *a; 17 | *a = *b; 18 | *b = c; 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string into an integer. 5 | * 6 | * @s: the string to use. 7 | * 8 | * Return: integer. 9 | */ 10 | 11 | int _atoi(char *s) 12 | { 13 | int sign = 1, i = 0; 14 | unsigned int res = 0; 15 | 16 | while (!(s[i] <= '9' && s[i] >= '0') && s[i] != '\0') 17 | { 18 | if (s[i] == '-') 19 | sign *= -1; 20 | i++; 21 | } 22 | while (s[i] <= '9' && (s[i] >= '0' && s[i] != '\0')) 23 | { 24 | res = (res * 10) + (s[i] - '0'); 25 | i++; 26 | } 27 | res *= sign; 28 | return (res); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - print password. 8 | * 9 | * Return: 0. 10 | */ 11 | 12 | int main(void) 13 | { 14 | int ascii = 2772, i = 0, j, random; 15 | char password[100]; 16 | time_t t; 17 | 18 | srand((int) time(&t)); 19 | while (ascii > 126) 20 | { 21 | random = rand() % 126; 22 | password[i] = random; 23 | ascii -= random; 24 | i++; 25 | } 26 | if (ascii > 0) 27 | password[i] = ascii; 28 | else 29 | { 30 | i--; 31 | } 32 | 33 | 34 | for (j = 0; j <= i; j++) 35 | { 36 | printf("%c", password[j]); 37 | } 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - calculate the length of a string. 5 | * 6 | * @s: the string to calculate it's length. 7 | * Return: length of a string. 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int i; 13 | int length = 0; 14 | 15 | for (i = 0; s[i] != '\0'; i++) 16 | { 17 | length++; 18 | } 19 | return (length); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string followed by a new line.. 5 | * 6 | *@str: the string to print. 7 | * 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i; 13 | 14 | for (i = 0; str[i] != '\0'; i++) 15 | { 16 | _putchar(str[i]); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - print a string in reverse. 5 | * 6 | *@s: the string to print in reverse. 7 | * 8 | */ 9 | 10 | void print_rev(char *s) 11 | { 12 | int i = 0; 13 | int length; 14 | 15 | for (length = 0; s[length] != '\0'; length++) 16 | { 17 | } 18 | 19 | for (i = length - 1; i >= 0; i--) 20 | { 21 | _putchar(s[i]); 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverse a string. 5 | * 6 | *@s: the string to be reversed. 7 | */ 8 | 9 | void rev_string(char *s) 10 | { 11 | int length, j, i; 12 | char v1, v2; 13 | 14 | for (length = 0; s[length] != '\0'; length++) 15 | { 16 | } 17 | 18 | j = length - 1; 19 | i = 0; 20 | 21 | while (j > i) 22 | { 23 | v1 = s[i]; 24 | v2 = s[j]; 25 | s[i] = v2; 26 | s[j] = v1; 27 | j--; 28 | i++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints every other character of a string. 5 | *@str: the string to use. 6 | */ 7 | 8 | void puts2(char *str) 9 | { 10 | int i = 0; 11 | 12 | while (str[i] != '\0') 13 | { 14 | if (i % 2 == 0) 15 | { 16 | _putchar(str[i]); 17 | } 18 | i++; 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints the second half of a string. 5 | *@str: string to use. 6 | */ 7 | 8 | void puts_half(char *str) 9 | { 10 | int length, n, i; 11 | 12 | for (length = 0; str[length] != '\0'; length++) 13 | { 14 | } 15 | n = (length - 1) / 2; 16 | for (i = n + 1; str[i] != '\0'; i++) 17 | { 18 | _putchar(str[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - print an array. 6 | * @a: the array to print. 7 | * @n: array's length 8 | */ 9 | 10 | void print_array(int *a, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | printf("%d", a[i]); 17 | if (i < n - 1) 18 | { 19 | printf(", "); 20 | } 21 | } 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src, including \0. 5 | *@src: pointer. 6 | *@dest: pointer. 7 | *Return: the pointer to dest. 8 | */ 9 | 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i, length; 13 | 14 | for (length = 0; src[length] != '\0'; length++) 15 | { 16 | } 17 | 18 | for (i = 0; i <= length ; i++) 19 | { 20 | dest[i] = src[i]; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | ### 0x05. C - Pointers, arrays and strings 2 | 3 | ### A pointer is a variable that stores the memory address of another variable as its value. A pointer variable points to a data type (like int) of the same type, and is `created with the * operator`. 4 | 5 | - File: `main.h` is the header file containing all the function prototypes used in this project. 6 | 7 | - File: `0-reset_to_98.c` is a function that takes a pointer to an int as parameter and updates the value it points to to 98. 8 | 9 | - File: `1-swap.c` is a function that swaps the values of two integers. 10 | 11 | - File: `2-strlen.c` is a function that returns the length of a string. 12 | 13 | - File: `3-puts.c` is a function that prints a string, followed by a new line, to stdout. 14 | 15 | - File: `4-print_rev.c` is a function that prints a string, in reverse, followed by a new line. 16 | 17 | - File: `5-rev_string.c` is a function that reverses a string. 18 | 19 | - File: `6-puts2.c` is a function that prints one char out of 2 of a string, followed by a new line. 20 | 21 | - File: `7-puts_half.c` is a function that prints half of a string, followed by a new line. 22 | 23 | - File: `8-print_array.c` is a function that prints n elements of an array of integers, followed by a new line. 24 | 25 | - File: `9-strcpy.c` is a function that copies the string pointed to by src, including the terminating null byte (\0), to the buffer pointed to by dest. 26 | 27 | - File: `100-atoi.c` is a function that convert a string to an integer. 28 | 29 | - File: `101-keygen.c` is a program that generates random valid passwords for the program 101-crackme. 30 | 31 | - File: `_putchar.c` contains the _putchar() function definition. 32 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main 2 | 3 | int _putchar(char c); 4 | void reset_to_98(int *n); 5 | void swap_int(int *a, int *b); 6 | int _strlen(char *s); 7 | void _puts(char *str); 8 | void print_rev(char *s); 9 | void rev_string(char *s); 10 | void puts2(char *str); 11 | void puts_half(char *str); 12 | void print_array(int *a, int n); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | 16 | #define main 17 | #endif 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - function that concatenates two strings. 5 | * @src: cadena a copiar 6 | * @dest: destino de la cadena. 7 | * Return: dest. 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | while (dest[i] != '\0') 17 | { 18 | i++; 19 | } 20 | j = 0; 21 | while (src[j] != '\0') 22 | { 23 | dest[i] = src[j]; 24 | i++; 25 | j++; 26 | } 27 | dest[i] = '\0'; 28 | return (dest); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strncat - function that concatenates two strings. 6 | * @src: cadena a copiar 7 | * @dest: destino de la cadena. 8 | * @n: number. 9 | * Return: dest. 10 | */ 11 | 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int index = strlen(dest); 15 | int a = 0; 16 | 17 | while (a < n && *src) 18 | { 19 | dest[index + a] = *src; 20 | src++; 21 | a++; 22 | } 23 | dest[index + a] = '\0'; 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - Encodes a string using rot13. 5 | * @str: The string to be encoded. 6 | * 7 | * Return: A pointer to the encoded string. 8 | */ 9 | char *rot13(char *str) 10 | { 11 | int indx1 = 0, indx2; 12 | char alphabet[52] = {'A', 'B', 'C', 'D', 'E', 'F', 13 | 'G', 'H', 'I', 'J', 'K', 'L', 14 | 'M', 'N', 'O', 'P', 'Q', 'R', 15 | 'S', 'T', 'U', 'V', 'W', 'X', 16 | 'Y', 'Z', 'a', 'b', 'c', 'd', 17 | 'e', 'f', 'g', 'h', 'i', 'j', 18 | 'k', 'l', 'm', 'n', 'o', 'p', 19 | 'q', 'r', 's', 't', 'u', 'v', 20 | 'w', 'x', 'y', 'z'}; 21 | char rot13key[52] = {'N', 'O', 'P', 'Q', 'R', 'S', 22 | 'T', 'U', 'V', 'W', 'X', 'Y', 23 | 'Z', 'A', 'B', 'C', 'D', 'E', 24 | 'F', 'G', 'H', 'I', 'J', 'K', 25 | 'L', 'M', 'n', 'o', 'p', 'q', 26 | 'r', 's', 't', 'u', 'v', 'w', 27 | 'x', 'y', 'z', 'a', 'b', 'c', 28 | 'd', 'e', 'f', 'g', 'h', 'i', 29 | 'j', 'k', 'l', 'm'}; 30 | 31 | while (str[indx1]) 32 | { 33 | for (indx2 = 0; indx2 < 52; indx2++) 34 | { 35 | if (str[indx1] == alphabet[indx2]) 36 | { 37 | str[indx1] = rot13key[indx2]; 38 | break; 39 | } 40 | } 41 | 42 | indx1++; 43 | 44 | } 45 | return (str); 46 | } 47 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_number(98); 11 | _putchar('\n'); 12 | print_number(402); 13 | _putchar('\n'); 14 | print_number(1024); 15 | _putchar('\n'); 16 | print_number(0); 17 | _putchar('\n'); 18 | print_number(-98); 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer; 5 | * @n: integer to be printed; 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int n1; 10 | 11 | if (n < 0) 12 | { 13 | n1 = -n; 14 | _putchar('-'); 15 | } else 16 | { 17 | n1 = n; 18 | } 19 | 20 | if (n1 / 10) 21 | print_number(n1 / 10); 22 | 23 | _putchar((n1 % 10) + '0'); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverse array 5 | * @n: integer params 6 | * Return: 0 7 | */ 8 | 9 | void rev_string(char *n) 10 | { 11 | int i = 0; 12 | int j = 0; 13 | char temp; 14 | 15 | while (*(n + i) != '\0') 16 | { 17 | i++; 18 | } 19 | i--; 20 | 21 | for (j = 0; j < i; j++, i--) 22 | { 23 | temp = *(n + j); 24 | *(n + j) = *(n + i); 25 | *(n + i) = temp; 26 | } 27 | } 28 | 29 | /** 30 | * infinite_add - add 2 numbers together 31 | * @n1: text representation of 1st number to add 32 | * @n2: text representation of 2nd number to add 33 | * @r: pointer to buffer 34 | * @size_r: buffer size 35 | * Return: pointer to calling function 36 | */ 37 | 38 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 39 | { 40 | int overflow = 0, i = 0, j = 0, digits = 0; 41 | int val1 = 0, val2 = 0, temp_tot = 0; 42 | 43 | while (*(n1 + i) != '\0') 44 | i++; 45 | while (*(n2 + j) != '\0') 46 | j++; 47 | i--; 48 | j--; 49 | if (j >= size_r || i >= size_r) 50 | return (0); 51 | while (j >= 0 || i >= 0 || overflow == 1) 52 | { 53 | if (i < 0) 54 | val1 = 0; 55 | else 56 | val1 = *(n1 + i) - '0'; 57 | if (j < 0) 58 | val2 = 0; 59 | else 60 | val2 = *(n2 + j) - '0'; 61 | temp_tot = val1 + val2 + overflow; 62 | if (temp_tot >= 10) 63 | overflow = 1; 64 | else 65 | overflow = 0; 66 | if (digits >= (size_r - 1)) 67 | return (0); 68 | *(r + digits) = (temp_tot % 10) + '0'; 69 | digits++; 70 | j--; 71 | i--; 72 | } 73 | if (digits == size_r) 74 | return (0); 75 | *(r + digits) = '\0'; 76 | rev_string(r); 77 | return (r); 78 | } 79 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - Prints a buffer 10 bytes at a time, starting with 6 | * the byte position, then showing the hex content, 7 | * then displaying printable charcaters. 8 | * @b: The buffer to be printed. 9 | * @size: The number of bytes to be printed from the buffer. 10 | */ 11 | void print_buffer(char *b, int size) 12 | { 13 | int byte, index; 14 | 15 | for (byte = 0; byte < size; byte += 10) 16 | { 17 | printf("%08x: ", byte); 18 | 19 | for (index = 0; index < 10; index++) 20 | { 21 | if ((index + byte) >= size) 22 | printf(" "); 23 | 24 | else 25 | printf("%02x", *(b + index + byte)); 26 | 27 | if ((index % 2) != 0 && index != 0) 28 | printf(" "); 29 | } 30 | 31 | for (index = 0; index < 10; index++) 32 | { 33 | if ((index + byte) >= size) 34 | break; 35 | 36 | else if (*(b + index + byte) >= 31 && 37 | *(b + index + byte) <= 126) 38 | printf("%c", *(b + index + byte)); 39 | 40 | else 41 | printf("."); 42 | } 43 | 44 | if (byte >= size) 45 | continue; 46 | 47 | printf("\n"); 48 | } 49 | 50 | if (size <= 0) 51 | printf("\n"); 52 | } 53 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - function that copies a string.. 5 | * @src: cadena a copiar 6 | * @dest: destino de la cadena. 7 | * @n: number. 8 | * Return: void 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | i = 0; 16 | 17 | while (src[i] != '\0' && i < n) 18 | { 19 | dest[i] = src[i]; 20 | i++; 21 | } 22 | 23 | while (i < n) 24 | { 25 | dest[i] = '\0'; 26 | i++; 27 | } 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - function that compares two strings. 5 | * @s1: primera cadena. 6 | * @s2: segunda cadena. 7 | * Return: 0. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int res = 0; 13 | 14 | while (*s1 == *s2 && *s1 != '\0') 15 | { 16 | s1++; 17 | s2++; 18 | } 19 | if (s1 != s2) 20 | res = *s1 - *s2; 21 | 22 | return (res); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - function that reverses the content of an array of integers. 5 | * @a: puntero del array. 6 | * @n: numero de elementos del array. 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int i; 12 | int b; 13 | 14 | i = 0; 15 | while (i < n) 16 | { 17 | n--; 18 | b = a[i]; 19 | a[i] = a[n]; 20 | a[n] = b; 21 | i++; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - function that changes all lowercase letter 5 | * of a string to uppercase. 6 | *@n: cadena de caracteres. 7 | * Return: n. 8 | */ 9 | 10 | char *string_toupper(char *n) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | while (n[i] != '\0') 16 | { 17 | if (n[i] >= 'a' && n[i] <= 'z') 18 | n[i] = n[i] - 32; 19 | i++; 20 | } 21 | return (n); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cap_string - capitalizes all words of a string. 3 | * @s: string. 4 | * Return: string. 5 | */ 6 | char *cap_string(char *s) 7 | { 8 | int i; 9 | 10 | i = 0; 11 | while (s[i] != '\0') 12 | { 13 | if ((s[i - 1] == ' ' || s[i - 1] == '\n' 14 | || s[i - 1] == '\t' || s[i - 1] == ',' 15 | || s[i - 1] == ';' || s[i - 1] == '!' 16 | || s[i - 1] == '?' || s[i - 1] == '"' 17 | || s[i - 1] == '(' || s[i - 1] == ')' 18 | || s[i - 1] == '{' || s[i - 1] == '}' 19 | || s[i - 1] == '.') 20 | && (s[i] >= 'a' && s[i] <= 'z')) 21 | { 22 | s[i] = s[i] - 32; 23 | } 24 | else if ((s[0] >= 97 && s[0] <= 122)) 25 | { 26 | s[0] = s[0] - 32; 27 | } 28 | else 29 | { 30 | s[i] = s[i]; 31 | } 32 | i++; 33 | } 34 | return (s); 35 | } 36 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encodes a string into 1337. 4 | * @s: string to code 5 | * Return: the enconde string. 6 | */ 7 | char *leet(char *s) 8 | { 9 | int i; 10 | int j; 11 | int k; 12 | 13 | char l[] = "oOlLeEaAtT"; 14 | char e[] = "0011334477"; 15 | 16 | i = 0; 17 | while (s[i] != '\0') 18 | { 19 | j = 0; 20 | k = 0; 21 | while (l[j] != '\0') 22 | { 23 | if (s[i] == l[j]) 24 | { 25 | k = j; 26 | s[i] = e[k]; 27 | } 28 | j++; 29 | } 30 | i++; 31 | } 32 | return (s); 33 | } 34 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # 0x06. C - More pointers, arrays and strings Project 2 | 3 | File 0-strcat.c is a function that concatenates two strings. 4 | 5 | File 1-strncat.c is a function that concatenates two strings that will use at most n bytes from src. 6 | 7 | File 2-strncpy.c is a function that copies a string. 8 | 9 | File 3-strcmp.c is a function that compares two strings. 10 | 11 | File 4-rev_array.c a function that reverses the content of an array of integers. 12 | 13 | File 5-string_toupper.c is a function that changes all lowercase letters of a string to uppercase. 14 | 15 | File 6-cap_string.c is a function that capitalizes all words of a string. 16 | 17 | File 7-leet.c is a function that encodes a string into 1337. 18 | 19 | File 100-rot13.c is a function that encodes a string using rot13. 20 | 21 | File _putchar.c is a function that replaces the standard library function putchar(). 22 | 23 | File main.h is the header file containing all the function prototypes. 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _main_h_ 2 | #define _main_h_ 3 | 4 | int _putchar(char c); 5 | char *_strcat(char *dest, char *src); 6 | char *_strncat(char *dest, char *src, int n); 7 | char *_strncpy(char *dest, char *src, int n); 8 | int _strcmp(char *s1, char *s2); 9 | void reverse_array(int *a, int n); 10 | char *string_toupper(char *); 11 | char *cap_string(char *); 12 | char *leet(char *); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: source adress. 6 | * @to: target adress. 7 | * Return: no return. 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chessboard 5 | * @a: input pointer. 6 | * Return: no return. 7 | */ 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | unsigned int i, m = 0; 11 | 12 | for (i = 0; i < 64; i++) 13 | { 14 | if (i % 8 == 0 && i != 0) 15 | { 16 | m = i; 17 | _putchar('\n'); 18 | } 19 | _putchar(a[i / 8][i - m]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_diagsums - prints the sum of the two 5 | * diagonals of a square matrix of integers 6 | * @a: input pointer. 7 | * @size: size of the matrix 8 | * Return: no return. 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i, sum1 = 0, sum2 = 0; 13 | 14 | for (i = 0; i < (size * size); i++) 15 | { 16 | if (i % (size + 1) == 0) 17 | sum1 += *(a + i); 18 | if (i % (size - 1) == 0 && i != 0 && i < size * size - 1) 19 | sum2 += *(a + i); 20 | } 21 | printf("%d, %d\n", sum1, sum2); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # 0x07. C - Even more pointers, arrays and strings C Project 2 | ### File: `0-memset.c` is a function that fills memory with a constant byte. 3 | 4 | ### File: `1-memcpy.c` is a function that copies memory area. 5 | 6 | ### File: `2-strchr.c` is a function that locates a character in a string. 7 | 8 | ### File: `3-strspn.c` is a function that gets the length of a prefix substring. 9 | 10 | ### File: `4-strpbrk.c` is a function that searches a string for any of a set of bytes. 11 | 12 | ### File: `5-strstr.c` is a function that locates a substring. 13 | 14 | ### File: `7-print_chessboard.c` is a function that prints the chessboard. 15 | 16 | ### File: `8-print_diagsums.c` is a function that prints the sum of the two diagonals of a square matrix of integers. 17 | 18 | ### File `100-set_string.c` is a function that sets the value of a pointer to a char. 19 | 20 | ### File `main.h` is the header file with the functions prototypes. 21 | 22 | ### File `_putchar.c` is the file that contains the _putchar function. 23 | 24 | ## File: 101-crackme_password contains the password for the [crackme2](https://github.com/holbertonschool/0x06.c) executable. 25 | - You may need to install the `openssl` library to run the crakme2 program: `sudo apt install libssl-dev` 26 | - Edit the source list `sudo vim /etc/apt/sources.list` to add the following line: `deb http://security.ubuntu.com/ubuntu xenial-security main` Then `sudo apt update` and `sudo apt install libssl1.0.0` 27 | 28 | OR 29 | 30 | ## The Advanced task no. 9 31 | 1. open the file using `vi -b 101-cranckme_password` 32 | 2. when you see the vi window, don't use the insert mode, just type `:set binary` and hit enter 33 | 3. type `:set noeol` and hit enter 34 | 4. use the insert mode(i) and type abc123 35 | 5. esc `:wq` hit enter 36 | 37 | ==> Then git add . 38 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _main_h_ 2 | #define _main_h_ 3 | 4 | char *_memset(char *s, char b, unsigned int n); 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn(char *s, char *accept); 8 | char *_strpbrk(char *s, char *accept); 9 | char *_strstr(char *haystack, char *needle); 10 | int _putchar(char); 11 | void print_chessboard(char (*a)[8]); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *to); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - prints a string, followed by a new line 5 | * @s: string to print 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s == '\0') 10 | { 11 | _putchar('\n'); 12 | return; 13 | } 14 | _putchar(*s); 15 | _puts_recursion(s + 1); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints a string in reverse 5 | * @s: string to print 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s) 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 | int check_pal(char *s, int i, int len); 4 | int _strlen_recursion(char *s); 5 | 6 | /** 7 | * is_palindrome - checks if a string is a palindrome 8 | * @s: string to reverse 9 | * 10 | * Return: 1 if it is, 0 it's not 11 | */ 12 | int is_palindrome(char *s) 13 | { 14 | if (*s == 0) 15 | return (1); 16 | return (check_pal(s, 0, _strlen_recursion(s))); 17 | } 18 | 19 | /** 20 | * _strlen_recursion - returns the length of a string 21 | * @s: string to calculate the length of 22 | * 23 | * Return: length of the string 24 | */ 25 | int _strlen_recursion(char *s) 26 | { 27 | if (*s == '\0') 28 | return (0); 29 | return (1 + _strlen_recursion(s + 1)); 30 | } 31 | 32 | /** 33 | * check_pal - checks the characters recursively for palindrome 34 | * @s: string to check 35 | * @i: iterator 36 | * @len: length of the string 37 | * 38 | * Return: 1 if palindrome, 0 if not 39 | */ 40 | int check_pal(char *s, int i, int len) 41 | { 42 | if (*(s + i) != *(s + len - 1)) 43 | return (0); 44 | if (i >= len) 45 | return (1); 46 | return (check_pal(s, i + 1, len - 1)); 47 | } 48 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * wildcmp - compares two strings and returns 1 4 | * if the strings can be considered identical, 5 | * otherwise return 0. 6 | * @s1: the normal string 7 | * @s2: the special string containing "*" 8 | * 9 | * Return: 1 if identical, else 0 10 | */ 11 | int wildcmp(char *s1, char *s2) 12 | { 13 | if (*s2 == '\0' && *s1 == '\0') 14 | return (1); 15 | 16 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 17 | return (0); 18 | 19 | if (*s1 == *s2) 20 | return (wildcmp(s1 + 1, s2 + 1)); 21 | 22 | if (*s2 == '*') 23 | return (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string 5 | * @s: string to calculate the length of 6 | * 7 | * Return: length of the string 8 | */ 9 | int _strlen_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | return (0); 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a number 5 | * @n: number to return the factorial from 6 | * 7 | * Return: factorial of n 8 | */ 9 | int factorial(int n) 10 | { 11 | if (n < 0) 12 | return (-1); 13 | if (n == 0) 14 | return (1); 15 | return (n * factorial(n - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - returns the value of x raised to the power of y 5 | * @x: value to raise 6 | * @y: power 7 | * 8 | * Return: result of the power 9 | */ 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | return (-1); 14 | if (y == 0) 15 | return (1); 16 | return (x * _pow_recursion(x, y - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int actual_sqrt_recursion(int n, int i); 4 | 5 | /** 6 | * _sqrt_recursion - returns the natural square root of a number 7 | * @n: number to calculate the square root of 8 | * 9 | * Return: the resulting square root 10 | */ 11 | int _sqrt_recursion(int n) 12 | { 13 | if (n < 0) 14 | return (-1); 15 | return (actual_sqrt_recursion(n, 0)); 16 | } 17 | 18 | /** 19 | * actual_sqrt_recursion - recurses to find the natural 20 | * square root of a number 21 | * @n: number to calculate the sqaure root of 22 | * @i: iterator 23 | * 24 | * Return: the resulting square root 25 | */ 26 | int actual_sqrt_recursion(int n, int i) 27 | { 28 | if (i * i > n) 29 | return (-1); 30 | if (i * i == n) 31 | return (i); 32 | return (actual_sqrt_recursion(n, i + 1)); 33 | } 34 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int actual_prime(int n, int i); 4 | 5 | /** 6 | * is_prime_number - says if an integer is a prime number or not 7 | * @n: number to evaluate 8 | * 9 | * Return: 1 if n is a prime number, 0 if not 10 | */ 11 | int is_prime_number(int n) 12 | { 13 | if (n <= 1) 14 | return (0); 15 | return (actual_prime(n, n - 1)); 16 | } 17 | 18 | /** 19 | * actual_prime - calculates if a number is prime recursively 20 | * @n: number to evaluate 21 | * @i: iterator 22 | * 23 | * Return: 1 if n is prime, 0 if not 24 | */ 25 | int actual_prime(int n, int i) 26 | { 27 | if (i == 1) 28 | return (1); 29 | if (n % i == 0 && i > 0) 30 | return (0); 31 | return (actual_prime(n, i - 1)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | 0x08-recursion 2 | 3 | File 0-puts_recursion.c is a function that prints a string, followed by a new line. 4 | 5 | File 1-print_rev_recursion.c is a function that prints a string in reverse. 6 | 7 | File 2-strlen_recursion.c is a function that returns the length of a string. 8 | 9 | File 3-factorial.c is a function that returns the factorial of a given number. 10 | 11 | File 4-pow_recursion.c is a function that returns the value of x raised to the power of y. 12 | 13 | File 5-sqrt_recursion.c is a function that returns the natural square root of a number. 14 | 15 | File 6-is_prime_number.c is a function that returns 1 if the input integer is a prime number, otherwise return 0. 16 | 17 | File 100-is_palindrome.c is a function that returns 1 if a string is a palindrome and 0 if not. 18 | 19 | File 101-wildcmp.c is a function that compares two strings and returns 1 if the strings can be considered identical, otherwise return 0. It uses the concept of wildcards. 20 | 21 | File _putchar.c is a file containing the _putchar function. 22 | 23 | File main.h is the header file containing the function prototypes. 24 | -------------------------------------------------------------------------------- /0x08-recursion/_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 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | void _puts_recursion(char *s); 6 | void _print_rev_recursion(char *s); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int x, int y); 10 | int _sqrt_recursion(int n); 11 | int is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memset - fills memory with a constant byte 5 | * @s: memory area to be filled 6 | * @b: char to copy 7 | * @n: number of times to copy b 8 | * 9 | * Return: pointer to the memory area s 10 | */ 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | s[i] = b; 18 | } 19 | 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concatenates two strings 5 | * @dest: string to append to 6 | * @src: string to add 7 | * 8 | * Return: a pointer to the resulting string 9 | */ 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i, j; 13 | 14 | i = 0; 15 | j = 0; 16 | 17 | while (dest[i] != '\0') 18 | i++; 19 | 20 | while (src[j] != '\0') 21 | { 22 | dest[i] = src[j]; 23 | j++; 24 | i++; 25 | } 26 | 27 | dest[i] = '\0'; 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memcpy - copies memory area 5 | * @dest: destination memory area 6 | * @src: memory area to copy from 7 | * @n: number of bytes to copy 8 | * 9 | * Return: pointer to dest 10 | */ 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * 9 | * Return: a pointer to the resulting string dest 10 | */ 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | i++; 20 | 21 | while (src[j] != '\0' && j < n) 22 | { 23 | dest[i] = src[j]; 24 | i++; 25 | j++; 26 | } 27 | 28 | dest[i] = '\0'; 29 | 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * 7 | * Return: the int converted from the string 8 | */ 9 | int _atoi(char *s) 10 | { 11 | int i, d, n, len, f, digit; 12 | 13 | i = 0; 14 | d = 0; 15 | n = 0; 16 | len = 0; 17 | f = 0; 18 | digit = 0; 19 | 20 | while (s[len] != '\0') 21 | len++; 22 | 23 | while (i < len && f == 0) 24 | { 25 | if (s[i] == '-') 26 | ++d; 27 | 28 | if (s[i] >= '0' && s[i] <= '9') 29 | { 30 | digit = s[i] - '0'; 31 | if (d % 2) 32 | digit = -digit; 33 | n = n * 10 + digit; 34 | f = 1; 35 | if (s[i + 1] < '0' || s[i + 1] > '9') 36 | break; 37 | f = 0; 38 | } 39 | i++; 40 | } 41 | 42 | if (f == 0) 43 | return (0); 44 | 45 | return (n); 46 | } 47 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_strchr - locates a character in a string 6 | * @s: string to search 7 | * @c: char to find 8 | * 9 | * Return: a pointer to the first occurrence of the character 10 | * c in the string s, or NULL if the character is not found 11 | */ 12 | char *_strchr(char *s, char c) 13 | { 14 | int a; 15 | 16 | while (1) 17 | { 18 | a = *s++; 19 | if (a == c) 20 | { 21 | return (s - 1); 22 | } 23 | if (a == 0) 24 | { 25 | return (NULL); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string 5 | * @s: string to evaluate 6 | * 7 | * Return: the length of the string 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | 15 | while (s[i] != '\0') 16 | { 17 | i++; 18 | } 19 | 20 | return (i); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copies a string 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes to copy 8 | * 9 | * Return: pointer to the resulting string 10 | */ 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | i = 0; 16 | 17 | while (src[i] != '\0' && i < n) 18 | { 19 | dest[i] = src[i]; 20 | i++; 21 | } 22 | 23 | while (i < n) 24 | { 25 | dest[i] = '\0'; 26 | i++; 27 | } 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks for lowercase character 5 | * @c: the character to check 6 | * Return: 1 if c is lowercase, 0 otherwise 7 | */ 8 | int _islower(int c) 9 | { 10 | return (c >= 'a' && c <= 'z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | void _puts(char *str) 8 | { 9 | int i; 10 | 11 | for (i = 0; str[i] != '\0'; i++) 12 | { 13 | _putchar(str[i]); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compares two strings 5 | * @s1: first string to compare 6 | * @s2: second string to compare 7 | * 8 | * Return: less than 0 if s1 is less than s2, 0 if they're equal, 9 | * more than 0 if s1 is greater than s2 10 | */ 11 | int _strcmp(char *s1, char *s2) 12 | { 13 | while (*s1 == *s2) 14 | { 15 | if (*s1 == '\0') 16 | { 17 | return (0); 18 | } 19 | s1++; 20 | s2++; 21 | } 22 | return (*s1 - *s2); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strspn - gets the length of a prefix substring 5 | * @s: string to evaluate 6 | * @accept: string containing the list of characters to match in s 7 | * 8 | * Return: the number of bytes in the initial segment 9 | * of s which consist only of bytes from accept 10 | */ 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | int i, j, f, flag; 14 | 15 | f = 0; 16 | 17 | for (i = 0; s[i] != '\0'; i++) 18 | { 19 | flag = 0; 20 | for (j = 0; accept[j] != '\0'; j++) 21 | { 22 | if (s[i] == accept[j]) 23 | { 24 | f++; 25 | flag = 1; 26 | } 27 | } 28 | if (flag == 0) 29 | { 30 | return (f); 31 | } 32 | } 33 | 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks for alphabetic character 5 | * @c: the character to be checked 6 | * Return: 1 if c is a letter, 0 otherwise 7 | */ 8 | int _isalpha(int c) 9 | { 10 | return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_strpbrk - searches a string for any of a set of bytes 6 | * @s: string to search 7 | * @accept: stringcontaining the bytes to look for 8 | * 9 | * Return: pointer to the byte in s that matches one of the bytes in accept 10 | * or NULL if no such byte is found 11 | */ 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | int i, j; 15 | 16 | for (i = 0; *s != '\0'; i++) 17 | { 18 | for (j = 0; accept[j] != '\0'; j++) 19 | { 20 | if (*s == accept[j]) 21 | { 22 | return (s); 23 | } 24 | } 25 | s++; 26 | } 27 | 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_strstr - locates a substring 6 | * @haystack: string to search in 7 | * @needle: substring to look for 8 | * 9 | * Return: pointer to the beginning of the located substring 10 | * or NULL if the substring is not found 11 | */ 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | int i, j; 15 | 16 | for (i = 0; haystack[i] != '\0'; i++) 17 | { 18 | for (j = 0; needle[j] != '\0'; j++) 19 | { 20 | if (haystack[i + j] != needle[j]) 21 | break; 22 | } 23 | if (!needle[j]) 24 | return (&haystack[i]); 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - computes the absolute value of an integer 5 | * @n: the int to check 6 | * Return: the absolute value of int 7 | */ 8 | int _abs(int n) 9 | { 10 | if (n >= 0) 11 | { 12 | return (n); 13 | } 14 | return (-n); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * 10 | * Return: the pointer to dest 11 | */ 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int len, i; 15 | 16 | len = 0; 17 | 18 | while (src[len] != '\0') 19 | { 20 | len++; 21 | } 22 | 23 | for (i = 0; i < len; i++) 24 | { 25 | dest[i] = src[i]; 26 | } 27 | dest[i] = '\0'; 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | ### static libraries 2 | 3 | -------------------------------------------------------------------------------- /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/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/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 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int main(void) 4 | { 5 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 6 | return (0); 7 | } 8 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/main.o -------------------------------------------------------------------------------- /0x09-static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x09-static_libraries/quote -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - prints the name of the program 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc __attribute__((unused)), char *argv[]) 12 | { 13 | printf("%s\n", *argv); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - print the number of arguments passed to the program 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char *argv[] __attribute__((unused))) 12 | { 13 | printf("%d\n", argc - 1); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - prints the minimum number of coins to 7 | * make change for an amount of money 8 | * @argc: number of arguments 9 | * @argv: array of arguments 10 | * 11 | * Return: 0 (Success), 1 (Error) 12 | */ 13 | int main(int argc, char *argv[]) 14 | { 15 | int num, j, result; 16 | int coins[] = {25, 10, 5, 2, 1}; 17 | 18 | if (argc != 2) 19 | { 20 | printf("Error\n"); 21 | return (1); 22 | } 23 | 24 | num = atoi(argv[1]); 25 | result = 0; 26 | 27 | if (num < 0) 28 | { 29 | printf("0\n"); 30 | return (0); 31 | } 32 | 33 | for (j = 0; j < 5 && num >= 0; j++) 34 | { 35 | while (num >= coins[j]) 36 | { 37 | result++; 38 | num -= coins[j]; 39 | } 40 | } 41 | 42 | printf("%d\n", result); 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - prints all arguments it receives 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < argc; i++) 16 | { 17 | printf("%s\n", argv[i]); 18 | } 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _atoi - converts a string to an integer 6 | * @s: string to be converted 7 | * 8 | * Return: the int converted from the string 9 | */ 10 | int _atoi(char *s) 11 | { 12 | int i, d, n, len, f, digit; 13 | 14 | i = 0; 15 | d = 0; 16 | n = 0; 17 | len = 0; 18 | f = 0; 19 | digit = 0; 20 | 21 | while (s[len] != '\0') 22 | len++; 23 | 24 | while (i < len && f == 0) 25 | { 26 | if (s[i] == '-') 27 | ++d; 28 | 29 | if (s[i] >= '0' && s[i] <= '9') 30 | { 31 | digit = s[i] - '0'; 32 | if (d % 2) 33 | digit = -digit; 34 | n = n * 10 + digit; 35 | f = 1; 36 | if (s[i + 1] < '0' || s[i + 1] > '9') 37 | break; 38 | f = 0; 39 | } 40 | i++; 41 | } 42 | 43 | if (f == 0) 44 | return (0); 45 | 46 | return (n); 47 | } 48 | 49 | /** 50 | * main - multiplies two numbers 51 | * @argc: number of arguments 52 | * @argv: array of arguments 53 | * 54 | * Return: 0 (Success), 1 (Error) 55 | */ 56 | int main(int argc, char *argv[]) 57 | { 58 | int result, num1, num2; 59 | 60 | if (argc < 3 || argc > 3) 61 | { 62 | printf("Error\n"); 63 | return (1); 64 | } 65 | 66 | num1 = _atoi(argv[1]); 67 | num2 = _atoi(argv[2]); 68 | result = num1 * num2; 69 | 70 | printf("%d\n", result); 71 | 72 | return (0); 73 | } 74 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _atoi - converts a string to an integer 6 | * @s: string to be converted 7 | * 8 | * Return: the int converted from the string 9 | */ 10 | int _atoi(char *s) 11 | { 12 | int i, d, n, len, f, digit; 13 | 14 | i = 0; 15 | d = 0; 16 | n = 0; 17 | len = 0; 18 | f = 0; 19 | digit = 0; 20 | 21 | while (s[len] != '\0') 22 | len++; 23 | 24 | while (i < len && f == 0) 25 | { 26 | if (s[i] == '-') 27 | ++d; 28 | 29 | if (s[i] >= '0' && s[i] <= '9') 30 | { 31 | digit = s[i] - '0'; 32 | if (d % 2) 33 | digit = -digit; 34 | n = n * 10 + digit; 35 | f = 1; 36 | if (s[i + 1] < '0' || s[i + 1] > '9') 37 | break; 38 | f = 0; 39 | } 40 | i++; 41 | } 42 | 43 | if (f == 0) 44 | return (0); 45 | 46 | return (n); 47 | } 48 | 49 | /** 50 | * main - adds two positive number 51 | * @argc: number of arguments 52 | * @argv: array of arguents 53 | * 54 | * Return: 0 (Success), or 1 (Success) 55 | */ 56 | int main(int argc, char *argv[]) 57 | { 58 | int sum, num, i, j, k; 59 | 60 | sum = 0; 61 | 62 | for (i = 1; i < argc; i++) 63 | { 64 | for (j = 0; argv[i][j] != '\0'; j++) 65 | { 66 | if (argv[i][j] > '9' || argv[i][j] < '0') 67 | { 68 | puts("Error"); 69 | return (1); 70 | } 71 | } 72 | } 73 | 74 | for (k = 1; k < argc; k++) 75 | { 76 | num = _atoi(argv[k]); 77 | if (num >= 0) 78 | { 79 | sum += num; 80 | } 81 | } 82 | 83 | printf("%d\n", sum); 84 | return (0); 85 | } 86 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | ### 0x0A-argc_argv 2 | _____________________________________________________________________________________________________________ 3 | 4 | File 0-whatsmyname.c is a program that prints its name, followed by a new line. 5 | 6 | File 1-args.c is a program that prints the number of arguments passed into it. 7 | 8 | File 2-args.c is a program that prints all arguments it receives. 9 | 10 | File 3-mul.c is a program that multiplies two numbers. 11 | 12 | File 4-add.c is a program that adds positive numbers. 13 | -------------------------------------------------------------------------------- /0x0A-argc_argv/_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 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | int _atoi(char *s); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *create_array - creates an array of chars, 6 | * and initializes it with a specific char 7 | * @size: size of the array to create 8 | * @c: char to initialize the array c 9 | * 10 | * Return: pointer to the array (Success), NULL (Error) 11 | */ 12 | char *create_array(unsigned int size, char c) 13 | { 14 | char *p; 15 | unsigned int i = 0; 16 | 17 | if (size == 0) 18 | return (NULL); 19 | 20 | p = (char *) malloc(sizeof(char) * size); 21 | 22 | if (p == NULL) 23 | return (0); 24 | 25 | while (i < size) 26 | { 27 | *(p + i) = c; 28 | i++; 29 | } 30 | 31 | *(p + i) = '\0'; 32 | 33 | return (p); 34 | } 35 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *_strdup - copies the string given as parameter 6 | * @str: string to duplicate 7 | * 8 | * Return: pointer to the copied string (Success), NULL (Error) 9 | */ 10 | char *_strdup(char *str) 11 | { 12 | char *dup; 13 | unsigned int i, len; 14 | 15 | i = 0; 16 | len = 0; 17 | 18 | if (str == NULL) 19 | return (NULL); 20 | 21 | while (str[len]) 22 | len++; 23 | 24 | dup = malloc(sizeof(char) * (len + 1)); 25 | 26 | if (dup == NULL) 27 | return (NULL); 28 | 29 | while ((dup[i] = str[i]) != '\0') 30 | i++; 31 | 32 | return (dup); 33 | } 34 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | *argstostr - concatenates all arguments of the program. 5 | *@ac: argument count. 6 | *@av: pointer to array of size ac. 7 | *Return: NULL if ac == 0 or av == null, Pointer to new string. 8 | *NULL on fail. 9 | */ 10 | char *argstostr(int ac, char **av) 11 | { 12 | int i, j, k, size; 13 | char *arg; 14 | 15 | size = 0; 16 | k = 0; 17 | if (ac == 0 || av == NULL) 18 | return (NULL); 19 | i = 0; 20 | while (i < ac) 21 | { 22 | j = 0; 23 | while (av[i][j]) 24 | { 25 | size++; 26 | j++; 27 | } 28 | size++; 29 | i++; 30 | } 31 | arg = malloc((sizeof(char) * size) + 1); 32 | if (arg == NULL) 33 | return (NULL); 34 | i = 0; 35 | while (i < ac) 36 | { 37 | j = 0; 38 | while (av[i][j]) 39 | { 40 | arg[k] = av[i][j]; 41 | j++; 42 | k++; 43 | } 44 | arg[k] = '\n'; 45 | k++; 46 | i++; 47 | } 48 | arg[k] = '\0'; 49 | return (arg); 50 | } 51 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * count_word - helper function to count the number of words in a string 6 | * @s: string to evaluate 7 | * 8 | * Return: number of words 9 | */ 10 | int count_word(char *s) 11 | { 12 | int flag, c, w; 13 | 14 | flag = 0; 15 | w = 0; 16 | 17 | for (c = 0; s[c] != '\0'; c++) 18 | { 19 | if (s[c] == ' ') 20 | flag = 0; 21 | else if (flag == 0) 22 | { 23 | flag = 1; 24 | w++; 25 | } 26 | } 27 | 28 | return (w); 29 | } 30 | /** 31 | * **strtow - splits a string into words 32 | * @str: string to split 33 | * 34 | * Return: pointer to an array of strings (Success) 35 | * or NULL (Error) 36 | */ 37 | char **strtow(char *str) 38 | { 39 | char **matrix, *tmp; 40 | int i, k = 0, len = 0, words, c = 0, start, end; 41 | 42 | while (*(str + len)) 43 | len++; 44 | words = count_word(str); 45 | if (words == 0) 46 | return (NULL); 47 | 48 | matrix = (char **) malloc(sizeof(char *) * (words + 1)); 49 | if (matrix == NULL) 50 | return (NULL); 51 | 52 | for (i = 0; i <= len; i++) 53 | { 54 | if (str[i] == ' ' || str[i] == '\0') 55 | { 56 | if (c) 57 | { 58 | end = i; 59 | tmp = (char *) malloc(sizeof(char) * (c + 1)); 60 | if (tmp == NULL) 61 | return (NULL); 62 | while (start < end) 63 | *tmp++ = str[start++]; 64 | *tmp = '\0'; 65 | matrix[k] = tmp - c; 66 | k++; 67 | c = 0; 68 | } 69 | } 70 | else if (c++ == 0) 71 | start = i; 72 | } 73 | 74 | matrix[k] = NULL; 75 | 76 | return (matrix); 77 | } 78 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *str_concat - concatenates two strings 6 | * @s1: string to concatenate 7 | * @s2: other string to concatenate 8 | * 9 | * Return: pointer to the new string created (Success), or NULL (Error) 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *s3; 14 | unsigned int i = 0, j = 0, len1 = 0, len2 = 0; 15 | 16 | while (s1 && s1[len1]) 17 | len1++; 18 | while (s2 && s2[len2]) 19 | len2++; 20 | 21 | s3 = malloc(sizeof(char) * (len1 + len2 + 1)); 22 | if (s3 == NULL) 23 | return (NULL); 24 | 25 | i = 0; 26 | j = 0; 27 | 28 | if (s1) 29 | { 30 | while (i < len1) 31 | { 32 | s3[i] = s1[i]; 33 | i++; 34 | } 35 | } 36 | 37 | if (s2) 38 | { 39 | while (i < (len1 + len2)) 40 | { 41 | s3[i] = s2[j]; 42 | i++; 43 | j++; 44 | } 45 | } 46 | s3[i] = '\0'; 47 | 48 | return (s3); 49 | } 50 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * **alloc_grid - creates a two dimensional array of ints 6 | * @width: width of the matrix 7 | * @height: height of the matrix 8 | * 9 | * Return: pointer to the created matrix (Success) 10 | * or NULL (Error) 11 | */ 12 | int **alloc_grid(int width, int height) 13 | { 14 | int **arr; 15 | int i, j; 16 | 17 | if (height <= 0 || width <= 0) 18 | return (NULL); 19 | 20 | arr = (int **) malloc(sizeof(int *) * height); 21 | 22 | if (arr == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; i < height; i++) 26 | { 27 | arr[i] = (int *) malloc(sizeof(int) * width); 28 | if (arr[i] == NULL) 29 | { 30 | free(arr); 31 | for (j = 0; j <= i; j++) 32 | free(arr[j]); 33 | return (NULL); 34 | } 35 | } 36 | 37 | for (i = 0; i < height; i++) 38 | { 39 | for (j = 0; j < width; j++) 40 | { 41 | arr[i][j] = 0; 42 | } 43 | } 44 | return (arr); 45 | } 46 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * free_grid - frees the memory allocate for the grid 6 | * created by alloc_grid() 7 | * @grid: grid to free 8 | * @height: height of the grid 9 | */ 10 | void free_grid(int **grid, int height) 11 | { 12 | int i; 13 | 14 | if (grid == NULL || height == 0) 15 | return; 16 | 17 | for (i = 0; i < height; i++) 18 | free(grid[i]); 19 | 20 | free(grid); 21 | } 22 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # 0x0B. C - malloc Project 2 | 3 | ### File: `0-create_array.c` is a function that creates an array of chars, and initializes it with a specific char. 4 | 5 | ### File: `1-strdup.c` is a function that returns a pointer to a newly allocated space in memory, which contains a copy of the string given as a parameter. 6 | 7 | ### File: `2-str_concat.c` is a function that concatenates two strings. 8 | 9 | ### File: `3-alloc_grid.c` is a function that returns a pointer to a 2 dimensional array of integers. 10 | 11 | ### File: `4-free_grid.c` is a function that frees a 2 dimensional grid previously created by our alloc_grid function. 12 | 13 | ### File: `100-argstostr.c` is a function that concatenates all the arguments of our program. 14 | 15 | ### File: `101-strtow.c` is a function that splits a string into words. 16 | 17 | ### File: `main.h` is a header file contaning all the function prototypes. 18 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | char **strtow(char *str); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *malloc_checked - allocates memory using malloc 6 | * @b: number of bytes to allocate 7 | * 8 | * Return: a pointer to the allocated memory 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | void *ptr; 13 | 14 | ptr = malloc(b); 15 | 16 | if (ptr == NULL) 17 | exit(98); 18 | 19 | return (ptr); 20 | } 21 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *string_nconcat - concatenates n bytes of a string to another string 6 | * @s1: string to append to 7 | * @s2: string to concatenate from 8 | * @n: number of bytes from s2 to concatenate to s1 9 | * 10 | * Return: pointer to the resulting string 11 | */ 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | char *s; 15 | unsigned int i = 0, j = 0, len1 = 0, len2 = 0; 16 | 17 | while (s1 && s1[len1]) 18 | len1++; 19 | while (s2 && s2[len2]) 20 | len2++; 21 | 22 | if (n < len2) 23 | s = malloc(sizeof(char) * (len1 + n + 1)); 24 | else 25 | s = malloc(sizeof(char) * (len1 + len2 + 1)); 26 | 27 | if (!s) 28 | return (NULL); 29 | 30 | while (i < len1) 31 | { 32 | s[i] = s1[i]; 33 | i++; 34 | } 35 | 36 | while (n < len2 && i < (len1 + n)) 37 | s[i++] = s2[j++]; 38 | 39 | while (n >= len2 && i < (len1 + len2)) 40 | s[i++] = s2[j++]; 41 | 42 | s[i] = '\0'; 43 | 44 | return (s); 45 | } 46 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *_realloc - reallocates a memory block using malloc and free 6 | * @ptr: pointer to the memory previsouly allocated by malloc 7 | * @old_size: size of the allocated memory for ptr 8 | * @new_size: new size of the new memory block 9 | * 10 | * Return: pointer to the newly allocated memory block 11 | */ 12 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 13 | { 14 | char *ptr1; 15 | char *old_ptr; 16 | unsigned int i; 17 | 18 | if (new_size == old_size) 19 | return (ptr); 20 | 21 | if (new_size == 0 && ptr) 22 | { 23 | free(ptr); 24 | return (NULL); 25 | } 26 | 27 | if (!ptr) 28 | return (malloc(new_size)); 29 | 30 | ptr1 = malloc(new_size); 31 | if (!ptr1) 32 | return (NULL); 33 | 34 | old_ptr = ptr; 35 | 36 | if (new_size < old_size) 37 | { 38 | for (i = 0; i < new_size; i++) 39 | ptr1[i] = old_ptr[i]; 40 | } 41 | 42 | if (new_size > old_size) 43 | { 44 | for (i = 0; i < old_size; i++) 45 | ptr1[i] = old_ptr[i]; 46 | } 47 | 48 | free(ptr); 49 | return (ptr1); 50 | } 51 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *_memset - fills memory with a constant byte 6 | * @s: memory area to be filled 7 | * @b: char to copy 8 | * @n: number of times to copy b 9 | * 10 | * Return: pointer to the memory area s 11 | */ 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | s[i] = b; 19 | } 20 | 21 | return (s); 22 | } 23 | 24 | /** 25 | * *_calloc - allocates memory for an array 26 | * @nmemb: number of elements in the array 27 | * @size: size of each element 28 | * 29 | * Return: pointer to allocated memory 30 | */ 31 | void *_calloc(unsigned int nmemb, unsigned int size) 32 | { 33 | char *ptr; 34 | 35 | if (nmemb == 0 || size == 0) 36 | return (NULL); 37 | 38 | ptr = malloc(size * nmemb); 39 | 40 | if (ptr == NULL) 41 | return (NULL); 42 | 43 | _memset(ptr, 0, nmemb * size); 44 | 45 | return (ptr); 46 | } 47 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * *array_range - creates an array of integers 6 | * @min: minimum range of values stored 7 | * @max: maximum range of values stored and number of elements 8 | * 9 | * Return: pointer to the new array 10 | */ 11 | int *array_range(int min, int max) 12 | { 13 | int *ptr; 14 | int i, size; 15 | 16 | if (min > max) 17 | return (NULL); 18 | 19 | size = max - min + 1; 20 | 21 | ptr = malloc(sizeof(int) * size); 22 | 23 | if (ptr == NULL) 24 | return (NULL); 25 | 26 | for (i = 0; min <= max; i++) 27 | ptr[i] = min++; 28 | 29 | return (ptr); 30 | } 31 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | #### 0x0C-more_malloc_free 2 | 3 | File 0-malloc_checked.c is a function that allocates memory using malloc. 4 | 5 | File 1-string_nconcat.c is a function that concatenates two strings. 6 | 7 | File 2-calloc.c is a function that allocates memory for an array, using malloc. 8 | 9 | File 3-array_range.c is a function that creates an array of integers. 10 | 11 | File 100-realloc.c is a function that reallocates a memory block using malloc and free. 12 | 13 | File 101-mul.c is a program that multiplies two positive numbers. 14 | 15 | File main.h is the header file for all the function prototypes.. 16 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | void *malloc_checked(unsigned int b); 6 | char *string_nconcat(char *s1, char *s2, unsigned int n); 7 | void *_calloc(unsigned int nmemb, unsigned int size); 8 | int *array_range(int min, int max); 9 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 10 | void errors(void); 11 | int _strlen(char *s); 12 | int is_digit(char *s); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < (0) ? -(x) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | ### 0x0D-preprocessor 2 | 3 | File 0-object_like_macro.h is a header file that defines a macro named SIZE as an abbreviation for the token 1024. 4 | 5 | File 1-pi.h is a header file that defines a macro named PI as an abbreviation for the token 3.14159265359. 6 | 7 | File 2-main.c is a program that prints the name of the file it was compiled from, followed by a new line. 8 | 9 | File 3-function_like_macro.h is a function-like macro ABS(x) that computes the absolute value of a number x. 10 | 11 | File 4-sum.h is a function-like macro SUM(x, y) that computes the sum of the numbers x and y. 12 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * init_dog - initializes a variable of type struct dog 6 | * @d: pointer to struct dog to initialize 7 | * @name: name to initialize 8 | * @age: age to initialize 9 | * @owner: owner to initialize 10 | */ 11 | void init_dog(struct dog *d, char *name, float age, char *owner) 12 | { 13 | if (d == NULL) 14 | d = malloc(sizeof(struct dog)); 15 | d->name = name; 16 | d->age = age; 17 | d->owner = owner; 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * print_dog - prints a struct dog. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (d) 13 | { 14 | if (!(d->name)) 15 | printf("Name: (nil)\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | printf("Age: %f\n", d->age); 20 | 21 | if (!(d->owner)) 22 | printf("Owner: (nil)\n"); 23 | else 24 | printf("Owner: %s\n", d->owner); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * _strlen - returns the length of a string 6 | * @s: string to evaluate 7 | * 8 | * Return: the length of the string 9 | */ 10 | int _strlen(char *s) 11 | { 12 | int i; 13 | 14 | i = 0; 15 | 16 | while (s[i] != '\0') 17 | { 18 | i++; 19 | } 20 | 21 | return (i); 22 | } 23 | 24 | /** 25 | * *_strcpy - copies the string pointed to by src 26 | * including the terminating null byte (\0) 27 | * to the buffer pointed to by dest 28 | * @dest: pointer to the buffer in which we copy the string 29 | * @src: string to be copied 30 | * 31 | * Return: the pointer to dest 32 | */ 33 | char *_strcpy(char *dest, char *src) 34 | { 35 | int len, i; 36 | 37 | len = 0; 38 | 39 | while (src[len] != '\0') 40 | { 41 | len++; 42 | } 43 | 44 | for (i = 0; i < len; i++) 45 | { 46 | dest[i] = src[i]; 47 | } 48 | dest[i] = '\0'; 49 | 50 | return (dest); 51 | } 52 | 53 | /** 54 | * new_dog - creates a new dog 55 | * @name: name of the dog 56 | * @age: age of the dog 57 | * @owner: owner of the dog 58 | * 59 | * Return: pointer to the new dog (Success), NULL otherwise 60 | */ 61 | dog_t *new_dog(char *name, float age, char *owner) 62 | { 63 | dog_t *dog; 64 | int len1, len2; 65 | 66 | len1 = _strlen(name); 67 | len2 = _strlen(owner); 68 | 69 | dog = malloc(sizeof(dog_t)); 70 | if (dog == NULL) 71 | return (NULL); 72 | 73 | dog->name = malloc(sizeof(char) * (len1 + 1)); 74 | if (dog->name == NULL) 75 | { 76 | free(dog); 77 | return (NULL); 78 | } 79 | dog->owner = malloc(sizeof(char) * (len2 + 1)); 80 | if (dog->owner == NULL) 81 | { 82 | free(dog); 83 | free(dog->name); 84 | return (NULL); 85 | } 86 | _strcpy(dog->name, name); 87 | _strcpy(dog->owner, owner); 88 | dog->age = age; 89 | 90 | return (dog); 91 | } 92 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * free_dog - frees memory allocated for a struct dog 6 | * @d: struct dog to free 7 | */ 8 | void free_dog(dog_t *d) 9 | { 10 | if (d) 11 | { 12 | free(d->name); 13 | free(d->owner); 14 | free(d); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | ### 0x0E-structures_typedef 2 | File dog.h is a header file containing a new type "struct dog". 3 | 4 | File 1-init_dog.c a function that initialize a variable of type struct dog. 5 | 6 | File 2-print_dog.c is a function that prints a struct dog. 7 | 8 | File 4-new_dog.c is a function that creates a new dog. 9 | 10 | File 5-free_dog.c is a function that frees dogs. 11 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * struct dog - a dog's basic info 6 | * @name: First member 7 | * @age: Second member 8 | * @owner: Third member 9 | * 10 | * Description: Longer description 11 | */ 12 | struct dog 13 | { 14 | char *name; 15 | float age; 16 | char *owner; 17 | }; 18 | 19 | /** 20 | * dog_t - typedef for struct dog 21 | */ 22 | typedef struct dog dog_t; 23 | 24 | void init_dog(struct dog *d, char *name, float age, char *owner); 25 | void print_dog(struct dog *d); 26 | dog_t *new_dog(char *name, float age, char *owner); 27 | void free_dog(dog_t *d); 28 | char *_strcpy(char *dest, char *src); 29 | int _strlen(char *s); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_name - prints a name 6 | * @name: name to print 7 | * @f: pointer to the printing function 8 | */ 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | if (!name || !f) 12 | return; 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x0F-function_pointers/0-print_name.c.swp -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * array_iterator - executes a function given as a parameter 6 | * on each element of an array 7 | * @array: array to iterate over 8 | * @size: size of the array 9 | * @action: pointer to function used 10 | */ 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | unsigned int i; 14 | 15 | if (!array || !action) 16 | return; 17 | 18 | for (i = 0; i < size; i++) 19 | action(array[i]); 20 | } 21 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints its own opcodes 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int bytes, i; 14 | char *arr; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | exit(1); 20 | } 21 | 22 | bytes = atoi(argv[1]); 23 | 24 | if (bytes < 0) 25 | { 26 | printf("Error\n"); 27 | exit(2); 28 | } 29 | 30 | arr = (char *)main; 31 | 32 | for (i = 0; i < bytes; i++) 33 | { 34 | if (i == bytes - 1) 35 | { 36 | printf("%02hhx\n", arr[i]); 37 | break; 38 | } 39 | printf("%02hhx ", arr[i]); 40 | } 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - earches for an integer 5 | * @array: array to search in 6 | * @size: size of the array 7 | * @cmp: pointer to the comparing function 8 | * 9 | * Return: index of the first element for which 10 | * the cmp function does not return 0, or -1 if no match is found 11 | * or size is negative 12 | */ 13 | int int_index(int *array, int size, int (*cmp)(int)) 14 | { 15 | int i; 16 | 17 | if (array && cmp) 18 | { 19 | for (i = 0; i < size; i++) 20 | { 21 | if (cmp(array[i]) != 0) 22 | return (i); 23 | } 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H_ 2 | #define _CALC_H_ 3 | 4 | #include 5 | #include 6 | /** 7 | * struct op - Struct op 8 | * 9 | * @op: The operator 10 | * @f: The function associated 11 | */ 12 | typedef struct op 13 | { 14 | char *op; 15 | int (*f)(int a, int b); 16 | } op_t; 17 | 18 | int op_add(int a, int b); 19 | int op_sub(int a, int b); 20 | int op_mul(int a, int b); 21 | int op_div(int a, int b); 22 | int op_mod(int a, int b); 23 | int (*get_op_func(char *s))(int, int); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * get_op_func - selects the correct function to perform 5 | * the operation asked by the user. 6 | * @s: char operator. 7 | * 8 | * Return: pointer to the function that corresponds to the operator. 9 | */ 10 | int (*get_op_func(char *s))(int, int) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | int i = 0; 21 | 22 | while (i < 10) 23 | { 24 | if (s[0] == ops->op[i]) 25 | break; 26 | i++; 27 | } 28 | 29 | return (ops[i / 2].f); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * @argc: argument count. 6 | * @argv: argument vector. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int a, b; 13 | int (*operation)(int, int); 14 | 15 | if (argc != 4) 16 | { 17 | printf("Error\n"); 18 | exit(98); 19 | } 20 | 21 | if (argv[2][1]) 22 | { 23 | printf("Error\n"); 24 | exit(99); 25 | } 26 | 27 | operation = get_op_func(argv[2]); 28 | 29 | if (operation == NULL) 30 | { 31 | printf("Error\n"); 32 | exit(99); 33 | } 34 | 35 | a = atoi(argv[1]); 36 | b = atoi(argv[3]); 37 | 38 | printf("%d\n", operation(a, b)); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * op_add - adds two numbers. 5 | * @a: first number. 6 | * @b: second number. 7 | * 8 | * Return: add. 9 | */ 10 | int op_add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | 15 | /** 16 | * op_sub - subctracts two numbers. 17 | * @a: first number. 18 | * @b: second number. 19 | * 20 | * Return: difference. 21 | */ 22 | int op_sub(int a, int b) 23 | { 24 | return (a - b); 25 | } 26 | 27 | /** 28 | * op_mul - multiplies two numbers. 29 | * @a: first number. 30 | * @b: second number. 31 | * 32 | * Return: multiplication. 33 | */ 34 | int op_mul(int a, int b) 35 | { 36 | return (a * b); 37 | } 38 | 39 | /** 40 | * op_div - divides two numbers. 41 | * @a: first number. 42 | * @b: second number. 43 | * 44 | * Return: division. 45 | */ 46 | int op_div(int a, int b) 47 | { 48 | if (b == 0) 49 | { 50 | printf("Error\n"); 51 | exit(100); 52 | } 53 | return (a / b); 54 | } 55 | 56 | /** 57 | * op_mod - calculates the module of two numbers. 58 | * @a: first number. 59 | * @b: second number. 60 | * 61 | * Return: remainder of the division. 62 | */ 63 | int op_mod(int a, int b) 64 | { 65 | if (b == 0) 66 | { 67 | printf("Error\n"); 68 | exit(100); 69 | } 70 | return (a % b); 71 | } 72 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | File 0-print_name.c is a function that prints a name. 2 | 3 | File 1-array_iterator.c is a function that executes a function given as a parameter on each element of an array. 4 | 5 | File 2-int_index.c is a function that searches for an integer. 6 | 7 | Files 3-main.c, 3-op_functions.c, 3-get_op_func.c, 3-calc.h are part of a program that performs simple operations. 8 | 9 | File function_pointers.h is the header file that contains all these functions' prototypes. 10 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNC_H 2 | #define FUNC_H 3 | #include 4 | 5 | void print_name(char *name, void (*f)(char *)); 6 | void array_iterator(int *array, size_t size, void (*action)(int)); 7 | int int_index(int *array, int size, int (*cmp)(int)); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - calculates the sum of all its parameters 5 | * @n: number of arguments passed to the function 6 | * 7 | * Return: the resulting sum 8 | */ 9 | int sum_them_all(const unsigned int n, ...) 10 | { 11 | unsigned int i; 12 | int sum = 0; 13 | va_list list; 14 | 15 | va_start(list, n); 16 | 17 | for (i = 0; i < n; i++) 18 | sum += va_arg(list, int); 19 | 20 | va_end(list); 21 | 22 | return (sum); 23 | } 24 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_numbers - prints numbers given as parameters 5 | * @separator: string to be printed between numbers 6 | * @n: number of integers passed to the function 7 | */ 8 | void print_numbers(const char *separator, const unsigned int n, ...) 9 | { 10 | unsigned int i; 11 | va_list list; 12 | 13 | va_start(list, n); 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | if (!separator) 18 | printf("%d", va_arg(list, int)); 19 | else if (separator && i == 0) 20 | printf("%d", va_arg(list, int)); 21 | else 22 | printf("%s%d", separator, va_arg(list, int)); 23 | } 24 | 25 | va_end(list); 26 | 27 | printf("\n"); 28 | } 29 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - prints strings 5 | * @separator: separator between strings 6 | * @n: number of arguments 7 | */ 8 | void print_strings(const char *separator, const unsigned int n, ...) 9 | { 10 | unsigned int i; 11 | char *str; 12 | 13 | va_list list; 14 | 15 | va_start(list, n); 16 | 17 | for (i = 0; i < n; i++) 18 | { 19 | str = va_arg(list, char *); 20 | if (!str) 21 | str = "(nil)"; 22 | if (!separator) 23 | printf("%s", str); 24 | else if (separator && i == 0) 25 | printf("%s", str); 26 | else 27 | printf("%s%s", separator, str); 28 | } 29 | 30 | printf("\n"); 31 | 32 | va_end(list); 33 | } 34 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_all - prints anything 5 | * @format: list of types of arguments passed to the function 6 | */ 7 | void print_all(const char * const format, ...) 8 | { 9 | int i = 0; 10 | char *str, *sep = ""; 11 | 12 | va_list list; 13 | 14 | va_start(list, format); 15 | 16 | if (format) 17 | { 18 | while (format[i]) 19 | { 20 | switch (format[i]) 21 | { 22 | case 'c': 23 | printf("%s%c", sep, va_arg(list, int)); 24 | break; 25 | case 'i': 26 | printf("%s%d", sep, va_arg(list, int)); 27 | break; 28 | case 'f': 29 | printf("%s%f", sep, va_arg(list, double)); 30 | break; 31 | case 's': 32 | str = va_arg(list, char *); 33 | if (!str) 34 | str = "(nil)"; 35 | printf("%s%s", sep, str); 36 | break; 37 | default: 38 | i++; 39 | continue; 40 | } 41 | sep = ", "; 42 | i++; 43 | } 44 | } 45 | 46 | printf("\n"); 47 | va_end(list); 48 | } 49 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VAR_FUNC_H 2 | #define VAR_FUNC_H 3 | 4 | #include 5 | #include 6 | 7 | int sum_them_all(const unsigned int n, ...); 8 | void print_numbers(const char *separator, const unsigned int n, ...); 9 | void print_strings(const char *separator, const unsigned int n, ...); 10 | void print_all(const char * const format, ...); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_list - prints all the elements of a list_t list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t print_list(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | if (h->str == NULL) 16 | printf("[%d] %s\n", 0, "(nil)"); 17 | else 18 | printf("[%d] %s\n", h->len, h->str); 19 | h = h->next; 20 | nelem++; 21 | } 22 | return (nelem); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * list_len - returns then number of elements in a list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t list_len(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | h = h->next; 16 | nelem++; 17 | } 18 | return (nelem); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * bmain - function executed before main 5 | * Return: no return. 6 | */ 7 | 8 | void __attribute__ ((constructor)) bmain() 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x12-singly_linked_lists/101-hello_holberton.o -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node - adds a new node at the beginning 4 | * of a list_t list. 5 | * @head: head of the linked list. 6 | * @str: string to store in the list. 7 | * Return: address of the head. 8 | */ 9 | 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *new; 13 | size_t nchar; 14 | 15 | new = malloc(sizeof(list_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->str = strdup(str); 20 | 21 | for (nchar = 0; str[nchar]; nchar++) 22 | ; 23 | 24 | new->len = nchar; 25 | new->next = *head; 26 | *head = new; 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - adds a new node at the end 5 | * of a list_t list. 6 | * @head: head of the linked list. 7 | * @str: string to store in the list. 8 | * Return: address of the head. 9 | */ 10 | 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *new, *temp; 14 | size_t nchar; 15 | 16 | new = malloc(sizeof(list_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->str = strdup(str); 21 | 22 | for (nchar = 0; str[nchar]; nchar++) 23 | ; 24 | 25 | new->len = nchar; 26 | new->next = NULL; 27 | temp = *head; 28 | 29 | if (temp == NULL) 30 | { 31 | *head = new; 32 | } 33 | else 34 | { 35 | while (temp->next != NULL) 36 | temp = temp->next; 37 | temp->next = new; 38 | } 39 | 40 | return (*head); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list 5 | * @head: head of the linked list. 6 | * Return: no return. 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *current; 12 | 13 | while ((current = head) != NULL) 14 | { 15 | head = head->next; 16 | free(current->str); 17 | free(current); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x12-singly_linked_lists/hello -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct list_s 18 | { 19 | char *str; 20 | unsigned int len; 21 | struct list_s *next; 22 | } list_t; 23 | 24 | size_t print_list(const list_t *h); 25 | size_t list_len(const list_t *h); 26 | list_t *add_node(list_t **head, const char *str); 27 | list_t *add_node_end(list_t **head, const char *str); 28 | void free_list(list_t *head); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the elements of a list. 5 | * @h: head of a list. 6 | * 7 | * Return: numbers of nodes. 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t nnodes = 0; 12 | 13 | while (h != NULL) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns the number of elements in 5 | * a linked list. 6 | * @h: head of a list. 7 | * 8 | * Return: numbers of nodes. 9 | */ 10 | size_t listint_len(const listint_t *h) 11 | { 12 | size_t nnodes = 0; 13 | 14 | while (h != NULL) 15 | { 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a new node 5 | * at a given position. 6 | * @head: head of a list. 7 | * @idx: index of the list where the new node is 8 | * added. 9 | * @n: integer element. 10 | * 11 | * Return: the address of the new node, or NULL if it 12 | * failed. 13 | */ 14 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 15 | { 16 | unsigned int i; 17 | listint_t *new; 18 | listint_t *h; 19 | 20 | h = *head; 21 | 22 | if (idx != 0) 23 | { 24 | for (i = 0; i < idx - 1 && h != NULL; i++) 25 | { 26 | h = h->next; 27 | } 28 | } 29 | 30 | if (h == NULL && idx != 0) 31 | return (NULL); 32 | 33 | new = malloc(sizeof(listint_t)); 34 | if (new == NULL) 35 | return (NULL); 36 | 37 | new->n = n; 38 | 39 | if (idx == 0) 40 | { 41 | new->next = *head; 42 | *head = new; 43 | } 44 | else 45 | { 46 | new->next = h->next; 47 | h->next = new; 48 | } 49 | 50 | return (new); 51 | } 52 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: pointer to the first node. 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *p; 12 | listint_t *n; 13 | 14 | p = NULL; 15 | n = NULL; 16 | 17 | while (*head != NULL) 18 | { 19 | n = (*head)->next; 20 | (*head)->next = p; 21 | p = *head; 22 | *head = n; 23 | } 24 | 25 | *head = p; 26 | return (*head); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * print_listint_safe - prints a linked list. 28 | * @head: head of a list. 29 | * 30 | * Return: number of nodes in the list. 31 | */ 32 | size_t print_listint_safe(const listint_t *head) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | 37 | hptr = NULL; 38 | while (head != NULL) 39 | { 40 | new = malloc(sizeof(listp_t)); 41 | 42 | if (new == NULL) 43 | exit(98); 44 | 45 | new->p = (void *)head; 46 | new->next = hptr; 47 | hptr = new; 48 | 49 | add = hptr; 50 | 51 | while (add->next != NULL) 52 | { 53 | add = add->next; 54 | if (head == add->p) 55 | { 56 | printf("-> [%p] %d\n", (void *)head, head->n); 57 | free_listp(&hptr); 58 | return (nnodes); 59 | } 60 | } 61 | 62 | printf("[%p] %d\n", (void *)head, head->n); 63 | head = head->next; 64 | nnodes++; 65 | } 66 | 67 | free_listp(&hptr); 68 | return (nnodes); 69 | } 70 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp2(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * free_listint_safe - frees a linked list. 28 | * @h: head of a list. 29 | * 30 | * Return: size of the list that was freed. 31 | */ 32 | size_t free_listint_safe(listint_t **h) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | listint_t *curr; 37 | 38 | hptr = NULL; 39 | while (*h != NULL) 40 | { 41 | new = malloc(sizeof(listp_t)); 42 | 43 | if (new == NULL) 44 | exit(98); 45 | 46 | new->p = (void *)*h; 47 | new->next = hptr; 48 | hptr = new; 49 | 50 | add = hptr; 51 | 52 | while (add->next != NULL) 53 | { 54 | add = add->next; 55 | if (*h == add->p) 56 | { 57 | *h = NULL; 58 | free_listp2(&hptr); 59 | return (nnodes); 60 | } 61 | } 62 | 63 | curr = *h; 64 | *h = (*h)->next; 65 | free(curr); 66 | nnodes++; 67 | } 68 | 69 | *h = NULL; 70 | free_listp2(&hptr); 71 | return (nnodes); 72 | } 73 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp2(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * free_listint_safe - frees a linked list. 28 | * @h: head of a list. 29 | * 30 | * Return: size of the list that was freed. 31 | */ 32 | size_t free_listint_safe(listint_t **h) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | listint_t *curr; 37 | 38 | hptr = NULL; 39 | while (*h != NULL) 40 | { 41 | new = malloc(sizeof(listp_t)); 42 | 43 | if (new == NULL) 44 | exit(98); 45 | 46 | new->p = (void *)*h; 47 | new->next = hptr; 48 | hptr = new; 49 | 50 | add = hptr; 51 | 52 | while (add->next != NULL) 53 | { 54 | add = add->next; 55 | if (*h == add->p) 56 | { 57 | *h = NULL; 58 | free_listp2(&hptr); 59 | return (nnodes); 60 | } 61 | } 62 | 63 | curr = *h; 64 | *h = (*h)->next; 65 | free(curr); 66 | nnodes++; 67 | } 68 | 69 | *h = NULL; 70 | free_listp2(&hptr); 71 | return (nnodes); 72 | } 73 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - add a new node at the beginning 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | 15 | new = malloc(sizeof(listint_t)); 16 | 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->next = *head; 22 | *head = new; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - add a new node at the end 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint_end(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | listint_t *temp; 15 | 16 | (void)temp; 17 | 18 | new = malloc(sizeof(listint_t)); 19 | 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | new->n = n; 24 | new->next = NULL; 25 | temp = *head; 26 | if (*head == NULL) 27 | { 28 | *head = new; 29 | } 30 | else 31 | { 32 | while (temp->next != NULL) 33 | { 34 | temp = temp->next; 35 | } 36 | temp->next = new; 37 | } 38 | 39 | return (*head); 40 | } 41 | -------------------------------------------------------------------------------- /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 a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *temp; 12 | 13 | while ((temp = head) != NULL) 14 | { 15 | head = head->next; 16 | free(temp); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint2(listint_t **head) 10 | { 11 | listint_t *temp; 12 | listint_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes the head node of 5 | * a linked list 6 | * @head: head of a list. 7 | * 8 | * Return: head node's data. 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | int hnode; 13 | listint_t *h; 14 | listint_t *curr; 15 | 16 | if (*head == NULL) 17 | return (0); 18 | 19 | curr = *head; 20 | 21 | hnode = curr->n; 22 | 23 | h = curr->next; 24 | 25 | free(curr); 26 | 27 | *head = h; 28 | 29 | return (hnode); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the nth node of a linked list 5 | * @head: head of a list. 6 | * @index: index of the node. 7 | * 8 | * Return: nth node. If node does not exist, returns NULL. 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < index && head != NULL; i++) 15 | { 16 | head = head->next; 17 | } 18 | 19 | return (head); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - returns the sum of all the data (n) of 5 | * a linked list. 6 | * @head: head of a list. 7 | * 8 | * Return: sum of all the data (n). 9 | */ 10 | int sum_listint(listint_t *head) 11 | { 12 | int sum; 13 | 14 | sum = 0; 15 | while (head != NULL) 16 | { 17 | sum += head->n; 18 | head = head->next; 19 | } 20 | 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a new node 5 | * at a given position. 6 | * @head: head of a list. 7 | * @idx: index of the list where the new node is 8 | * added. 9 | * @n: integer element. 10 | * 11 | * Return: the address of the new node, or NULL if it 12 | * failed. 13 | */ 14 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 15 | { 16 | unsigned int i; 17 | listint_t *new; 18 | listint_t *h; 19 | 20 | h = *head; 21 | 22 | if (idx != 0) 23 | { 24 | for (i = 0; i < idx - 1 && h != NULL; i++) 25 | { 26 | h = h->next; 27 | } 28 | } 29 | 30 | if (h == NULL && idx != 0) 31 | return (NULL); 32 | 33 | new = malloc(sizeof(listint_t)); 34 | if (new == NULL) 35 | return (NULL); 36 | 37 | new->n = n; 38 | 39 | if (idx == 0) 40 | { 41 | new->next = *head; 42 | *head = new; 43 | } 44 | else 45 | { 46 | new->next = h->next; 47 | h->next = new; 48 | } 49 | 50 | return (new); 51 | } 52 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x13-more_singly_linked_lists 2 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _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 | * for Holberton project 14 | */ 15 | typedef struct listint_s 16 | { 17 | int n; 18 | struct listint_s *next; 19 | } listint_t; 20 | 21 | /** 22 | * struct listp_s - singly linked list 23 | * @p: pointers of nodes 24 | * @next: points to the next node 25 | * 26 | * Description: singly linked list of pointers 27 | */ 28 | typedef struct listp_s 29 | { 30 | void *p; 31 | struct listp_s *next; 32 | } listp_t; 33 | 34 | size_t print_listint(const listint_t *h); 35 | size_t listint_len(const listint_t *h); 36 | listint_t *add_nodeint(listint_t **head, const int n); 37 | listint_t *add_nodeint_end(listint_t **head, const int n); 38 | void free_listint(listint_t *head); 39 | void free_listint2(listint_t **head); 40 | int pop_listint(listint_t **head); 41 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 42 | int sum_listint(listint_t *head); 43 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 44 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 45 | listint_t *reverse_listint(listint_t **head); 46 | size_t print_listint_safe(const listint_t *head); 47 | size_t free_listint_safe(listint_t **h); 48 | listint_t *find_listint_loop(listint_t *head); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to an 5 | * unsigned int. 6 | * @b: binary. 7 | * 8 | * Return: unsigned int. 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int ui; 13 | int len, base_two; 14 | 15 | if (!b) 16 | return (0); 17 | 18 | ui = 0; 19 | 20 | for (len = 0; b[len] != '\0'; len++) 21 | ; 22 | 23 | for (len--, base_two = 1; len >= 0; len--, base_two *= 2) 24 | { 25 | if (b[len] != '0' && b[len] != '1') 26 | { 27 | return (0); 28 | } 29 | 30 | if (b[len] & 1) 31 | { 32 | ui += base_two; 33 | } 34 | } 35 | 36 | return (ui); 37 | } 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary representation 5 | * of a number. 6 | * @n: unsigned long int. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_binary(unsigned long int n) 11 | { 12 | if (n >> 0) 13 | { 14 | if (n >> 1) 15 | print_binary(n >> 1); 16 | _putchar((n & 1) + '0'); 17 | } 18 | else 19 | { 20 | _putchar('0'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * 6 | * Return: 0 if big endian, 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | unsigned int x; 11 | char *c; 12 | 13 | x = 1; 14 | c = (char *) &x; 15 | 16 | return ((int)*c); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given 5 | * index. 6 | * @n: unsigned long int input. 7 | * @index: index of the bit. 8 | * 9 | * Return: value of the bit. 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (n == 0 && index < 64) 16 | return (0); 17 | 18 | for (i = 0; i <= 63; n >>= 1, i++) 19 | { 20 | if (index == i) 21 | { 22 | return (n & 1); 23 | } 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets the value of a bit to 1. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | *n = (*n | m); 20 | 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a bit to 0. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | 20 | if (*n & m) 21 | *n ^= m; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - returns the number of bits you would 5 | * need to flip to get from one number to another 6 | * @n: number one. 7 | * @m: number two. 8 | * 9 | * Return: number of bits. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned int nbits; 14 | 15 | for (nbits = 0; n || m; n >>= 1, m >>= 1) 16 | { 17 | if ((n & 1) != (m & 1)) 18 | nbits++; 19 | } 20 | 21 | return (nbits); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | 0x14-bit_manipulation 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | unsigned int binary_to_uint(const char *b); 5 | void print_binary(unsigned long int n); 6 | int _putchar(char c); 7 | int get_bit(unsigned long int n, unsigned int index); 8 | int set_bit(unsigned long int *n, unsigned int index); 9 | int clear_bit(unsigned long int *n, unsigned int index); 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 11 | int get_endianness(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - reads a text file and prints the letters 5 | * @filename: filename. 6 | * @letters: numbers of letters printed. 7 | * 8 | * Return: numbers of letters printed. It fails, returns 0. 9 | */ 10 | ssize_t read_textfile(const char *filename, size_t letters) 11 | { 12 | int fd; 13 | ssize_t nrd, nwr; 14 | char *buf; 15 | 16 | if (!filename) 17 | return (0); 18 | 19 | fd = open(filename, O_RDONLY); 20 | 21 | if (fd == -1) 22 | return (0); 23 | 24 | buf = malloc(sizeof(char) * (letters)); 25 | if (!buf) 26 | return (0); 27 | 28 | nrd = read(fd, buf, letters); 29 | nwr = write(STDOUT_FILENO, buf, nrd); 30 | 31 | close(fd); 32 | 33 | free(buf); 34 | 35 | return (nwr); 36 | } 37 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file 5 | * @filename: filename. 6 | * @text_content: content writed in the file. 7 | * 8 | * Return: 1 if it success. -1 if it fails. 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | int nletters; 14 | int rwr; 15 | 16 | if (!filename) 17 | return (-1); 18 | 19 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 20 | 21 | if (fd == -1) 22 | return (-1); 23 | 24 | if (!text_content) 25 | text_content = ""; 26 | 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | 35 | close(fd); 36 | 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text at the end of a file 5 | * @filename: filename. 6 | * @text_content: added content. 7 | * 8 | * Return: 1 if the file exists. -1 if the fails does not exist 9 | * or if it fails. 10 | */ 11 | int append_text_to_file(const char *filename, char *text_content) 12 | { 13 | int fd; 14 | int nletters; 15 | int rwr; 16 | 17 | if (!filename) 18 | return (-1); 19 | 20 | fd = open(filename, O_WRONLY | O_APPEND); 21 | 22 | if (fd == -1) 23 | return (-1); 24 | 25 | if (text_content) 26 | { 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | } 35 | 36 | close(fd); 37 | 38 | return (1); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * error_file - checks if files can be opened. 6 | * @file_from: file_from. 7 | * @file_to: file_to. 8 | * @argv: arguments vector. 9 | * Return: no return. 10 | */ 11 | void error_file(int file_from, int file_to, char *argv[]) 12 | { 13 | if (file_from == -1) 14 | { 15 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 16 | exit(98); 17 | } 18 | if (file_to == -1) 19 | { 20 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]); 21 | exit(99); 22 | } 23 | } 24 | 25 | /** 26 | * main - check the code for Holberton School students. 27 | * @argc: number of arguments. 28 | * @argv: arguments vector. 29 | * Return: Always 0. 30 | */ 31 | int main(int argc, char *argv[]) 32 | { 33 | int file_from, file_to, err_close; 34 | ssize_t nchars, nwr; 35 | char buf[1024]; 36 | 37 | if (argc != 3) 38 | { 39 | dprintf(STDERR_FILENO, "%s\n", "Usage: cp file_from file_to"); 40 | exit(97); 41 | } 42 | 43 | file_from = open(argv[1], O_RDONLY); 44 | file_to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0664); 45 | error_file(file_from, file_to, argv); 46 | 47 | nchars = 1024; 48 | while (nchars == 1024) 49 | { 50 | nchars = read(file_from, buf, 1024); 51 | if (nchars == -1) 52 | error_file(-1, 0, argv); 53 | nwr = write(file_to, buf, nchars); 54 | if (nwr == -1) 55 | error_file(0, -1, argv); 56 | } 57 | 58 | err_close = close(file_from); 59 | if (err_close == -1) 60 | { 61 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 62 | exit(100); 63 | } 64 | 65 | err_close = close(file_to); 66 | if (err_close == -1) 67 | { 68 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 69 | exit(100); 70 | } 71 | return (0); 72 | } 73 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all the elements of a 5 | * dlistint_t list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | printf("%d\n", h->n); 25 | count++; 26 | h = h->next; 27 | } 28 | 29 | return (count); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements in 5 | * a double linked list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | count++; 25 | h = h->next; 26 | } 27 | 28 | return (count); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds a new node at the beginning 5 | * of a dlistint_t list 6 | * 7 | * @head: head of the list 8 | * @n: value of the element 9 | * Return: the address of the new element 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | dlistint_t *h; 15 | 16 | new = malloc(sizeof(dlistint_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->prev = NULL; 22 | h = *head; 23 | 24 | if (h != NULL) 25 | { 26 | while (h->prev != NULL) 27 | h = h->prev; 28 | } 29 | 30 | new->next = h; 31 | 32 | if (h != NULL) 33 | h->prev = new; 34 | 35 | *head = new; 36 | 37 | return (new); 38 | } 39 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds a new node at the end 5 | * of a dlistint_t list 6 | * 7 | * @head: head of the list 8 | * @n: value of the element 9 | * Return: the address of the new element 10 | */ 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *h; 14 | dlistint_t *new; 15 | 16 | new = malloc(sizeof(dlistint_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->next = NULL; 22 | 23 | h = *head; 24 | 25 | if (h != NULL) 26 | { 27 | while (h->next != NULL) 28 | h = h->next; 29 | h->next = new; 30 | } 31 | else 32 | { 33 | *head = new; 34 | } 35 | 36 | new->prev = h; 37 | 38 | return (new); 39 | } 40 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a dlistint_t list 5 | * 6 | * @head: head of the list 7 | * Return: no return 8 | */ 9 | void free_dlistint(dlistint_t *head) 10 | { 11 | dlistint_t *tmp; 12 | 13 | if (head != NULL) 14 | while (head->prev != NULL) 15 | head = head->prev; 16 | 17 | while ((tmp = head) != NULL) 18 | { 19 | head = head->next; 20 | free(tmp); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - returns the 5 | * nth node of a dlistint_t linked list 6 | * 7 | * @head: head of the list 8 | * @index: index of the nth node 9 | * Return: nth node 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | 18 | while (head->prev != NULL) 19 | head = head->prev; 20 | 21 | i = 0; 22 | 23 | while (head != NULL) 24 | { 25 | if (i == index) 26 | break; 27 | head = head->next; 28 | i++; 29 | } 30 | 31 | return (head); 32 | } 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all the data (n) 5 | * of a doubly linked list 6 | * 7 | * @head: head of the list 8 | * Return: sum of the data 9 | */ 10 | int sum_dlistint(dlistint_t *head) 11 | { 12 | int sum; 13 | 14 | sum = 0; 15 | 16 | if (head != NULL) 17 | { 18 | while (head->prev != NULL) 19 | head = head->prev; 20 | 21 | while (head != NULL) 22 | { 23 | sum += head->n; 24 | head = head->next; 25 | } 26 | } 27 | 28 | return (sum); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - inserts a new node at 5 | * a given position 6 | * 7 | * @h: head of the list 8 | * @idx: index of the new node 9 | * @n: value of the new node 10 | * Return: the address of the new node, or NULL if it failed 11 | */ 12 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 13 | { 14 | dlistint_t *new; 15 | dlistint_t *head; 16 | unsigned int i; 17 | 18 | new = NULL; 19 | if (idx == 0) 20 | new = add_dnodeint(h, n); 21 | else 22 | { 23 | head = *h; 24 | i = 1; 25 | if (head != NULL) 26 | while (head->prev != NULL) 27 | head = head->prev; 28 | while (head != NULL) 29 | { 30 | if (i == idx) 31 | { 32 | if (head->next == NULL) 33 | new = add_dnodeint_end(h, n); 34 | else 35 | { 36 | new = malloc(sizeof(dlistint_t)); 37 | if (new != NULL) 38 | { 39 | new->n = n; 40 | new->next = head->next; 41 | new->prev = head; 42 | head->next->prev = new; 43 | head->next = new; 44 | } 45 | } 46 | break; 47 | } 48 | head = head->next; 49 | i++; 50 | } 51 | } 52 | 53 | return (new); 54 | } 55 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - deletes the node at index of a 5 | * dlistint_t linked list 6 | * 7 | * @head: head of the list 8 | * @index: index of the new node 9 | * Return: 1 if it succeeded, -1 if it failed 10 | */ 11 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 12 | { 13 | dlistint_t *h1; 14 | dlistint_t *h2; 15 | unsigned int i; 16 | 17 | h1 = *head; 18 | 19 | if (h1 != NULL) 20 | while (h1->prev != NULL) 21 | h1 = h1->prev; 22 | 23 | i = 0; 24 | 25 | while (h1 != NULL) 26 | { 27 | if (i == index) 28 | { 29 | if (i == 0) 30 | { 31 | *head = h1->next; 32 | if (*head != NULL) 33 | (*head)->prev = NULL; 34 | } 35 | else 36 | { 37 | h2->next = h1->next; 38 | 39 | if (h1->next != NULL) 40 | h1->next->prev = h2; 41 | } 42 | 43 | free(h1); 44 | return (1); 45 | } 46 | h2 = h1; 47 | h1 = h1->next; 48 | i++; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | 0x17-doubly_linked_lists 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * for ALX project 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /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/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/Tolulope05/alx-low_level_programming/raw/master/0x18-dynamic_libraries/nrandom.so 3 | export LD_PRELOAD=/tmp/nrandom.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | 0x18-dynamic_libraries 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef DYNAMIC_LIB 2 | #define DYNAMIC_LIB 3 | #include 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *str); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | #endif /* DYNAMIC_LIB */ 25 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Creates a hash table. 5 | * @size: The size of the array. 6 | * 7 | * Return: If an error occurs - NULL. 8 | * Otherwise - a pointer to the new hash table. 9 | */ 10 | hash_table_t *hash_table_create(unsigned long int size) 11 | { 12 | hash_table_t *ht; 13 | unsigned long int i; 14 | 15 | ht = malloc(sizeof(hash_table_t)); 16 | if (ht == NULL) 17 | return (NULL); 18 | 19 | ht->size = size; 20 | ht->array = malloc(sizeof(hash_node_t *) * size); 21 | if (ht->array == NULL) 22 | return (NULL); 23 | for (i = 0; i < size; i++) 24 | ht->array[i] = NULL; 25 | 26 | return (ht); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - Hash function implementing the djb2 algorithm. 5 | * @str: The string to hash. 6 | * 7 | * Return: The calculated hash. 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/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - Get the index at which a key/value 5 | * pair should be stored in array of a hash table. 6 | * @key: The key to get the index of. 7 | * @size: The size of the array of the hash table. 8 | * 9 | * Return: The index of the key. 10 | * Description: Uses the djb2 algorithm. 11 | */ 12 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 13 | { 14 | return (hash_djb2(key) % size); 15 | } 16 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_set - Add or update an element in a hash table. 5 | * @ht: A pointer to the hash table. 6 | * @key: The key to add - cannot be an empty string. 7 | * @value: The value associated with key. 8 | * 9 | * Return: Upon failure - 0. 10 | * Otherwise - 1. 11 | */ 12 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 13 | { 14 | hash_node_t *new; 15 | char *value_copy; 16 | unsigned long int index, i; 17 | 18 | if (ht == NULL || key == NULL || *key == '\0' || value == NULL) 19 | return (0); 20 | 21 | value_copy = strdup(value); 22 | if (value_copy == NULL) 23 | return (0); 24 | 25 | index = key_index((const unsigned char *)key, ht->size); 26 | for (i = index; ht->array[i]; i++) 27 | { 28 | if (strcmp(ht->array[i]->key, key) == 0) 29 | { 30 | free(ht->array[i]->value); 31 | ht->array[i]->value = value_copy; 32 | return (1); 33 | } 34 | } 35 | 36 | new = malloc(sizeof(hash_node_t)); 37 | if (new == NULL) 38 | { 39 | free(value_copy); 40 | return (0); 41 | } 42 | new->key = strdup(key); 43 | if (new->key == NULL) 44 | { 45 | free(new); 46 | return (0); 47 | } 48 | new->value = value_copy; 49 | new->next = ht->array[index]; 50 | ht->array[index] = new; 51 | 52 | return (1); 53 | } 54 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - Retrieve the value associated with 5 | * a key in a hash table. 6 | * @ht: A pointer to the hash table. 7 | * @key: The key to get the value of. 8 | * 9 | * Return: If the key cannot be matched - NULL. 10 | * Otherwise - the value associated with key in ht. 11 | */ 12 | char *hash_table_get(const hash_table_t *ht, const char *key) 13 | { 14 | hash_node_t *node; 15 | unsigned long int index; 16 | 17 | if (ht == NULL || key == NULL || *key == '\0') 18 | return (NULL); 19 | 20 | index = key_index((const unsigned char *)key, ht->size); 21 | if (index >= ht->size) 22 | return (NULL); 23 | 24 | node = ht->array[index]; 25 | while (node && strcmp(node->key, key) != 0) 26 | node = node->next; 27 | 28 | return ((node == NULL) ? NULL : node->value); 29 | } 30 | -------------------------------------------------------------------------------- /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/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/README.md: -------------------------------------------------------------------------------- 1 | ### 0x1A-hash_tables 2 | ## Python Dictionaries 3 | Python dictionaries are implemented using hash tables. When you will be done with this project, you will be able to better understand the power and simplicity of Python dictionaries. So much is actually happening when you type d = {'a': 1, 'b': 2}, but everything looks so simple for the user. Python doesn’t use the exact same implementation than the one you will work on today though. If you are curious on how it works under the hood, here is a good blog post about how dictionaries are implemented in Python 2.7 (not mandatory). 4 | 5 | Note that all dictionaries are not implemented using hash tables and there is a difference between a dictionary and a hash table. Read more here (not mandatory). 6 | ## General 7 |
    8 |
  • Allowed editors: vi, vim, emacs
  • 9 |
  • All your files will be compiled on Ubuntu 20.04 LTS using gcc, using the options -Wall -Werror -Wextra -pedantic -std=gnu89
  • 10 |
  • All your files should end with a new line
  • 11 |
  • A README.md file, at the root of the folder of the project is mandatory
  • 12 |
  • Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl
  • 13 |
  • You are not allowed to use global variables
  • 14 |
  • No more than 5 functions per file
  • 15 |
  • You are allowed to use the C standard library
  • 16 |
  • The prototypes of all your functions should be included in your header file called hash_tables.h
  • 17 |
  • Don’t forget to push your header file
  • 18 |
  • All your header files should be include guarded
  • 19 |
20 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 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/tests/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 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/tests/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 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_set(ht, "betty", "holberton"); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | char *value; 15 | 16 | ht = hash_table_create(1024); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_set(ht, "c", "isfun"); 25 | 26 | value = hash_table_get(ht, "python"); 27 | printf("%s:%s\n", "python", value); 28 | value = hash_table_get(ht, "Jennie"); 29 | printf("%s:%s\n", "Jennie", value); 30 | value = hash_table_get(ht, "N"); 31 | printf("%s:%s\n", "N", value); 32 | value = hash_table_get(ht, "Asterix"); 33 | printf("%s:%s\n", "Asterix", value); 34 | value = hash_table_get(ht, "Betty"); 35 | printf("%s:%s\n", "Betty", value); 36 | value = hash_table_get(ht, "98"); 37 | printf("%s:%s\n", "98", value); 38 | value = hash_table_get(ht, "c"); 39 | printf("%s:%s\n", "c", value); 40 | value = hash_table_get(ht, "javascript"); 41 | printf("%s:%s\n", "javascript", value); 42 | return (EXIT_SUCCESS); 43 | } 44 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 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, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_print(ht); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 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, "Jennie", "and Jay love asm"); 21 | hash_table_set(ht, "N", "queens"); 22 | hash_table_set(ht, "Asterix", "Obelix"); 23 | hash_table_set(ht, "Betty", "Holberton"); 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", "Jennie"); 34 | hash_table_set(ht, "hetairas", "Jennie Z"); 35 | hash_table_set(ht, "mentioner", "Jennie"); 36 | hash_table_set(ht, "hetairas", "Jennie Z Chu"); 37 | hash_table_print(ht); 38 | hash_table_delete(ht); 39 | return (EXIT_SUCCESS); 40 | } 41 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | all: $(SRC) 4 | $(CC) $(SRC) -o school 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 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) $(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 | 8 | The grid represents water by 0 and land by 1. 9 | 10 | Args: 11 | grid (list): A list of list of integers representing an island. 12 | Returns: 13 | The perimeter of the island defined in grid. 14 | """ 15 | width = len(grid[0]) 16 | height = len(grid) 17 | edges = 0 18 | size = 0 19 | 20 | for i in range(height): 21 | for j in range(width): 22 | if grid[i][j] == 1: 23 | size += 1 24 | if (j > 0 and grid[i][j - 1] == 1): 25 | edges += 1 26 | if (i > 0 and grid[i - 1][j] == 1): 27 | edges += 1 28 | return size * 4 - edges * 2 29 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x1C-makefiles/main.o -------------------------------------------------------------------------------- /0x1C-makefiles/school: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x1C-makefiles/school -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_school 5 | * 6 | * Return: Nothing 7 | */ 8 | void print_school(void) 9 | { 10 | printf("j#0000000000000000000000000000000000000\n"); 11 | printf("j#000000000000000000@Q**g00000000000000\n"); 12 | printf("j#0000000000000000*]++]4000000000000000\n"); 13 | printf("j#000000000000000k]++]++*N#000000000000\n"); 14 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 15 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 16 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 17 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 18 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 19 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 20 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 21 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 22 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 23 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 24 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 25 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 26 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 27 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 28 | printf("j#00000000000w]++++]*0##000000000000000\n"); 29 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 30 | printf("j#00000000000000000we]+]Q00000000000000\n"); 31 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 32 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 33 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 34 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x1C-makefiles/school.o -------------------------------------------------------------------------------- /0x1C-makefiles/tests/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeDroid999/alx-low_level_programming/c5d081b20a7ec88b6d3dfbb9f93c8cace5d25c2b/0x1E-search_algorithms/0-linear -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 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/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * recursive_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int recursive_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | return ((int)half); 33 | 34 | if (value < array[half]) 35 | return (recursive_search(array, half, value)); 36 | 37 | half++; 38 | 39 | return (recursive_search(array + half, size - half, value) + half); 40 | } 41 | 42 | /** 43 | * binary_search - calls to binary_search to return 44 | * the index of the number 45 | * 46 | * @array: input array 47 | * @size: size of the array 48 | * @value: value to search in 49 | * Return: index of the number 50 | */ 51 | int binary_search(int *array, size_t size, int value) 52 | { 53 | int index; 54 | 55 | index = recursive_search(array, size, value); 56 | 57 | if (index >= 0 && array[index] != value) 58 | return (-1); 59 | 60 | return (index); 61 | } 62 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | int index, m, k, prev; 16 | 17 | if (array == NULL || size == 0) 18 | return (-1); 19 | 20 | m = (int)sqrt((double)size); 21 | k = 0; 22 | prev = index = 0; 23 | 24 | do { 25 | printf("Value checked array[%d] = [%d]\n", index, array[index]); 26 | 27 | if (array[index] == value) 28 | return (index); 29 | k++; 30 | prev = index; 31 | index = k * m; 32 | } while (index < (int)size && array[index] < value); 33 | 34 | printf("Value found between indexes [%d] and [%d]\n", prev, index); 35 | 36 | for (; prev <= index && prev < (int)size; prev++) 37 | { 38 | printf("Value checked array[%d] = [%d]\n", prev, array[prev]); 39 | if (array[prev] == value) 40 | return (prev); 41 | } 42 | 43 | return (-1); 44 | } 45 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in an array of 5 | * integers using the Interpolation search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: index of the number 11 | */ 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t pos, low, high; 15 | double f; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | low = 0; 21 | high = size - 1; 22 | 23 | while (size) 24 | { 25 | f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]); 26 | pos = (size_t)(low + f); 27 | printf("Value checked array[%d]", (int)pos); 28 | 29 | if (pos >= size) 30 | { 31 | printf(" is out of range\n"); 32 | break; 33 | } 34 | else 35 | { 36 | printf(" = [%d]\n", array[pos]); 37 | } 38 | 39 | if (array[pos] == value) 40 | return ((int)pos); 41 | 42 | if (array[pos] < value) 43 | low = pos + 1; 44 | else 45 | high = pos - 1; 46 | 47 | if (low == high) 48 | break; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * rec_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int rec_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | { 33 | if (half > 0) 34 | return (rec_search(array, half + 1, value)); 35 | return ((int)half); 36 | } 37 | 38 | if (value < array[half]) 39 | return (rec_search(array, half + 1, value)); 40 | 41 | half++; 42 | return (rec_search(array + half, size - half, value) + half); 43 | } 44 | 45 | /** 46 | * advanced_binary - calls to rec_search to return 47 | * the index of the number 48 | * 49 | * @array: input array 50 | * @size: size of the array 51 | * @value: value to search in 52 | * Return: index of the number 53 | */ 54 | int advanced_binary(int *array, size_t size, int value) 55 | { 56 | int index; 57 | 58 | index = rec_search(array, size, value); 59 | 60 | if (index >= 0 && array[index] != value) 61 | return (-1); 62 | 63 | return (index); 64 | } 65 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @list: input list 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | listint_t *jump_list(listint_t *list, size_t size, int value) 14 | { 15 | size_t index, k, m; 16 | listint_t *prev; 17 | 18 | if (list == NULL || size == 0) 19 | return (NULL); 20 | 21 | m = (size_t)sqrt((double)size); 22 | index = 0; 23 | k = 0; 24 | 25 | do { 26 | prev = list; 27 | k++; 28 | index = k * m; 29 | 30 | while (list->next && list->index < index) 31 | list = list->next; 32 | 33 | if (list->next == NULL && index != list->index) 34 | index = list->index; 35 | 36 | printf("Value checked at index [%d] = [%d]\n", (int)index, list->n); 37 | 38 | } while (index < size && list->next && list->n < value); 39 | 40 | printf("Value found between indexes "); 41 | printf("[%d] and [%d]\n", (int)prev->index, (int)list->index); 42 | 43 | for (; prev && prev->index <= list->index; prev = prev->next) 44 | { 45 | printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n); 46 | if (prev->n == value) 47 | return (prev); 48 | } 49 | 50 | return (NULL); 51 | } 52 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - searches for a value in a skip list 5 | * 6 | * @list: input list 7 | * @value: value to search in 8 | * Return: index of the number 9 | */ 10 | skiplist_t *linear_skip(skiplist_t *list, int value) 11 | { 12 | skiplist_t *go; 13 | 14 | if (list == NULL) 15 | return (NULL); 16 | 17 | go = list; 18 | 19 | do { 20 | list = go; 21 | go = go->express; 22 | printf("Value checked at index "); 23 | printf("[%d] = [%d]\n", (int)go->index, go->n); 24 | } while (go->express && go->n < value); 25 | 26 | if (go->express == NULL) 27 | { 28 | list = go; 29 | while (go->next) 30 | go = go->next; 31 | } 32 | 33 | printf("Value found between indexes "); 34 | printf("[%d] and [%d]\n", (int)list->index, (int)go->index); 35 | 36 | while (list != go->next) 37 | { 38 | printf("Value checked at index [%d] = [%d]\n", (int)list->index, list->n); 39 | if (list->n == value) 40 | return (list); 41 | list = list->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.MD: -------------------------------------------------------------------------------- 1 | ### 0x1E-search_algorithms 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SORTING_ALGOS_H_ 2 | #define _SORTING_ALGOS_H_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * 10 | * @n: Integer 11 | * @index: Index of the node in the list 12 | * @next: Pointer to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct listint_s 18 | { 19 | int n; 20 | size_t index; 21 | struct listint_s *next; 22 | } listint_t; 23 | 24 | /** 25 | * struct skiplist_s - Singly linked list with an express lane 26 | * 27 | * @n: Integer 28 | * @index: Index of the node in the list 29 | * @next: Pointer to the next node 30 | * @express: Pointer to the next node in the express lane 31 | * 32 | * Description: singly linked list node structure with an express lane 33 | * for Holberton project 34 | */ 35 | typedef struct skiplist_s 36 | { 37 | int n; 38 | size_t index; 39 | struct skiplist_s *next; 40 | struct skiplist_s *express; 41 | } skiplist_t; 42 | 43 | int linear_search(int *array, size_t size, int value); 44 | int binary_search(int *array, size_t size, int value); 45 | int jump_search(int *array, size_t size, int value); 46 | int interpolation_search(int *array, size_t size, int value); 47 | int exponential_search(int *array, size_t size, int value); 48 | int advanced_binary(int *array, size_t size, int value); 49 | listint_t *jump_list(listint_t *list, size_t size, int value); 50 | skiplist_t *linear_skip(skiplist_t *list, int value); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2022-present https://github.com/CodeDroid999 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | --------------------------------------------------------------------------------