├── 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 ├── long └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor ├── 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 ├── _putchar.c └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-main.c ├── 100-rot13.c ├── 101-main.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-main.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── 7-main.c ├── README.md ├── _putchar.c └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 101-crackme_password_guide ├── 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 ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── _putchar.c └── main.h ├── 0x0B-malloc_free ├── .2-str_concat.c.swp ├── 0-create_array.c ├── 1-main.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-main.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md ├── _putchar.c ├── c ├── main.h ├── s └── test ├── 0x0C-more_malloc_free ├── .100-realloc.c.swp ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── _putchar.c └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h ├── README.md ├── _putchar.c └── main.h ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md ├── _putchar.c └── 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 ├── _putchar.c └── 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 ├── 0-main.c ├── 1-main.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 2-main.c ├── 3-main.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 4-main.c ├── 5-flip_bits.c ├── 5-main.c ├── README.md ├── _putchar.c ├── a ├── b ├── bin ├── c ├── crackme3 ├── d ├── e ├── f ├── h └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md ├── a.out └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_win.sh ├── README.md ├── libdynamic.so └── main.h ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md ├── a ├── data-structures_guide_1.c ├── data-structures_guide_2.c └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── 5-main.py ├── README.md ├── m.h ├── main.c ├── school.c └── test_files │ └── 5-island_perimeter.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 └── practice ├── .gitignore ├── 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 ├── _putchar.c ├── _putchar.o ├── main.c └── main.o ├── 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 ├── libmy.a ├── main.c ├── main.h └── main.o /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE 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 - print the string in the put function 6 | * 7 | * Description: using the main function 8 | * this program prints "and that piece of art is useful - Dora Korpar 9 | * Return: 0 10 | */ 11 | int main(void) 12 | { 13 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 14 | return (1); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the string in the put function 5 | * 6 | * Description: using the main function 7 | * this program prints "Programing is like building a multilingual puzzle 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | puts("\"Programming is like building a multilingual puzzle"); 13 | return (0); 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the string in the put function 5 | * 6 | * Description: using the main function 7 | * this program prints "Programing is like building a multilingual puzzle 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | printf("with proper grammar, but the outcome is a piece of art,\n"); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the string in the put function 5 | * 6 | * Description: using the main function 7 | * this program prints "Programming is like building a multilingual puzzle 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | char c; 13 | int i; 14 | long li; 15 | long long lli; 16 | float f; 17 | 18 | printf("Size of a char: %ld byte(s)\n", sizeof(c)); 19 | printf("Size of an int: %ld byte(s)\n", sizeof(i)); 20 | printf("Size of a long int: %ld byte(s)\n", sizeof(li)); 21 | printf("Size of a long long int: %ld byte(s)\n", sizeof(lli)); 22 | printf("Size of a float: %ld byte(s)\n", sizeof(f)); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | ## `C programming is fun!` 2 | Wow, it's so cool that I just wrote my first C program as a student at ALX! I'm feeling excited and proud of myself. ALX is a software engineering program that equips students with the skills to become competent developers. As a beginner, writing my first C program is a significant milestone, and it's the first step towards my goal of becoming a skilled programmer. I'm grateful for this opportunity to learn and grow, and I'm looking forward to mastering more programming languages and concepts as I progress in the program. 3 | 4 | ![](https://miro.medium.com/max/1024/0*4ty0Adbdg4dsVBo3.png) 5 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - print if the number is positive, zero, or negative 7 | * 8 | * Description: using the main function 9 | * this program prints "Programming is positive, zero, or negtive 10 | *Return: 0 11 | */ 12 | int main(void) 13 | { 14 | int n; 15 | 16 | srand(time(0)); 17 | n = rand() - RAND_MAX / 2; 18 | /* your code goes there */ 19 | if (n > 0) 20 | { 21 | printf("%i is positive\n", n); 22 | } 23 | else if (n == 0) 24 | { 25 | printf("%i is zero\n", n); 26 | } 27 | else if (n < 0) 28 | { 29 | printf("%i is negative\n", n); 30 | } 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - print if the number is positive, zero, or negative 6 | * Description: using the main function 7 | * this program prints "Programming is positive, zero, or negative 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | int l; 14 | 15 | srand(time(0)); 16 | n = rand() - RAND_MAX / 2; 17 | l = n % 10; 18 | 19 | if (l > 5) 20 | { 21 | printf("Last digit of %d is %d and is greater than 5\n", n, l); 22 | } 23 | else if (l == 0) 24 | { 25 | printf("Last digit of %d is %d and is 0\n", n, l); 26 | } 27 | else 28 | { 29 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, l); 30 | } 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print single digit numbers 4 | * 5 | * Description: print single digits numbers with commas 6 | * Return: always return 0 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | int j; 12 | 13 | for (i = 10; i <= 19; i++) 14 | { 15 | for (j = 10; j <= 19; j++) 16 | { 17 | if ((j % 10) > (i % 10)) 18 | { 19 | putchar((i % 10) + '0'); 20 | putchar((j % 10) + '0'); 21 | if (i != 18 || j != 19) 22 | { 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | } 27 | } 28 | } 29 | putchar('\n'); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints single digit numbers 4 | * 5 | * Description: prints single digit numbers with commas 6 | * Return: always returns 0 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | int b; 12 | int c; 13 | 14 | for (a = 0; a <= 9; a++) 15 | { 16 | for (b = 0; b <= 9; b++) 17 | { 18 | for (c = 0; c <= 9; c++) 19 | { 20 | if (c > b && b > a) 21 | { 22 | putchar(a + '0'); 23 | putchar(b + '0'); 24 | putchar(c + '0'); 25 | if (a != 7 || b != 8 || c != 9) 26 | { 27 | putchar(','); 28 | putchar(' '); 29 | } 30 | } 31 | } 32 | } 33 | } 34 | putchar('\n'); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print if the number is positive, zero, or negative 4 | * 5 | * Description: using the main function 6 | * this program prints "Programming is positive, zero, or negative 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | int j; 13 | 14 | for (i = 0; i <= 99; i++) 15 | { 16 | for (j = i + 1; j <= 99; j++) 17 | { 18 | putchar(i / 10 + '0'); 19 | putchar(i % 10 + '0'); 20 | putchar(' '); 21 | putchar(j / 10 + '0'); 22 | putchar(j % 10 + '0'); 23 | if (!(i == 98 && j == 99)) 24 | { 25 | putchar(','); 26 | putchar(' '); 27 | } 28 | } 29 | } 30 | putchar('\n'); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print if the number is positive, zero, or negative 4 | * 5 | * Description: using the main function 6 | * this is a program that prints "Programming is a positive, zero, or negative 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char ch; 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 | * main - print if the number is positive, zero, or negative 4 | * 5 | * Description: using the main function 6 | * this program prints "Programming is positive, zero, or negative 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char ch; 12 | for (ch = 'a' ; ch <= 'z' ; ch++) 13 | { 14 | putchar(ch); 15 | } 16 | for (ch = 'A' ; ch <= 'Z' ; ch++) 17 | { 18 | putchar(ch); 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: This program prints the lowercase alphabet, 7 | * excluding the letters 'e' and 'q'. 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | 12 | int main(void) 13 | { 14 | char ch; 15 | 16 | for (ch = 'a'; ch <= 'z'; ch++) 17 | { 18 | if (ch != 'e' && ch != 'q') 19 | { 20 | putchar(ch); /* Print the character if it is not 'e' or 'q' */ 21 | } 22 | } 23 | putchar('\n'); /* Print a new line after printing the alphabet */ 24 | 25 | return (0); /* program has executed successfully */ 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print if number is positive, zero, or negative 4 | * 5 | * Description: using the main function 6 | * this program prints "Programming is positive, zero, or negative 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 0 ; i < 10 ; i++) 14 | { 15 | printf("%i", i); 16 | } 17 | printf("\n"); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print if the number is positive, zero, or negative 4 | * 5 | * Description: using the main function 6 | * this program prints "Programming is positive, zero, or negative 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | for (n = '0' ; n <= '9' ; n++) 14 | { 15 | putchar(n); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print if the number is positive, zero, or negative 4 | * 5 | * Descrption: using the main function 6 | * this program prints "Programming is positive, zero, or negative 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | for (c = 'z' ; c >= 'a'; c--) 14 | { 15 | putchar(c); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print if the number is positive, zero, or negative 4 | * 5 | * Descrption: using the main function 6 | * this program prints "Programming is positive, zero, or negative 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char c; 12 | 13 | for (c = '0' ; c <= '9' ; c++) 14 | { 15 | putchar(c); 16 | } 17 | for (c = 'a' ; c <= 'f' ; c++) 18 | { 19 | putchar(c); 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print single digit numbers 4 | * 5 | * Return: always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | 11 | for (i = 10; i < 20; i++) 12 | { 13 | putchar((i % 10) + '0'); 14 | if (i != 19) 15 | { 16 | putchar(','); 17 | putchar(' '); 18 | } 19 | } 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | variables, if, else, and while statements 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - print _putchar 5 | * Description: using the main function 6 | * this prints _putchar followoed by a new line 7 | * Return: always 0 8 | */ 9 | int main(void) 10 | { 11 | _putchar('_'); 12 | _putchar('p'); 13 | _putchar('u'); 14 | _putchar('t'); 15 | _putchar('c'); 16 | _putchar('h'); 17 | _putchar('a'); 18 | _putchar('r'); 19 | _putchar('\n'); 20 | 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_alphabet - function to print abc 4 | * 5 | * Return: 0 6 | */ 7 | void print_alphabet(void) 8 | { 9 | char c; 10 | 11 | for (c = 'a'; c <= 'z'; c++) 12 | { 13 | _putchar(c); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - a function that adds two integers and returns the result 5 | * @x: the first integer to be added 6 | * @y: the second integer to be added 7 | * Return: the result of the addition 8 | */ 9 | int add(int x, int y) 10 | { 11 | return (x + y); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - a program that computes and prints the sum of all multiple 5 | * of 3 or 5 below 1024 (excluded), 6 | * followed by a new line 7 | * Return: 0 if successful 8 | */ 9 | int main(void) 10 | { 11 | unsigned long int sum_3, sum_5, sum; 12 | int x; 13 | 14 | sum_3 = 0; 15 | sum_5 = 0; 16 | sum = 0; 17 | 18 | for (x = 0; x <= 1024; x++) 19 | { 20 | if ((x % 3) == 0) 21 | { 22 | sum_3 = sum_3 + x; 23 | } 24 | else if ((x % 5) == 0) 25 | { 26 | sum_5 = sum_5 + x; 27 | } 28 | } 29 | sum = sum_3 + sum_5; 30 | printf("%lu\n", sum); 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - a program that prints the first 50 Fibonacci numbers 4 | * starting with 1 and 2, followed by a new line. 5 | * Description: computes and prints the sum of all the multiples of 3 or 6 | * 5 bellow 1024 (excluded), floowed by a new line 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | long int x, y, z, next; 12 | 13 | y = 1; 14 | z = 2; 15 | for (x = 1; x <= 50; x++) 16 | { 17 | if (y != 20365011074) 18 | { 19 | printf("%ld, ", y); 20 | } else 21 | { 22 | printf("%ld\n", y); 23 | } 24 | next = y + z; 25 | y = z; 26 | z = next; 27 | } 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - a program that finds and prints the sum of the even-valued terms 5 | * Description: computes and prints even numbers < 4,000,000 6 | * followed by a new line 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | unsigned long int j, k, next, sum; 13 | 14 | j = 1; 15 | k = 2; 16 | sum = 0; 17 | 18 | for (i = 1; i <= 33; i++) 19 | { 20 | if (j < 4000000 && (j % 2) == 0) 21 | { 22 | sum = sum + j; 23 | } 24 | next = j + k; 25 | j = k; 26 | k = next; 27 | } 28 | printf("%lu\n", sum); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - prints numbers from n to 98 6 | * @n: integer argument 7 | * Description: numbers seperated by commas 8 | */ 9 | void print_to_98(int n) 10 | { 11 | while (n < 98) 12 | { 13 | printf("%d, ", n); 14 | n++; 15 | } 16 | while (n > 98) 17 | { 18 | printf("%d, ", n); 19 | n--; 20 | } 21 | if (n == 98) 22 | printf("%d", n); 23 | printf("\n"); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_alphabet_x10 - function to print abc 10 times 4 | * followed by a new line 5 | * Return: 0 6 | */ 7 | void print_alphabet_x10(void) 8 | { 9 | char c, i; 10 | 11 | for (i = 0; i <= 9; i++) 12 | { 13 | for (c = 'a'; c <= 'z'; c++) 14 | { 15 | _putchar(c); 16 | } 17 | _putchar('\n'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _islower - function to check for lowercase character 4 | * @c: is the int that will use for the argument of the function 5 | * Return: 0 6 | */ 7 | int _islower(int c) 8 | { 9 | if (c >= 'a' && c <= 'z') 10 | { 11 | return (1); 12 | } 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - function to check if c is a letter, lowercase or uppercase 4 | * @c: is the int that will use for the argument of the function 5 | * Return: 0 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 10 | 11 | { 12 | return (1); 13 | } 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_sign - Determine if the input number 4 | * is greater, equal or lesss than zero. 5 | * @n: The input number as an integer. 6 | * Return: 1 is greater than zero. 0 is zero. 7 | * -1 is less than zero. 8 | */ 9 | int print_sign(int n) 10 | { 11 | if (n > 0) 12 | { 13 | _putchar(43); 14 | return (1); 15 | } 16 | else if (n < 0) 17 | { 18 | _putchar(45); 19 | return (-1); 20 | } 21 | else 22 | { 23 | _putchar(48); 24 | return (0); 25 | } 26 | _putchar('\n'); 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - function that computes the absolute value of an integer 4 | * @d: input number as an integer. 5 | * Return: absolute value 6 | */ 7 | int _abs(int d) 8 | { 9 | if (d > 0 || d == 0) 10 | { 11 | return (d); 12 | } 13 | else 14 | return (d * -1); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - a function that prints the last digit of a number 5 | * @n: integer to be accepted 6 | * Return: the value of the last digit 7 | */ 8 | int print_last_digit(int n) 9 | { 10 | int ld; 11 | 12 | if (n < 0) 13 | n = -n; 14 | ld = n % 10; 15 | 16 | if (ld < 0) 17 | ld = -ld; 18 | _putchar(ld + '0'); 19 | 20 | return (ld); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - a function that 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 x = 0, j; 10 | 11 | while (x < 24) 12 | { 13 | j = 0; 14 | while (j < 60) 15 | { 16 | _putchar((x / 10) + '0'); 17 | _putchar((x % 10) + '0'); 18 | _putchar(':'); 19 | _putchar((j / 10) + '0'); 20 | _putchar((j % 10) + '0'); 21 | _putchar('\n'); 22 | j++; 23 | } 24 | x++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - a function that prints the 9 times table. 5 | * starting with 0 6 | */ 7 | void times_table(void) 8 | { 9 | int num, mult, prod; 10 | 11 | for (num = 0; num <= 9; num++) 12 | { 13 | _putchar('0'); 14 | for (mult = 1; mult <= 9; mult++) 15 | { 16 | _putchar(','); 17 | _putchar(' '); 18 | prod = num * mult; 19 | if (prod <= 9) 20 | _putchar(' '); 21 | else 22 | _putchar((prod / 10) + '0'); 23 | _putchar((prod % 10) + '0'); 24 | } 25 | _putchar('\n'); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # C functions nested loops 2 | ![](https://i.pinimg.com/originals/06/60/ef/0660efe82fa3da42ed56eef013171835.gif) 3 | ## Flow chart 4 | ![](https://media.geeksforgeeks.org/wp-content/uploads/20220914155658/flowchartofnesteddowhileloop.png) 5 | -------------------------------------------------------------------------------- /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 | #ifndef HOLBERTON_H_INCLUDED 2 | #define HOLBERTON_H_INCLUDED 3 | 4 | int _putchar(char c); 5 | void print_alphabet(void); 6 | void print_alphabet_x10(void); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int print_sign(int n); 10 | int _abs(int); 11 | int print_last_digit(int); 12 | void jack_bauer(void); 13 | void times_table(void); 14 | int add(int, int); 15 | void print_to_98(int n); 16 | void print_times_table(int n); 17 | void num(int r, int n); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /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 | int main(void) 8 | { 9 | int i; 10 | 11 | i = 0; 12 | 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 | if (a >= b && a >= c) 15 | { 16 | largest = a; 17 | } 18 | else if (b >= a && b >= c) 19 | { 20 | largest = b; 21 | } 22 | else 23 | { 24 | largest = c; 25 | } 26 | return (largest); 27 | } 28 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) 16 | { 17 | if (month >= 3 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%2d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | # `Debugging` 2 | 3 | - What is debugging? 4 | 5 | - What are some methods of debugging manually? 6 | 7 | - How to read the error messages? 8 | 9 | ![](https://cdn.educba.com/academy/wp-content/uploads/2019/09/What-is-Debugging.png) 10 | -------------------------------------------------------------------------------- /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 | void print_remaining_days(int month, int day, int year); 9 | int convert_day(int month, int day); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks if a character is uppercase or not 5 | * @c: character to be tested 6 | * Return: 1 whether it is, 0 otherwise 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if ((c >= 'A') && (c <= 'Z')) 12 | { 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks whether a character is a digit or not 5 | * @c: tested character 6 | * Return: 1 if it is, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if ((c >= 48) && (c <= 57)) 11 | { 12 | return (1); 13 | } 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_triangle - Prints a triangle 6 | * @size: size parameter of triangle 7 | * Return: returns nothing 8 | */ 9 | 10 | void print_triangle(int size) 11 | { 12 | int i, j, k; 13 | 14 | if (size <= 0) 15 | { 16 | _putchar('\n'); 17 | } 18 | else 19 | { 20 | for (i = 1; i <= size; i++) 21 | { 22 | for (j = size; j > i; j--) 23 | { 24 | _putchar(' '); 25 | } 26 | for (k = 1; k <= j; k++) 27 | { 28 | _putchar('#'); 29 | } 30 | _putchar('\n'); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x04-more_functions_nested_loops/100-prime_factor -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print largest prime factor of 612852475143 5 | * Return: 0 on Success 6 | */ 7 | int main(void) 8 | { 9 | unsigned long num = 612852475143; 10 | unsigned long i; 11 | unsigned long large_prime; 12 | 13 | for (i = 2; i <= num; i++) 14 | { 15 | if (num % i == 0) 16 | { 17 | large_prime = i; 18 | num /= large_prime; 19 | } 20 | } 21 | printf("%lu\n", large_prime); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer 5 | * @n: random integer 6 | * Return: void 7 | */ 8 | void print_number(int n) 9 | { 10 | char ld, cr; 11 | int r; 12 | int ct = 0; 13 | 14 | if (n < 0) 15 | { 16 | _putchar ('-'); 17 | ld = (char)('0' - (n % 10)); 18 | n = n / -10; 19 | } 20 | 21 | else 22 | { 23 | ld = (char)((n % 10) + '0'); 24 | n = n / 10; 25 | } 26 | 27 | r = 0; 28 | while (n > 0) 29 | { 30 | r = r * 10 + (n % 10); 31 | n = n / 10; 32 | ct++; 33 | } 34 | 35 | while (r > 0) 36 | { 37 | cr = (char)((r % 10) + '0'); 38 | _putchar(cr); 39 | r = r / 10; 40 | ct--; 41 | } 42 | while (ct != 0) 43 | { 44 | _putchar('0'); 45 | ct--; 46 | } 47 | _putchar(ld); 48 | } 49 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two numbers 5 | * @a: first number 6 | * @b: second number 7 | * Return: result 8 | */ 9 | 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_numbers - function that print all numbers from 6 | * 0 to 9 7 | * 8 | * Return: returns nothing 9 | */ 10 | 11 | void print_numbers(void) 12 | { 13 | int n; 14 | 15 | for (n = 48; n < 58; n++) 16 | { 17 | putchar(n); 18 | } 19 | putchar(10); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_most_numbers - prints numbers except 2 and 4 6 | * 7 | * Return: returns nothing 8 | */ 9 | 10 | void print_most_numbers(void) 11 | { 12 | int i; 13 | 14 | for (i = 0; i <= 9; i++) 15 | { 16 | if ((i != 2) && (i != 4)) 17 | { 18 | _putchar('0' + i); 19 | } 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * more_numbers - prints numbers to 14 6 | * 7 | * Return: returns nothing 8 | */ 9 | 10 | void more_numbers(void) 11 | { 12 | int n1, n2; 13 | 14 | for (n1 = 0; n1 < 10; n1++) 15 | { 16 | for (n2 = 0; n2 <= 14; n2++) 17 | { 18 | if (n2 > 9) 19 | { 20 | putchar((n2 / 10) + '0'); 21 | } 22 | putchar((n2 % 10) + '0'); 23 | } 24 | putchar(10); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_line - prints a straight line 6 | * 7 | * @n: parameter 8 | * Returns: returns nothing 9 | */ 10 | 11 | void print_line(int n) 12 | { 13 | while (n-- > 0) 14 | { 15 | putchar('_'); 16 | } 17 | putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_diagonal - prints a diagonal 6 | * @n: parameter 7 | * Return: returns nothing 8 | */ 9 | 10 | void print_diagonal(int n) 11 | { 12 | int len, space; 13 | 14 | if (n > 0) 15 | { 16 | for (len = 0; len < n; len++) 17 | { 18 | for (space = 0; space < len; space++) 19 | { 20 | putchar(' '); 21 | } 22 | 23 | putchar('\\'); 24 | 25 | if (len == (n - 1)) 26 | { 27 | continue; 28 | } 29 | putchar('\n'); 30 | } 31 | } 32 | putchar('\n'); 33 | } 34 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_square - function that prints 10 times the numbers, from 0 to 14 6 | * @size: parameter 7 | * Return: returns nothing 8 | */ 9 | 10 | void print_square(int size) 11 | { 12 | int i = 0, j; 13 | 14 | if (size <= 0) 15 | { 16 | _putchar('\n'); 17 | } 18 | else 19 | { 20 | while (i < size) 21 | { 22 | j = 0; 23 | 24 | while (j < size) 25 | { 26 | _putchar('#'); 27 | 28 | j++; 29 | } 30 | _putchar('\n'); 31 | 32 | i++; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print 1 to 100, except certain multiples 4 | * multiple of 3 only displays "Fizz" instad of number. 5 | * multiple of 5 displays "Buzz" instead of number. 6 | * multiples of 3 and 5 display "FizzBuzz" 7 | * Return: 0 on Success 8 | */ 9 | int main(void) 10 | { 11 | int i; 12 | 13 | for (i = 1; i <= 100; i++) 14 | { 15 | if (i % 3 == 0 && i % 5 == 0) 16 | { 17 | printf(" FizzBuzz"); 18 | } 19 | else if (i % 3 == 0) 20 | { 21 | printf(" Fizz"); 22 | } 23 | else if (i % 5 == 0) 24 | { 25 | printf(" Buzz"); 26 | } 27 | else 28 | { 29 | printf(" %d", i); 30 | } 31 | 32 | 33 | } 34 | printf("\n"); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | - What are nested loops and how to use them 2 | 3 | - What is a function and how do you use functions 4 | 5 | - What is the difference between a declaration and a definition of a function 6 | 7 | - What is a prototype 8 | 9 | - Scope of variables 10 | 11 | - What are the gcc flags -Wall -Werror -pedantic -Wextra -std=gnu89 12 | 13 | - What are header files and how to to use them with #include 14 | 15 | 16 | ![](https://media.geeksforgeeks.org/wp-content/uploads/20220914155543/Flowchartofnestedforloop.png) 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | /** 5 | * main - header file for prototypes 6 | * void prototypes (void) 7 | * int prototypes (int) 8 | */ 9 | 10 | int _putchar(char); 11 | int _isupper(int c); 12 | int _isdigit(int c); 13 | int mul(int a, int b); 14 | void print_numbers(void); 15 | void print_most_numbers(void); 16 | void more_numbers(void); 17 | void print_line(int n); 18 | void print_diagonal(int n); 19 | void print_square(int size); 20 | void print_triangle(int size); 21 | void print_number(int n); 22 | 23 | #endif /* MAIN_H */ 24 | 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - resets value to 98 5 | * 6 | * @n: pointer integer 7 | * Return: nothing 8 | */ 9 | 10 | void reset_to_98(int *n) 11 | { 12 | *n = 98; 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swaps two integers' values 5 | * @a: first integer 6 | * @b: second integer 7 | * Return: returns nothing 8 | */ 9 | 10 | void swap_int(int *a, int *b) 11 | { 12 | int c = *b; 13 | int d = *a; 14 | 15 | *a = c; 16 | *b = d; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "2-strlen.c" 4 | 5 | /** 6 | * _atoi - This function converts a string to an integer. 7 | * @s: char array string 8 | * Description: Number in the string can be preceded by an infinite 9 | * number of characters. 10 | * You need to take into account all -/+ signs before the number. 11 | * If there are no numbers in the string, return 0. 12 | * No need to check for overflow. 13 | * Not allowed to hard-code special values. 14 | * Return: first integer found in string 15 | */ 16 | 17 | int _atoi(char *s) 18 | { 19 | int i; 20 | int h, p; 21 | 22 | h = 0; 23 | p = -1; 24 | for (i = 0; s[i] != '\0'; i++) 25 | { 26 | if (s[i] == '-') 27 | p *= -1; 28 | 29 | if (s[i] > 47 && s[i] < 58) 30 | { 31 | if (h < 0) 32 | h = (h * 10) - (s[i] - '0'); 33 | else 34 | h = (s[i] - '0') * -1; 35 | 36 | if (s[i + 1] < 48 || s[i + 1] > 57) 37 | break; 38 | } 39 | } 40 | if (p < 0) 41 | h *= -1; 42 | 43 | return (h); 44 | } 45 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - generates keygen. 6 | * Return: return 0 always. 7 | */ 8 | 9 | int main(void) 10 | { 11 | int r = 0, c = 0; 12 | time_t t; 13 | 14 | srand((unsigned int) time(&t)); 15 | while (c < 2772) 16 | { 17 | r = rand() % 128; 18 | if ((c + r) > 2772) 19 | break; 20 | c = c + r; 21 | printf("%c", r); 22 | } 23 | printf("%c\n", (2772 - c)); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the lenght of a string 5 | * @s: string 6 | * Return: returns length as integer; 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*(s + len) != '\0') 14 | len++; 15 | return (len); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _puts - prints a string 6 | * @str: string to print 7 | * 8 | * Description: prints a string 9 | * Return: return 0 10 | */ 11 | 12 | void _puts(char *str) 13 | { 14 | int i = 0; 15 | 16 | while (*(str + i) != '\0') 17 | { 18 | putchar(*(str + i)); 19 | i++; 20 | } 21 | putchar(10); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * print_rev - Prints a string in reverse order 7 | * @s: string to print in reserve order 8 | * Return: returns 0 9 | */ 10 | 11 | void print_rev(char *s) 12 | { 13 | int len = strlen(s); 14 | 15 | while (len--) 16 | putchar(*(s + len)); 17 | putchar(10); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "2-strlen.c" 4 | 5 | /** 6 | * rev_string - Reverse a string 7 | * @s: string to be reversed 8 | * 9 | * Return: returns nothing 10 | */ 11 | 12 | void rev_string(char *s) 13 | { 14 | int i; 15 | char t; 16 | 17 | for (i = 0; i < _strlen(s) / 2; i++) 18 | { 19 | t = s[i]; 20 | s[i] = s[_strlen(s) - i - 1]; 21 | s[_strlen(s) - i - 1] = t; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * puts2 - prints every other character 6 | * @str: string 7 | * 8 | * Return: returns 0 9 | */ 10 | 11 | void puts2(char *str) 12 | { 13 | int i = 0; 14 | 15 | while (*(str + i) != '\0') 16 | { 17 | if (i % 2 == 0) 18 | putchar(*(str + i)); 19 | i++; 20 | } 21 | putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "2-strlen.c" 4 | 5 | /** 6 | * puts_half - This function prints half of a string foloowed by a new line. 7 | * @str: pointer parameter 8 | */ 9 | 10 | void puts_half(char *str) 11 | { 12 | int i; 13 | int oE = 0; 14 | 15 | if (_strlen(str) % 2 != 0) 16 | { 17 | oE += 1; 18 | } 19 | for (i = (_strlen(str) + oE) / 2; i < _strlen(str); i++) 20 | { 21 | _putchar(str[i]); 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_array - this function prints n elements of an array of integers. 6 | * @a: pointer parameter 7 | * @n: integer variable 8 | */ 9 | 10 | void print_array(int *a, int n) 11 | { 12 | int inc; 13 | 14 | for (inc = 0; inc < n; inc++) 15 | { 16 | if (inc != n - 1) 17 | printf("%d, ", a[inc]); 18 | else 19 | printf("%d", a[inc]); 20 | } 21 | putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strcpy - This function copies a string pointed to scr and dest 6 | * @dest: pointer parameter 7 | * @src: pointer parameter 8 | * Return: char 9 | */ 10 | 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int inc = 0; 14 | 15 | while (*(src + inc) != '\0') 16 | { 17 | *(dest + inc) = *(src + inc); 18 | inc++; 19 | } 20 | *(dest + inc) = '\0'; 21 | 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | What are pointers and how to use them 2 | 3 | What are arrays and how to use them 4 | 5 | What are the differences between pointers and arrays 6 | 7 | How to use strings and how to manipulate them 8 | 9 | Scope of variables 10 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H_INCLUDED 2 | #define MAIN_H_INCLUDED 3 | 4 | /** 5 | * void prototypes() 6 | * int prototypes() 7 | */ 8 | 9 | void reset_to_98(int *n); 10 | void swap_int(int *a, int *b); 11 | int _strlen(char *s); 12 | void _puts(char *str); 13 | void print_rev(char *s); 14 | void rev_string(char *s); 15 | void puts2(char *str); 16 | void puts_half(char *str); 17 | void print_array(int *a, int n); 18 | char *_strcpy(char *dest, char *src); 19 | int _atoi(char *s); 20 | int _putchar(char c); 21 | 22 | #endif /* MIAN_H_INCLUDED */ 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strcat - concatenate two strings 5 | * @dest: char string to concatenate to 6 | * @src: char string 7 | * Return: ponter to resulting string 'dest' 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | for (j = 0; dest[j] != '\0'; j++) 16 | {} 17 | 18 | for (i = 0; src[i] != '\0'; i++) 19 | { 20 | dest[j + i] = src[i]; 21 | } 22 | dest[j + i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenate two strings 5 | * @dest: string 6 | * @src: string 7 | * @n: number of elements to concatenate in 8 | * Return: pointer to resulting 'dest' 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int x, y; 14 | 15 | for (x = 0; dest[x] != '\0'; x++) 16 | ; 17 | 18 | for (y = 0; src[y] != '\0' && n > 0; y++, n--, x++) 19 | { 20 | dest[x] = src[y]; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | char s[] = "Rot13 (\"rotate by 13 places\", sometimes hyphenated Rot-13) is a simple letter substitution cipher.\n"; 13 | char *p; 14 | 15 | p = rot13(s); 16 | printf("%s", p); 17 | printf("------------------------------------\n"); 18 | printf("%s", s); 19 | printf("------------------------------------\n"); 20 | p = rot13(s); 21 | printf("%s", p); 22 | printf("------------------------------------\n"); 23 | printf("%s", s); 24 | printf("------------------------------------\n"); 25 | p = rot13(s); 26 | printf("%s", p); 27 | printf("------------------------------------\n"); 28 | printf("%s", s); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: return always 0. 7 | */ 8 | 9 | int main(void) 10 | { 11 | print_number(98); 12 | _putchar('\n'); 13 | print_number(402); 14 | _putchar('\n'); 15 | print_number(1024); 16 | _putchar('\n'); 17 | print_number(0); 18 | _putchar('\n'); 19 | print_number(-98); 20 | _putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_number - prints an integer; 6 | * @n: integer to be printed; 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int n1; 11 | 12 | if (n < 0) 13 | { 14 | n1 = -n; 15 | _putchar('-'); 16 | } 17 | else 18 | { 19 | n1 = n; 20 | } 21 | if (n1 / 10) 22 | print_number(n1 / 10); 23 | 24 | _putchar((n1 % 10) + '0'); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * infinite_add - adds two numbers 5 | * @n1: first number 6 | * @n2: second number 7 | * @r: buffer for result 8 | * @size_r: buffer size 9 | * 10 | * Return: address of r or 0 11 | */ 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 13 | { 14 | int i, j, k, l, m, n; 15 | 16 | for (i = 0; n1[i]; i++) 17 | ; 18 | for (j = 0; n2[j]; j++) 19 | ; 20 | if (i > size_r || j > size_r) 21 | return (0); 22 | m = 0; 23 | for (i -= 1, j -= 1, k = 0; k < size_r - 1; i--, j--, k++) 24 | { 25 | n = m; 26 | if (i >= 0) 27 | n += n1[i] - '0'; 28 | if (j >= 0) 29 | n += n2[j] - '0'; 30 | if (i < 0 && j < 0 && n == 0) 31 | { 32 | break; 33 | } 34 | m = n / 10; 35 | r[k] = n % 10 + '0'; 36 | } 37 | r[k] = '\0'; 38 | if (i >= 0 || j >= 0 || m) 39 | return (0); 40 | for (k -= 1, l = 0; l < k; k--, l++) 41 | { 42 | m = r[k]; 43 | r[k] = r[l]; 44 | r[l] = m; 45 | } 46 | return (r); 47 | } 48 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - prints a buffer 6 | * @b: buffer. 7 | * @size: size of buffer. 8 | * Return: no return 9 | */ 10 | void print_buffer(char *b, int size) 11 | { 12 | int j, k, l; 13 | 14 | if (size <= 0) 15 | printf("\n"); 16 | else 17 | { 18 | for (j = 0; j < size; j += 10) 19 | { 20 | printf("%.8x:", j); 21 | for (k = j; k < j + 10; k++) 22 | { 23 | if (k % 2 == 0) 24 | printf(" "); 25 | if (k < size) 26 | printf("%.2x", *(b + k)); 27 | else 28 | printf(" "); 29 | } 30 | printf(" "); 31 | for (l = j; l < j + 10; l++) 32 | { 33 | if (l >= size) 34 | break; 35 | if (*(b + l) < 32 || *(b + l) > 126) 36 | printf("%c", '.'); 37 | else 38 | printf("%c", *(b + l)); 39 | } 40 | printf("\n"); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0 8 | */ 9 | int main(void) 10 | { 11 | char s1[98]; 12 | char *ptr; 13 | int i; 14 | 15 | for (i = 0; 1 < 98 - 1; i++) 16 | { 17 | s1[i] = '*'; 18 | } 19 | s1[i] = '\0'; 20 | printf("%s\n", s1); 21 | ptr = _strncpy(s1, "First, solve the problem. Then, write the code\n", 5); 22 | printf("%s\n", s1); 23 | printf("%s\n", ptr); 24 | ptr = _strncpy(s1, "First, solve the problem. Then, write the code\n", 90); 25 | printf("%s", s1); 26 | printf("%s", ptr); 27 | for (i = 0; i < 98; i++) 28 | { 29 | if (i % 10) 30 | { 31 | printf(" "); 32 | } 33 | if (!(i % 10) && i) 34 | { 35 | printf("\n"); 36 | } 37 | printf("0x%02x", s1[i]); 38 | } 39 | printf("\n"); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strncpy - Copy a string starting from index 0 of 'dest' 6 | * @dest: destination to copy string 7 | * @src: source to copy string 8 | * @n: number of chars to copy over 9 | * 10 | * Return: pointer to the resulting string 11 | */ 12 | 13 | char *_strncpy(char *dest, char *src, int n) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | 19 | while (src[i] != '\0' && i < n) 20 | { 21 | dest[i] = src[i]; 22 | i++; 23 | } 24 | 25 | while (i < n) 26 | { 27 | dest[i] = '\0'; 28 | i++; 29 | } 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Compare two strings 5 | * @s1: first string 6 | * @s2: second string 7 | * Return: negative int if s1 < s2, 0 if matching, and positive int if s1 > s2 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int y; 13 | 14 | for (y = 0; s1[y] != '\0' || s2[y] != '\0'; y++) 15 | { 16 | if (s1[y] != s2[y]) 17 | return (s1[y] - s2[y]); 18 | } 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - reverse the contents of an array of integer 5 | * @a: array of integers to reverse 6 | * @n: number of elements in array 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int x; 12 | int y; 13 | 14 | for (x = 0; x < (n / 2); x++) 15 | { 16 | y = a[x]; 17 | a[x] = a[n - x - 1]; 18 | a[n - x - 1] = y; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - Change all lowercase letters to uppercase 5 | * @s: string to make upper 6 | * Return: return value of upper 7 | */ 8 | 9 | char *string_toupper(char *s) 10 | { 11 | int i; 12 | 13 | for (i = 0; s[i] != '\0'; i++) 14 | { 15 | if (s[i] >= 'a' && s[i] <= 'z') 16 | { 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 - capitalize all words of a string 5 | * @str: string 6 | * Return: str 7 | */ 8 | 9 | char *cap_string(char *str) 10 | { 11 | int i, c; 12 | int trigger; 13 | char nots[] = ",;.!?(){}\n\t\" "; 14 | 15 | for (i = 0, trigger = 0; str[i] != '\0'; i++) 16 | { 17 | if (str[0] > 96 && str[0] < 123) 18 | trigger = 1; 19 | for (c = 0; nots[c] != '\0'; c++) 20 | { 21 | if (nots[c] == str[i]) 22 | trigger = 1; 23 | } 24 | if (trigger) 25 | { 26 | if (str[i] > 96 && str[i] < 123) 27 | { 28 | str[i] -= 32; 29 | trigger = 0; 30 | } 31 | else if (str[i] > 64 && str[i] < 91) 32 | trigger = 0; 33 | else if (str[i] > 47 && str[i] < 58) 34 | trigger = 0; 35 | } 36 | } 37 | return (str); 38 | } 39 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * leet - transform to leet 6 | * @s: char array string type 7 | * Return: the resulting string; 8 | */ 9 | 10 | char *leet(char *s) 11 | { 12 | int i, j; 13 | 14 | char *a = "aAeEOotTlL"; 15 | char *b = "4433007711"; 16 | 17 | for (i = 0; s[i] != '\0'; i++) 18 | { 19 | for (j = 0; j < 10; j++) 20 | { 21 | if (s[i] == a[j]) 22 | { 23 | s[i] = b[j]; 24 | } 25 | } 26 | } 27 | return (s); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code for 6 | * 7 | * Return: Always 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | char s[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\n"; 13 | char *p; 14 | 15 | p = leet(s); 16 | printf("%s", p); 17 | printf("%s", s); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | What are pointers and how to use them 2 | 3 | What are arrays and how to use them 4 | 5 | What are the differences between pointers and arrays 6 | 7 | How to use strings and how to manipulate them 8 | 9 | Scope of variables 10 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * 6 | * Return: On success 1. 7 | * On error, -1 is returned, and errno is set appropriately. 8 | */ 9 | 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H_INCLUDED 2 | #define MAIN_H_INCLUDED 3 | 4 | /** 5 | * void prototypes() 6 | * int prototypes() 7 | */ 8 | 9 | char *_strcat(char *dest, char *src); 10 | char *_strncat(char *dest, char *src, int n); 11 | char *_strncpy(char *dest, char *src, int n); 12 | int _strcmp(char *s1, char *s2); 13 | void reverse_array(int *a, int n); 14 | char *string_toupper(char *); 15 | char *cap_string(char *); 16 | char *rot13(char *); 17 | void print_number(int n); 18 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 19 | void print_buffer(char *b, int size); 20 | int _putchar(char c); 21 | 22 | #endif /* MAIN_H_INCLUDED */ 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_memset - fills memory with a constant byte, 6 | * @s: address to memory area. 7 | * @b: char to be used. 8 | * @n: number of bytes to the memory block. 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; n > 0; i++, n--) 17 | { 18 | s[i] = b; 19 | } 20 | 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _memcpy - copies memory area, 6 | * @dest: destination memory area. 7 | * @src: source memory area. 8 | * @n: bytes filled. 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *_memcpy(char *dest, char *src, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; i < n; i++) 17 | *(dest + i) = *(src + i); 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * set_string - sets the value of a pointer to a char 6 | * @s: source adress. 7 | * @to: target adress. 8 | * Return: no return. 9 | */ 10 | 11 | void set_string(char **s, char *to) 12 | { 13 | *s = to; 14 | } 15 | -------------------------------------------------------------------------------- /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. 7 | * @c: character. 8 | * Return: the pointer to the first occurrence of the character c. 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | unsigned int i = 0; 14 | 15 | for (; *(s + i) != '\0'; i++) 16 | if (*(s + i) == c) 17 | return (s + i); 18 | if (*(s + i) == c) 19 | return (s + i); 20 | return ('\0'); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strspn - gets the length of a prefix substring. 6 | * @s: initial segment. 7 | * @accept: accepted bytes. 8 | * Return: the number of accepted bytes. 9 | */ 10 | 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int i, j, bool; 14 | 15 | for (i = 0; *(s + i) != '\0'; i++) 16 | { 17 | bool = 1; 18 | for (j = 0; *(accept + j) != '\0'; j++) 19 | { 20 | if (*(s + i) == *(accept + j)) 21 | { 22 | bool = 0; 23 | break; 24 | } 25 | } 26 | if (bool == 1) 27 | break; 28 | } 29 | return (i); 30 | } 31 | -------------------------------------------------------------------------------- /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: first string. 7 | * @accept: second string. 8 | * Return: a pointer to the byte in s that matches one of the 9 | * bytes in accept, or NULL if no such byte is found. 10 | */ 11 | 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | unsigned int i, j; 15 | 16 | for (i = 0; *(s + i) != '\0'; i++) 17 | { 18 | for (j = 0; *(accept + j) != '\0'; j++) 19 | { 20 | if (*(s + i) == *(accept + j)) 21 | return (s + i); 22 | } 23 | } 24 | return ('\0'); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strstr - finds the first occurrence of the substring. 6 | * needle in the string haystack. 7 | * @haystack: entire string. 8 | * @needle: substring. 9 | * Return: pointer to the beginning of located substring or 10 | * NULL if the substring is not found. 11 | * 12 | */ 13 | 14 | char *_strstr(char *haystack, char *needle) 15 | { 16 | char *bhaystack; 17 | char *pneedle; 18 | 19 | 20 | while (*haystack != '\0') 21 | { 22 | bhaystack = haystack; 23 | pneedle = needle; 24 | 25 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 26 | { 27 | haystack++; 28 | pneedle++; 29 | } 30 | if (!*pneedle) 31 | return (bhaystack); 32 | haystack = bhaystack + 1; 33 | } 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_chessboard - prints the chessboard 6 | * @a: input pointer. 7 | * Return: no return. 8 | * 9 | */ 10 | 11 | void print_chessboard(char (*a)[8]) 12 | { 13 | unsigned int i, m = 0; 14 | 15 | for (i = 0; i < 64; i++) 16 | { 17 | if (i % 8 == 0 && i != 0) 18 | { 19 | m = i; 20 | _putchar('\n'); 21 | } 22 | _putchar(a[i / 8][i - m]); 23 | } 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - prints the sum of the two 6 | * diagonals of a square matrix of integers 7 | * @a: input pointer. 8 | * @size: size of the matrix 9 | * Return: no return. 10 | */ 11 | 12 | void print_diagsums(int *a, int size) 13 | { 14 | int i, sum1 = 0, sum2 = 0; 15 | 16 | for (i = 0; i < (size * size); i++) 17 | { 18 | if (i % (size + 1) == 0) 19 | sum1 += *(a + i); 20 | if (i % (size - 1) == 0 && i != 0 && i < size * size - 1) 21 | sum2 += *(a + i); 22 | } 23 | printf("%d, %d\n", sum1, sum2); 24 | 25 | } 26 | 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | What are pointers to pointers and how to use them 2 | 3 | What are multidimensional arrays and how to use them 4 | 5 | What are the most common C standard library functions to manipulate strings 6 | -------------------------------------------------------------------------------- /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 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H_INCLUDED 2 | #define MAIN_H_INCLUDED 3 | 4 | /** 5 | * void prototypes() 6 | * int prototypes() 7 | */ 8 | 9 | char *_memset(char *s, char b, unsigned int n); 10 | char *_memcpy(char *dest, char *src, unsigned int n); 11 | char *_strchr(char *s, char c); 12 | unsigned int _strspn(char *s, char *accept); 13 | char *_strpbrk(char *s, char *accept); 14 | char *_strstr(char *haystack, char *needle); 15 | void print_chessboard(char (*a)[8]); 16 | void print_diagsums(int *a, int size); 17 | void set_string(char **s, char *to); 18 | int _putchar(char c); 19 | 20 | #endif /* MAIN_H_INCLUDED */ 21 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _puts_recursion - prints a string, followed by a new line. 6 | * @s: string 7 | * Return: no return. 8 | */ 9 | 10 | void _puts_recursion(char *s) 11 | { 12 | if (*s != '\0') 13 | { 14 | _putchar(*s); 15 | _puts_recursion(s + 1); 16 | } 17 | else 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _print_rev_recursion - prints a string in reverse. 6 | * @s: string 7 | * Return: no return. 8 | */ 9 | 10 | void _print_rev_recursion(char *s) 11 | { 12 | if (*s != '\0') 13 | { 14 | _print_rev_recursion(s + 1); 15 | _putchar(*s); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strlen_recursion - returns the length of a string. 6 | * @s: string 7 | * Return: the length of a string. 8 | */ 9 | int _strlen_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | return (0); 13 | else 14 | return (1 + _strlen_recursion(s + 1)); 15 | } 16 | 17 | /** 18 | * comparator - compares each character of the string. 19 | * @s: string 20 | * @n1: smallest iterator. 21 | * @n2: biggest iterator. 22 | * Return: . 23 | */ 24 | int comparator(char *s, int n1, int n2) 25 | { 26 | if (*(s + n1) == *(s + n2)) 27 | { 28 | if (n1 == n2 || n1 == n2 + 1) 29 | return (1); 30 | return (0 + comparator(s, n1 + 1, n2 - 1)); 31 | } 32 | return (0); 33 | } 34 | 35 | /** 36 | * is_palindrome - detects if a string is a palindrome. 37 | * @s: string. 38 | * Return: 1 if s is a palindrome, 0 if not. 39 | */ 40 | int is_palindrome(char *s) 41 | { 42 | if (*s == '\0') 43 | return (1); 44 | return (comparator(s, 0, _strlen_recursion(s) - 1)); 45 | } 46 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | /** 2 | * wildcmp - compares two strings. 3 | * @s1: string 1. 4 | * @s2: string 2. It can contains a * as a special character. 5 | * Return: 1 if are identical, 0 if not. 6 | */ 7 | int wildcmp(char *s1, char *s2) 8 | { 9 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 10 | return (0); 11 | if (*s1 == '\0' && *s2 == '\0') 12 | return (1); 13 | if (*s1 == *s2) 14 | return (wildcmp(s1 + 1, s2 + 1)); 15 | if (*s2 == '*') 16 | return (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strlen_recursion - returns the length of a string. 6 | * @s: string 7 | * Return: the length of a string. 8 | */ 9 | 10 | int _strlen_recursion(char *s) 11 | { 12 | if (*s == '\0') 13 | return (0); 14 | else 15 | return (1 + _strlen_recursion(s + 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * factorial - returns the factorial of a given number. 6 | * @n: input number. 7 | * Return: factorial of the number. 8 | */ 9 | 10 | int factorial(int n) 11 | { 12 | if (n < 0) 13 | return (-1); 14 | else if (n == 0) 15 | return (1); 16 | else 17 | return (n * factorial(n - 1)); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _pow_recursion - returns the value of x raised to the power of y. 6 | * @x: base. 7 | * @y: exponent. 8 | * Return: value of the exponentiation. 9 | */ 10 | 11 | int _pow_recursion(int x, int y) 12 | { 13 | if (y < 0) 14 | return (-1); 15 | else if (y == 0) 16 | return (1); 17 | else 18 | return (x * _pow_recursion(x, y - 1)); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * power_operation - returns the natural square root of a number. 6 | * @n: input number. 7 | * @c: iterator. 8 | * Return: square root or -1. 9 | */ 10 | int power_operation(int n, int c) 11 | { 12 | if (c % (n / c) == 0) 13 | { 14 | if (c * (n / c) == n) 15 | return (c); 16 | else 17 | return (-1); 18 | } 19 | return (0 + power_operation(n, c + 1)); 20 | } 21 | /** 22 | * * _sqrt_recursion - returns the natural square root of a number. 23 | * @n: input number. 24 | * Return: natural square root. 25 | */ 26 | int _sqrt_recursion(int n) 27 | { 28 | if (n < 0) 29 | return (-1); 30 | if (n == 0) 31 | return (0); 32 | if (n == 1) 33 | return (1); 34 | return (power_operation(n, 2)); 35 | } 36 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * is_prime - detects if an input number is a prime number. 6 | * @n: input number. 7 | * @c: iterator. 8 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 9 | */ 10 | int is_prime(unsigned int n, unsigned int c) 11 | { 12 | if (n % c == 0) 13 | { 14 | if (n == c) 15 | return (1); 16 | else 17 | return (0); 18 | } 19 | return (0 + is_prime(n, c + 1)); 20 | } 21 | /** 22 | * is_prime_number - detects if an input number is a prime number. 23 | * @n: input number. 24 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 25 | */ 26 | int is_prime_number(int n) 27 | { 28 | if (n == 0) 29 | return (0); 30 | if (n < 0) 31 | return (0); 32 | if (n == 1) 33 | return (0); 34 | return (is_prime(n, 2)); 35 | } 36 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | What is recursion 2 | 3 | How to implement recursion 4 | 5 | In what situations you should implement recursion 6 | 7 | In what situations you shouldn’t implement recursion 8 | -------------------------------------------------------------------------------- /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 MAIN_H_INCLUDED 2 | #define MAIN_H_INCLUDED 3 | 4 | /** 5 | * void prototypes() 6 | * int prototypes() 7 | */ 8 | 9 | int _putchar(char c); 10 | void _puts_recursion(char *s); 11 | void _print_rev_recursion(char *s); 12 | int _strlen_recursion(char *s); 13 | int factorial(int n); 14 | int _pow_recursion(int x, int y); 15 | int _sqrt_recursion(int n); 16 | int is_prime_number(int n); 17 | int is_palindrome(char *s); 18 | int wildcmp(char *s1, char *s2); 19 | 20 | #endif /* MAIN_H_INCLUDED */ 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | int _isupper(int c); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | char *_memset(char *s, char b, unsigned int n); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | char *_strcat(char *dest, char *src); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | int _isdigit(int c); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | char *_memcpy(char *dest, char *src, unsigned int n); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | char *_strncat(char *dest, char *src, int n); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | int _atoi(char *s); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | char *_strchr(char *s, char c); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | int _strlen(char *s); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | char *_strncpy(char *dest, char *src, int n); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | int _islower(int c); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | void _puts(char *str); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | int _strcmp(char *s1, char *s2); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | unsigned int _strspn(char *s, char *accept); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | int _isalpha(int c); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | char *_strpbrk(char *s, char *accept); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | char *_strstr(char *haystack, char *needle); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | int _abs(int d); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | char *_strcpy(char *dest, char *src); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What is a static library, how does it work, how to create one, and how to use it 4 | 5 | Basic usage of ar, ranlib, nm 6 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -Werror -Wextra -c *.c 3 | ar rc liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif /* MAIN_H */ 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - print the name of the file followed by a new line 6 | * @argc: argument count 7 | * @argv: arguments passed to main as an array of strings 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | (void) argc; 14 | 15 | printf("%s\n", argv[0]); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - print the num of args you passed to it 6 | * @argc: argument count 7 | * @argv: argument vector, array of strings 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | (void) argv; 14 | 15 | printf("%d\n", argc - 1); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - print the min number of coins to make change for an amount of money 7 | * @argc: argument count 8 | * @argv: argument vector, array of strings 9 | * Return: 1 if error, 0 otherwise 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int total, count; 15 | unsigned int i; 16 | char *p; 17 | int cents[] = {25, 10, 5, 2}; 18 | 19 | if (argc != 2) 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | total = strtol(argv[1], &p, 10); 25 | count = 0; 26 | 27 | if (!*p) 28 | { 29 | while (total > 1) 30 | { 31 | for (i = 0; i < sizeof(cents[i]); i++) 32 | { 33 | if (total >= cents[i]) 34 | { 35 | count += total / cents[i]; 36 | total = total % cents[i]; 37 | } 38 | } 39 | } 40 | if (total == 1) 41 | count++; 42 | } 43 | else 44 | { 45 | printf("Error\n"); 46 | return (1); 47 | } 48 | 49 | printf("%d\n", count); 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - print all arguments it receives 6 | * @argc: argument count 7 | * @argv: argument vector, array of strings 8 | * Return: 0 9 | */ 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 "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - multiply 2 numbers passed to main, or Error 7 | * @argc: argument count 8 | * @argv: argument vector 9 | * Return: 1 if error, 0 if function runs correctly 10 | */ 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | (void) argc; 15 | 16 | if (argv[1] && argv[2]) 17 | { 18 | printf("%d\n", atoi(argv[1]) * atoi(argv[2])); 19 | return (0); 20 | } 21 | else 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - add 2 positive numbers and print the result 7 | * @argc: argument count 8 | * @argv: argument vector, array of strings 9 | * Description: If no number is passed to program, print 0. 10 | * If one of the numbers contain non-digits, print Error. 11 | * Return: 1 if error, 0 if function runs properly. 12 | */ 13 | 14 | int main(int argc, char *argv[]) 15 | { 16 | int total, i; 17 | char *p; 18 | int num; 19 | 20 | total = 0; 21 | if (argc > 1) 22 | { 23 | for (i = 1; argv[i]; i++) 24 | { 25 | num = strtol(argv[i], &p, 10); 26 | if (!*p) 27 | total += num; 28 | else 29 | { 30 | printf("Error\n"); 31 | return (1); 32 | } 33 | } 34 | } 35 | printf("%d\n", total); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | How to use arguments passed to your program 2 | 3 | What are two prototypes of main that you know of, and in which case do you use one or the other 4 | 5 | How to use __attribute__((unused)) or (void) to compile functions with unused variables or parameters 6 | -------------------------------------------------------------------------------- /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 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char c); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0B-malloc_free/.2-str_concat.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x0B-malloc_free/.2-str_concat.c.swp -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_array - creates an array of chars, and 5 | * initializes it with a specific char. 6 | * @size: Size of the array 7 | * @c: Character to insert 8 | * Return: NULL if size is zero or if it fails, 9 | * pointer to array if everything is normal. 10 | */ 11 | 12 | char *create_array(unsigned int size, char c) 13 | { 14 | char *array; 15 | unsigned int index; 16 | 17 | if (size == 0) 18 | return (NULL); 19 | array = malloc(sizeof(char) * size); 20 | 21 | if (array == NULL) 22 | return (NULL); 23 | 24 | for (index = 0; index < size; index++) 25 | array[index] = c; 26 | 27 | return (array); 28 | } 29 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = _strdup("ALX SE"); 15 | if (s == NULL) 16 | { 17 | printf("failed to allocate memory\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include "string.h" 3 | /** 4 | * _strdup - returns a pointer to a newly allocated 5 | * space in memory, which contains a copy of the 6 | * string given as a parameter. 7 | * @str:String to be copied 8 | * 9 | * Return: NULL in case of error, pointer to allocated 10 | * space 11 | */ 12 | 13 | char *_strdup(char *str) 14 | { 15 | char *ptr; 16 | int i; 17 | int len; 18 | 19 | /* checking if the string is a NULL */ 20 | if (str == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; str[i]; i++) 24 | len++; 25 | /* allocating memory for the new string */ 26 | ptr = malloc(sizeof(char) * (len + 1)); 27 | 28 | /* checking if memory was succesfull */ 29 | if (ptr == NULL) 30 | return (NULL); 31 | 32 | /* copying the contents of the string to the new string */ 33 | for (i = 0; str[i]; i++) 34 | { 35 | ptr[i] = str[i]; 36 | } 37 | 38 | /* adding the NULL terminator character */ 39 | ptr[len] = '\0'; 40 | 41 | /* return the pointer to the new string */ 42 | return (ptr); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code for ALX School students. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = str_concat("Betty ", "Holberton"); 15 | if (s == NULL) 16 | { 17 | printf("failed\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - a function that concatenates two strings. 6 | * @s1:First string 7 | * @s2:Second string 8 | * 9 | * Return: NULL in case of failure , but pointer to new string in 10 | * case of success 11 | */ 12 | char *str_concat(char *s1, char *s2) 13 | { 14 | int i, j; 15 | int len1, len2; 16 | char *ptr; 17 | 18 | if (s1 == NULL) 19 | { 20 | s1 = ""; 21 | } 22 | 23 | if (s2 == NULL) 24 | { 25 | s2 = ""; 26 | } 27 | 28 | len1 = strlen(s1); 29 | len2 = strlen(s2); 30 | 31 | ptr = malloc(sizeof(char) * (len1 + len2 + 1)); 32 | 33 | if (ptr == NULL) 34 | { 35 | return (NULL); 36 | } 37 | 38 | for (i = 0; i < len1; i++) 39 | { 40 | ptr[i] = s1[i]; 41 | } 42 | 43 | for (j = 0; j < len2; j++) 44 | { 45 | ptr[len1 + j] = s2[j]; 46 | } 47 | 48 | ptr[len1 + len2] = '\0'; 49 | return (ptr); 50 | } 51 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * alloc_grid - returns a pointer to a 5 | * 2 dimensional array of integers. 6 | * @width: width oàf array 7 | * @height: height of array 8 | * 9 | * Return: pointer to the array 10 | */ 11 | 12 | int **alloc_grid(int width, int height) 13 | { 14 | int **array; 15 | int i = 0, j; 16 | 17 | if (width == 0 || height == 0) 18 | return (NULL); 19 | array = (int **) malloc(sizeof(int *) * height); 20 | if (array != NULL) 21 | { 22 | for (; i < height; i++) 23 | { 24 | array[i] = (int *) malloc(sizeof(int) * width); 25 | if (array[i] != NULL) 26 | { 27 | for (j = 0; j < width; j++) 28 | array[i][j] = 0; 29 | } 30 | else 31 | { 32 | while (i >= 0) 33 | { 34 | free(array[i]); 35 | i--; 36 | } 37 | free(array); 38 | return (NULL); 39 | } 40 | } 41 | return (array); 42 | } 43 | else 44 | { 45 | return (NULL); 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * free_grid - frees a 2d array 5 | * @grid:memory block to be freed 6 | * @height:height of the array 7 | * Return:returns void 8 | */ 9 | 10 | void free_grid(int **grid, int height) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < height; i++) 15 | free(grid[i]); 16 | 17 | free(grid); 18 | } 19 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What is the difference between automatic and dynamic allocation 4 | 5 | What is malloc and free and how to use them 6 | 7 | Why and when use malloc 8 | 9 | How to use valgrind to check for memory leak 10 | -------------------------------------------------------------------------------- /0x0B-malloc_free/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _putchar - writes the character c to stdout 4 | * @c: The character to print 5 | * 6 | * Return: On success 1. 7 | * On error, -1 is returned, and errno is set appropriately. 8 | */ 9 | 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /0x0B-malloc_free/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x0B-malloc_free/c -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | int _putchar(char); 7 | char *create_array(unsigned int size, char c); 8 | char *_strdup(char *str); 9 | char *str_concat(char *s1, char *s2); 10 | int **alloc_grid(int width, int height); 11 | void free_grid(int **grid, int height); 12 | char *argstostr(int ac, char **av); 13 | char **strtow(char *str); 14 | 15 | #endif /* MAIN_H */ 16 | -------------------------------------------------------------------------------- /0x0B-malloc_free/s: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x0B-malloc_free/s -------------------------------------------------------------------------------- /0x0B-malloc_free/test: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * str_concat - a function that concatenates two strings. 5 | * @s1:First string 6 | * @s2:Second string 7 | * 8 | * Return: NULL in case of failure , but pointer to new string in 9 | * case of success 10 | */ 11 | 12 | char *str_concat(char *s1, char *s2) 13 | { 14 | char *concat_str; 15 | int index, concat_index = 0, len = 0; 16 | 17 | if (s1 == NULL) 18 | s1 = ""; 19 | 20 | if (s2 == NULL) 21 | s2 = ""; 22 | 23 | for (index = 0; s1[index] || s2[index]; index++) 24 | len++; 25 | 26 | concat_str = malloc(sizeof(char) * len); 27 | 28 | if (concat_str == NULL) 29 | return (NULL); 30 | 31 | for (index = 0; s1[index]; index++) 32 | concat_str[concat_index++] = s1[index]; 33 | 34 | for (index = 0; s2[index]; index++) 35 | concat_str[concat_index++] = s2[index]; 36 | 37 | return (concat_str); 38 | } 39 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/.100-realloc.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x0C-more_malloc_free/.100-realloc.c.swp -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * malloc_checked - allocates memory using malloc 6 | * @b: memory to allocate 7 | * 8 | * Return: pointer to allocated memory or normal process termination if error 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | void *p; 13 | 14 | p = malloc(b); 15 | if (p == NULL) 16 | exit(98); 17 | return (p); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - concatenates two strings. 6 | * @s1: first string to copy 7 | * @s2: second string to copy 8 | * @n: number of bytes of s2 to copy 9 | * 10 | * Return: char pointer to newly allocated place in memory 11 | */ 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | unsigned int i, j, k; 15 | char *s; 16 | 17 | if (s1 == NULL) 18 | i = 0; 19 | else 20 | { 21 | for (i = 0; s1[i]; i++) 22 | ; 23 | } 24 | if (s2 == NULL) 25 | j = 0; 26 | else 27 | { 28 | for (j = 0; s2[j]; j++) 29 | ; 30 | } 31 | if (j > n) 32 | j = n; 33 | s = malloc(sizeof(char) * (i + j + 1)); 34 | if (s == NULL) 35 | return (NULL); 36 | for (k = 0; k < i; k++) 37 | s[k] = s1[k]; 38 | for (k = 0; k < j; k++) 39 | s[k + i] = s2[k]; 40 | s[i + j] = '\0'; 41 | return (s); 42 | } 43 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _realloc - reallocates a memory block using malloc and free. 6 | * @ptr: pointer to previously allocated memory 7 | * @old_size: size of allocated space for ptr 8 | * @new_size: size of newly allocated space 9 | * 10 | * Return: pointer to newly allocated memory, or NULL if failure 11 | */ 12 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 13 | { 14 | char *p; 15 | unsigned int i, max = new_size; 16 | char *oldp = ptr; 17 | 18 | if (ptr == NULL) 19 | { 20 | p = malloc(new_size); 21 | return (p); 22 | } 23 | else if (new_size == 0) 24 | { 25 | free(ptr); 26 | return (NULL); 27 | } 28 | else if (new_size == old_size) 29 | return (ptr); 30 | 31 | p = malloc(new_size); 32 | if (p == NULL) 33 | return (NULL); 34 | if (new_size > old_size) 35 | max = old_size; 36 | for (i = 0; i < max; i++) 37 | p[i] = oldp[i]; 38 | free(ptr); 39 | return (p); 40 | } 41 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _calloc - allocates memory for an array, using malloc. 6 | * @nmemb: number of elements in the array 7 | * @size: size in bytes of the elements 8 | * 9 | * Return: void pointer to allocated memory 10 | */ 11 | void *_calloc(unsigned int nmemb, unsigned int size) 12 | { 13 | char *p; 14 | unsigned int i; 15 | 16 | if (nmemb == 0 || size == 0) 17 | return (NULL); 18 | p = malloc(nmemb * size); 19 | if (p == NULL) 20 | return (NULL); 21 | for (i = 0; i < (nmemb * size); i++) 22 | p[i] = 0; 23 | return (p); 24 | } 25 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * array_range - creates an array of integers. 6 | * @min: first integer number 7 | * @max: last integer number 8 | * 9 | * Return: pointer to newly allocated memory or NULL if fails 10 | */ 11 | int *array_range(int min, int max) 12 | { 13 | int i, l; 14 | int *a; 15 | 16 | if (min > max) 17 | return (NULL); 18 | l = max - min + 1; 19 | a = malloc(sizeof(int) * l); 20 | if (a == NULL) 21 | return (NULL); 22 | for (i = 0; i < l; i++, min++) 23 | { 24 | a[i] = min; 25 | } 26 | return (a); 27 | } 28 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | How to use the exit function 4 | 5 | What are the functions calloc and realloc from the standard library and how to use them 6 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/_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 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | void *malloc_checked(unsigned int b); 6 | char *string_nconcat(char *s1, char *s2, unsigned int n); 7 | void *_calloc(unsigned int nmemb, unsigned int size); 8 | int *array_range(int min, int max); 9 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 10 | 11 | #endif /*MAIN_H*/ 12 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_LIKE_MACRO_H 2 | #define OBJECT_LIKE_MACRO_H 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif /* PI */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file program was compiled from 5 | * Return: Always 0 (success) 6 | */ 7 | 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef THREE_FUNCTION_LIKE_MACRO_H 2 | 3 | #define THREE_FUNCTION_LIKE_MACRO_H 4 | 5 | #define ABS(x) (x > 0 ? (x) : (x) * -1) 6 | 7 | #endif /* THREE_FUNCTION_LIKE_MACRO_H */ 8 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef FOUR_SUM_H 2 | 3 | #define FOUR_SUM_H 4 | 5 | #define SUM(x, y) ((x) + (y)) 6 | 7 | #endif /* FOUR_SUM_H */ 8 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What are macros and how to use them 4 | 5 | What are the most common predefined macros 6 | 7 | How to include guard your header files 8 | -------------------------------------------------------------------------------- /0x0D-preprocessor/_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 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x0D-preprocessor/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include "0-object_like_macro.h" 6 | #include "1-pi.h" 7 | #include "3-function_like_macro.h" 8 | #include "4-sum.h" 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /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/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dog.h" 4 | 5 | /** 6 | * new_dog - new dog 7 | * @name: name's dog 8 | * @age: age's dog 9 | * @owner: owner's dog 10 | * Return: newdog 11 | */ 12 | 13 | dog_t *new_dog(char *name, float age, char *owner) 14 | { 15 | 16 | int i = 0, j = 0, k; 17 | dog_t *doge; 18 | 19 | while (name[i] != '\0') 20 | i++; 21 | while (owner[j] != '\0') 22 | j++; 23 | doge = malloc(sizeof(dog_t)); 24 | if (doge == NULL) 25 | { 26 | free(doge); 27 | return (NULL); 28 | 29 | } 30 | doge->name = malloc(i * sizeof(doge->name)); 31 | if (doge->name == NULL) 32 | { 33 | free(doge->name); 34 | free(doge); 35 | return (NULL); 36 | } 37 | for (k = 0; k <= i; k++) 38 | doge->name[k] = name[k]; 39 | doge->age = age; 40 | doge->owner = malloc(j * sizeof(doge->owner)); 41 | if (doge->owner == NULL) 42 | { 43 | free(doge->owner); 44 | free(doge->name); 45 | free(doge); 46 | return (NULL); 47 | } 48 | for (k = 0; k <= j; k++) 49 | doge->owner[k] = owner[k]; 50 | return (doge); 51 | } 52 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * free_dog - free memory of dog 6 | * @d: poiner to dog 7 | */ 8 | 9 | void free_dog(dog_t *d) 10 | { 11 | if (d) 12 | { 13 | if (d->name) 14 | free(d->name); 15 | if (d->owner) 16 | free(d->owner); 17 | free(d); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What are structures, when, why and how to use them 4 | 5 | How to use typedef 6 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_DOG 2 | #define FILE_DOG 3 | 4 | /** 5 | * struct dog - structure dog 6 | * @name: atribute pointer to char name's dog 7 | * @age: atribute float age's dog 8 | * @owner: atribute pointer to char owner's dog 9 | */ 10 | 11 | struct dog 12 | { 13 | char *name; 14 | float age; 15 | char *owner; 16 | }; 17 | 18 | typedef struct dog dog_t; 19 | 20 | void init_dog(struct dog *d, char *name, float age, char *owner); 21 | 22 | void print_dog(struct dog *d); 23 | 24 | dog_t *new_dog(char *name, float age, char *owner); 25 | 26 | void free_dog(dog_t *d); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /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 /* CALC_H */ 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/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What are function pointers and how to use them 4 | 5 | What does a function pointer exactly hold 6 | 7 | Where does a function pointer point to in the virtual memory 8 | -------------------------------------------------------------------------------- /0x0F-function_pointers/_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 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNC_H 2 | #define FUNC_H 3 | 4 | #include 5 | 6 | void print_name(char *name, void (*f)(char *)); 7 | void array_iterator(int *array, size_t size, void (*action)(int)); 8 | int int_index(int *array, int size, int (*cmp)(int)); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - adds num 5 | * @n: param 6 | * Return: int 7 | */ 8 | 9 | int sum_them_all(const unsigned int n, ...) 10 | { 11 | va_list valist; 12 | unsigned int i; 13 | int sum = 0; 14 | 15 | if (n == 0) 16 | return (0); 17 | 18 | va_start(valist, n); 19 | 20 | for (i = 0; i < n; i++) 21 | sum += va_arg(valist, int); 22 | 23 | va_end(valist); 24 | 25 | return (sum); 26 | } 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_numbers - prints num 5 | * @separator: pointer param 6 | * @n: param 7 | */ 8 | void print_numbers(const char *separator, const unsigned int n, ...) 9 | { 10 | va_list valist; 11 | unsigned int i; 12 | 13 | va_start(valist, n); 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | printf("%d", va_arg(valist, int)); 18 | if (separator && i < n - 1) 19 | printf("%s", separator); 20 | } 21 | 22 | printf("\n"); 23 | va_end(valist); 24 | } 25 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_strings - prints strings 5 | * @separator: pointer param 6 | * @n: param 7 | */ 8 | void print_strings(const char *separator, const unsigned int n, ...) 9 | { 10 | va_list valist; 11 | unsigned int i; 12 | char *str; 13 | 14 | va_start(valist, n); 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | str = va_arg(valist, char *); 19 | 20 | if (str) 21 | printf("%s", str); 22 | else 23 | printf("(nil)"); 24 | 25 | if (i < n - 1) 26 | if (separator) 27 | printf("%s", separator); 28 | } 29 | 30 | printf("\n"); 31 | va_end(valist); 32 | } 33 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * print_all - prints all 5 | * @format: param 6 | */ 7 | void print_all(const char * const format, ...) 8 | { 9 | va_list valist; 10 | unsigned int i = 0, j, k = 0; 11 | char *str; 12 | const char t_arg[] = "cifs"; 13 | 14 | va_start(valist, format); 15 | while (format && format[i]) 16 | { 17 | j = 0; 18 | while (t_arg[j]) 19 | { 20 | if (format[i] == t_arg[j] && k) 21 | { 22 | printf(", "); 23 | break; 24 | } j++; 25 | } 26 | switch (format[i]) 27 | { 28 | case 'c': 29 | printf("%c", va_arg(valist, int)), k = 1; 30 | break; 31 | case 'i': 32 | printf("%d", va_arg(valist, int)), k = 1; 33 | break; 34 | case 'f': 35 | printf("%f", va_arg(valist, double)), k = 1; 36 | break; 37 | case 's': 38 | str = va_arg(valist, char *), k = 1; 39 | if (!str) 40 | { 41 | printf("(nil)"); 42 | break; 43 | } 44 | printf("%s", str); 45 | break; 46 | } i++; 47 | } 48 | printf("\n"), va_end(valist); 49 | } 50 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What are variadic functions 4 | 5 | How to use va_start, va_arg and va_end macros 6 | 7 | Why and how to use the const type qualifier 8 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _VARIADIC_FUNCTIONS_ 2 | #define _VARIADIC_FUNCTIONS_ 3 | 4 | #include 5 | #include 6 | 7 | void print_strings(const char *separator, const unsigned int n, ...); 8 | void print_all(const char * const format, ...); 9 | int sum_them_all(const unsigned int n, ...); 10 | void print_numbers(const char *separator, const unsigned int n, ...); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_list - prints list. 6 | * @h: param list 7 | * Return: struct 8 | */ 9 | 10 | size_t print_list(const list_t *h) 11 | { 12 | size_t l_node; 13 | 14 | l_node = 0; 15 | while (h != NULL) 16 | { 17 | if (h->str == NULL) 18 | printf("[%d] %s\n", 0, "(nil)"); 19 | else 20 | printf("[%d] %s\n", h->len, h->str); 21 | h = h->next; 22 | l_node++; 23 | } 24 | return (l_node); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_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 elements in a linked list_t list. 8 | */ 9 | size_t list_len(const list_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 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * bmain - function executed before main 5 | * Return: no return. 6 | */ 7 | 8 | void __attribute__ ((constructor)) bmain() 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_node - Adds a new node at the beginning 6 | * of a list_t list. 7 | * @head: A pointer to the head of the list_t list. 8 | * @str: The string to be added to the list_t list. 9 | * 10 | * Return: If the function fails - NULL. 11 | * Otherwise - the address of the new element. 12 | */ 13 | list_t *add_node(list_t **head, const char *str) 14 | { 15 | list_t *new; 16 | char *dup_str; 17 | int len; 18 | 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | dup_str = strdup(str); 24 | if (dup_str == NULL) 25 | { 26 | free(new); 27 | return (NULL); 28 | } 29 | 30 | for (len = 0; str[len];) 31 | len++; 32 | 33 | new->str = dup_str; 34 | new->len = len; 35 | new->next = *head; 36 | 37 | *head = new; 38 | 39 | return (new); 40 | } 41 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_node_end - Adds a new node at the end 6 | * of a list_t list. 7 | * @head: A pointer the head of the list_t list. 8 | * @str: The string to be added to the list_t list. 9 | * 10 | * Return: If the function fails - NULL. 11 | * Otherwise - the address of the new element. 12 | */ 13 | list_t *add_node_end(list_t **head, const char *str) 14 | { 15 | char *dup; 16 | int len; 17 | list_t *new, *last; 18 | 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | dup = strdup(str); 24 | if (str == NULL) 25 | { 26 | free(new); 27 | return (NULL); 28 | } 29 | 30 | for (len = 0; str[len];) 31 | len++; 32 | 33 | new->str = dup; 34 | new->len = len; 35 | new->next = NULL; 36 | 37 | if (*head == NULL) 38 | *head = new; 39 | 40 | else 41 | { 42 | last = *head; 43 | while (last->next != NULL) 44 | last = last->next; 45 | last->next = new; 46 | } 47 | 48 | return (*head); 49 | } 50 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_list - Frees a list_t list 6 | * @head: A pointer to the list_t list 7 | */ 8 | void free_list(list_t *head) 9 | { 10 | list_t *tmp; 11 | 12 | while (head) 13 | { 14 | tmp = head->next; 15 | free(head->str); 16 | free(head); 17 | head = tmp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | When and why using linked lists vs arrays 4 | 5 | How to build and use linked lists 6 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct list_s 18 | { 19 | char *str; 20 | unsigned int len; 21 | struct list_s *next; 22 | } list_t; 23 | 24 | size_t print_list(const list_t *h); 25 | size_t list_len(const list_t *h); 26 | list_t *add_node(list_t **head, const char *str); 27 | list_t *add_node_end(list_t **head, const char *str); 28 | void free_list(list_t *head); 29 | 30 | #endif /* _LISTS_ */ 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the number of elements in a linked lists 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 != NULL) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | num++; 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 if it succeeded, -1 if it failed. 9 | */ 10 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 11 | { 12 | unsigned int i; 13 | listint_t *prev; 14 | listint_t *next; 15 | 16 | prev = *head; 17 | 18 | if (index != 0) 19 | { 20 | for (i = 0; i < index - 1 && prev != NULL; i++) 21 | { 22 | prev = prev->next; 23 | } 24 | 25 | } 26 | 27 | if (prev == NULL || (prev->next == NULL && index != 0)) 28 | { 29 | return (-1); 30 | } 31 | 32 | next = prev->next; 33 | 34 | if (index != 0) 35 | { 36 | prev->next = next->next; 37 | free(next); 38 | } 39 | else 40 | { 41 | free(prev); 42 | *head = next; 43 | } 44 | 45 | return (1); 46 | } 47 | -------------------------------------------------------------------------------- /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 *p; 12 | listint_t *n; 13 | 14 | p = NULL; 15 | n = NULL; 16 | 17 | while (*head != NULL) 18 | { 19 | n = (*head)->next; 20 | (*head)->next = p; 21 | p = *head; 22 | *head = n; 23 | } 24 | 25 | *head = p; 26 | return (*head); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/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 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /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 == NULL) 16 | return (NULL); 17 | 18 | new->n = n; 19 | new->next = *head; 20 | *head = new; 21 | 22 | return (*head); 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 == NULL) 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 | * Return: return nothing 8 | */ 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *temp; 12 | 13 | while ((temp = head) != NULL) 14 | { 15 | head = head->next; 16 | free(temp); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a linked list 5 | * @head: 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 nth node of a linked list 5 | * @head: head of a list. 6 | * @index: index of the node. 7 | * 8 | * Return: nth node. If node does not exist, returns NULL. 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < index && head != NULL; i++) 15 | { 16 | head = head->next; 17 | } 18 | 19 | return (head); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - calculates the sum of all the data in a listint_t list 5 | * @head: first node in the linked list 6 | * 7 | * Return: the resulting sum 8 | */ 9 | int sum_listint(listint_t *head) 10 | { 11 | int sum; 12 | 13 | sum = 0; 14 | while (head != NULL) 15 | { 16 | sum += head->n; 17 | head = head->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/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | How to use linked lists 4 | 5 | Start to look for the right source of information without too much help 6 | -------------------------------------------------------------------------------- /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/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "main.h" 4 | 5 | 6 | 7 | /** 8 | 9 | * main - check the code 10 | 11 | * 12 | 13 | * Return: Always 0. 14 | 15 | */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | unsigned int n; 22 | 23 | 24 | 25 | n = binary_to_uint("1"); 26 | 27 | printf("%u\n", n); 28 | 29 | n = binary_to_uint("101"); 30 | 31 | printf("%u\n", n); 32 | 33 | n = binary_to_uint("1e01"); 34 | 35 | printf("%u\n", n); 36 | 37 | n = binary_to_uint("1100010"); 38 | 39 | printf("%u\n", n); 40 | 41 | n = binary_to_uint("0000000000000000000110010010"); 42 | 43 | printf("%u\n", n); 44 | 45 | return (0); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "main.h" 4 | 5 | 6 | 7 | /** 8 | 9 | * main - check the code 10 | 11 | * 12 | 13 | * Return: Always 0. 14 | 15 | */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | print_binary(0); 22 | 23 | printf("\n"); 24 | 25 | print_binary(1); 26 | 27 | printf("\n"); 28 | 29 | print_binary(98); 30 | 31 | printf("\n"); 32 | 33 | print_binary(1024); 34 | 35 | printf("\n"); 36 | 37 | print_binary((1 << 10) + 1); 38 | 39 | printf("\n"); 40 | 41 | return (0); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary equivalent of a decimal number 5 | * @n: number to print in binary 6 | */ 7 | void print_binary(unsigned long int n) 8 | { 9 | int i, count = 0; 10 | unsigned long int current; 11 | 12 | for (i = 63; i >= 0; i--) 13 | { 14 | current = n >> i; 15 | 16 | if (current & 1) 17 | { 18 | _putchar('1'); 19 | count++; 20 | } 21 | else if (count) 22 | _putchar('0'); 23 | } 24 | if (!count) 25 | _putchar('0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * Return: 0 if big endian, 1 if little endian 6 | */ 7 | int get_endianness(void) 8 | { 9 | int num; 10 | 11 | num = 1; 12 | if (*(char *)&num == 1) 13 | return (1); 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given index 5 | * @n: unsigned long int input. 6 | * @index: index of the bit. 7 | * 8 | * Return: value of the bit. 9 | */ 10 | int get_bit(unsigned long int n, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | if (n == 0 && index < 64) 15 | return (0); 16 | 17 | for (i = 0; i <= 63; n >>= 1, i++) 18 | { 19 | if (index == i) 20 | { 21 | return (n & 1); 22 | } 23 | } 24 | 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "main.h" 4 | 5 | 6 | 7 | /** 8 | 9 | * main - check the code 10 | 11 | * 12 | 13 | * Return: Always 0. 14 | 15 | */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | int n; 22 | 23 | 24 | 25 | n = get_bit(1024, 10); 26 | 27 | printf("%d\n", n); 28 | 29 | n = get_bit(98, 1); 30 | 31 | printf("%d\n", n); 32 | 33 | n = get_bit(1024, 0); 34 | 35 | printf("%d\n", n); 36 | 37 | return (0); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "main.h" 4 | 5 | 6 | 7 | /** 8 | 9 | * main - check the code 10 | 11 | * 12 | 13 | * Return: Always 0. 14 | 15 | */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | unsigned long int n; 22 | 23 | 24 | 25 | n = 1024; 26 | 27 | set_bit(&n, 5); 28 | 29 | printf("%lu\n", n); 30 | 31 | n = 0; 32 | 33 | set_bit(&n, 10); 34 | 35 | printf("%lu\n", n); 36 | 37 | n = 98; 38 | 39 | set_bit(&n, 0); 40 | 41 | printf("%lu\n", n); 42 | 43 | return (0); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets the value of a bit to 1. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | *n = (*n | m); 20 | 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a bit to 0. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | 20 | if (*n & m) 21 | *n ^= m; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "main.h" 4 | 5 | 6 | 7 | /** 8 | 9 | * main - check the code 10 | 11 | * 12 | 13 | * Return: Always 0. 14 | 15 | */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | unsigned long int n; 22 | 23 | 24 | 25 | n = 1024; 26 | 27 | clear_bit(&n, 10); 28 | 29 | printf("%lu\n", n); 30 | 31 | n = 0; 32 | 33 | clear_bit(&n, 10); 34 | 35 | printf("%lu\n", n); 36 | 37 | n = 98; 38 | 39 | clear_bit(&n, 1); 40 | 41 | printf("%lu\n", n); 42 | 43 | return (0); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - returns the number of bits you would 5 | * need to flip to get from one number to another 6 | * @n: number one. 7 | * @m: number two. 8 | * 9 | * Return: number of bits. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned int nbits; 14 | 15 | for (nbits = 0; n || m; n >>= 1, m >>= 1) 16 | { 17 | if ((n & 1) != (m & 1)) 18 | nbits++; 19 | } 20 | 21 | return (nbits); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "main.h" 4 | 5 | 6 | 7 | /** 8 | 9 | * main - check the code 10 | 11 | * 12 | 13 | * Return: Always 0. 14 | 15 | */ 16 | 17 | int main(void) 18 | 19 | { 20 | 21 | unsigned int n; 22 | 23 | 24 | 25 | n = flip_bits(1024, 1); 26 | 27 | printf("%u\n", n); 28 | 29 | n = flip_bits(402, 98); 30 | 31 | printf("%u\n", n); 32 | 33 | n = flip_bits(1024, 3); 34 | 35 | printf("%u\n", n); 36 | 37 | n = flip_bits(1024, 1025); 38 | 39 | printf("%u\n", n); 40 | 41 | return (0); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | Look for the right source of information without too much help 4 | 5 | How to manipulate bits and use bitwise operators 6 | -------------------------------------------------------------------------------- /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 | 11 | int _putchar(char c) 12 | { 13 | return (write(1, &c, 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x14-bit_manipulation/a -------------------------------------------------------------------------------- /0x14-bit_manipulation/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x14-bit_manipulation/b -------------------------------------------------------------------------------- /0x14-bit_manipulation/bin: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to an 5 | * unsigned int. 6 | * @b: binary. 7 | * 8 | * Return: unsigned int. 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int ui; 13 | int len, base_two; 14 | 15 | if (!b) 16 | return (0); 17 | 18 | ui = 0; 19 | 20 | for (len = 0; b[len] != '\0'; len++) 21 | ; 22 | 23 | for (len--, base_two = 1; len >= 0; len--, base_two *= 2) 24 | { 25 | if (b[len] != '0' && b[len] != '1') 26 | { 27 | return (0); 28 | } 29 | 30 | if (b[len] & 1) 31 | { 32 | ui += base_two; 33 | } 34 | } 35 | 36 | return (ui); 37 | } 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x14-bit_manipulation/c -------------------------------------------------------------------------------- /0x14-bit_manipulation/d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x14-bit_manipulation/d -------------------------------------------------------------------------------- /0x14-bit_manipulation/e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x14-bit_manipulation/e -------------------------------------------------------------------------------- /0x14-bit_manipulation/f: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x14-bit_manipulation/f -------------------------------------------------------------------------------- /0x14-bit_manipulation/h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x14-bit_manipulation/h -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char c); 5 | 6 | unsigned int binary_to_uint(const char *b); 7 | 8 | void print_binary(unsigned long int n); 9 | 10 | int get_bit(unsigned long int n, unsigned int index); 11 | 12 | int set_bit(unsigned long int *n, unsigned int index); 13 | 14 | int clear_bit(unsigned long int *n, unsigned int index); 15 | 16 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 17 | 18 | int get_endianness(void); 19 | 20 | int _putchar(char c); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - reads a text file and prints the letters 5 | * @filename: filename. 6 | * @letters: numbers of letters printed. 7 | * 8 | * Return: numbers of letters printed. It fails, returns 0. 9 | */ 10 | ssize_t read_textfile(const char *filename, size_t letters) 11 | { 12 | int fd; 13 | ssize_t nrd, nwr; 14 | char *buf; 15 | 16 | if (!filename) 17 | return (0); 18 | 19 | fd = open(filename, O_RDONLY); 20 | 21 | if (fd == -1) 22 | return (0); 23 | 24 | buf = malloc(sizeof(char) * (letters)); 25 | if (!buf) 26 | return (0); 27 | 28 | nrd = read(fd, buf, letters); 29 | nwr = write(STDOUT_FILENO, buf, nrd); 30 | 31 | close(fd); 32 | 33 | free(buf); 34 | 35 | return (nwr); 36 | } 37 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file 5 | * @filename: filename. 6 | * @text_content: content writed in the file. 7 | * 8 | * Return: 1 if it success. -1 if it fails. 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | int nletters; 14 | int rwr; 15 | 16 | if (!filename) 17 | return (-1); 18 | 19 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 20 | if (fd == -1) 21 | return (-1); 22 | 23 | if (!text_content) 24 | text_content = ""; 25 | 26 | for (nletters = 0; text_content[nletters]; nletters++) 27 | ; 28 | 29 | rwr = write(fd, text_content, nletters); 30 | 31 | if (rwr == -1) 32 | return (-1); 33 | 34 | close(fd); 35 | 36 | return (1); 37 | } 38 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text at the end of a file 5 | * @filename: filename. 6 | * @text_content: added content. 7 | * 8 | * Return: 1 if the file exists. -1 if the fails does not exist or if it fails 9 | */ 10 | int append_text_to_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | int nletters; 14 | int rwr; 15 | 16 | if (filename == NULL) 17 | return (-1); 18 | 19 | fd = open(filename, O_WRONLY | O_APPEND); 20 | 21 | if (fd == -1) 22 | return (-1); 23 | 24 | if (text_content) 25 | { 26 | for (nletters = 0; text_content[nletters]; nletters++) 27 | ; 28 | 29 | rwr = write(fd, text_content, nletters); 30 | 31 | if (rwr == -1) 32 | return (-1); 33 | } 34 | 35 | close(fd); 36 | 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | Look for the right source of information online 4 | 5 | How to create, open, close, read and write files 6 | 7 | What are file descriptors 8 | 9 | What are the 3 standard file descriptors, what are their purpose and what are their POSIX names 10 | 11 | How to use the I/O system calls open, close, read and write 12 | 13 | What are and how to use the flags O_RDONLY, O_WRONLY, O_RDWR 14 | 15 | What are file permissions, and how to set them when creating a file with the open system call 16 | 17 | What is a system call 18 | 19 | What is the difference between a function and a system call 20 | -------------------------------------------------------------------------------- /0x15-file_io/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x15-file_io/a.out -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_ 2 | #define _MAIN_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all the elements of a 5 | * dlistint_t list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | printf("%d\n", h->n); 25 | count++; 26 | h = h->next; 27 | } 28 | 29 | return (count); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements in 5 | * a double linked list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | count++; 25 | h = h->next; 26 | } 27 | 28 | return (count); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /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/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - deletes the node at index of a 5 | * dlistint_t linked list 6 | * 7 | * @head: head of the list 8 | * @index: index of the new node 9 | * Return: 1 if it succeeded, -1 if it failed 10 | */ 11 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 12 | { 13 | dlistint_t *h1; 14 | dlistint_t *h2; 15 | unsigned int i; 16 | 17 | h1 = *head; 18 | 19 | if (h1 != NULL) 20 | while (h1->prev != NULL) 21 | h1 = h1->prev; 22 | 23 | i = 0; 24 | 25 | while (h1 != NULL) 26 | { 27 | if (i == index) 28 | { 29 | if (i == 0) 30 | { 31 | *head = h1->next; 32 | if (*head != NULL) 33 | (*head)->prev = NULL; 34 | } 35 | else 36 | { 37 | h2->next = h1->next; 38 | 39 | if (h1->next != NULL) 40 | h1->next->prev = h2; 41 | } 42 | 43 | free(h1); 44 | return (1); 45 | } 46 | h2 = h1; 47 | h1 = h1->next; 48 | i++; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # Doubly linked lists 2 | General 3 | 4 | What is a doubly linked list 5 | 6 | How to use doubly linked lists 7 | 8 | Start to look for the right source of information without too much help 9 | 10 | 11 | ### More Info 12 | ## Please use this data structure for this project: 13 | 14 | * struct dlistint_s - doubly linked list 15 | * @n: integer 16 | * @prev: points to the previous node 17 | * @next: points to the next node 18 | * 19 | * Description: doubly linked list node structure 20 | * 21 | 22 | typedef struct dlistint_s 23 | 24 | { 25 | 26 | int n; 27 | 28 | struct dlistint_s *prev; 29 | 30 | struct dlistint_s *next; 31 | 32 | } dlistint_t; 33 | 34 | 35 | 36 | ![final](https://static.packt-cdn.com/products/9781785285493/graphics/B05348_05_11.jpg) 37 | -------------------------------------------------------------------------------- /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 Holberton project 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -fPIC -c *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/sidneyriffic/holbertonschool-low_level_programming/raw/master/0x17-dynamic_libraries/putshack.so 3 | export LD_PRELOAD=/tmp/putshack.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What is a dynamic library, how does it work, how to create one, and how to use it 4 | 5 | What is the environment variable $LD_LIBRARY_PATH and how to use it 6 | 7 | What are the differences between static and shared libraries 8 | 9 | Basic usage nm, ldd, ldconfig 10 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | int _islower(int c); 9 | int _isalpha(int c); 10 | int _abs(int n); 11 | int _isupper(int c); 12 | int _isdigit(int c); 13 | int _strlen(char *s); 14 | void _puts(char *s); 15 | char *_strcpy(char *dest, char *src); 16 | int _atoi(char *s); 17 | char *_strcat(char *dest, char *src); 18 | char *_strncat(char *dest, char *src, int n); 19 | char *_strncpy(char *dest, char *src, int n); 20 | int _strcmp(char *s1, char *s2); 21 | char *_memset(char *s, char b, unsigned int n); 22 | char *_memcpy(char *dest, char *src, unsigned int n); 23 | char *_strchr(char *s, char c); 24 | unsigned int _strspn(char *s, char *accept); 25 | char *_strpbrk(char *s, char *accept); 26 | char *_strstr(char *haystack, char *needle); 27 | 28 | #endif /* MAIN_H */ 29 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - A function that 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 | 11 | hash_table_t *hash_table_create(unsigned long int size) 12 | { 13 | hash_table_t *hash_table_created; 14 | unsigned long int i; 15 | 16 | /* allocate space for the hash table */ 17 | hash_table_created = malloc(sizeof(hash_table_t)); 18 | 19 | if (hash_table_created == NULL) 20 | return (NULL); 21 | 22 | hash_table_created->size = size; 23 | hash_table_created->array = malloc(sizeof(hash_node_t *) * size); 24 | 25 | if (hash_table_created->array == NULL) 26 | return (NULL); 27 | 28 | for (i = 0; i < size; i++) 29 | { 30 | hash_table_created->array[i] = NULL; 31 | } 32 | 33 | return (hash_table_created); 34 | } 35 | -------------------------------------------------------------------------------- /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 | 10 | unsigned long int hash_djb2(const unsigned char *str) 11 | { 12 | unsigned long int hash; 13 | int count; 14 | 15 | hash = 5381; 16 | while ((count = *str++)) 17 | hash = ((hash << 5) + hash) + count; /* hash * 33 + count */ 18 | 19 | return (hash); 20 | } 21 | -------------------------------------------------------------------------------- /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 pair should 5 | * 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 | * 11 | * Description: Uses the djb2 algorithm. 12 | */ 13 | 14 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 15 | { 16 | return (hash_djb2(key) % size); 17 | } 18 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - Retrieve the value associated with 5 | * a key in a hash table. 6 | * @ht: A pointer to the hash table. 7 | * @key: The key to get the value of. 8 | * 9 | * Return: If the key cannot be matched - NULL. 10 | * Otherwise - the value associated with key in ht. 11 | */ 12 | char *hash_table_get(const hash_table_t *ht, const char *key) 13 | { 14 | hash_node_t *node; 15 | unsigned long int index; 16 | 17 | if (ht == NULL || key == NULL || *key == '\0') 18 | return (NULL); 19 | 20 | index = key_index((const unsigned char *)key, ht->size); 21 | if (index >= ht->size) 22 | return (NULL); 23 | 24 | node = ht->array[index]; 25 | while (node && strcmp(node->key, key) != 0) 26 | node = node->next; 27 | 28 | return ((node == NULL) ? NULL : node->value); 29 | } 30 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "./hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - Prints a hash table. 5 | * @ht: A pointer to the hash table to print. 6 | * 7 | * Description: Key/value pairs are printed in the order 8 | * they appear in the array of the hash table. 9 | */ 10 | void hash_table_print(const hash_table_t *ht) 11 | { 12 | hash_node_t *node; 13 | unsigned long int i; 14 | unsigned char comma_flag = 0; 15 | 16 | if (ht == NULL) 17 | return; 18 | 19 | printf("{"); 20 | for (i = 0; i < ht->size; i++) 21 | { 22 | if (ht->array[i] != NULL) 23 | { 24 | if (comma_flag == 1) 25 | printf(", "); 26 | 27 | node = ht->array[i]; 28 | while (node != NULL) 29 | { 30 | printf("'%s': '%s'", node->key, node->value); 31 | node = node->next; 32 | if (node != NULL) 33 | printf(", "); 34 | } 35 | comma_flag = 1; 36 | } 37 | } 38 | printf("}\n"); 39 | } 40 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - Deletes a hash table. 5 | * @ht: A pointer to a hash table. 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | hash_table_t *head = ht; 10 | hash_node_t *node, *tmp; 11 | unsigned long int i; 12 | 13 | for (i = 0; i < ht->size; i++) 14 | { 15 | if (ht->array[i] != NULL) 16 | { 17 | node = ht->array[i]; 18 | while (node != NULL) 19 | { 20 | tmp = node->next; 21 | free(node->key); 22 | free(node->value); 23 | free(node); 24 | node = tmp; 25 | } 26 | } 27 | } 28 | free(head->array); 29 | free(head); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What is a hash function 4 | 5 | What makes a good hash function 6 | 7 | What is a hash table, how do they work and how to use them 8 | 9 | What is a collision and what are the main ways of dealing with collisions in the context of a hash table 10 | 11 | What are the advantages and drawbacks of using hash tables 12 | 13 | What are the most common use cases of hash tables 14 | -------------------------------------------------------------------------------- /0x1A-hash_tables/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/0x1A-hash_tables/a -------------------------------------------------------------------------------- /0x1A-hash_tables/data-structures_guide_1.c: -------------------------------------------------------------------------------- 1 | /* Please use these data structures for this project: */ 2 | /** 3 | * struct hash_node_s - Node of a hash table 4 | * 5 | * @key: The key, string 6 | * The key is unique in the HashTable 7 | * @value: The value corresponding to a key 8 | * @next: A pointer to the next node of the List 9 | */ 10 | typedef struct hash_node_s 11 | { 12 | char *key; 13 | char *value; 14 | struct hash_node_s *next; 15 | } hash_node_t; 16 | 17 | /** 18 | * struct hash_table_s - Hash table data structure 19 | * 20 | * @size: The size of the array 21 | * @array: An array of size @size 22 | * Each cell of this array is a pointer to the first node of a linked list, 23 | * because we want our HashTable to use a Chaining collision handling 24 | */ 25 | typedef struct hash_table_s 26 | { 27 | unsigned long int size; 28 | hash_node_t **array; 29 | } hash_table_t; 30 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | all: $(SRC) 4 | $(CC) $(SRC) -o school 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | .PHONY : all clean oclean fclean re 7 | 8 | all : m.h $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean : 12 | $(RM) *~ $(NAME) 13 | 14 | oclean : 15 | $(RM) $(OBJ) 16 | 17 | fclean : clean oclean 18 | 19 | re : oclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 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 | The grid represents water by 0 and land by 1. 8 | Args: 9 | grid (list): A list of list of integers representing an island. 10 | Returns: 11 | The perimeter of the island defined in grid. 12 | """ 13 | width = len(grid[0]) 14 | height = len(grid) 15 | edges = 0 16 | size = 0 17 | 18 | for i in range(height): 19 | for j in range(width): 20 | if grid[i][j] == 1: 21 | size += 1 22 | if (j > 0 and grid[i][j - 1] == 1): 23 | edges += 1 24 | if (i > 0 and grid[i - 1][j] == 1): 25 | edges += 1 26 | return size * 4 - edges * 2 27 | -------------------------------------------------------------------------------- /0x1C-makefiles/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 | 17 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | #General 2 | - What are make, Makefiles 3 | - When, why and how to use Makefiles 4 | - What are rules and how to set and use them 5 | - What are explicit and implicit rules 6 | - What are the most common / useful rules 7 | - What are variables and how to set and use them 8 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /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 | ![](https://media.tenor.com/Qz2VqMX0MisAAAAM/just-blame-on-the-algorithm.gif) 3 | 4 | ## Resources 5 | Read or watch: 6 | - Search algorithm 7 | - Space complexity (1) 8 | 9 | # General 10 | - What is a search algorithm 11 | - What is a linear search 12 | - What is a binary search 13 | - What is the best search algorithm to use depending on your needs 14 | # 15 | ![](https://blog.penjee.com/wp-content/uploads/2015/11/binary-search-tree-sorted-array-animation.gif) 16 | # 17 | ![](https://blog.penjee.com/wp-content/uploads/2015/12/optimal-binary-search-tree-from-sorted-array.gif) 18 | # 19 | -------------------------------------------------------------------------------- /practice/.gitignore/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks if a character is uppercase or not 5 | * @c: character to be tested 6 | * Return: 1 whether it is, 0 otherwise 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if ((c >= 'A') && (c <= 'Z')) 12 | { 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /practice/.gitignore/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/0-isupper.o -------------------------------------------------------------------------------- /practice/.gitignore/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_memset - fills memory with a constant byte, 6 | * @s: address to memory area. 7 | * @b: char to be used. 8 | * @n: number of bytes to the memory block. 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; n > 0; i++, n--) 17 | { 18 | s[i] = b; 19 | } 20 | 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /practice/.gitignore/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/0-memset.o -------------------------------------------------------------------------------- /practice/.gitignore/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strcat - concatenate two strings 5 | * @dest: char string to concatenate to 6 | * @src: char string 7 | * Return: ponter to resulting string 'dest' 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | for (j = 0; dest[j] != '\0'; j++) 16 | {} 17 | 18 | for (i = 0; src[i] != '\0'; i++) 19 | { 20 | dest[j + i] = src[i]; 21 | } 22 | dest[j + i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /practice/.gitignore/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/0-strcat.o -------------------------------------------------------------------------------- /practice/.gitignore/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks whether a character is a digit or not 5 | * @c: tested character 6 | * Return: 1 if it is, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if ((c >= 48) && (c <= 57)) 11 | { 12 | return (1); 13 | } 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /practice/.gitignore/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/1-isdigit.o -------------------------------------------------------------------------------- /practice/.gitignore/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _memcpy - copies memory area, 6 | * @dest: destination memory area. 7 | * @src: source memory area. 8 | * @n: bytes filled. 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *_memcpy(char *dest, char *src, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; i < n; i++) 17 | *(dest + i) = *(src + i); 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /practice/.gitignore/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/1-memcpy.o -------------------------------------------------------------------------------- /practice/.gitignore/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenate two strings 5 | * @dest: string 6 | * @src: string 7 | * @n: number of elements to concatenate in 8 | * Return: pointer to resulting 'dest' 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int x, y; 14 | 15 | for (x = 0; dest[x] != '\0'; x++) 16 | ; 17 | 18 | for (y = 0; src[y] != '\0' && n > 0; y++, n--, x++) 19 | { 20 | dest[x] = src[y]; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /practice/.gitignore/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/1-strncat.o -------------------------------------------------------------------------------- /practice/.gitignore/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "2-strlen.c" 4 | 5 | /** 6 | * _atoi - This function converts a string to an integer. 7 | * @s: char array string 8 | * Description: Number in the string can be preceded by an infinite 9 | * number of characters. 10 | * You need to take into account all -/+ signs before the number. 11 | * If there are no numbers in the string, return 0. 12 | * No need to check for overflow. 13 | * Not allowed to hard-code special values. 14 | * Return: first integer found in string 15 | */ 16 | 17 | int _atoi(char *s) 18 | { 19 | int i; 20 | int h, p; 21 | 22 | h = 0; 23 | p = -1; 24 | for (i = 0; s[i] != '\0'; i++) 25 | { 26 | if (s[i] == '-') 27 | p *= -1; 28 | 29 | if (s[i] > 47 && s[i] < 58) 30 | { 31 | if (h < 0) 32 | h = (h * 10) - (s[i] - '0'); 33 | else 34 | h = (s[i] - '0') * -1; 35 | 36 | if (s[i + 1] < 48 || s[i + 1] > 57) 37 | break; 38 | } 39 | } 40 | if (p < 0) 41 | h *= -1; 42 | 43 | return (h); 44 | } 45 | -------------------------------------------------------------------------------- /practice/.gitignore/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/100-atoi.o -------------------------------------------------------------------------------- /practice/.gitignore/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strchr - locates a character in a string, 6 | * @s: string. 7 | * @c: character. 8 | * Return: the pointer to the first occurrence of the character c. 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | unsigned int i = 0; 14 | 15 | for (; *(s + i) != '\0'; i++) 16 | if (*(s + i) == c) 17 | return (s + i); 18 | if (*(s + i) == c) 19 | return (s + i); 20 | return ('\0'); 21 | } 22 | -------------------------------------------------------------------------------- /practice/.gitignore/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/2-strchr.o -------------------------------------------------------------------------------- /practice/.gitignore/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the lenght of a string 5 | * @s: string 6 | * Return: returns length as integer; 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*(s + len) != '\0') 14 | len++; 15 | return (len); 16 | } 17 | -------------------------------------------------------------------------------- /practice/.gitignore/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/2-strlen.o -------------------------------------------------------------------------------- /practice/.gitignore/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strncpy - Copy a string starting from index 0 of 'dest' 6 | * @dest: destination to copy string 7 | * @src: source to copy string 8 | * @n: number of chars to copy over 9 | * 10 | * Return: pointer to the resulting string 11 | */ 12 | 13 | char *_strncpy(char *dest, char *src, int n) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | 19 | while (src[i] != '\0' && i < n) 20 | { 21 | dest[i] = src[i]; 22 | i++; 23 | } 24 | 25 | while (i < n) 26 | { 27 | dest[i] = '\0'; 28 | i++; 29 | } 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /practice/.gitignore/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/2-strncpy.o -------------------------------------------------------------------------------- /practice/.gitignore/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _islower - function to check for lowercase character 4 | * @c: is the int that will use for the argument of the function 5 | * Return: 0 6 | */ 7 | int _islower(int c) 8 | { 9 | if (c >= 'a' && c <= 'z') 10 | { 11 | return (1); 12 | } 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /practice/.gitignore/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/3-islower.o -------------------------------------------------------------------------------- /practice/.gitignore/3-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _puts - prints a string 6 | * @str: string to print 7 | * 8 | * Description: prints a string 9 | * Return: return 0 10 | */ 11 | 12 | void _puts(char *str) 13 | { 14 | int i = 0; 15 | 16 | while (*(str + i) != '\0') 17 | { 18 | putchar(*(str + i)); 19 | i++; 20 | } 21 | putchar(10); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /practice/.gitignore/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/3-puts.o -------------------------------------------------------------------------------- /practice/.gitignore/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Compare two strings 5 | * @s1: first string 6 | * @s2: second string 7 | * Return: negative int if s1 < s2, 0 if matching, and positive int if s1 > s2 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int y; 13 | 14 | for (y = 0; s1[y] != '\0' || s2[y] != '\0'; y++) 15 | { 16 | if (s1[y] != s2[y]) 17 | return (s1[y] - s2[y]); 18 | } 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /practice/.gitignore/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/3-strcmp.o -------------------------------------------------------------------------------- /practice/.gitignore/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strspn - gets the length of a prefix substring. 6 | * @s: initial segment. 7 | * @accept: accepted bytes. 8 | * Return: the number of accepted bytes. 9 | */ 10 | 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int i, j, bool; 14 | 15 | for (i = 0; *(s + i) != '\0'; i++) 16 | { 17 | bool = 1; 18 | for (j = 0; *(accept + j) != '\0'; j++) 19 | { 20 | if (*(s + i) == *(accept + j)) 21 | { 22 | bool = 0; 23 | break; 24 | } 25 | } 26 | if (bool == 1) 27 | break; 28 | } 29 | return (i); 30 | } 31 | -------------------------------------------------------------------------------- /practice/.gitignore/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/3-strspn.o -------------------------------------------------------------------------------- /practice/.gitignore/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - function to check if c is a letter, lowercase or uppercase 4 | * @c: is the int that will use for the argument of the function 5 | * Return: 0 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 10 | 11 | { 12 | return (1); 13 | } 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /practice/.gitignore/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/4-isalpha.o -------------------------------------------------------------------------------- /practice/.gitignore/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: first string. 7 | * @accept: second string. 8 | * Return: a pointer to the byte in s that matches one of the 9 | * bytes in accept, or NULL if no such byte is found. 10 | */ 11 | 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | unsigned int i, j; 15 | 16 | for (i = 0; *(s + i) != '\0'; i++) 17 | { 18 | for (j = 0; *(accept + j) != '\0'; j++) 19 | { 20 | if (*(s + i) == *(accept + j)) 21 | return (s + i); 22 | } 23 | } 24 | return ('\0'); 25 | } 26 | -------------------------------------------------------------------------------- /practice/.gitignore/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/4-strpbrk.o -------------------------------------------------------------------------------- /practice/.gitignore/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strstr - finds the first occurrence of the substring. 6 | * needle in the string haystack. 7 | * @haystack: entire string. 8 | * @needle: substring. 9 | * Return: pointer to the beginning of located substring or 10 | * NULL if the substring is not found. 11 | * 12 | */ 13 | 14 | char *_strstr(char *haystack, char *needle) 15 | { 16 | char *bhaystack; 17 | char *pneedle; 18 | 19 | 20 | while (*haystack != '\0') 21 | { 22 | bhaystack = haystack; 23 | pneedle = needle; 24 | 25 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 26 | { 27 | haystack++; 28 | pneedle++; 29 | } 30 | if (!*pneedle) 31 | return (bhaystack); 32 | haystack = bhaystack + 1; 33 | } 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /practice/.gitignore/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/5-strstr.o -------------------------------------------------------------------------------- /practice/.gitignore/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - function that computes the absolute value of an integer 4 | * @d: input number as an integer. 5 | * Return: absolute value 6 | */ 7 | int _abs(int d) 8 | { 9 | if (d > 0 || d == 0) 10 | { 11 | return (d); 12 | } 13 | else 14 | return (d * -1); 15 | } 16 | -------------------------------------------------------------------------------- /practice/.gitignore/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/6-abs.o -------------------------------------------------------------------------------- /practice/.gitignore/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strcpy - This function copies a string pointed to scr and dest 6 | * @dest: pointer parameter 7 | * @src: pointer parameter 8 | * Return: char 9 | */ 10 | 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int inc = 0; 14 | 15 | while (*(src + inc) != '\0') 16 | { 17 | *(dest + inc) = *(src + inc); 18 | inc++; 19 | } 20 | *(dest + inc) = '\0'; 21 | 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /practice/.gitignore/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/9-strcpy.o -------------------------------------------------------------------------------- /practice/.gitignore/_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 | -------------------------------------------------------------------------------- /practice/.gitignore/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/_putchar.o -------------------------------------------------------------------------------- /practice/.gitignore/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | int main(void) 6 | 7 | { 8 | 9 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 10 | 11 | return (0); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /practice/.gitignore/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/.gitignore/main.o -------------------------------------------------------------------------------- /practice/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks if a character is uppercase or not 5 | * @c: character to be tested 6 | * Return: 1 whether it is, 0 otherwise 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if ((c >= 'A') && (c <= 'Z')) 12 | { 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /practice/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/0-isupper.o -------------------------------------------------------------------------------- /practice/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * *_memset - fills memory with a constant byte, 6 | * @s: address to memory area. 7 | * @b: char to be used. 8 | * @n: number of bytes to the memory block. 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; n > 0; i++, n--) 17 | { 18 | s[i] = b; 19 | } 20 | 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /practice/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/0-memset.o -------------------------------------------------------------------------------- /practice/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _strcat - concatenate two strings 5 | * @dest: char string to concatenate to 6 | * @src: char string 7 | * Return: ponter to resulting string 'dest' 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | for (j = 0; dest[j] != '\0'; j++) 16 | {} 17 | 18 | for (i = 0; src[i] != '\0'; i++) 19 | { 20 | dest[j + i] = src[i]; 21 | } 22 | dest[j + i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /practice/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/0-strcat.o -------------------------------------------------------------------------------- /practice/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks whether a character is a digit or not 5 | * @c: tested character 6 | * Return: 1 if it is, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if ((c >= 48) && (c <= 57)) 11 | { 12 | return (1); 13 | } 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /practice/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/1-isdigit.o -------------------------------------------------------------------------------- /practice/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _memcpy - copies memory area, 6 | * @dest: destination memory area. 7 | * @src: source memory area. 8 | * @n: bytes filled. 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *_memcpy(char *dest, char *src, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; i < n; i++) 17 | *(dest + i) = *(src + i); 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /practice/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/1-memcpy.o -------------------------------------------------------------------------------- /practice/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatenate two strings 5 | * @dest: string 6 | * @src: string 7 | * @n: number of elements to concatenate in 8 | * Return: pointer to resulting 'dest' 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int x, y; 14 | 15 | for (x = 0; dest[x] != '\0'; x++) 16 | ; 17 | 18 | for (y = 0; src[y] != '\0' && n > 0; y++, n--, x++) 19 | { 20 | dest[x] = src[y]; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /practice/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/1-strncat.o -------------------------------------------------------------------------------- /practice/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include "2-strlen.c" 4 | 5 | /** 6 | * _atoi - This function converts a string to an integer. 7 | * @s: char array string 8 | * Description: Number in the string can be preceded by an infinite 9 | * number of characters. 10 | * You need to take into account all -/+ signs before the number. 11 | * If there are no numbers in the string, return 0. 12 | * No need to check for overflow. 13 | * Not allowed to hard-code special values. 14 | * Return: first integer found in string 15 | */ 16 | 17 | int _atoi(char *s) 18 | { 19 | int i; 20 | int h, p; 21 | 22 | h = 0; 23 | p = -1; 24 | for (i = 0; s[i] != '\0'; i++) 25 | { 26 | if (s[i] == '-') 27 | p *= -1; 28 | 29 | if (s[i] > 47 && s[i] < 58) 30 | { 31 | if (h < 0) 32 | h = (h * 10) - (s[i] - '0'); 33 | else 34 | h = (s[i] - '0') * -1; 35 | 36 | if (s[i + 1] < 48 || s[i + 1] > 57) 37 | break; 38 | } 39 | } 40 | if (p < 0) 41 | h *= -1; 42 | 43 | return (h); 44 | } 45 | -------------------------------------------------------------------------------- /practice/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/100-atoi.o -------------------------------------------------------------------------------- /practice/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strchr - locates a character in a string, 6 | * @s: string. 7 | * @c: character. 8 | * Return: the pointer to the first occurrence of the character c. 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | unsigned int i = 0; 14 | 15 | for (; *(s + i) != '\0'; i++) 16 | if (*(s + i) == c) 17 | return (s + i); 18 | if (*(s + i) == c) 19 | return (s + i); 20 | return ('\0'); 21 | } 22 | -------------------------------------------------------------------------------- /practice/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/2-strchr.o -------------------------------------------------------------------------------- /practice/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the lenght of a string 5 | * @s: string 6 | * Return: returns length as integer; 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int len = 0; 12 | 13 | while (*(s + len) != '\0') 14 | len++; 15 | return (len); 16 | } 17 | -------------------------------------------------------------------------------- /practice/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/2-strlen.o -------------------------------------------------------------------------------- /practice/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strncpy - Copy a string starting from index 0 of 'dest' 6 | * @dest: destination to copy string 7 | * @src: source to copy string 8 | * @n: number of chars to copy over 9 | * 10 | * Return: pointer to the resulting string 11 | */ 12 | 13 | char *_strncpy(char *dest, char *src, int n) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | 19 | while (src[i] != '\0' && i < n) 20 | { 21 | dest[i] = src[i]; 22 | i++; 23 | } 24 | 25 | while (i < n) 26 | { 27 | dest[i] = '\0'; 28 | i++; 29 | } 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /practice/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/2-strncpy.o -------------------------------------------------------------------------------- /practice/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _islower - function to check for lowercase character 4 | * @c: is the int that will use for the argument of the function 5 | * Return: 0 6 | */ 7 | int _islower(int c) 8 | { 9 | if (c >= 'a' && c <= 'z') 10 | { 11 | return (1); 12 | } 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /practice/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/3-islower.o -------------------------------------------------------------------------------- /practice/3-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _puts - prints a string 6 | * @str: string to print 7 | * 8 | * Description: prints a string 9 | * Return: return 0 10 | */ 11 | 12 | void _puts(char *str) 13 | { 14 | int i = 0; 15 | 16 | while (*(str + i) != '\0') 17 | { 18 | putchar(*(str + i)); 19 | i++; 20 | } 21 | putchar(10); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /practice/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/3-puts.o -------------------------------------------------------------------------------- /practice/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Compare two strings 5 | * @s1: first string 6 | * @s2: second string 7 | * Return: negative int if s1 < s2, 0 if matching, and positive int if s1 > s2 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int y; 13 | 14 | for (y = 0; s1[y] != '\0' || s2[y] != '\0'; y++) 15 | { 16 | if (s1[y] != s2[y]) 17 | return (s1[y] - s2[y]); 18 | } 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /practice/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/3-strcmp.o -------------------------------------------------------------------------------- /practice/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strspn - gets the length of a prefix substring. 6 | * @s: initial segment. 7 | * @accept: accepted bytes. 8 | * Return: the number of accepted bytes. 9 | */ 10 | 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int i, j, bool; 14 | 15 | for (i = 0; *(s + i) != '\0'; i++) 16 | { 17 | bool = 1; 18 | for (j = 0; *(accept + j) != '\0'; j++) 19 | { 20 | if (*(s + i) == *(accept + j)) 21 | { 22 | bool = 0; 23 | break; 24 | } 25 | } 26 | if (bool == 1) 27 | break; 28 | } 29 | return (i); 30 | } 31 | -------------------------------------------------------------------------------- /practice/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/3-strspn.o -------------------------------------------------------------------------------- /practice/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - function to check if c is a letter, lowercase or uppercase 4 | * @c: is the int that will use for the argument of the function 5 | * Return: 0 6 | */ 7 | int _isalpha(int c) 8 | { 9 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 10 | 11 | { 12 | return (1); 13 | } 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /practice/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/4-isalpha.o -------------------------------------------------------------------------------- /practice/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: first string. 7 | * @accept: second string. 8 | * Return: a pointer to the byte in s that matches one of the 9 | * bytes in accept, or NULL if no such byte is found. 10 | */ 11 | 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | unsigned int i, j; 15 | 16 | for (i = 0; *(s + i) != '\0'; i++) 17 | { 18 | for (j = 0; *(accept + j) != '\0'; j++) 19 | { 20 | if (*(s + i) == *(accept + j)) 21 | return (s + i); 22 | } 23 | } 24 | return ('\0'); 25 | } 26 | -------------------------------------------------------------------------------- /practice/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/4-strpbrk.o -------------------------------------------------------------------------------- /practice/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strstr - finds the first occurrence of the substring. 6 | * needle in the string haystack. 7 | * @haystack: entire string. 8 | * @needle: substring. 9 | * Return: pointer to the beginning of located substring or 10 | * NULL if the substring is not found. 11 | * 12 | */ 13 | 14 | char *_strstr(char *haystack, char *needle) 15 | { 16 | char *bhaystack; 17 | char *pneedle; 18 | 19 | 20 | while (*haystack != '\0') 21 | { 22 | bhaystack = haystack; 23 | pneedle = needle; 24 | 25 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 26 | { 27 | haystack++; 28 | pneedle++; 29 | } 30 | if (!*pneedle) 31 | return (bhaystack); 32 | haystack = bhaystack + 1; 33 | } 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /practice/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/5-strstr.o -------------------------------------------------------------------------------- /practice/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - function that computes the absolute value of an integer 4 | * @d: input number as an integer. 5 | * Return: absolute value 6 | */ 7 | int _abs(int d) 8 | { 9 | if (d > 0 || d == 0) 10 | { 11 | return (d); 12 | } 13 | else 14 | return (d * -1); 15 | } 16 | -------------------------------------------------------------------------------- /practice/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/6-abs.o -------------------------------------------------------------------------------- /practice/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strcpy - This function copies a string pointed to scr and dest 6 | * @dest: pointer parameter 7 | * @src: pointer parameter 8 | * Return: char 9 | */ 10 | 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int inc = 0; 14 | 15 | while (*(src + inc) != '\0') 16 | { 17 | *(dest + inc) = *(src + inc); 18 | inc++; 19 | } 20 | *(dest + inc) = '\0'; 21 | 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /practice/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/9-strcpy.o -------------------------------------------------------------------------------- /practice/README.md: -------------------------------------------------------------------------------- 1 | General 2 | 3 | What is a static library, how does it work, how to create one, and how to use it 4 | 5 | Basic usage of ar, ranlib, nm 6 | -------------------------------------------------------------------------------- /practice/_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 | -------------------------------------------------------------------------------- /practice/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/_putchar.o -------------------------------------------------------------------------------- /practice/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -Werror -Wextra -c *.c 3 | ar rc liball.a *.o 4 | -------------------------------------------------------------------------------- /practice/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/libmy.a -------------------------------------------------------------------------------- /practice/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | 4 | 5 | int main(void) 6 | 7 | { 8 | 9 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 10 | 11 | return (0); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /practice/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif /* MAIN_H */ 26 | -------------------------------------------------------------------------------- /practice/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/besthor/alx-low_level_programming/baf1b6e7e6b126df81fa4fbe60da0f04c8875c38/practice/main.o --------------------------------------------------------------------------------