├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md ├── _putchar.c └── main.h ├── 0x03-debugging ├── .3-print_remaining_days.c.swp ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-convert_day.c ├── 3-main_a.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── .8-print_square.c.swp ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── .0-reset_to_98.c.swp ├── 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 ├── _putchar.c └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md ├── _putchar.c └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md ├── _putchar.c └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md ├── _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.swp ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-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 ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── 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 ├── _putchar.c └── main.h ├── 0x15-file_io ├── .100-elf_header.c.swp ├── .main.h.swp ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-create_dynamic_lib.sh ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 100-operations.so ├── 101-make_me_win.sh ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── _putchar.c ├── _putchar.o ├── liball.so ├── libdynamic.so ├── libmy.a ├── main.h ├── main.o ├── operations.c └── operations.o ├── 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 ├── 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 ├── school.c └── tests │ └── 5-main.py ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - prints exactly "and that piece of art is useful" 6 | * - Dora Korpar, 2015-10-19", 7 | * followed by a new line, to the standard error. 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 13 | return (1); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -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 | /** 4 | * main - prints the phrase "Programming is like building a multilingual puzzle 5 | * Return: 0 if exited properly, non-zero otherwise 6 | */ 7 | int main(void) 8 | { 9 | puts("\"Programming is like building a multilingual puzzle"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the phrase with proper grammar, 5 | * but the outcome is a piece of art, 6 | * Return: 0 if exited properly, non-zero otherwise 7 | */ 8 | int main(void) 9 | { 10 | printf("with proper grammar, but the outcome is a piece of art,\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the size of various types 5 | * Return: 0 if exited properly, non-zero otherwise 6 | */ 7 | int main(void) 8 | { 9 | printf("Size of a char: %d byte(s)\n", sizeof(char)); 10 | printf("Size of an int: %d byte(s)\n", sizeof(int)); 11 | printf("Size of a long int: %d byte(s)\n", sizeof(long int)); 12 | printf("Size of a long long int: %d byte(s)\n", sizeof(long long int)); 13 | printf("Size of a float: %d byte(s)\n", sizeof(float)); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - assigns a random number to int n everytime 7 | * it executes, and prints it 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | if (n > 0) 17 | printf("%d is positive\n", n); 18 | else if (n == 0) 19 | printf("%d is zero\n", n); 20 | else if (n < 0) 21 | printf("%d is negative\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - prints the last digit of the random 7 | * number stored in the variable n 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | printf("Last digit of %d is %d ", n, n % 10); 17 | if (n % 10 > 5) 18 | printf("and is greater than 5\n"); 19 | else if (n % 10 == 0) 20 | printf("and is 0\n"); 21 | else if (n % 10 < 6 && n % 10 != 0) 22 | printf("and is less than 6 and not 0\n"); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all possible different combinations of two digits 5 | * Return: ALways 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int n, m; 10 | 11 | for (n = 48; n <= 56; n++) 12 | { 13 | for (m = 49; m <= 57; m++) 14 | { 15 | if (m > n) 16 | { 17 | putchar(n); 18 | putchar(m); 19 | if (n != 56 || m != 57) 20 | { 21 | putchar(','); 22 | putchar(' '); 23 | } 24 | } 25 | } 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all possible different combinations of three digits 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int n, m, l; 10 | 11 | for (n = 48; n < 58; n++) 12 | { 13 | for (m = 49; m < 58; m++) 14 | { 15 | for (l = 50; l < 58; l++) 16 | { 17 | if (l > m && m > n) 18 | { 19 | putchar(n); 20 | putchar(m); 21 | putchar(l); 22 | if (n != 55 || m != 56) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all possible combinations of two two-digit numbers 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 100; i++) 12 | { 13 | for (j = 0; j < 100; j++) 14 | { 15 | if (i < j) 16 | { 17 | putchar((i / 10) + 48); 18 | putchar((i % 10) + 48); 19 | putchar(' '); 20 | putchar((j / 10) + 48); 21 | putchar((j % 10) + 48); 22 | if (i != 98 || j != 99) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, and then in uppercase, 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | putchar(ch); 14 | for (ch = 'A'; ch <= 'Z'; ch++) 15 | putchar(ch); 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the alphabet in lowercase, 5 | * followed by a new line, except q and e 6 | * Return: Aways 0 (Sucess) 7 | */ 8 | int main(void) 9 | { 10 | char ch = 'a'; 11 | 12 | while (ch <= 'z') 13 | { 14 | if (ch != 'e' && ch != 'q') 15 | { 16 | putchar(ch); 17 | } 18 | ch++; 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all single digit numbers of base 5 | * 10 starting from 0, followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = 0; n < 10; n++) 13 | printf("%d", n); 14 | printf("\n"); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all single digit numbers of base 10 starting from 0, 5 | * followed by a new line, using putchar 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = 48; n < 58; n++) 13 | { 14 | putchar(n); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the lowercase alphabet in reverse, 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char ch; 11 | 12 | for (ch = 'z'; ch >= 'a'; ch--) 13 | { 14 | putchar(ch); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all the numbers of base 16 in lowercase, 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | char ch; 12 | 13 | for (n = 48; n < 58; n++) 14 | { 15 | putchar(n); 16 | } 17 | for (ch = 'a'; ch <= 'f'; ch++) 18 | { 19 | putchar(ch); 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints all possible combinations of single-digit numbers 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int n; 10 | 11 | for (n = 48; n < 58; n++) 12 | { 13 | putchar(n); 14 | if (n != 57) 15 | { 16 | putchar(','); 17 | putchar(' '); 18 | } 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - prints _putchar, followed by a new line 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | _putchar('_'); 10 | _putchar('p'); 11 | _putchar('u'); 12 | _putchar('t'); 13 | _putchar('c'); 14 | _putchar('h'); 15 | _putchar('a'); 16 | _putchar('r'); 17 | _putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - Entry point 5 | * Return:0 6 | */ 7 | 8 | void print_alphabet(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | _putchar(c); 15 | c++; 16 | 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 | * @a: int to be added to b 6 | * @b: int to be added to a 7 | * Return: sum of a and b 8 | */ 9 | int add(int a, int b) 10 | { 11 | int c; 12 | 13 | c = a + b; 14 | 15 | return (c); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the n times table, starting with 0 5 | * @n: number of the times table 6 | */ 7 | void print_times_table(int n) 8 | { 9 | int i, j, k; 10 | 11 | if (n >= 0 && n <= 15) 12 | { 13 | for (i = 0; i <= n; i++) 14 | { 15 | for (j = 0; j <= n; j++) 16 | { 17 | k = j * i; 18 | if (j == 0) 19 | { 20 | _putchar(k + '0'); 21 | } else if (k < 10 && j != 0) 22 | { 23 | _putchar(','); 24 | _putchar(' '); 25 | _putchar(' '); 26 | _putchar(' '); 27 | _putchar(k + '0'); 28 | } else if (k >= 10 && k < 100) 29 | { 30 | _putchar(','); 31 | _putchar(' '); 32 | _putchar(' '); 33 | _putchar((k / 10) + '0'); 34 | _putchar((k % 10) + '0'); 35 | } else if (k >= 100) 36 | { 37 | _putchar(','); 38 | _putchar(' '); 39 | _putchar((k / 100) + '0'); 40 | _putchar(((k / 10) % 10) + '0'); 41 | _putchar((k % 10) + '0'); 42 | } 43 | } 44 | _putchar('\n'); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - computes and prints the sum of all the multiples 5 | * of 3 or 5 below 1024 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int sum3, sum5, sum; 11 | int i; 12 | 13 | sum3 = 0; 14 | sum5 = 0; 15 | sum = 0; 16 | 17 | for (i = 0; i < 1024; ++i) 18 | { 19 | if ((i % 3) == 0) 20 | { 21 | sum3 = sum3 + i; 22 | } else if ((i % 5) == 0) 23 | { 24 | sum5 = sum5 + i; 25 | } 26 | } 27 | sum = sum3 + sum5; 28 | printf("%lu\n", sum); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the first 50 Fibonacci numbers, starting with 1 and 2 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | long int i, j, k, next; 11 | 12 | j = 1; 13 | 14 | k = 2; 15 | 16 | for (i = 1; i <= 50; ++i) 17 | { 18 | if (j != 20365011074) 19 | { 20 | printf("%ld, ", j); 21 | } else 22 | { 23 | printf("%ld\n", j); 24 | } 25 | next = j + k; 26 | j = k; 27 | k = next; 28 | } 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the sum of the even-valued terms 5 | * followed by a new line 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | unsigned long int j, k, next, sum; 12 | 13 | j = 1; 14 | k = 2; 15 | sum = 0; 16 | 17 | for (i = 1; i <= 33; ++i) 18 | { 19 | if (j < 4000000 && (j % 2) == 0) 20 | { 21 | sum = sum + j; 22 | } 23 | next = j + k; 24 | j = k; 25 | k = next; 26 | } 27 | 28 | printf("%lu\n", sum); 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - finds and prints the first 98 Fibonacci numbers, 5 | * starting with 1 and 2 6 | * followed by a new line 7 | * Return: ALways 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int i, j, k, j1, j2, k1, k2; 12 | 13 | j = 1; 14 | k = 2; 15 | 16 | printf("%lu", j); 17 | 18 | for (i = 1; i < 91; i++) 19 | { 20 | printf(", %lu", k); 21 | k = k + j; 22 | j = k - j; 23 | } 24 | 25 | j1 = j / 1000000000; 26 | j2 = j % 1000000000; 27 | k1 = k / 1000000000; 28 | k2 = k % 1000000000; 29 | 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 | 40 | printf("\n"); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - prints all natural numbers from n to 98, 6 | * followed by a new line 7 | * @n: print from this number 8 | */ 9 | void print_to_98(int n) 10 | { 11 | int i, j; 12 | 13 | if (n <= 98) 14 | { 15 | for (i = n; i <= 98; i++) 16 | { 17 | if (i != 98) 18 | printf("%d, ", i); 19 | else if (i == 98) 20 | printf("%d\n", i); 21 | } 22 | } else if (n >= 98) 23 | { 24 | for (j = n; j >= 98; j--) 25 | { 26 | if (j != 98) 27 | printf("%d, ", j); 28 | else if (j == 98) 29 | printf("%d\n", j); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - prints 10 times the alphabet, in lowercase, 5 | * followed by a new line 6 | */ 7 | void print_alphabet_x10(void) 8 | { 9 | char ch; 10 | int i; 11 | 12 | i = 0; 13 | 14 | while (i < 10) 15 | { 16 | ch = 'a'; 17 | while (ch <= 'z') 18 | { 19 | _putchar(ch); 20 | ch++; 21 | } 22 | _putchar('\n'); 23 | i++; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - prints the sign of a number 5 | * @n: the int to check 6 | * Return: 1 and prints + if n is greater than zero 7 | * 0 and prints 0 if n is zero 8 | * -1 and prints - if n is less than zero 9 | */ 10 | int print_sign(int n) 11 | { 12 | if (n > 0) 13 | { 14 | _putchar('+'); 15 | return (1); 16 | } else if (n == 0) 17 | { 18 | _putchar(48); 19 | return (0); 20 | } else if (n < 0) 21 | { 22 | _putchar('-'); 23 | } 24 | return (-1); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/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 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - prints the last digit of a number 5 | * @n: the int to extract the last digit from 6 | * Return: value of the last digit 7 | */ 8 | int print_last_digit(int n) 9 | { 10 | int a; 11 | 12 | if (n < 0) 13 | n = -n; 14 | 15 | a = n % 10; 16 | 17 | if (a < 0) 18 | a = -a; 19 | 20 | _putchar(a + '0'); 21 | 22 | return (a); 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 | * starting from 00:00 to 23:59 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int i, j; 10 | 11 | i = 0; 12 | 13 | while (i < 24) 14 | { 15 | j = 0; 16 | while (j < 60) 17 | { 18 | _putchar((i / 10) + '0'); 19 | _putchar((i % 10) + '0'); 20 | _putchar(':'); 21 | _putchar((j / 10) + '0'); 22 | _putchar((j % 10) + '0'); 23 | _putchar('\n'); 24 | j++; 25 | } 26 | i++; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - prints the 9 times table, starting with 0 5 | */ 6 | void times_table(void) 7 | { 8 | int i, j, k; 9 | 10 | for (i = 0; i < 10; i++) 11 | { 12 | for (j = 0; j < 10; j++) 13 | { 14 | k = j * i; 15 | if (j == 0) 16 | { 17 | _putchar(k + '0'); 18 | } 19 | 20 | if (k < 10 && j != 0) 21 | { 22 | _putchar(','); 23 | _putchar(' '); 24 | _putchar(' '); 25 | _putchar(k + '0'); 26 | } else if (k >= 10) 27 | { 28 | _putchar(','); 29 | _putchar(' '); 30 | _putchar((k / 10) + '0'); 31 | _putchar((k % 10) + '0'); 32 | } 33 | } 34 | _putchar('\n'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int); 8 | int print_last_digit(int); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int, int); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /0x03-debugging/.3-print_remaining_days.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x03-debugging/.3-print_remaining_days.c.swp -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /*while (i < 10) */ 17 | /*{ */ 18 | /* putchar(i);*/ 19 | /*} */ 20 | 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a >= b && a >= c) 16 | { 17 | largest = a; 18 | } 19 | else if (b >= a && b >= c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | 28 | return (largest); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-debugging/3-convert_day.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * convert_day - converts day of month to day of year, without accounting 5 | * for leap year 6 | * @month: month in number format 7 | * @day: day of month 8 | * Return: day of year 9 | */ 10 | 11 | int convert_day(int month, int day) 12 | { 13 | switch (month) 14 | { 15 | case 2: 16 | day = 31 + day; 17 | break; 18 | case 3: 19 | day = 59 + day; 20 | break; 21 | case 4: 22 | day = 90 + day; 23 | break; 24 | case 5: 25 | day = 120 + day; 26 | break; 27 | case 6: 28 | day = 151 + day; 29 | break; 30 | case 7: 31 | day = 181 + day; 32 | break; 33 | case 8: 34 | day = 212 + day; 35 | break; 36 | case 9: 37 | day = 243 + day; 38 | break; 39 | case 10: 40 | day = 273 + day; 41 | break; 42 | case 11: 43 | day = 304 + day; 44 | break; 45 | case 12: 46 | day = 334 + day; 47 | break; 48 | default: 49 | break; 50 | } 51 | return (day); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /0x03-debugging/3-main_a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - takes a date and prints how many days are left in the year, taking 6 | * leap years into account 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int month; 13 | int day; 14 | int year; 15 | 16 | month = 02; 17 | day = 29; 18 | year = 2000; 19 | 20 | printf("Date: %02d/%02d/%04d\n", month, day, year); 21 | 22 | day = convert_day(month, day); 23 | 24 | print_remaining_days(month, day, year); 25 | 26 | return (0); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /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 year 9 | * @year: year 10 | * Return: void 11 | */ 12 | void print_remaining_days(int month, int day, int year) 13 | { 14 | if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) 15 | { 16 | if (month > 2) 17 | day++; 18 | printf("Day of the year: %d\n", day); 19 | printf("Remaining days: %d\n", 366 - day); 20 | } 21 | else 22 | { 23 | if (month == 2 && day == 60) 24 | { 25 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 26 | } 27 | else 28 | { 29 | printf("Day of the year: %d\n", day); 30 | printf("Remaining days: %d\n", 365 - day); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/.8-print_square.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x04-more_functions_nested_loops/.8-print_square.c.swp -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/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 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle, followed by a new line 5 | * @size: size of the triangle 6 | */ 7 | void print_triangle(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 1; i <= size; i++) 17 | { 18 | for (j = i; j < size; j++) 19 | { 20 | _putchar(' '); 21 | } 22 | 23 | for (j = 1; j <= i; j++) 24 | { 25 | _putchar('#'); 26 | } 27 | 28 | _putchar('\n'); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - finds and prints the largest prime factor of the number 612852475143 6 | * followed by a new line 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | long int n; 12 | long int max; 13 | long int i; 14 | 15 | n = 612852475143; 16 | max = -1; 17 | 18 | while (n % 2 == 0) 19 | { 20 | max = 2; 21 | n /= 2; 22 | } 23 | 24 | for (i = 3; i <= sqrt(n); i = i + 2) 25 | { 26 | while (n % i == 0) 27 | { 28 | max = i; 29 | n = n / i; 30 | } 31 | } 32 | 33 | if (n > 2) 34 | max = n; 35 | 36 | printf("%ld\n", max); 37 | 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer 5 | * @n: integer to be printed 6 | */ 7 | 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 | { 22 | print_number(n1 / 10); 23 | } 24 | 25 | _putchar((n1 % 10) + '0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two integers 5 | * @a: int to be multiplied to b 6 | * @b: int to be multiplied to a 7 | * Return: the result of the operation 8 | */ 9 | int mul(int a, int b) 10 | { 11 | int c; 12 | 13 | c = a * b; 14 | 15 | return (c); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints the numbers, from 0 to 9, 5 | * followed by a new line 6 | */ 7 | void print_numbers(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | _putchar(i + '0'); 14 | } 15 | 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints the numbers, from 0 to 9, 5 | * except 2 and 4, followed by a new line 6 | */ 7 | void print_most_numbers(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | if (i != 2 && i != 4) 14 | { 15 | _putchar(i + '0'); 16 | } 17 | } 18 | 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 10 times the numbers, from 0 to 14 5 | * followed by a new line 6 | */ 7 | void more_numbers(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | for (j = 0; j < 15; j++) 14 | { 15 | if (j >= 10) 16 | _putchar(j / 10 + '0'); 17 | _putchar(j % 10 + '0'); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - draws a straight line in the terminal 5 | * @n: number of times the character _ should be printed 6 | */ 7 | void print_line(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i; 15 | 16 | for (i = 1; i <= n; i++) 17 | { 18 | _putchar('_'); 19 | } 20 | _putchar('\n'); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - draws a diagonal line on the terminal 5 | * @n: number of times the character \ should be printed 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | for (j = 0; j < n; j++) 19 | { 20 | if (j == i) 21 | _putchar('\\'); 22 | else if (j < i) 23 | _putchar(' '); 24 | } 25 | _putchar('\n'); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints a square, followed by a new line; 5 | * @size: size of the square 6 | */ 7 | void print_square(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 0; i < size; i++) 17 | { 18 | for (j = 0; j < size; j++) 19 | { 20 | _putchar('#'); 21 | } 22 | _putchar('\n'); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the numbers from 1 to 100, followed by a new line 6 | * but for multiples of three prints Fizz instead of the number 7 | * and for the multiples of five prints Buzz 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 1; i <= 100; i++) 15 | { 16 | if (i % 3 == 0 && i % 5 != 0) 17 | { 18 | printf(" Fizz"); 19 | } else if (i % 5 == 0 && i % 3 != 0) 20 | { 21 | printf(" Buzz"); 22 | } else if (i % 3 == 0 && i % 5 == 0) 23 | { 24 | printf(" FizzBuzz"); 25 | } else if (i == 1) 26 | { 27 | printf("%d", i); 28 | } else 29 | { 30 | printf(" %d", i); 31 | } 32 | } 33 | printf("\n"); 34 | 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | int _isupper(int c); 6 | int _isdigit(int c); 7 | int mul(int a, int b); 8 | void print_numbers(void); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/.0-reset_to_98.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x05-pointers_arrays_strings/.0-reset_to_98.c.swp -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value of the variable 5 | * the pointer points to to 98 6 | * @n: pointer to the variable to update 7 | */ 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swaps the values of two integers 5 | * @a: pointer to first value 6 | * @b: pointer to second value 7 | */ 8 | void swap_int(int *a, int *b) 9 | { 10 | int c; 11 | 12 | c = *a; 13 | *a = *b; 14 | *b = c; 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * 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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - program that generates random valid 7 | * passwords for the program 101-crackme 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(void) 12 | { 13 | int pass[100]; 14 | int i, sum, n; 15 | 16 | sum = 0; 17 | 18 | srand(time(NULL)); 19 | 20 | for (i = 0; i < 100; i++) 21 | { 22 | pass[i] = rand() % 78; 23 | sum += (pass[i] + '0'); 24 | putchar(pass[i] + '0'); 25 | if ((2772 - sum) - '0' < 78) 26 | { 27 | n = 2772 - sum - '0'; 28 | sum += n; 29 | putchar(n + '0'); 30 | break; 31 | } 32 | } 33 | 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string to stdout 5 | * @str: pointer to the string to print 6 | */ 7 | 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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string in reverse, followed by a new line 5 | * @s: string to be printed 6 | */ 7 | void print_rev(char *s) 8 | { 9 | int i, j, len; 10 | 11 | i = 0; 12 | 13 | while (s[i] != '\0') 14 | { 15 | i++; 16 | } 17 | 18 | len = i; 19 | 20 | for (j = len - 1; j >= 0; j--) 21 | { 22 | _putchar(s[j]); 23 | } 24 | 25 | _putchar('\n'); 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverses a string 5 | * @s: string to be reversed 6 | */ 7 | void rev_string(char *s) 8 | { 9 | char tmp; 10 | int i, len, len1; 11 | 12 | len = 0; 13 | len1 = 0; 14 | 15 | while (s[len] != '\0') 16 | { 17 | len++; 18 | } 19 | 20 | len1 = len - 1; 21 | 22 | for (i = 0; i < len / 2; i++) 23 | { 24 | tmp = s[i]; 25 | s[i] = s[len1]; 26 | s[len1--] = tmp; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints one char out of 2 of a string 5 | * followed by a new line 6 | * @str: string to print the chars from 7 | */ 8 | void puts2(char *str) 9 | { 10 | int len, i; 11 | 12 | len = 0; 13 | 14 | while (str[len] != '\0') 15 | { 16 | len++; 17 | } 18 | 19 | for (i = 0; i < len; i += 2) 20 | { 21 | _putchar(str[i]); 22 | } 23 | 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints half of a string 5 | * followed by a new line 6 | * @str: string to be printed 7 | */ 8 | void puts_half(char *str) 9 | { 10 | int len, n, i; 11 | 12 | len = 0; 13 | 14 | while (str[len] != '\0') 15 | { 16 | len++; 17 | } 18 | 19 | if (len % 2 == 0) 20 | { 21 | for (i = len / 2; str[i] != '\0'; i++) 22 | { 23 | _putchar(str[i]); 24 | } 25 | } else if (len % 2) 26 | { 27 | for (n = (len - 1) / 2; n < len - 1; n++) 28 | { 29 | _putchar(str[n + 1]); 30 | } 31 | } 32 | _putchar('\n'); 33 | } 34 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_array - prints n elements of an array of integers 6 | * followed by a new line 7 | * @a: array to be printed 8 | * @n: number of elements to print 9 | */ 10 | void print_array(int *a, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | if (i == 0) 17 | printf("%d", a[i]); 18 | else 19 | printf(", %d", a[i]); 20 | } 21 | printf("\n"); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * including the terminating null byte (\0) 6 | * to the buffer pointed to by dest 7 | * @dest: pointer to the buffer in which we copy the string 8 | * @src: string to be copied 9 | * 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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/_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 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | void reset_to_98(int *n); 6 | void swap_int(int *a, int *b); 7 | int _strlen(char *s); 8 | void _puts(char *str); 9 | void print_rev(char *s); 10 | void rev_string(char *s); 11 | void puts2(char *str); 12 | void puts_half(char *str); 13 | void print_array(int *a, int n); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concatenates two strings 5 | * @dest: 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 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenates n bytes from a string to another 5 | * @dest: destination string 6 | * @src: source string 7 | * @n: number of bytes of str to concatenate 8 | * 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 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encodes a string in rot13 5 | * @s: string to be encoded 6 | * 7 | * Return: the resulting string 8 | */ 9 | char *rot13(char *s) 10 | { 11 | int i, j; 12 | 13 | char a[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 14 | char b[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | for (j = 0; a[j] != '\0'; j++) 19 | { 20 | if (s[i] == a[j]) 21 | { 22 | s[i] = b[j]; 23 | break; 24 | } 25 | } 26 | } 27 | 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer; 5 | * @n: 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/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_line - prints a s bytes of a buffer 5 | * @c: buffer to print 6 | * @s: bytes of buffer to print 7 | * @l: line of buffer to print 8 | * 9 | * Return: void 10 | */ 11 | 12 | void print_line(char *c, int s, int l) 13 | { 14 | int j, k; 15 | for (j = 0; j <= 9; j++) 16 | { 17 | if (j <= s) 18 | printf("%02x", c[l * 10 + j]); 19 | else 20 | printf(" "); 21 | if (j % 2) 22 | putchar(' '); 23 | } 24 | for (k = 0; k <= s; k++) 25 | { 26 | if (c[l * 10 + k] > 31 && c[l * 10 + k] < 127) 27 | putchar(c[l * 10 + k]); 28 | else 29 | putchar('.'); 30 | } 31 | } 32 | 33 | /** 34 | * print_buffer - prints a buffer 35 | * @b: buffer to print 36 | * @size: size of buffer 37 | * 38 | * Return: void 39 | */ 40 | void print_buffer(char *b, int size) 41 | { 42 | int i; 43 | for (i = 0; i <= (size - 1) / 10 && size; i++) 44 | { 45 | printf("%08x: ", i * 10); 46 | if (i < size / 10) 47 | { 48 | print_line(b, 9, i); 49 | } 50 | else 51 | { 52 | print_line(b, size % 10 - 1, i); 53 | } 54 | putchar('\n'); 55 | } 56 | if (size == 0) 57 | putchar('\n'); 58 | } 59 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/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 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/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 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - reverses an array of integers 5 | * @a: array to be reversed 6 | * @n: number of elements in the array 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i, j, tmp; 11 | 12 | j = n - 1; 13 | 14 | for (i = 0; i < n / 2; i++) 15 | { 16 | tmp = a[i]; 17 | a[i] = a[j]; 18 | a[j--] = tmp; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - changes all lowercase letters of a string 5 | * to uppercase 6 | * @s: string to modify 7 | * 8 | * Return: the resulting string 9 | */ 10 | char *string_toupper(char *s) 11 | { 12 | int i; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | { 16 | if (s[i] >= 'a' && s[i] <= 'z') 17 | s[i] = s[i] - 32; 18 | } 19 | 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes everey word of a string 5 | * @s: string to modify 6 | * 7 | * Return: the resulting string 8 | */ 9 | char *cap_string(char *s) 10 | { 11 | int i, j; 12 | 13 | char spe[13] = {' ', '\t', '\n', ',', ';', '.', 14 | '!', '?', '"', '(', ')', '{', '}'}; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | if (i == 0 && s[i] >= 'a' && s[i] <= 'z') 19 | s[i] -= 32; 20 | 21 | for (j = 0; j < 13; j++) 22 | { 23 | if (s[i] == spe[j]) 24 | { 25 | if (s[i + 1] >= 'a' && s[i + 1] <= 'z') 26 | { 27 | s[i + 1] -= 32; 28 | } 29 | } 30 | } 31 | } 32 | 33 | return (s); 34 | } 35 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - encodes a string in 1337 5 | * @s: string to be encoded 6 | * 7 | * Return: the resulting string; 8 | */ 9 | char *leet(char *s) 10 | { 11 | int i, j; 12 | 13 | char *a = "aAeEoOtTlL"; 14 | char *b = "4433007711"; 15 | 16 | for (i = 0; s[i] != '\0'; i++) 17 | { 18 | for (j = 0; j < 10; j++) 19 | { 20 | if (s[i] == a[j]) 21 | { 22 | s[i] = b[j]; 23 | } 24 | } 25 | } 26 | 27 | return (s); 28 | } 29 | -------------------------------------------------------------------------------- /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/_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 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 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 | char *rot13(char *); 14 | void print_number(int n); 15 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/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 | -------------------------------------------------------------------------------- /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: pointer to pointer we need to set to 6 | * @to: string to set 7 | */ 8 | void set_string(char **s, char *to) 9 | { 10 | *s = to; 11 | } 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strspn - gets the length of a prefix substring 5 | * @s: string 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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chessboard 5 | * @a: two dimension array to print 6 | */ 7 | void print_chessboard(char (*a)[8]) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 8; i++) 12 | { 13 | for (j = 0; j < 8; j++) 14 | { 15 | _putchar(a[i][j]); 16 | } 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_diagsums - prints the sum of the two diagonals 6 | * of a square matrix of integers 7 | * @a: square matrix of which we print the sum of diagonals 8 | * @size: size of the matrix 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i; 13 | 14 | unsigned int sum, sum1; 15 | 16 | sum = 0; 17 | sum1 = 0; 18 | 19 | for (i = 0; i < size; i++) 20 | { 21 | sum += a[(size * i) + i]; 22 | sum1 += a[(size * (i + 1)) - (i + 1)]; 23 | } 24 | 25 | printf("%d, %d\n", sum, sum1); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/_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 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | char *_memset(char *s, char b, unsigned int n); 6 | char *_memcpy(char *dest, char *src, unsigned int n); 7 | char *_strchr(char *s, char c); 8 | unsigned int _strspn(char *s, char *accept); 9 | char *_strpbrk(char *s, char *accept); 10 | char *_strstr(char *haystack, char *needle); 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 | File 0-puts_recursion.c is a function that prints a string, followed by a new line. 2 | 3 | File 1-print_rev_recursion.c is a function that prints a string in reverse. 4 | 5 | File 2-strlen_recursion.c is a function that returns the length of a string. 6 | 7 | File 3-factorial.c is a function that returns the factorial of a given number. 8 | 9 | File 4-pow_recursion.c is a function that returns the value of x raised to the power of y. 10 | 11 | File 5-sqrt_recursion.c is a function that returns the natural square root of a number. 12 | 13 | File 6-is_prime_number.c is a function that returns 1 if the input integer is a prime number, otherwise return 0. 14 | 15 | File 100-is_palindrome.c is a function that returns 1 if a string is a palindrome and 0 if not. 16 | 17 | 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. 18 | 19 | File _putchar.c is a file containing the _putchar function. 20 | 21 | File main.h is the header file containing the function prototypes. 22 | -------------------------------------------------------------------------------- /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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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 MAIN_H 2 | #define MAIN_H 3 | 4 | 5 | int _putchar(char c); 6 | int _islower(int c); 7 | int _isalpha(int c); 8 | int _abs(int n); 9 | int _isupper(int c); 10 | int _isdigit(int c); 11 | int _strlen(char *s); 12 | void _puts(char *s); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | char *_strcat(char *dest, char *src); 16 | char *_strncat(char *dest, char *src, int n); 17 | char *_strncpy(char *dest, char *src, int n); 18 | int _strcmp(char *s1, char *s2); 19 | char *_memset(char *s, char b, unsigned int n); 20 | char *_memcpy(char *dest, char *src, unsigned int n); 21 | char *_strchr(char *s, char c); 22 | unsigned int _strspn(char *s, char *accept); 23 | char *_strpbrk(char *s, char *accept); 24 | char *_strstr(char *haystack, char *needle); 25 | 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x09-static_libraries/main.o -------------------------------------------------------------------------------- /0x09-static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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 | File 0-whatsmyname.c is a program that prints its name, followed by a new line. 2 | 3 | File 1-args.c is a program that prints the number of arguments passed into it. 4 | 5 | File 2-args.c is a program that prints all arguments it receives. 6 | 7 | File 3-mul.c is a program that multiplies two numbers. 8 | 9 | File 4-add.c is a program that adds positive numbers. 10 | -------------------------------------------------------------------------------- /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 | File 0-malloc_checked.c is a function that allocates memory using malloc. 2 | 3 | File 1-string_nconcat.c is a function that concatenates two strings. 4 | 5 | File 2-calloc.c is a function that allocates memory for an array, using malloc. 6 | 7 | File 3-array_range.c is a function that creates an array of integers. 8 | 9 | File 100-realloc.c is a function that reallocates a memory block using malloc and free. 10 | 11 | File 101-mul.c is a program that multiplies two positive numbers. 12 | 13 | File main.h is the header file for all the function prototypes.. 14 | -------------------------------------------------------------------------------- /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 | File 0-object_like_macro.h is a header file that defines a macro named SIZE as an abbreviation for the token 1024. 2 | 3 | File 1-pi.h is a header file that defines a macro named PI as an abbreviation for the token 3.14159265359. 4 | 5 | File 2-main.c is a program that prints the name of the file it was compiled from, followed by a new line. 6 | 7 | File 3-function_like_macro.h is a function-like macro ABS(x) that computes the absolute value of a number x. 8 | 9 | File 4-sum.h is a function-like macro SUM(x, y) that computes the sum of the numbers x and y. 10 | -------------------------------------------------------------------------------- /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 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * print_dog - prints a struct dog 7 | * @d: struct dog to print 8 | */ 9 | void print_dog(struct dog *d) 10 | { 11 | if (d == NULL) 12 | return; 13 | 14 | if (d->name == NULL) 15 | d->name = "(nil)"; 16 | if (d->owner == NULL) 17 | d->owner = "(nil)"; 18 | 19 | printf("Name: %s\nAge: %f\nOwner: %s\n", d->name, d->age, d->owner); 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 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 | File dog.h is a header file containing a new type "struct dog". 2 | 3 | File 1-init_dog.c a function that initialize a variable of type struct dog. 4 | 5 | File 2-print_dog.c is a function that prints a struct dog. 6 | 7 | File 4-new_dog.c is a function that creates a new dog. 8 | 9 | File 5-free_dog.c is a function that frees dogs. 10 | -------------------------------------------------------------------------------- /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.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x0F-function_pointers/.0-print_name.c.swp -------------------------------------------------------------------------------- /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/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 | #include 7 | 8 | /** 9 | * struct op - Struct op 10 | * 11 | * @op: The operator 12 | * @f: The function associated 13 | */ 14 | typedef struct op 15 | { 16 | char *op; 17 | int (*f)(int a, int b); 18 | } op_t; 19 | 20 | int op_add(int a, int b); 21 | int op_sub(int a, int b); 22 | int op_mul(int a, int b); 23 | int op_div(int a, int b); 24 | int op_mod(int a, int b); 25 | int (*get_op_func(char *s))(int, int); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * get_op_func - function pointer that selects the correct function to perform 5 | * the operation asked by the user 6 | * @s: the operator given by the user 7 | * 8 | * Return: pointer to the function that corresponds to the 9 | * operator given as a parameter 10 | */ 11 | int (*get_op_func(char *s))(int, int) 12 | { 13 | op_t ops[] = { 14 | {"+", op_add}, 15 | {"-", op_sub}, 16 | {"*", op_mul}, 17 | {"/", op_div}, 18 | {"%", op_mod}, 19 | {NULL, NULL} 20 | }; 21 | int i; 22 | 23 | i = 0; 24 | 25 | while (ops[i].op) 26 | { 27 | if (strcmp(ops[i].op, s) == 0) 28 | return (ops[i].f); 29 | i++; 30 | } 31 | 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * main - program that perfroms simple operations 5 | * @argc: number of arguments 6 | * @argv: array of arguments 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int arg1, arg2, result; 13 | char o; 14 | int (*func)(int, int); 15 | 16 | if (argc != 4) 17 | { 18 | printf("Error\n"); 19 | exit(98); 20 | } 21 | 22 | arg1 = atoi(argv[1]); 23 | arg2 = atoi(argv[3]); 24 | 25 | func = get_op_func(argv[2]); 26 | 27 | if (!func) 28 | { 29 | printf("Error\n"); 30 | exit(99); 31 | } 32 | 33 | o = *argv[2]; 34 | 35 | if ((o == '/' || o == '%') && arg2 == 0) 36 | { 37 | printf("Error\n"); 38 | exit(100); 39 | } 40 | 41 | result = func(arg1, arg2); 42 | 43 | printf("%d\n", result); 44 | 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * op_add - calculates the sum of two integers 5 | * @a: first integer 6 | * @b: second integer 7 | * 8 | * Return: sum of a and b 9 | */ 10 | int op_add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | 15 | /** 16 | * op_sub - calculates the difference of two integers 17 | * @a: first integer 18 | * @b: second integer 19 | * 20 | * Return: difference of a and b 21 | */ 22 | int op_sub(int a, int b) 23 | { 24 | return (a - b); 25 | } 26 | 27 | /** 28 | * op_mul - calculates the product of two integers 29 | * @a: first integer 30 | * @b: second integer 31 | * 32 | * Return: product of a and b 33 | */ 34 | int op_mul(int a, int b) 35 | { 36 | return (a * b); 37 | } 38 | 39 | /** 40 | * op_div - calculates the division of two integers 41 | * @a: first integer 42 | * @b: second integer 43 | * 44 | * Return: result of the division of a and b 45 | */ 46 | int op_div(int a, int b) 47 | { 48 | return (a / b); 49 | } 50 | 51 | /** 52 | * op_mod - calculates the remainder of the division of two integers 53 | * @a: first integer 54 | * @b: second integer 55 | * 56 | * Return: remainder of a divided b 57 | */ 58 | int op_mod(int a, int b) 59 | { 60 | return (a % b); 61 | } 62 | -------------------------------------------------------------------------------- /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/README.md: -------------------------------------------------------------------------------- 1 | File 0-sum_them_all.c is a function that returns the sum of all its parameters. 2 | 3 | File 1-print_numbers.c is a function that prints numbers, followed by a new line. 4 | 5 | File 2-print_strings.c is a function that prints strings, followed by a new line. 6 | 7 | File 3-print_all.c is a function that prints anything. 8 | 9 | File variadic_functions.h is the header file that contains all the function prototypes. 10 | -------------------------------------------------------------------------------- /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 2 | #include "lists.h" 3 | 4 | /** 5 | * print_list - prints all the elements of a linked list 6 | * @h: pointer to the list_t list to print 7 | * 8 | * Return: the number of nodes printed 9 | */ 10 | size_t print_list(const list_t *h) 11 | { 12 | size_t s = 0; 13 | 14 | while (h) 15 | { 16 | if (!h->str) 17 | printf("[0] (nil)\n"); 18 | else 19 | printf("[%u] %s\n", h->len, h->str); 20 | h = h->next; 21 | s++; 22 | } 23 | 24 | return (s); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * list_len - returns the number of elements in a linked list 6 | * @h: pointer to the list_t list 7 | * 8 | * Return: number of elements in h 9 | */ 10 | size_t list_len(const list_t *h) 11 | { 12 | size_t n = 0; 13 | 14 | while (h) 15 | { 16 | n++; 17 | h = h->next; 18 | } 19 | return (n); 20 | } 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void first(void) __attribute__ ((constructor)); 4 | 5 | /** 6 | * first - prints a sentence before the main 7 | * function is executed 8 | */ 9 | void first(void) 10 | { 11 | printf("You're beat! and yet, you must allow,\n"); 12 | printf("I bore my house upon my back!\n"); 13 | } 14 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | global main 2 | extern printf 3 | main: 4 | mov edi, format 5 | xor eax, eax 6 | call printf 7 | mov eax, 0 8 | ret 9 | format: db `Hello, Holberton\n`,0 10 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * add_node - adds a new node at the beginning of a linked list 7 | * @head: double pointer to the list_t list 8 | * @str: new string to add in the node 9 | * 10 | * Return: the address of the new element, or NULL if it fails 11 | */ 12 | list_t *add_node(list_t **head, const char *str) 13 | { 14 | list_t *new; 15 | unsigned int len = 0; 16 | 17 | while (str[len]) 18 | len++; 19 | 20 | new = malloc(sizeof(list_t)); 21 | if (!new) 22 | return (NULL); 23 | 24 | new->str = strdup(str); 25 | new->len = len; 26 | new->next = (*head); 27 | (*head) = new; 28 | 29 | return (*head); 30 | } 31 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * add_node_end - adds a new node at the end of a linked list 7 | * @head: double pointer to the list_t list 8 | * @str: string to put in the new node 9 | * 10 | * Return: address of the new element, or NULL if it failed 11 | */ 12 | list_t *add_node_end(list_t **head, const char *str) 13 | { 14 | list_t *new; 15 | list_t *temp = *head; 16 | unsigned int len = 0; 17 | 18 | while (str[len]) 19 | len++; 20 | 21 | new = malloc(sizeof(list_t)); 22 | if (!new) 23 | return (NULL); 24 | 25 | new->str = strdup(str); 26 | new->len = len; 27 | new->next = NULL; 28 | 29 | if (*head == NULL) 30 | { 31 | *head = new; 32 | return (new); 33 | } 34 | 35 | while (temp->next) 36 | temp = temp->next; 37 | 38 | temp->next = new; 39 | 40 | return (new); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * free_list - frees a linked list 6 | * @head: list_t list to be freed 7 | */ 8 | void free_list(list_t *head) 9 | { 10 | list_t *temp; 11 | 12 | while (head) 13 | { 14 | temp = head->next; 15 | free(head->str); 16 | free(head); 17 | head = temp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | File 0-print_list.c is a function that prints all the elements of a list_t list. 2 | 3 | File 1-list_len.c is a a function that returns the number of elements in a linked list_t list. 4 | 5 | File 2-add_node.c is a function that adds a new node at the beginning of a list_t list. 6 | 7 | File 3-add_node_end.c is a function that adds a new node at the end of a list_t list. 8 | 9 | File 4-free_list.c is a function that frees a list_t list. 10 | 11 | File 100-first.c is a function that prints a sentence before the main function is executed. 12 | 13 | File 101-hello_holberton.asm is 64-bit program in assembly that prints "Hello, Holberton", followed by a new line. 14 | 15 | File lists.h is the header file containing the definition of the list_t struct and all the prototypes of the above functions. 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | /** 5 | * struct list_s - singly linked list 6 | * @str: string - (malloc'ed string) 7 | * @len: length of the string 8 | * @next: points to the next node 9 | * 10 | * Description: singly linked list node structure 11 | * for Holberton project 12 | */ 13 | typedef struct list_s 14 | { 15 | char *str; 16 | unsigned int len; 17 | struct list_s *next; 18 | } list_t; 19 | 20 | size_t print_list(const list_t *h); 21 | size_t list_len(const list_t *h); 22 | list_t *add_node(list_t **head, const char *str); 23 | list_t *add_node_end(list_t **head, const char *str); 24 | void free_list(list_t *head); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the elements of a linked list 5 | * @h: linked list of type listint_t to print 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t num = 0; 12 | 13 | while (h) 14 | { 15 | printf("%d\n", h->n); 16 | num++; 17 | h = h->next; 18 | } 19 | 20 | return (num); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns the number of elements in a linked lists 5 | * @h: linked list of type listint_t to traverse 6 | * 7 | * Return: number of nodes 8 | */ 9 | size_t listint_len(const listint_t *h) 10 | { 11 | size_t num = 0; 12 | 13 | while (h) 14 | { 15 | num++; 16 | h = h->next; 17 | } 18 | 19 | return (num); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes a node in a linked list at a certain index 5 | * @head: pointer to the first element in the list 6 | * @index: index of the node to delete 7 | * 8 | * Return: 1 (Success), or -1 (Fail) 9 | */ 10 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 11 | { 12 | listint_t *temp = *head; 13 | listint_t *current = NULL; 14 | unsigned int i = 0; 15 | 16 | if (*head == NULL) 17 | return (-1); 18 | 19 | if (index == 0) 20 | { 21 | *head = (*head)->next; 22 | free(temp); 23 | return (1); 24 | } 25 | 26 | while (i < index - 1) 27 | { 28 | if (!temp || !(temp->next)) 29 | return (-1); 30 | temp = temp->next; 31 | i++; 32 | } 33 | 34 | 35 | current = temp->next; 36 | temp->next = current->next; 37 | free(current); 38 | 39 | return (1); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list 5 | * @head: pointer to the first node in the list 6 | * 7 | * Return: pointer to the first node in the new list 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *prev = NULL; 12 | listint_t *next = NULL; 13 | 14 | while (*head) 15 | { 16 | next = (*head)->next; 17 | (*head)->next = prev; 18 | prev = *head; 19 | *head = next; 20 | } 21 | 22 | *head = prev; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_listint_safe - function that prints a linked list with a loop safely. 4 | * @head: pointer to the 1st node of the linked list 5 | * Return: new_node 6 | */ 7 | size_t print_listint_safe(const listint_t *head) 8 | { 9 | const listint_t *tmp_n = NULL; 10 | const listint_t *l_n = NULL; 11 | size_t counter = 0; 12 | size_t new_n; 13 | 14 | tmp_n = head; 15 | while (tmp_n) 16 | { 17 | printf("[%p] %d\n", (void *)tmp_n, tmp_n->n); 18 | counter++; 19 | tmp_n = tmp_n->next; 20 | l_n = head; 21 | new_n = 0; 22 | while (new_n < counter) 23 | { 24 | if (tmp_n == l_n) 25 | { 26 | printf("-> [%p] %d\n", (void *)tmp_n, tmp_n->n); 27 | return (counter); 28 | } 29 | l_n = l_n->next; 30 | new_n++; 31 | } 32 | if (!head) 33 | exit(98); 34 | } 35 | return (counter); 36 | } 37 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint_safe - frees a linked list 5 | * @h: pointer to the first node in the linked list 6 | * 7 | * Return: number of elements in the freed list 8 | */ 9 | size_t free_listint_safe(listint_t **h) 10 | { 11 | size_t len = 0; 12 | int diff; 13 | listint_t *temp; 14 | 15 | if (!h || !*h) 16 | return (0); 17 | 18 | while (*h) 19 | { 20 | diff = *h - (*h)->next; 21 | if (diff > 0) 22 | { 23 | temp = (*h)->next; 24 | free(*h); 25 | *h = temp; 26 | len++; 27 | } 28 | else 29 | { 30 | free(*h); 31 | *h = NULL; 32 | len++; 33 | break; 34 | } 35 | } 36 | 37 | *h = NULL; 38 | 39 | return (len); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - finds the loop in a linked list 5 | * @head: linked list to search for 6 | * 7 | * Return: address of the node where the loop starts, or NULL 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *slow = head; 12 | listint_t *fast = head; 13 | 14 | if (!head) 15 | return (NULL); 16 | 17 | while (slow && fast && fast->next) 18 | { 19 | fast = fast->next->next; 20 | slow = slow->next; 21 | if (fast == slow) 22 | { 23 | slow = head; 24 | while (slow != fast) 25 | { 26 | slow = slow->next; 27 | fast = fast->next; 28 | } 29 | return (fast); 30 | } 31 | } 32 | 33 | return (NULL); 34 | } 35 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - adds a new node at the beginning of a linked list 5 | * @head: pointer to the first node in the list 6 | * @n: data to insert in that new node 7 | * 8 | * Return: pointer to the new node, or NULL if it fails 9 | */ 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | listint_t *new; 13 | 14 | new = malloc(sizeof(listint_t)); 15 | if (!new) 16 | return (NULL); 17 | 18 | new->n = n; 19 | new->next = *head; 20 | *head = new; 21 | 22 | return (new); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - adds a node at the end of a linked list 5 | * @head: pointer to the first element in the list 6 | * @n: data to insert in the new element 7 | * 8 | * Return: pointer to the new node, or NULL if it fails 9 | */ 10 | listint_t *add_nodeint_end(listint_t **head, const int n) 11 | { 12 | listint_t *new; 13 | listint_t *temp = *head; 14 | 15 | new = malloc(sizeof(listint_t)); 16 | if (!new) 17 | return (NULL); 18 | 19 | new->n = n; 20 | new->next = NULL; 21 | 22 | if (*head == NULL) 23 | { 24 | *head = new; 25 | return (new); 26 | } 27 | 28 | while (temp->next) 29 | temp = temp->next; 30 | 31 | temp->next = new; 32 | 33 | return (new); 34 | } 35 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a linked list 5 | * @head: listint_t list to be freed 6 | */ 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *temp; 10 | 11 | while (head) 12 | { 13 | temp = head->next; 14 | free(head); 15 | head = temp; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a linked list 5 | * @head: pointer to the listint_t list to be freed 6 | */ 7 | void free_listint2(listint_t **head) 8 | { 9 | listint_t *temp; 10 | 11 | if (head == NULL) 12 | return; 13 | 14 | while (*head) 15 | { 16 | temp = (*head)->next; 17 | free(*head); 18 | *head = temp; 19 | } 20 | 21 | *head = NULL; 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes the head node of a linked list 5 | * @head: pointer to the first element in the linked list 6 | * 7 | * Return: the data inside the elements that was deleted, 8 | * or 0 if the list is empty 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | listint_t *temp; 13 | int num; 14 | 15 | if (!head || !*head) 16 | return (0); 17 | 18 | num = (*head)->n; 19 | temp = (*head)->next; 20 | free(*head); 21 | *head = temp; 22 | 23 | return (num); 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the node at a certain index in a linked list 5 | * @head: first node in the linked list 6 | * @index: index of the node to return 7 | * 8 | * Return: pointer to the node we're looking for, or NULL 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i = 0; 13 | listint_t *temp = head; 14 | 15 | while (temp && i < index) 16 | { 17 | temp = temp->next; 18 | i++; 19 | } 20 | 21 | return (temp ? temp : NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - calculates the sum of all the data in a listint_t list 5 | * @head: first node in the linked list 6 | * 7 | * Return: resulting sum 8 | */ 9 | int sum_listint(listint_t *head) 10 | { 11 | int sum = 0; 12 | listint_t *temp = head; 13 | 14 | while (temp) 15 | { 16 | sum += temp->n; 17 | temp = temp->next; 18 | } 19 | 20 | return (sum); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a new node in a linked list, 5 | * at a given position 6 | * @head: pointer to the first node in the list 7 | * @idx: index where the new node is added 8 | * @n: data to insert in the new node 9 | * 10 | * Return: pointer to the new node, or NULL 11 | */ 12 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 13 | { 14 | unsigned int i; 15 | listint_t *new; 16 | listint_t *temp = *head; 17 | 18 | new = malloc(sizeof(listint_t)); 19 | if (!new || !head) 20 | return (NULL); 21 | 22 | new->n = n; 23 | new->next = NULL; 24 | 25 | if (idx == 0) 26 | { 27 | new->next = *head; 28 | *head = new; 29 | return (new); 30 | } 31 | 32 | for (i = 0; temp && i < idx; i++) 33 | { 34 | if (i == idx - 1) 35 | { 36 | new->next = temp->next; 37 | temp->next = new; 38 | return (new); 39 | } 40 | else 41 | temp = temp->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 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 | size_t print_listint(const listint_t *h); 22 | size_t listint_len(const listint_t *h); 23 | listint_t *add_nodeint(listint_t **head, const int n); 24 | listint_t *add_nodeint_end(listint_t **head, const int n); 25 | void free_listint(listint_t *head); 26 | void free_listint2(listint_t **head); 27 | int pop_listint(listint_t **head); 28 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 29 | int sum_listint(listint_t *head); 30 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 31 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 32 | listint_t *reverse_listint(listint_t **head); 33 | size_t print_listint_safe(const listint_t *head); 34 | size_t free_listint_safe(listint_t **h); 35 | listint_t *find_listint_loop(listint_t *head); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * binary_to_uint - convert a binary number to an unsigned int 6 | * @b: char string 7 | * Return: converted decimal number or 0 if there is an unconvertable char 8 | */ 9 | unsigned int binary_to_uint(const char *b) 10 | { 11 | unsigned int total, power; 12 | int len; 13 | 14 | if (b == NULL) 15 | return (0); 16 | 17 | for (len = 0; b[len]; len++) 18 | { 19 | if (b[len] != '0' && b[len] != '1') 20 | return (0); 21 | } 22 | 23 | for (power = 1, total = 0, len--; len >= 0; len--, power *= 2) 24 | { 25 | if (b[len] == '1') 26 | total += power; 27 | } 28 | 29 | return (total); 30 | } 31 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_binary - print binary representation of a number 6 | * @n: decimal number to print as binary 7 | */ 8 | void print_binary(unsigned long int n) 9 | { 10 | unsigned long int temp; 11 | int shifts; 12 | 13 | if (n == 0) 14 | { 15 | printf("0"); 16 | return; 17 | } 18 | 19 | for (temp = n, shifts = 0; (temp >>= 1) > 0; shifts++) 20 | ; 21 | 22 | for (; shifts >= 0; shifts--) 23 | { 24 | if ((n >> shifts) & 1) 25 | printf("1"); 26 | else 27 | printf("0"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - check endianness 5 | * Return: 0 if big endian, or 1 if little endian 6 | */ 7 | int get_endianness(void) 8 | { 9 | int num; 10 | 11 | num = 1; 12 | if (*(char *)&num == 1) 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * get_bit - get the value of a bit at a given index 5 | * @n: number to evaluate 6 | * @index: index starting from 0, of the bit we want to get 7 | * Return: Value of bit at index, or -1 if error 8 | */ 9 | int get_bit(unsigned long int n, unsigned int index) 10 | { 11 | unsigned long int hold; 12 | 13 | if (index > 64) 14 | return (-1); 15 | 16 | hold = n >> index; 17 | 18 | return (hold & 1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets value of a bit to 1 at a given index 5 | * @n: decimal number passed by pointer 6 | * @index: index position to change, starting from 0 7 | * Return: 1 if it worked, -1 if error 8 | */ 9 | int set_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned long int p; 12 | 13 | if (index > 64) 14 | return (-1); 15 | 16 | for (p = 1; index > 0; index--, p *= 2) 17 | ; 18 | *n += p; 19 | 20 | return (1); 21 | } 22 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * clear_bit - set the value of a bit to 0 at a given index 5 | * @n: pointer to decimal number to change 6 | * @index: index position to change 7 | * Return: 1 if it worked, -1 if error 8 | */ 9 | int clear_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned long int i; 12 | unsigned int hold; 13 | 14 | if (index > 64) 15 | return (-1); 16 | hold = index; 17 | for (i = 1; hold > 0; i *= 2, hold--) 18 | ; 19 | 20 | if ((*n >> index) & 1) 21 | *n -= i; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * flip_bits - flip bits to convert one number to another number 6 | * @n: first number 7 | * @m: second number to convert to 8 | * Return: number of bits that was needed to flip 9 | */ 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 11 | { 12 | unsigned long int diff; 13 | int counter; 14 | 15 | diff = n ^ m; 16 | counter = 0; 17 | 18 | while (diff) 19 | { 20 | counter++; 21 | diff &= (diff - 1); 22 | } 23 | 24 | return (counter); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | 0x14. C - Bit manipulation.... 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/_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 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_H 3 | int _putchar(char c); 4 | unsigned int binary_to_uint(const char *b); 5 | void print_binary(unsigned long int n); 6 | int get_bit(unsigned long int n, unsigned int index); 7 | int set_bit(unsigned long int *n, unsigned int index); 8 | int clear_bit(unsigned long int *n, unsigned int index); 9 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 10 | int get_endianness(void); 11 | #endif 12 | -------------------------------------------------------------------------------- /0x15-file_io/.100-elf_header.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x15-file_io/.100-elf_header.c.swp -------------------------------------------------------------------------------- /0x15-file_io/.main.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x15-file_io/.main.h.swp -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * read_textfile - Reads a text file and prints it to POSIX stdout. 6 | * @filename: A pointer to the name of the file. 7 | * @letters: The number of letters the 8 | * function should read and print. 9 | * 10 | * Return: If the function fails or filename is NULL - 0. 11 | * O/w - the actual number of bytes the function can read and print. 12 | */ 13 | ssize_t read_textfile(const char *filename, size_t letters) 14 | { 15 | ssize_t o, r, w; 16 | char *buffer; 17 | 18 | if (filename == NULL) 19 | return (0); 20 | 21 | buffer = malloc(sizeof(char) * letters); 22 | if (buffer == NULL) 23 | return (0); 24 | 25 | o = open(filename, O_RDONLY); 26 | r = read(o, buffer, letters); 27 | w = write(STDOUT_FILENO, buffer, r); 28 | 29 | if (o == -1 || r == -1 || w == -1 || w != r) 30 | { 31 | free(buffer); 32 | return (0); 33 | } 34 | 35 | free(buffer); 36 | close(o); 37 | 38 | return (w); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - Creates a file. 5 | * @filename: A pointer to the name of the file to create. 6 | * @text_content: A pointer to a string to write to the file. 7 | * 8 | * Return: If the function fails - -1. 9 | * Otherwise - 1. 10 | */ 11 | int create_file(const char *filename, char *text_content) 12 | { 13 | int o, w, len = 0; 14 | 15 | if (filename == NULL) 16 | return (-1); 17 | 18 | if (text_content != NULL) 19 | { 20 | for (len = 0; text_content[len];) 21 | len++; 22 | } 23 | 24 | o = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600); 25 | w = write(o, text_content, len); 26 | 27 | if (o == -1 || w == -1) 28 | return (-1); 29 | 30 | close(o); 31 | 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - Appends text at the end of a file. 5 | * @filename: A pointer to the name of the file. 6 | * @text_content: The string to add to the end of the file. 7 | * 8 | * Return: If the function fails or filename is NULL - -1. 9 | * If the file does not exist the user lacks write permissions - -1. 10 | * Otherwise - 1. 11 | */ 12 | int append_text_to_file(const char *filename, char *text_content) 13 | { 14 | int o, w, len = 0; 15 | 16 | if (filename == NULL) 17 | return (-1); 18 | 19 | if (text_content != NULL) 20 | { 21 | for (len = 0; text_content[len];) 22 | len++; 23 | } 24 | 25 | o = open(filename, O_WRONLY | O_APPEND); 26 | w = write(o, text_content, len); 27 | 28 | if (o == -1 || w == -1) 29 | return (-1); 30 | 31 | close(o); 32 | 33 | return (1); 34 | } 35 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | # 0x15. C - File 2 | ## Resources: 3 | ### Read or watch: 4 | 5 | [File descriptors](https://en.wikipedia.org/wiki/File_descriptor) 6 | 7 | 8 | [C Programming in Linux Tutorial #024 - open() read() write() Functions](https://www.youtube.com/watch?v=dP3N8g7h8gY) 9 | 10 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /* 5 | * File: holberton.h 6 | * Auth: Godswill Kalu 7 | * Desc: Header file containing prototypes for all functions 8 | * written in the 0x14-file_io directory. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | ssize_t read_textfile(const char *filename, size_t letters); 17 | int create_file(const char *filename, char *text_content); 18 | int append_text_to_file(const char *filename, char *text_content); 19 | 20 | #endif /* MAIN_H */ 21 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /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/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/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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/0-memset.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -fPIC -c *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/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/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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/3-islower.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/3-puts.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/6-abs.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x18-dynamic_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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/_putchar.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/liball.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/liball.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/libmy.a -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/main.o -------------------------------------------------------------------------------- /0x18-dynamic_libraries/operations.c: -------------------------------------------------------------------------------- 1 | int add(int i, int k); 2 | int sub(int i, int k); 3 | int mul(int i, int k); 4 | int div(int i, int k); 5 | int mod(int i, int k); 6 | 7 | /** 8 | * add - adds two integers 9 | * @i: first integer to add 10 | * @k: second integer to add 11 | * Return: the sum 12 | */ 13 | 14 | int add(int i, int k) 15 | { 16 | return (i + k); 17 | } 18 | 19 | /** 20 | * sub - subtracts two integers 21 | * @i: first integer to add 22 | * @k: second integer to add 23 | * Return: the subtract 24 | */ 25 | 26 | int sub(int i, int k) 27 | { 28 | return (i - k); 29 | } 30 | 31 | /** 32 | * mul - multiplies two integers 33 | * @i: first integer to add 34 | * @k: second integer to add 35 | * Return: the multiple 36 | */ 37 | 38 | int mul(int i, int k) 39 | { 40 | return (i * k); 41 | } 42 | 43 | /** 44 | * div - divideds two integers 45 | * @i: first integer to add 46 | * @k: second integer to add 47 | * Return: the dividend integer 48 | */ 49 | 50 | int div(int i, int k) 51 | { 52 | return (i / k); 53 | } 54 | 55 | /** 56 | mod - finds the modulus of two integers 57 | @i: first integer to add 58 | @k: second integer to add 59 | Return: the modulus 60 | */ 61 | 62 | int mod(int i, int k) 63 | { 64 | return (i % k); 65 | } 66 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/operations.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Lordwill1/alx-low_level_programming/852a0f453b95036f9dfbc66e3f2c6634f437fefe/0x18-dynamic_libraries/operations.o -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Creates a hash table. 5 | * @size: 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/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 -------------------------------------------------------------------------------- /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/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/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.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 5 | * integers using the Linear search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: Always EXIT_SUCCESS 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < (int)size; i++) 20 | { 21 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 22 | if (value == array[i]) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * recursive_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int recursive_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | return ((int)half); 33 | 34 | if (value < array[half]) 35 | return (recursive_search(array, half, value)); 36 | 37 | half++; 38 | 39 | return (recursive_search(array + half, size - half, value) + half); 40 | } 41 | 42 | /** 43 | * binary_search - calls to binary_search to return 44 | * the index of the number 45 | * 46 | * @array: input array 47 | * @size: size of the array 48 | * @value: value to search in 49 | * Return: index of the number 50 | */ 51 | int binary_search(int *array, size_t size, int value) 52 | { 53 | int index; 54 | 55 | index = recursive_search(array, size, value); 56 | 57 | if (index >= 0 && array[index] != value) 58 | return (-1); 59 | 60 | return (index); 61 | } 62 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | int index, m, k, prev; 16 | 17 | if (array == NULL || size == 0) 18 | return (-1); 19 | 20 | m = (int)sqrt((double)size); 21 | k = 0; 22 | prev = index = 0; 23 | 24 | do { 25 | printf("Value checked array[%d] = [%d]\n", index, array[index]); 26 | 27 | if (array[index] == value) 28 | return (index); 29 | k++; 30 | prev = index; 31 | index = k * m; 32 | } while (index < (int)size && array[index] < value); 33 | 34 | printf("Value found between indexes [%d] and [%d]\n", prev, index); 35 | 36 | for (; prev <= index && prev < (int)size; prev++) 37 | { 38 | printf("Value checked array[%d] = [%d]\n", prev, array[prev]); 39 | if (array[prev] == value) 40 | return (prev); 41 | } 42 | 43 | return (-1); 44 | } 45 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in an array of 5 | * integers using the Interpolation search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: index of the number 11 | */ 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t pos, low, high; 15 | double f; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | low = 0; 21 | high = size - 1; 22 | 23 | while (size) 24 | { 25 | f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]); 26 | pos = (size_t)(low + f); 27 | printf("Value checked array[%d]", (int)pos); 28 | 29 | if (pos >= size) 30 | { 31 | printf(" is out of range\n"); 32 | break; 33 | } 34 | else 35 | { 36 | printf(" = [%d]\n", array[pos]); 37 | } 38 | 39 | if (array[pos] == value) 40 | return ((int)pos); 41 | 42 | if (array[pos] < value) 43 | low = pos + 1; 44 | else 45 | high = pos - 1; 46 | 47 | if (low == high) 48 | break; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * rec_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int rec_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | { 33 | if (half > 0) 34 | return (rec_search(array, half + 1, value)); 35 | return ((int)half); 36 | } 37 | 38 | if (value < array[half]) 39 | return (rec_search(array, half + 1, value)); 40 | 41 | half++; 42 | return (rec_search(array + half, size - half, value) + half); 43 | } 44 | 45 | /** 46 | * advanced_binary - calls to rec_search to return 47 | * the index of the number 48 | * 49 | * @array: input array 50 | * @size: size of the array 51 | * @value: value to search in 52 | * Return: index of the number 53 | */ 54 | int advanced_binary(int *array, size_t size, int value) 55 | { 56 | int index; 57 | 58 | index = rec_search(array, size, value); 59 | 60 | if (index >= 0 && array[index] != value) 61 | return (-1); 62 | 63 | return (index); 64 | } 65 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @list: input list 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | listint_t *jump_list(listint_t *list, size_t size, int value) 14 | { 15 | size_t index, k, m; 16 | listint_t *prev; 17 | 18 | if (list == NULL || size == 0) 19 | return (NULL); 20 | 21 | m = (size_t)sqrt((double)size); 22 | index = 0; 23 | k = 0; 24 | 25 | do { 26 | prev = list; 27 | k++; 28 | index = k * m; 29 | 30 | while (list->next && list->index < index) 31 | list = list->next; 32 | 33 | if (list->next == NULL && index != list->index) 34 | index = list->index; 35 | 36 | printf("Value checked at index [%d] = [%d]\n", (int)index, list->n); 37 | 38 | } while (index < size && list->next && list->n < value); 39 | 40 | printf("Value found between indexes "); 41 | printf("[%d] and [%d]\n", (int)prev->index, (int)list->index); 42 | 43 | for (; prev && prev->index <= list->index; prev = prev->next) 44 | { 45 | printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n); 46 | if (prev->n == value) 47 | return (prev); 48 | } 49 | 50 | return (NULL); 51 | } 52 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - searches for a value in a skip list 5 | * 6 | * @list: input list 7 | * @value: value to search in 8 | * Return: index of the number 9 | */ 10 | skiplist_t *linear_skip(skiplist_t *list, int value) 11 | { 12 | skiplist_t *go; 13 | 14 | if (list == NULL) 15 | return (NULL); 16 | 17 | go = list; 18 | 19 | do { 20 | list = go; 21 | go = go->express; 22 | printf("Value checked at index "); 23 | printf("[%d] = [%d]\n", (int)go->index, go->n); 24 | } while (go->express && go->n < value); 25 | 26 | if (go->express == NULL) 27 | { 28 | list = go; 29 | while (go->next) 30 | go = go->next; 31 | } 32 | 33 | printf("Value found between indexes "); 34 | printf("[%d] and [%d]\n", (int)list->index, (int)go->index); 35 | 36 | while (list != go->next) 37 | { 38 | printf("Value checked at index [%d] = [%d]\n", (int)list->index, list->n); 39 | if (list->n == value) 40 | return (list); 41 | list = list->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Search Algorithms 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 | --------------------------------------------------------------------------------