├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 11-print_to_98.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md ├── _putchar.c └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-convert_day.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── Makefile ├── README.md └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── libmy.a ├── main.c ├── main.h └── main.o ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── README.md~ └── main.h ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── Makefile ├── README.md ├── images │ ├── 0.png │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ └── 6.png ├── main.h └── test_files │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 101-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── print_grid.c │ └── simple_print_buffer.c ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md ├── README.md~ ├── _putchar.c └── main.h ├── 0x0D-preprocessor ├── #3-function_like_macro.h# ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h ├── README.md └── README.md~ ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md ├── 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 ├── README.md~ └── function_pointers.h ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 0-print_dlistint.c~ ├── 1-dlistint_len.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 ├── win.c └── win.so ├── 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 ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py └── README.md ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE > c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE > $(echo $CFILE|cut -d"." -f1).o 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -masm=intel $CFILE -S > $(echo $CFILE|cut -d"." -f1).s 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | /** 5 | * main - prints to string 6 | * Description: Prints "and that piece of art is useful.." without puts 7 | * Return: 1 8 | */ 9 | 10 | int main(void) 11 | { 12 | char *s = "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n"; 13 | long l = 59; 14 | long fd = 1; 15 | long syscall = 1; 16 | long ret = 0; 17 | __asm__ ("syscall" 18 | : "=a" (ret) 19 | : "a" (syscall), 20 | "D" (fd), 21 | "S" (s), 22 | "d" (l)); 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -S > $(echo $CFILE|cut -d"." -f1).s 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * Description: prints "\"Programming is like building a multilingual puzzle" 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | puts("\"Programming is like building a multilingual puzzle"); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print a string 5 | * Description: use printf 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | printf("%s", "with proper grammar, but the outcome is a piece of art,\n"); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints sized of characters 4 | * 5 | *Return: Always 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | printf("Size of a char: %zu byte(s)\n", sizeof(char)); 10 | printf("Size of an int: %zu byte(s)\n", sizeof(int)); 11 | printf("Size of a long int: %zu byte(s)\n", sizeof(long int)); 12 | printf("Size of a long long int: %zu byte(s)\n", sizeof(long long int)); 13 | printf("Size of a float: %zu byte(s)\n", sizeof(float)); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | # 0x00. C - Hello, World 2 | 3 | In this project, I learned about compilation using `gcc`, entry 4 | points using `main`, and text-printing functions in C. GCC is basically a C compiler. 5 | 6 | ## Tasks :page_with_curl: 7 | 8 | * **0. Preprocessor** 9 | * [0-preprocessor](./0-preprocessor): Bash script that runs a C file saved in the 10 | variable `$CFILE` through the preprocessor and saves the result in the file `c`. 11 | 12 | * **1. Compiler** 13 | * [1-compiler](./1-compiler): Bash script that compiles a C file saved in the 14 | variable `$CFILE` that does not link; saves the result in an output file of the 15 | same name but with a `.o` extension. 16 | * Example: If the C file is `main.c`, the output is `main.o`. 17 | 18 | * **2. Assembler** 19 | * [2-assembler](./2-assembler): Bash script that generates the assembly code of a 20 | C code saved in the variable `$CFILE`; saves the result in an output file of the 21 | same name but with a `.s` extension. 22 | * Example: If the C file is `main.c`, the result is `main.s`. 23 | 24 | * **3. Name** 25 | * [3-name](./3-name): Bash Script that compiles a C file saved in the variable 26 | `$CFILE` and creates an executable `cisfun`. 27 | 28 | * **4. Hello, puts** 29 | * [4-puts.c](./4-puts.c): C program that prints exactly `"Programming is like building 30 | a multilingual puzzle`, followed by a new line, using the function `puts`. 31 | 32 | * **5. Hello, printf** 33 | * [5-printf.c](./5-printf.c): C program that prints exactly `with proper grammer, but 34 | the outcome is a piece of art,`, followed by a new line, using the function `printf`. 35 | 36 | * **6. Size is not grandeur, and territory does not make a nation** 37 | * [6-size.c](./6-size.c): C program that prints the size of various types on the computer 38 | it is compiled and run on. 39 | 40 | * **7. Intel** 41 | * [100-intel](./100-intel): Script that generates the assembly code in Intel syntax of a 42 | C file saved in the variable $CFILE; saves the result in an output file of the same name 43 | but with a `.s` extension. 44 | * Example: If the C file is `main.c`, the output is `main.s`. 45 | 46 | * **8. UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity** 47 | * [101-quote.c](./101-quote.c): C program that prints exactly `and that piece of art is 48 | useful" - Dora Korpar, 2015-10-19`, followed by a new line, to the standard error, 49 | without using any functions listed in the NAME sesction of the man(3) `printf` or man(3) 50 | `puts`. 51 | *** 52 | learning all these was pretty interesting!! 53 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - entry point 6 | * Description: prints if a number is positive or negative. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | if (n > 0) 16 | { 17 | printf("%d is positive\n", n); 18 | } 19 | else if (n == 0) 20 | { 21 | printf("%d is zero\n", n); 22 | } 23 | else if (n < 0) 24 | { 25 | printf("%d is negative\n", n); 26 | } 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * Description: checks on the last digit of the assigned var 7 | * Return: Always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int n, m; 12 | 13 | srand(time(0)); 14 | n = rand() - RAND_MAX / 2; 15 | m = n % 10; 16 | if (m > 5) 17 | { 18 | printf("Last digit of %d is %d and is greater than 5\n", n, m); 19 | } 20 | else if (m < 6 && m != 0) 21 | { 22 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, m); 23 | } 24 | else 25 | { 26 | printf("Last digit of %d is %d and is 0\n", n, m); 27 | } 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entyr point 5 | * Description: prints two digits combination 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int c, i; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | for (i = '0'; i <= '9'; i++) 15 | { 16 | if (c < i) 17 | { 18 | putchar(c); 19 | putchar(i); 20 | 21 | if (c != '8' || (c == '8' && i != '9')) 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 | /** 4 | * main - Prints 3 combination of numbers 5 | * 6 | * Return: Always (Success) 7 | */ 8 | int main(void) 9 | { 10 | int c, i, k; 11 | 12 | for (c = '0'; c <= '9'; c++) 13 | { 14 | for (i = '0'; i <= '9'; i++) 15 | { 16 | for (k = '0'; k <= '9'; k++) 17 | { 18 | if (c < i && i < k) 19 | { 20 | putchar(c); 21 | putchar(i); 22 | putchar(k); 23 | 24 | if (c != '7') 25 | { 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | } 31 | } 32 | } 33 | putchar('\n'); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints all possible combination of 2-digit numbers 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int c, i, k, j; 11 | 12 | for (c = 48; c <= 57; c++) 13 | { 14 | for (i = 48; i <= 57; i++) 15 | { 16 | for (k = 48; k <= 57; k++) 17 | { 18 | for (j = 48; j <= 57; j++) 19 | { 20 | if (((k + j) > (c + i) && k >= c) || c < k) 21 | { 22 | putchar(c); 23 | putchar(i); 24 | putchar(' '); 25 | putchar(k); 26 | putchar(j); 27 | 28 | if (c + i + k + j == 227 && c == 57) 29 | { 30 | break; 31 | } 32 | else 33 | { 34 | putchar(','); 35 | putchar(' '); 36 | } 37 | } 38 | } 39 | } 40 | } 41 | } 42 | putchar('\n'); 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * Description: prints the alphabet in lowercase\n 7 | * Return: Always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int ch; 12 | 13 | for (ch = 'a'; ch <= 'z'; ch++) 14 | { 15 | putchar(ch); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints alphabet in lowercase then uppercase 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 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 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints lowercase alphabets 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int ch; 11 | 12 | for (ch = 'a'; ch <= 'z'; ch++) 13 | { 14 | if (ch != 'e' && ch != 'q') 15 | { 16 | putchar(ch); 17 | } 18 | else 19 | { 20 | } 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main -Entry point 5 | * Description: prints allsingle digit numbers of 6 | * base 10 from 0 7 | * Return: Always 0 (success) 8 | */ 9 | int main(void) 10 | { 11 | int a; 12 | 13 | for (a = 0; a < 10; a++) 14 | { 15 | printf("%d", a); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints char below 10 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | 12 | for (a = 0; a < 10; a++) 13 | { 14 | putchar(a + '0'); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints lowercase alphabets in reverse 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | 12 | for (a = 'z'; a >= 'a'; a--) 13 | { 14 | putchar(a); 15 | } 16 | putchar('\n'); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: Print base 16 numbers 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | char d = '0'; 12 | 13 | while (d <= '9') 14 | { 15 | putchar(d); 16 | d++; 17 | } 18 | for (c = 'a'; c <= 'f'; c++) 19 | { 20 | putchar(c); 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - Entry point 5 | * Description: prints numbers 6 | * Return: Always 0 (success) 7 | */ 8 | int main(void) 9 | { 10 | int i; 11 | 12 | for (i = '0'; i <= '9' ; i++) 13 | { 14 | putchar(i); 15 | 16 | if (i != '9') 17 | { 18 | putchar(','); 19 | putchar(' '); 20 | } 21 | } 22 | 23 | putchar('\n'); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - Prints _putchar as a message. 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int str[] = {95, 112, 117, 116, 99, 104, 97, 114}; 10 | int count, sz; 11 | 12 | sz = sizeof(str) / sizeof(int); 13 | for (count = 0; count < sz; count++) 14 | { 15 | _putchar(str[count]); 16 | } 17 | _putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - Entry point 5 | * Description: a function that prints the alphabet, 6 | * in lowecase followed by a new line 7 | * Return: void 8 | */ 9 | void print_alphabet(void) 10 | { 11 | char letter = 'a'; 12 | 13 | while (letter <= 'z') 14 | { 15 | _putchar(letter); 16 | letter++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - adds two integers 5 | * @n1: integer 1 6 | * @n2: integer 2 7 | * Description: give two integers, returns sum 8 | * Return: sum 9 | */ 10 | 11 | int add(int n1, int n2) 12 | { 13 | int sum; 14 | 15 | sum = n1 + n2; 16 | 17 | return (sum); 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - Prints a multiplication table up to param 5 | * @n: The number to be treated 6 | * 7 | * Return: Number matrix 8 | */ 9 | void print_times_table(int n) 10 | { 11 | int x, y, z; 12 | 13 | if (n >= 0 && n <= 14) 14 | { 15 | for (x = 0; x <= n; x++) 16 | { 17 | for (y = 0; y <= n; y++) 18 | { 19 | z = x * y; 20 | if (z > 99) 21 | { 22 | _putchar(','); 23 | _putchar(32); 24 | _putchar((z / 100) + '0'); 25 | _putchar(((z / 10) % 10) + '0'); 26 | _putchar((z % 10) + '0'); 27 | } 28 | else if (z > 9) 29 | { 30 | _putchar(','); 31 | _putchar(32); 32 | _putchar(32); 33 | _putchar(((z / 10) % 10) + '0'); 34 | _putchar((z % 10) + '0'); 35 | } 36 | else 37 | { 38 | if (y != 0) 39 | { 40 | _putchar(','); 41 | _putchar(32); 42 | _putchar(32); 43 | _putchar(32); 44 | } 45 | _putchar(z + '0'); 46 | } 47 | } 48 | _putchar('\n'); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the sum of all multiples of 3 or 5 up to 102 5 | * Return: Always (Success) 6 | */ 7 | int main(void) 8 | { 9 | int i, z = 0; 10 | 11 | while (i < 1024) 12 | { 13 | if ((i % 3 == 0) || (i % 5 == 0)) 14 | { 15 | z += i; 16 | } 17 | 18 | i++; 19 | } 20 | 21 | printf("%d\n", z); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the first 52 fibonacci numbers 5 | * 6 | * Return: Nothing! 7 | */ 8 | int main(void) 9 | { 10 | int i = 0; 11 | long j = 1, k = 2; 12 | 13 | while (i < 50) 14 | { 15 | if (i == 0) 16 | printf("%ld", j); 17 | else if (i == 1) 18 | printf(", %ld", k); 19 | else 20 | { 21 | k += j; 22 | j = k - j; 23 | printf(", %ld", k); 24 | } 25 | 26 | ++i; 27 | } 28 | 29 | printf("\n"); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: computes and prints even number < 4,000,000 5 | * 5 below 1024 (excluded), followed by a new line 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int a = 0, b = 1, next = 0; 11 | int sum = 0; 12 | 13 | while (next < 4000000) 14 | { 15 | next = a + b; 16 | a = b; 17 | b = next; 18 | if (next % 2 == 0) 19 | sum += next; 20 | } 21 | printf("%i\n", sum); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | /** 5 | * numLength - returns the lenth of string 6 | * @num : operand number 7 | * Return: number of digits 8 | */ 9 | 10 | int numLength(int num) 11 | { 12 | int length = 0; 13 | 14 | if (!num) 15 | { 16 | return (1); 17 | } 18 | 19 | while (num) 20 | { 21 | num = num / 10; 22 | length += 1; 23 | } 24 | 25 | return (length); 26 | } 27 | /** 28 | * *main - prints the first 98 fibonaci sequences 29 | * Return: 0 30 | */ 31 | 32 | int main(void) 33 | { 34 | unsigned long f1 = 1, f2 = 2, tmp, mx = 100000000, f1o = 0, f2o = 0, tmpo = 0; 35 | short int i = 1, initial0s; 36 | 37 | while (i <= 98) 38 | { 39 | if (f1o > 0) 40 | printf("%lu", f1o); 41 | initial0s = numLength(mx) - 1 - numLength(f1); 42 | while (f1o > 0 && initial0s > 0) 43 | { 44 | printf("%i", 0); 45 | initial0s--; 46 | } 47 | printf("%lu", f1); 48 | 49 | tmp = (f1 + f2) % mx; 50 | tmpo = f1o + f2o + (f1 + f2) / mx; 51 | f1 = f2; 52 | f1o = f2o; 53 | f2 = tmp; 54 | f2o = tmpo; 55 | 56 | if (i != 98) 57 | printf(", "); 58 | else 59 | printf("\n"); 60 | i++; 61 | } 62 | return (0); 63 | } 64 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - prints numbers to 98 6 | * @n: starting integer 7 | * 8 | * Description: prints all natural number from n - 98 9 | * 10 | * Return: void 11 | */ 12 | void print_to_98(int n) 13 | { 14 | if (n < 98) 15 | { 16 | while (n < 98) 17 | { 18 | printf("%d, ", n); 19 | n++; 20 | } 21 | } 22 | else if (n > 98) 23 | { 24 | while (n > 98) 25 | { 26 | printf("%d, ", n); 27 | n--; 28 | } 29 | } 30 | printf("98\n"); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - Make alphabet x10 times 5 | * 6 | * Return: void 7 | */ 8 | void print_alphabet_x10(void) 9 | { 10 | char c; 11 | int i = 0; 12 | 13 | while (i <= 9) 14 | { 15 | for (c = 'a'; c <= 'z'; c++) 16 | { 17 | _putchar(c); 18 | } 19 | 20 | _putchar('\n'); 21 | 22 | i++; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Entry point 5 | * Description: checks is a character is lowercase 6 | * @c: the integer value it receives 7 | * Return: 1 if true. 0 if false. 8 | */ 9 | int _islower(int c) 10 | { 11 | int i = 'a'; 12 | 13 | for (i = 'a'; i <= 'z'; i++) 14 | { 15 | /* refer int c*/ 16 | if (c == i) 17 | { 18 | return (1); 19 | } 20 | } 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Entry point 5 | * @c: the integer value it receives 6 | * Description: checks if alphabet 7 | * Return: 1 if true. 0 if false 8 | */ 9 | 10 | int _isalpha(int c) 11 | { 12 | if (c >= 'a' && c <= 'z') 13 | { 14 | return (1); 15 | } 16 | else if (c >= 'A' && c <= 'Z') 17 | { 18 | return (1); 19 | } 20 | else 21 | { 22 | return (0); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - Entr point 5 | * @n: carrier variable 6 | * Description: prints the sign of a number 7 | * Return: 1 if n>0, 0 if n == 0, -1 if n<0 8 | */ 9 | 10 | int print_sign(int n) 11 | { 12 | if (n > 0) 13 | { 14 | _putchar('+'); 15 | return (1); 16 | } 17 | else if (n < 0) 18 | { 19 | _putchar('-'); 20 | return (-1); 21 | } 22 | _putchar('0'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - Entry point 5 | * @n: n is an integer 6 | * Description: prints the absolute value of an integer 7 | * Return: int 8 | */ 9 | 10 | int _abs(int n) 11 | { 12 | if (n > 0) 13 | { 14 | return (n); 15 | } 16 | else if (n < 0) 17 | { 18 | return (-n); 19 | } 20 | else 21 | { 22 | return (n); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - prints the last digit of a number 5 | * @n: n is an integer 6 | * Description: prints the last digit of a number 7 | * Return: integer 8 | */ 9 | 10 | int print_last_digit(int n) 11 | { 12 | int last = n % 10; 13 | 14 | if (n < 0) 15 | { 16 | last = last * -1; 17 | } 18 | _putchar(last + '0'); 19 | return (last); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints every minute of the day 5 | * Description: prints every minute of the day 6 | * Return: void 7 | */ 8 | void jack_bauer(void) 9 | { 10 | int hours_tens, hours_ones, minutes_tens, minutes_ones, hours_max; 11 | 12 | hours_max = 58; 13 | hours_tens = '0'; 14 | while (hours_tens < '3') 15 | { 16 | if (hours_tens == '2') 17 | { 18 | hours_max = '4'; 19 | } 20 | hours_ones = '0'; 21 | while (hours_ones < hours_max) 22 | { 23 | minutes_tens = '0'; 24 | while (minutes_tens < '6') 25 | { 26 | minutes_ones = '0'; 27 | while (minutes_ones < 58) 28 | { 29 | _putchar(hours_tens); 30 | _putchar(hours_ones); 31 | _putchar(':'); 32 | _putchar(minutes_tens); 33 | _putchar(minutes_ones); 34 | _putchar('\n'); 35 | minutes_ones++; 36 | } 37 | minutes_ones = '0'; 38 | minutes_tens++; 39 | } 40 | minutes_tens = '0'; 41 | hours_ones++; 42 | } 43 | hours_ones = '0'; 44 | hours_tens++; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - prints the 9 times table 5 | * Description: prints the 9 times table 6 | * Return: void 7 | */ 8 | 9 | void times_table(void) 10 | { 11 | int row, column, product, tens, ones; 12 | 13 | for (row = 0; row <= 9; row++) 14 | { 15 | for (column = 0; column <= 9; column++) 16 | { 17 | product = row * column; 18 | tens = product / 10; 19 | ones = product % 10; 20 | 21 | if (column == 0) 22 | { 23 | _putchar('0'); 24 | } 25 | else if (product < 10) 26 | { 27 | _putchar(','); 28 | _putchar(' '); 29 | _putchar(' '); 30 | _putchar(ones + '0'); 31 | } 32 | else 33 | { 34 | _putchar(','); 35 | _putchar(' '); 36 | _putchar(tens + '0'); 37 | _putchar(ones + '0'); 38 | } 39 | } 40 | _putchar('\n'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | 0x02-functions_nested_loops -------------------------------------------------------------------------------- /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 _main_h_ 2 | #define _main_h_ 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 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | /* 16 | *while (i < 10) 17 | *{ 18 | * putchar(i); 19 | *} 20 | */ 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first int 6 | * @b: second int 7 | * @c: third int 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest = b; 14 | 15 | if (a > b) 16 | { 17 | largest = a; 18 | } 19 | else if (c > b) 20 | { 21 | largest = c; 22 | } 23 | 24 | return (largest); 25 | } 26 | -------------------------------------------------------------------------------- /0x03-debugging/3-convert_day.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * convert_day - converts day of month to day of year, without accounting 5 | * for leap year 6 | * @month: month in number format 7 | * @day: day of month 8 | * 9 | * Return: days of the year 10 | */ 11 | 12 | int convert_day(int month, int day) 13 | { 14 | switch (month) 15 | { 16 | case 2: 17 | day = 31 + day; 18 | break; 19 | case 3: 20 | day = 59 + day; 21 | break; 22 | case 4: 23 | day = 90 + day; 24 | break; 25 | case 5: 26 | day = 120 + day; 27 | break; 28 | case 6: 29 | day = 151 + day; 30 | break; 31 | case 7: 32 | day = 181 + day; 33 | break; 34 | case 8: 35 | day = 212 + day; 36 | break; 37 | case 9: 38 | day = 243 + day; 39 | break; 40 | case 10: 41 | day = 273 + day; 42 | break; 43 | case 11: 44 | day = 304 + day; 45 | break; 46 | case 12: 47 | day = 334 + day; 48 | break; 49 | default: 50 | break; 51 | } 52 | return (day); 53 | } 54 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_remaining_days - takes a date and prints how many days are 5 | * left in the year, taking leap years into account 6 | * 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | */ 11 | 12 | void print_remaining_days(int month, int day, int year) 13 | { 14 | /** 15 | * leap year when it can be divided 16 | * by 100 and 400 or by 4 evenly 17 | */ 18 | if ((year % 100 == 0 && year % 400 == 0) || (year % 4 == 0)) 19 | { 20 | if (month > 2 && day >= 60) 21 | { 22 | day++; 23 | } 24 | 25 | printf("Day of the year: %d\n", day); 26 | printf("Remaining days: %d\n", 366 - day); 27 | 28 | } 29 | else 30 | { 31 | if (month == 2 && day == 60) 32 | { 33 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 34 | } 35 | else 36 | { 37 | printf("Day of the year: %d\n", day); 38 | printf("Remaining days: %d\n", 365 - day); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | Learning how to debug code. 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks is a character is uppercase 5 | * @c: carrier integer variable 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 65 && c <= 90) 12 | { 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks if a character is digit 5 | * @c: carrier variable 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - check for a digit 5 | * @size : integer type 6 | * Return:void 7 | */ 8 | 9 | void print_triangle(int size) 10 | { 11 | int i = 1, ii; 12 | 13 | while (i <= size && size > 0) 14 | { 15 | ii = 0; 16 | while (ii < size - i) 17 | { 18 | _putchar(' '); 19 | ii++; 20 | } 21 | ii = 0; 22 | while (ii < i) 23 | { 24 | _putchar('#'); 25 | ii++; 26 | } 27 | 28 | _putchar('\n'); 29 | i++; 30 | } 31 | if (i == 1) 32 | _putchar('\n'); 33 | } 34 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prime numbers 5 | * 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | long num = 612852475143; 12 | long divisor = 2; 13 | long larg_prim = 0; 14 | 15 | while (num != 1) 16 | { 17 | if (num % divisor == 0) 18 | { 19 | num = num / divisor; 20 | larg_prim = divisor; 21 | } 22 | divisor += 1; 23 | } 24 | printf("%ld\n", larg_prim); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_number - prints numbers 4 | * @n: number to be printed 5 | * Return:void 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int x; 11 | 12 | x = n; 13 | if (n < 0) 14 | { 15 | _putchar(45); 16 | x = -n; 17 | } 18 | if (x / 10) 19 | print_number(x / 10); 20 | _putchar((x % 10) + '0'); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two integers 5 | * 6 | * @a: first input 7 | * @b: second input 8 | * 9 | * Return: return results 10 | */ 11 | 12 | int mul(int a, int b) 13 | { 14 | return (a * b); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - print 0 - 9 5 | * 6 | * Return: void 7 | */ 8 | 9 | void print_numbers(void) 10 | { 11 | char a = 0; 12 | 13 | while (a <= 9) 14 | { 15 | _putchar(a + '0'); 16 | a++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints 0 - 9 except 2, 3 5 | * Description: prints 6 | * Return: void 7 | */ 8 | 9 | void print_most_numbers(void) 10 | { 11 | int a = 0; 12 | 13 | while (a <= 9) 14 | { 15 | if (a != 2 && a != 4) 16 | { 17 | _putchar(a + '0'); 18 | } 19 | a++; 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 0 - 14 5 | * 6 | * Return: void 7 | */ 8 | 9 | void more_numbers(void) 10 | { 11 | char n, c; 12 | int i = 0; 13 | 14 | while (i < 10) 15 | { 16 | for (n = 0; n <= 14; n++) 17 | { 18 | c = n; 19 | if (n > 9) 20 | { 21 | _putchar('1'); 22 | c = n % 10; 23 | } 24 | _putchar('0' + c); 25 | } 26 | 27 | _putchar('\n'); 28 | i++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - prints a line 5 | * @n: variable 6 | * 7 | * Return: 0 8 | */ 9 | 10 | void print_line(int n) 11 | { 12 | int a = 0; 13 | 14 | while (a < n && n > 0) 15 | { 16 | _putchar('_'); 17 | a++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - diagonal lines made of backslashes 5 | * @n: number of \ to be printed 6 | * Return: void 7 | */ 8 | 9 | void print_diagonal(int n) 10 | { 11 | int i = 0, ii; 12 | 13 | while (i < n && n > 0) 14 | { 15 | ii = 0; 16 | while (ii < i) 17 | { 18 | _putchar(' '); 19 | ii++; 20 | } 21 | 22 | _putchar('\\'); 23 | _putchar('\n'); 24 | i++; 25 | } 26 | if (i == 0) 27 | _putchar('\n'); 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - check for a digit 5 | * @n: number of _ to be printed 6 | * Return: void 7 | */ 8 | 9 | void print_square(int n) 10 | { 11 | int i = 0, ii; 12 | 13 | while (i < n && n > 0) 14 | { 15 | ii = 0; 16 | while (ii < n) 17 | { 18 | _putchar('#'); 19 | ii++; 20 | } 21 | _putchar('\n'); 22 | i++; 23 | } 24 | if (i == 0) 25 | _putchar('\n'); 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Fizz Buzz code 5 | * 6 | * Return: void 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i = 1; 12 | 13 | while (i <= 100) 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("%i", i); 30 | } 31 | if (i != 100) 32 | { 33 | putchar(' '); 34 | } 35 | 36 | i++; 37 | } 38 | putchar('\n'); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | 0x04. C - More functions, more nested loops 2 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _isupper(int c); 3 | int _isdigit(int c); 4 | int mul(int a, int b); 5 | void print_numbers(void); 6 | void print_most_numbers(void); 7 | void print_most_numbers(void); 8 | void more_numbers(void); 9 | void print_line(int n); 10 | void print_diagonal(int n); 11 | void print_square(int size); 12 | void print_triangle(int size); 13 | void print_number(int n); 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value of n to 98 5 | * @n: integer pointer to a variable to be updated 6 | * 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 the values of a and b 5 | * @a: pointer 1 6 | * @b: pointer 2 7 | * Return; 0 8 | */ 9 | 10 | void swap_int(int *a, int *b) 11 | { 12 | *a += *b; 13 | *b = *a - *b; 14 | *a = *a - *b; 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string to an integer. 5 | * @s: the string to be comverted. 6 | * 7 | *Return: The integer value of the comverted string. 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int sign = 1; 13 | unsigned int num = 0; 14 | 15 | do { 16 | 17 | if (*s == '-') 18 | sign *= -1; 19 | 20 | else if (*s >= '0' && *s <= '9') 21 | num = (num * 10) + (*s - '0'); 22 | 23 | else if (num > 0) 24 | break; 25 | } while (*s++); 26 | 27 | return (num * sign); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - generates keygen. 6 | * Return: 0 Always. 7 | */ 8 | int main(void) 9 | { 10 | int r = 0, c = 0; 11 | time_t t; 12 | 13 | srand((unsigned int) time(&t)); 14 | while (c < 2772) 15 | { 16 | r = rand() % 128; 17 | if ((c + r) > 2772) 18 | break; 19 | c = c + r; 20 | printf("%c", r); 21 | } 22 | printf("%c\n", (2772 - c)); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - checks the length of a string 5 | * @s: string to check 6 | * 7 | * Return: int length of the string 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int length = 0; 13 | 14 | while (s[length]) 15 | length++; 16 | return (length); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string and a new line 5 | * @str: pointer to a string to print 6 | * 7 | * Return: 0 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i = 0; 13 | 14 | while (str[i]) 15 | { 16 | _putchar(str[i]); 17 | i++; 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string in reverse 5 | * @s: the used string reference pointer 6 | * Return: 0 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int i = 0; 12 | 13 | while (s[i]) 14 | i++; 15 | while (i--) 16 | _putchar(s[i]); 17 | _putchar('\n'); 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - prints a string in reverse 5 | * @s: the used string to beb reversed 6 | * Return: 0 7 | */ 8 | 9 | void rev_string(char *s) 10 | { 11 | int len, i, half; 12 | char temp; 13 | 14 | for (len = 0; s[len] != '\0'; len++) 15 | ; 16 | i = 0; 17 | half = len / 2; 18 | 19 | while (half--) 20 | { 21 | temp = s[len - i - 1]; 22 | s[len - i - 1] = s[i]; 23 | s[i] = temp; 24 | i++; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints xters of a string 5 | * @str: the used string reference 6 | * Return: 0 7 | */ 8 | 9 | void puts2(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i] != '\0') 14 | { 15 | if (i % 2 == 0) 16 | { 17 | _putchar(str[i]); 18 | } 19 | i++; 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - prints the second half of the string 5 | * @str: the string reference 6 | * Return: 0 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | ; 15 | i++; 16 | for (i /= 2; str[i] != '\0'; i++) 17 | { 18 | _putchar(str[i]); 19 | } 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - print `n` elements of an array of integers 6 | * @a: int type array pointer 7 | * @n: int type integer 8 | * Description: Numbers must be separated by comma and space. 9 | * Numbers should be displayed in the same order they are stored in array. 10 | * You can only use _putchar to print. 11 | */ 12 | 13 | void print_array(int *a, int n) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | for (n--; n >= 0; n--, i++) 19 | { 20 | printf("%d", a[i]); 21 | if (n > 0) 22 | { 23 | printf(", "); 24 | } 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcpy - copies the string pointed to by src 5 | * @dest: char type string 6 | * @src: char type string 7 | * Description: Copy the string pointed to by pointer `src` to 8 | * the buffer pointed to by `dest` 9 | * Return: Pointer to `dest 10 | */ 11 | 12 | char *_strcpy(char *dest, char *src) 13 | { 14 | int i = -1; 15 | 16 | do { 17 | i++; 18 | dest[i] = src[i]; 19 | } while (src[i] != '\0'); 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | pointers and arrays -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _atoi(char *s); 2 | int _putchar(char c); 3 | void reset_to_98(int *n); 4 | void swap_int(int *a, int *b); 5 | int _strlen(char *s); 6 | void _puts(char *str); 7 | void print_rev(char *s); 8 | void rev_string(char *s); 9 | void puts2(char *str); 10 | void puts_half(char *str); 11 | void print_array(int *a, int n); 12 | char *_strcpy(char *dest, char *src); 13 | 14 | 15 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - concatenates @src to @dest 5 | * @src: the source string to append to @dest 6 | * @dest: the destiation string to be concatenated upon 7 | * Return:pointer to the resulting string 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int index = 0; 13 | int dest_len = 0; 14 | 15 | while (dest[index++]) 16 | dest_len++; 17 | for (index = 0; src[index]; index++) 18 | dest[dest_len++] = src[index]; 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - Concatenates two strings using at most 5 | * an inputted number of bytes from src. 6 | * @dest: The string to be appended upon. 7 | * @src: The string to be appended to dest. 8 | * @n: The number of bytes from src to be appended to dest. 9 | * 10 | * Return: A pointer to the resulting string dest. 11 | */ 12 | char *_strncat(char *dest, char *src, int n) 13 | { 14 | int index = 0, dest_len = 0; 15 | 16 | while (dest[index++]) 17 | dest_len++; 18 | for (index = 0; src[index] && index < n; index++) 19 | dest[dest_len++] = src[index]; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - Encodes a string using rot13. 5 | * @str: The string to be encoded. 6 | * 7 | * Return: A pointer to the encoded string. 8 | */ 9 | char *rot13(char *str) 10 | { 11 | int indx1 = 0, indx2; 12 | char alphabet[52] = {'A', 'B', 'C', 'D', 'E', 'F', 13 | 'G', 'H', 'I', 'J', 'K', 'L', 14 | 'M', 'N', 'O', 'P', 'Q', 'R', 15 | 'S', 'T', 'U', 'V', 'W', 'X', 16 | 'Y', 'Z', 'a', 'b', 'c', 'd', 17 | 'e', 'f', 'g', 'h', 'i', 'j', 18 | 'k', 'l', 'm', 'n', 'o', 'p', 19 | 'q', 'r', 's', 't', 'u', 'v', 20 | 'w', 'x', 'y', 'z'}; 21 | char rot13key[52] = {'N', 'O', 'P', 'Q', 'R', 'S', 22 | 'T', 'U', 'V', 'W', 'X', 'Y', 23 | 'Z', 'A', 'B', 'C', 'D', 'E', 24 | 'F', 'G', 'H', 'I', 'J', 'K', 25 | 'L', 'M', 'n', 'o', 'p', 'q', 26 | 'r', 's', 't', 'u', 'v', 'w', 27 | 'x', 'y', 'z', 'a', 'b', 'c', 28 | 'd', 'e', 'f', 'g', 'h', 'i', 29 | 'j', 'k', 'l', 'm'}; 30 | 31 | while (str[indx1]) 32 | { 33 | for (indx2 = 0; indx2 < 52; indx2++) 34 | { 35 | if (str[indx1] == alphabet[indx2]) 36 | { 37 | str[indx1] = rot13key[indx2]; 38 | break; 39 | } 40 | } 41 | 42 | indx1++; 43 | 44 | } 45 | return (str); 46 | } 47 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - Prints an integer. 5 | * @n: The integer to be printed. 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int num = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | num = -num; 16 | } 17 | 18 | if ((num / 10) > 0) 19 | print_number(num / 10); 20 | 21 | _putchar((num % 10) + '0'); 22 | } 23 | -------------------------------------------------------------------------------- /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 | char *add_strings(char *n1, char *n2, char *r, int r_index); 4 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 5 | 6 | /** 7 | * add_strings - Adds the numbers stored in two strings. 8 | * @n1: The string containing the first number to be added. 9 | * @n2: The string containing the second number to be added. 10 | * @r: The buffer to store the result. 11 | * @r_index: The current index of the buffer. 12 | * 13 | * Return: If r can store the sum - a pointer to the result. 14 | * If r cannot store the sum - 0. 15 | */ 16 | 17 | char *add_strings(char *n1, char *n2, char *r, int r_index) 18 | { 19 | int num, tens = 0; 20 | 21 | for (; *n1 && *n2; n1--, n2--, r_index--) 22 | { 23 | num = (*n1 - '0') + (*n2 - '0'); 24 | num += tens; 25 | *(r + r_index) = (num % 10) + '0'; 26 | tens = num / 10; 27 | } 28 | 29 | for (; *n1; n1--, r_index--) 30 | { 31 | num = (*n1 - '0') + tens; 32 | *(r + r_index) = (num % 10) + '0'; 33 | tens = num / 10; 34 | } 35 | 36 | for (; *n2; n2--, r_index--) 37 | { 38 | num = (*n2 - '0') + tens; 39 | *(r + r_index) = (num % 10) + '0'; 40 | tens = num / 10; 41 | } 42 | 43 | if (tens && r_index >= 0) 44 | { 45 | *(r + r_index) = (tens % 10) + '0'; 46 | return (r + r_index); 47 | } 48 | 49 | else if (tens && r_index < 0) 50 | return (0); 51 | 52 | return (r + r_index + 1); 53 | } 54 | /** 55 | * infinite_add - Adds two numbers. 56 | * @n1: The first number to be added. 57 | * @n2: The second number to be added. 58 | * @r: The buffer to store the result. 59 | * @size_r: The buffer size. 60 | * 61 | * Return: If r can store the sum - a pointer to the result. 62 | * If r cannot store the sum - 0. 63 | */ 64 | 65 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 66 | { 67 | int index, n1_len = 0, n2_len = 0; 68 | 69 | for (index = 0; *(n1 + index); index++) 70 | n1_len++; 71 | 72 | for (index = 0; *(n2 + index); index++) 73 | n2_len++; 74 | 75 | if (size_r <= n1_len + 1 || size_r <= n2_len + 1) 76 | return (0); 77 | 78 | n1 += n1_len - 1; 79 | n2 += n2_len - 1; 80 | *(r + size_r) = '\0'; 81 | 82 | return (add_strings(n1, n2, r, --size_r)); 83 | } 84 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - Prints a buffer 10 bytes at a time, starting with 6 | * the byte position, then showing the hex content, 7 | * then displaying printable charcaters. 8 | * @b: The buffer to be printed. 9 | * @size: The number of bytes to be printed from the buffer. 10 | */ 11 | 12 | void print_buffer(char *b, int size) 13 | { 14 | int byte, index; 15 | 16 | for (byte = 0; byte < size; byte += 10) 17 | { 18 | printf("%08x: ", byte); 19 | 20 | for (index = 0; index < 10; index++) 21 | { 22 | if ((index + byte) >= size) 23 | printf(" "); 24 | else 25 | printf("%02x", *(b + index + byte)); 26 | if ((index % 2) != 0 && index != 0) 27 | printf(" "); 28 | } 29 | for (index = 0; index < 10; index++) 30 | { 31 | if ((index + byte) >= size) 32 | break; 33 | else if (*(b + index + byte) >= 31 && 34 | *(b + index + byte) <= 126) 35 | printf("%c", *(b + index + byte)); 36 | else 37 | printf("."); 38 | } 39 | if (byte >= size) 40 | continue; 41 | printf("\n"); 42 | } 43 | if (size <= 0) 44 | printf("\n"); 45 | } 46 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - Copies at most an inputted number 5 | * of bytes from string src into dest. 6 | * @dest: The buffer storing the string copy. 7 | * @src: The source string. 8 | * @n: The maximum number of bytes to copied from src. 9 | * 10 | * Return: A pointer to the resulting string dest. 11 | */ 12 | char *_strncpy(char *dest, char *src, int n) 13 | { 14 | int index = 0, src_len = 0; 15 | 16 | while (src[index++]) 17 | src_len++; 18 | 19 | for (index = 0; src[index] && index < n; index++) 20 | dest[index] = src[index]; 21 | 22 | for (index = src_len; index < n; index++) 23 | dest[index] = '\0'; 24 | 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Compares pointers to two strings. 5 | * @s1: A pointer to the first string to be compared. 6 | * @s2: A pointer to the second string to be compared. 7 | * 8 | * Return: If str1 < str2, the negative difference of 9 | * the first unmatched characters. 10 | * If s1 == s2, 0. 11 | * If s1 > s2, the positive difference of 12 | * the first unmatched characters. 13 | */ 14 | int _strcmp(char *s1, char *s2) 15 | { 16 | while ((*s1 && *s2) && (*s1 == *s2)) 17 | { 18 | s1++; 19 | s2++; 20 | } 21 | 22 | return (*s1 - *s2); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - Reverses the content of an array of integers. 5 | * @a: The array of integers to be reversed. 6 | * @n: The number of elements in the array. 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int tmp, index; 12 | 13 | for (index = n - 1; index >= n / 2; index--) 14 | { 15 | tmp = a[n - 1 - index]; 16 | a[n - 1 - index] = a[index]; 17 | a[index] = tmp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - Changes all lowercase letters 5 | * of a string to uppercase. 6 | * @str: The string to be changed. 7 | * 8 | * Return: A pointer to the changed string. 9 | */ 10 | char *string_toupper(char *str) 11 | { 12 | int index = 0; 13 | 14 | while (str[index]) 15 | { 16 | if (str[index] >= 'a' && str[index] <= 'z') 17 | str[index] -= 32; 18 | 19 | index++; 20 | } 21 | 22 | return (str); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - Capitalizes all words of a string. 5 | * @str: The string to be capitalized. 6 | * 7 | * Return: A pointer to the changed string. 8 | */ 9 | char *cap_string(char *str) 10 | { 11 | int index = 0; 12 | 13 | while (str[index]) 14 | { 15 | while (!(str[index] >= 'a' && str[index] <= 'z')) 16 | index++; 17 | 18 | if (str[index - 1] == ' ' || 19 | str[index - 1] == '\t' || 20 | str[index - 1] == '\n' || 21 | str[index - 1] == ',' || 22 | str[index - 1] == ';' || 23 | str[index - 1] == '.' || 24 | str[index - 1] == '!' || 25 | str[index - 1] == '?' || 26 | str[index - 1] == '"' || 27 | str[index - 1] == '(' || 28 | str[index - 1] == ')' || 29 | str[index - 1] == '{' || 30 | str[index - 1] == '}' || 31 | index == 0) 32 | str[index] -= 32; 33 | 34 | index++; 35 | } 36 | 37 | return (str); 38 | } 39 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - Encodes a string to 1337. 5 | * @str: The string to be encoded. 6 | * 7 | * Return: A pointer to the encoded string. 8 | */ 9 | char *leet(char *str) 10 | { 11 | int indx1 = 0, indx2; 12 | char leet[8] = {'O', 'L', '?', 'E', 'A', '?', '?', 'T'}; 13 | 14 | while (str[indx1]) 15 | { 16 | for (indx2 = 0; indx2 <= 7; indx2++) 17 | { 18 | if (str[indx1] == leet[indx2] || 19 | str[indx1] - 32 == leet[indx2]) 20 | str[indx1] = indx2 + '0'; 21 | } 22 | 23 | indx1++; 24 | } 25 | 26 | return (str); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x06. C - More pointers, arrays and strings -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | char *_strcat(char *dest, char *src); 2 | char *_strncat(char *dest, char *src, int n); 3 | char *_strncpy(char *dest, char *src, int n); 4 | int _strcmp(char *s1, char *s2); 5 | void reverse_array(int *a, int n); 6 | char *string_toupper(char *); 7 | char *cap_string(char *); 8 | char *rot13(char *); 9 | void print_number(int n); 10 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 11 | void print_buffer(char *b, int size); 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memset - fills memory with a constant byte. 5 | * @s: pointer to put the constant 6 | * @b: constant 7 | * @n: max bytes to use 8 | * Return: s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; n > 0; i++, n--) 16 | { 17 | s[i] = b; 18 | } 19 | 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_memcpy - copies memory area 5 | * @dest: memory area 6 | * @src: source 7 | * @n: length of src 8 | * 9 | * Return: pointer to dest. 10 | */ 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - Sets the value of a pointer to a char. 5 | * @s: The pointer. 6 | * @to: The char. 7 | */ 8 | void set_string(char **s, char *to) 9 | { 10 | *s = to; 11 | } 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - Locates a character in a string. 5 | * @s: The string to be searched. 6 | * @c: The character to be located. 7 | * 8 | * Return: If c is found - a pointer to the first occurence. 9 | * If c is not found - NULL. 10 | */ 11 | 12 | char *_strchr(char *s, char c) 13 | { 14 | int index; 15 | 16 | for (index = 0; s[index] >= '\0'; index++) 17 | { 18 | if (s[index] == c) 19 | return (s + index); 20 | } 21 | 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - Gets the length of a prefix substring. 5 | * @s: The string to be searched. 6 | * @accept: The prefix to be measured. 7 | * 8 | * Return: The number of bytes in s which 9 | * consist only of bytes from accept. 10 | */ 11 | 12 | unsigned int _strspn(char *s, char *accept) 13 | { 14 | unsigned int bytes = 0; 15 | int index; 16 | 17 | while (*s) 18 | { 19 | for (index = 0; accept[index]; index++) 20 | { 21 | if (*s == accept[index]) 22 | { 23 | bytes++; 24 | break; 25 | } 26 | 27 | else if (accept[index + 1] == '\0') 28 | return (bytes); 29 | } 30 | 31 | s++; 32 | } 33 | 34 | return (bytes); 35 | } 36 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - Searches a string for any of a set of bytes. 5 | * @s: The string to be searched. 6 | * @accept: The set of bytes to be searched for. 7 | * 8 | * Return: If a set is matched - a pointer to the matched byte. 9 | * If no set is matched - NULL. 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int index; 14 | 15 | while (*s) 16 | { 17 | for (index = 0; accept[index]; index++) 18 | { 19 | if (*s == accept[index]) 20 | return (s); 21 | } 22 | 23 | s++; 24 | } 25 | return ('\0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - Locates a substring. 5 | * @haystack: The string to be searched. 6 | * @needle: The substring to be located. 7 | * 8 | * Return: If the substring is located - a pointer to the beginning 9 | * of the located substring. 10 | * If the substring is not located - NULL. 11 | */ 12 | 13 | char *_strstr(char *haystack, char *needle) 14 | { 15 | int index; 16 | 17 | if (*needle == 0) 18 | return (haystack); 19 | 20 | while (*haystack) 21 | { 22 | index = 0; 23 | 24 | if (haystack[index] == needle[index]) 25 | { 26 | do { 27 | if (needle[index + 1] == '\0') 28 | return (haystack); 29 | 30 | index++; 31 | 32 | } while (haystack[index] == needle[index]); 33 | } 34 | 35 | haystack++; 36 | } 37 | 38 | return ('\0'); 39 | } 40 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chess board 5 | * 6 | * @a: input pointer 7 | * 8 | * Return: return nothing 9 | */ 10 | 11 | void print_chessboard(char (*a)[8]) 12 | { 13 | int i, j; 14 | 15 | i = 0; 16 | j = 0; 17 | while (i < 64) 18 | { 19 | if (i % 8 == 0 && i != 0) 20 | { 21 | j = i; 22 | _putchar('\n'); 23 | } 24 | 25 | _putchar(a[i / 8][i - j]); 26 | i++; 27 | } 28 | _putchar('\n'); 29 | } 30 | -------------------------------------------------------------------------------- /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 diagonals 6 | * of a square matrix of integers. 7 | * @a: The matrix of integers. 8 | * @size: The size of the matrix. 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int index, sum1 = 0, sum2 = 0; 13 | 14 | for (index = 0; index < size; index++) 15 | { 16 | sum1 += a[index]; 17 | a += size; 18 | } 19 | 20 | a -= size; 21 | 22 | for (index = 0; index < size; index++) 23 | { 24 | sum2 += a[index]; 25 | a -= size; 26 | } 27 | 28 | printf("%d, %d\n", sum1, sum2); 29 | } 30 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | even more on pointers -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | 7 | char *_memset(char *s, char b, unsigned int n); 8 | char *_memcpy(char *dest, char *src, unsigned int n); 9 | char *_strchr(char *s, char c); 10 | unsigned int _strspn(char *s, char *accept); 11 | char *_strpbrk(char *s, char *accept); 12 | char *_strstr(char *haystack, char *needle); 13 | void print_chessboard(char (*a)[8]); 14 | void print_diagsums(int *a, int size); 15 | void set_string(char **s, char *to); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - prints a string, followed by a new line 5 | * @s: string to print 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (!*s) 10 | { 11 | _putchar('\n'); 12 | return; 13 | } 14 | _putchar(*s); 15 | _puts_recursion(s + 1); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints a string in reverse 5 | * @s: string to print 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (!*s) 10 | return; 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | } 14 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string 5 | * @s: string to check 6 | * Return: length of string 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | return (1 + _strlen_recursion(s + 1)); 13 | } 14 | 15 | /** 16 | * check_palindrome - checks if a string is a palindrome 17 | * @s: string to check 18 | * @len: length of string 19 | * Return: 1 if palindrome, 0 if not 20 | */ 21 | int check_palindrome(char *s, int len) 22 | { 23 | if (len <= 1) 24 | return (1); 25 | if (*s != *(s + len - 1)) 26 | return (0); 27 | return (check_palindrome(s + 1, len - 2)); 28 | } 29 | 30 | /** 31 | * is_palindrome - checks if a string is a palindrome 32 | * @s: string to check 33 | * Return: 1 if palindrome, 0 if not 34 | */ 35 | int is_palindrome(char *s) 36 | { 37 | int len = 0; 38 | 39 | if (*s == '\0') 40 | return (1); 41 | len = _strlen_recursion(s); 42 | return (check_palindrome(s, len)); 43 | } 44 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two strings and returns 1 if the strings can be 5 | * considered identical, otherwise return 0 6 | * @s1: string to compare 7 | * @s2: string to compare 8 | * Return: 1 if identical, 0 if not 9 | */ 10 | int wildcmp(char *s1, char *s2) 11 | { 12 | if (!*s1 && !*s2) 13 | return (1); 14 | if (*s2 == '*') 15 | return (wildcmp(s1, s2 + 1) || (*s1 != '\0' && wildcmp(s1 + 1, s2))); 16 | if (*s1 == *s2) 17 | return (wildcmp(s1 + 1, s2 + 1)); 18 | return (0); 19 | } 20 | 21 | /* 22 | * int wildcmp(char *s1, char *s2) 23 | *{ 24 | * if (*s1 == '\0' && *s2 == '\0') 25 | * return (1); 26 | * if (*s1 == *s2) 27 | * return (wildcmp(s1 + 1, s2 + 1)); 28 | * if (*s2 == '*') 29 | * { 30 | * if (*(s2 + 1) == '*') 31 | * return (wildcmp(s1, s2 + 1)); 32 | * if (wildcmp(s1, s2 + 1)) 33 | * return (1); 34 | * if (*s1 != '\0') 35 | * return (wildcmp(s1 + 1, s2)); 36 | * } 37 | * return (0); 38 | *} 39 | */ 40 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string 5 | * @s: string to be measured 6 | * Return: length of string 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | return (1 + _strlen_recursion(s + 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a given number 5 | * @n: number to be factored 6 | * Return: factorial of n 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n < 0) 11 | return (-1); 12 | if (n == 0) 13 | return (1); 14 | return (n * factorial(n - 1)); 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - returns the value of x raised to the power of y 5 | * @x: base 6 | * @y: exponent 7 | * 8 | * Return: x raised to the power of y 9 | */ 10 | int _pow_recursion(int x, int y) 11 | { 12 | if (y < 0) 13 | return (-1); 14 | if (y == 0) 15 | return (1); 16 | return (x * _pow_recursion(x, y - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _sqrt_helper - helper function for _sqrt_recursion 5 | * @n: number 6 | * @i: incrementer 7 | * 8 | * Return: natural square root of n 9 | */ 10 | int _sqrt_helper(int n, int i) 11 | { 12 | if (i * i == n) 13 | return (i); 14 | if (i * i > n) 15 | return (-1); 16 | return (_sqrt_helper(n, i + 1)); 17 | } 18 | 19 | /** 20 | * _sqrt_recursion - returns the natural square root of a number 21 | * @n: number 22 | * 23 | * Return: natural square root of n 24 | */ 25 | int _sqrt_recursion(int n) 26 | { 27 | if (n < 0) 28 | return (-1); 29 | return (_sqrt_helper(n, 0)); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * is_prime_helper - helper function for is_prime_number 5 | * @n: number 6 | * @i: incrementer 7 | * 8 | * Return: 1 if n is a prime number, otherwise 0 9 | */ 10 | int is_prime_helper(int n, int i) 11 | { 12 | if (i == n) 13 | return (1); 14 | if (n % i == 0) 15 | return (0); 16 | return (is_prime_helper(n, i + 1)); 17 | } 18 | 19 | /** 20 | * is_prime_number - returns 1 if the input integer is a prime number, 21 | * otherwise return 0 22 | * @n: number 23 | * 24 | * Return: 1 if n is a prime number, otherwise 0 25 | */ 26 | 27 | int is_prime_number(int n) 28 | { 29 | if (n <= 1) 30 | return (0); 31 | return (is_prime_helper(n, 2)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -pedantic -std=c89 3 | INCLUDES=-I. 4 | 5 | TEST_OUTPUT=test_output 6 | TEST_FILES=test_files 7 | BIN_DIR=bin 8 | 9 | FILE= 10 | 11 | TEST_FILE:=${TEST_FILES}/$(shell echo ${FILE} | sed 's/-[^-]*\./-main./g') 12 | OTHER_FILES:=$(addprefix ${TEST_FILES}/, $(shell ls ${TEST_FILES} | grep -v main)) 13 | 14 | ifeq ($(shell if [ -f ${TEST_FILE} ]; then echo 1; else echo 0; fi), 1) 15 | TEST_FILE:=${TEST_FILE} 16 | else 17 | TEST_FILE:= 18 | endif 19 | 20 | BIN:=${BIN_DIR}/$(shell basename ${FILE} .c) 21 | OUTPUT=${TEST_OUTPUT}/$(shell basename ${FILE} .c).out 22 | 23 | all: ${BIN_DIR} ${TEST_OUTPUT} 24 | @$(CC) $(INCLUDES) $(CFLAGS) -o ${BIN} ${TEST_FILE} ${OTHER_FILES} ${FILE} 25 | @./${BIN} > ${OUTPUT} && ./${BIN} 26 | 27 | ${BIN_DIR}: 28 | @mkdir -p $@ 29 | 30 | ${TEST_OUTPUT}: 31 | @mkdir -p $@ 32 | 33 | clean: 34 | @rm -rf ${BIN_DIR} 35 | @rm -rf ${TEST_OUTPUT} 36 | @rm -f ${BIN} 37 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | Recursion in C programming refers to the process of a function calling itself. In other words, a recursive function is a function that solves a problem by breaking it down into smaller instances of the same problem. 2 | 3 | Recursion involves two main components: 4 | 5 | Base Case: It is the condition that determines when the recursive function should stop calling itself and return a result. It acts as the terminating condition for the recursive process. Without a base case, the recursive function would continue calling itself indefinitely, resulting in a stack overflow error. 6 | Recursive Case: It is the part of the function where the function calls itself with modified arguments to solve a smaller instance of the problem. By solving smaller instances of the problem and combining their results, the function eventually solves the original problem. -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void _putchar(char c); 7 | void _puts_recursion(char *s); 8 | void _print_rev_recursion(char *s); 9 | int _strlen_recursion(char *s); 10 | int factorial(int n); 11 | int _pow_recursion(int x, int y); 12 | int _sqrt_recursion(int n); 13 | int is_prime_number(int n); 14 | int is_palindrome(char *s); 15 | int wildcmp(char *s1, char *s2); 16 | 17 | #endif /* MAIN_H */ 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks if for uppercase character 5 | * @c: the character to be checked 6 | * Return: returns 1 is c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | if (c >= 65 && c <= 90) 11 | return (1); 12 | else 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | *_memset - a function that fills memory with a constant byte 6 | * 7 | *@s: the area of memory to be filled 8 | *@b: the character to fill the area with 9 | *@n: the number of bytes to fill 10 | *Return: char* 11 | */ 12 | char *_memset(char *s, char b, unsigned int n) 13 | { 14 | unsigned int i; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | s[i] = b; 19 | } 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strcat - a function that cocantenates two strings 6 | * @dest: the pointer to the cocantenated string 7 | * @src: the string to be appended 8 | * Return: returns a pointer to the string dest 9 | */ 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i, c = 0, d = 0, j = 0; 13 | 14 | for (i = 0; dest[i] != 0; i++) 15 | c++; 16 | for (i = 0; src[i] != 0; i++) 17 | d++; 18 | for (i = c; src[j] != 0; i++) 19 | { 20 | dest[i] = src[j]; 21 | j++; 22 | } 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit 5 | * @c: the character to be checked 6 | * Return: returns 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= 48 && c <= 57) 11 | return (1); 12 | else 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _memcpy - a function that copies a memory area 6 | * @dest: the destination buffer 7 | * @src: the source buffer 8 | * @n: area size 9 | * Return: returns nothing 10 | */ 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strncat - a function that cocantenates two strings 6 | * @dest: the pointer to the cocantenated string 7 | * @src: the string to be appended 8 | * @n: the number of bytes from src to be appended 9 | * Return: returns a pointer to the string dest 10 | */ 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i, c = 0, j = 0; 14 | 15 | for (i = 0; dest[i] != 0; i++) 16 | c++; 17 | for (i = c; src[j] != 0 && j < n; i++) 18 | { 19 | if (i == 98) 20 | break; 21 | dest[i] = src[j]; 22 | j++; 23 | } 24 | dest[i] = '\0'; 25 | return (dest); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer 5 | * @s: string to be converted 6 | * Return: returns the int converted from the string 7 | */ 8 | int _atoi(char *s) 9 | { 10 | int i = 0; 11 | int d = 0; 12 | int n = 0; 13 | int len = 0; 14 | int f = 0; 15 | int digit = 0; 16 | 17 | while (s[len] != '\0') 18 | len++; 19 | 20 | while (i < len && f == 0) 21 | { 22 | if (s[i] == '-') 23 | ++d; 24 | 25 | if (s[i] >= '0' && s[i] <= '9') 26 | { 27 | digit = s[i] - '0'; 28 | if (d % 2) 29 | digit = -digit; 30 | n = n * 10 + digit; 31 | f = 1; 32 | if (s[i + 1] < '0' || s[i + 1] > '9') 33 | break; 34 | f = 0; 35 | } 36 | i++; 37 | } 38 | 39 | if (f == 0) 40 | return (0); 41 | 42 | return (n); 43 | } 44 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strchr - a function that locates a character in a string 6 | * @s: the string to locate the first occurence of c 7 | * @c: the character to locate 8 | * Return: returns a pointer to the first occurence of c 9 | */ 10 | char *_strchr(char *s, char c) 11 | { 12 | int i; 13 | 14 | for (i = 0; 1 ; i++) 15 | { 16 | if (s[i] == c) 17 | return ((s + i)); 18 | if (s[i] == 0) 19 | break; 20 | } 21 | return (NULL); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strlen - a function that returns the length of a string 6 | * @s: a pointer to a string 7 | * Return: returns the length of the string as an int 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int i; 12 | int c = 0; 13 | 14 | for (i = 0; 1 ; i++) 15 | { 16 | if (*(s + i) == 0) 17 | break; 18 | c++; 19 | } 20 | return (c); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strncpy - a function that copies a string 6 | * @dest: a pointer to the destination string 7 | * @src: a pointer to the source string 8 | * @n: number of bytes from src to be copied 9 | * Return: returns dest 10 | */ 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; src[i] != 0 && i < n; i++) 16 | { 17 | dest[i] = src[i]; 18 | } 19 | for ( ; i < n; i++) 20 | { 21 | dest[i] = '\0'; 22 | } 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - checks if a character is lower case and returns 1, else 0 5 | * @c: the character to be checked 6 | * Return: if c islower, return 1, else return 0 7 | */ 8 | int _islower(int c) 9 | { 10 | if (c >= 97 && c <= 122) 11 | return (1); 12 | else 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _puts - a function that prints a string, followed by a new line, to stdout 6 | * @str: the string of characters to be printed 7 | * Return: returns nothing 8 | */ 9 | void _puts(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; 1 ; i++) 14 | { 15 | if (*(str + i) == 0) 16 | { 17 | printf("\n"); 18 | break; 19 | } 20 | printf("%c", *(str + i)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strcmp - compares two strings 6 | * @s1: the string to be compared to 7 | * @s2: the string being compared 8 | * Return: returns -15 if s1 < s2, 15 if s1 > s2 9 | * and returns 0 if they're equal 10 | */ 11 | int _strcmp(char *s1, char *s2) 12 | { 13 | int i; 14 | 15 | for (i = 0; s1[i] == s2[i]; i++) 16 | { 17 | if (s1[i] == 0) 18 | return (0); 19 | } 20 | return (s1[i] - s2[i]); 21 | } 22 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strspn - a function that gets the length of a prefix substring 6 | * @s: the string to be tested 7 | * @accept: the string to find 8 | * Return: returns the number of bytes 9 | */ 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | unsigned int c = 0; 14 | 15 | for (i = 0; s[i] != 0; i++) 16 | { 17 | if (s[i] == ' ') 18 | break; 19 | for (j = 0; accept[j] != 0; j++) 20 | { 21 | if (s[i] == accept[j]) 22 | c++; 23 | } 24 | } 25 | return (c); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks if a character is an alphabet 5 | * @c: the ASCII value of the character to be checked 6 | * Return: returns 0 if its a letter, 0 otherwise 7 | */ 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 11 | return (1); 12 | else 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strpbrk - a function that searches a string for any of a set of bytes 6 | * @s: the string to be searched 7 | * @accept: the string with the pattern to be matched 8 | * Return: retirns a pointer to the byte in s that matches one of the bytes 9 | * in accept, or NULL if no such byte is found 10 | */ 11 | char *_strpbrk(char *s, char *accept) 12 | { 13 | int i, j; 14 | 15 | for (i = 0; s[i] != 0; i++) 16 | { 17 | for (j = 0; accept[j] != 0; j++) 18 | { 19 | if (s[i] == accept[j]) 20 | { 21 | return ((s + i)); 22 | } 23 | } 24 | } 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strstr - a function that locates a substring 6 | * @haystack: the string to be tested 7 | * @needle: the substring to be searched for 8 | * Return: returns a pointer to the beginning of the 9 | * located substring, or NULL if the substring is not found 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | int i, j = 0, k; 14 | 15 | for (i = 0; haystack[i] != 0; i++) 16 | { 17 | k = i; 18 | j = 0; 19 | for (; needle[j] != 0; ) 20 | { 21 | if (haystack[k++] == needle[j++]) 22 | { 23 | continue; 24 | } 25 | break; 26 | } 27 | if (needle[j] == '\0') 28 | return ((haystack + i)); 29 | } 30 | return (NULL); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - computes the absolute value of an integer 5 | * @num: the number to be converted to its absolute 6 | * Return: returns an integer 7 | */ 8 | int _abs(int num) 9 | { 10 | int abs = num * ((num > 0) - (num < 0)); 11 | 12 | return (abs); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * _strcpy - a function that copies the string pointed to by src, 6 | * including the terminating null byte (\0), to the buffer pointed to by dest 7 | * @dest: the array to be returned as a pointer 8 | * @src: the string to be copied 9 | * Return: returns the string pointer to dest 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int i; 14 | 15 | for (i = 0; 1 ; i++) 16 | { 17 | dest[i] = src[i]; 18 | if (src[i] == 0) 19 | break; 20 | } 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c *.c 3 | ar rc liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int main(void) 4 | { 5 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 6 | return (0); 7 | } 8 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | 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 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x09-static_libraries/main.o -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - a program that prints its name 5 | * @argc: the number of command-line arguments 6 | * @argv: an array of strings containing the command-line arguments 7 | * Return: returns 0 (success) 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | (void)argc; 12 | printf("%s\n", argv[0]); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - a program that prints the number of arguments passed into it 5 | * @argc: the number of command-line arguments 6 | * @argv: an array of strings containing the command-line arguments 7 | * Return: returns 0 (success) 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | (void) argv; 12 | printf("%d\n", argc - 1); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - a program that prints the minimum number of coins to make change 6 | * @argc: the number of command-line arguments 7 | * @argv: an array of strings that contain the command-line arguments 8 | * Return: returns 0 (sucessful), otherwise returns 1 on error 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int cents; 13 | int quarters_25, dimes_10, nickels_5, coin_2, pennies_1; 14 | int change; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | 22 | cents = atoi(argv[1]); 23 | 24 | if (cents < 0) 25 | { 26 | printf("0\n"); 27 | return (0); 28 | } 29 | quarters_25 = cents / 25; 30 | cents = cents - (quarters_25 * 25); 31 | 32 | dimes_10 = cents / 10; 33 | cents = cents - (dimes_10 * 10); 34 | 35 | nickels_5 = cents / 5; 36 | cents = cents - (nickels_5 * 5); 37 | 38 | coin_2 = cents / 2; 39 | cents = cents - (coin_2 * 2); 40 | 41 | pennies_1 = cents / 1; 42 | 43 | change = quarters_25 + dimes_10 + nickels_5 + coin_2 + pennies_1; 44 | printf("%d\n", change); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - a program that prints all arguments it receives 5 | * @argc: the number of command-line arguments 6 | * @argv: an array of strings containing the command-line arguments 7 | * Return: returns 0 (success) 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | int i = 0; 12 | 13 | for (i = 0; i < argc; i++) 14 | { 15 | printf("%s\n", argv[i]); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - a program that multiplies two numbers 6 | * @argc: the number of command-line arguments 7 | * @argv: an array of strings containing the command-line arguments 8 | * Return: returns 0 (success) 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | if (argc != 3) 13 | { 14 | printf("Error\n"); 15 | return (1); 16 | } 17 | printf("%d\n", atoi(argv[1]) * atoi(argv[2])); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - a program that adds positive numbers 7 | * @argc: the number of command-line arguments 8 | * @argv: an array of strings containing the command-line arguments 9 | * Return: returns 0 (success) 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int i; 14 | int sum = 0; 15 | char *ptr = 0; 16 | 17 | if (argc == 1) 18 | { 19 | printf("0\n"); 20 | return (0); 21 | } 22 | for (i = 1; i < argc; i++) 23 | { 24 | sum += strtol(argv[i], &ptr, 10); 25 | if (*ptr != 0) 26 | { 27 | printf("Error\n"); 28 | return (1); 29 | } 30 | } 31 | printf("%d\n", sum); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | This folder contains solutions to 0x0A. C - argc, argv 2 | cohort 18 3 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md~: -------------------------------------------------------------------------------- 1 | This folder contains solutions to 0x0A. C - argc, argv 2 | cohort 9 3 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | 6 | #endif /*MAIN_H*/ 7 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * create_array - creates an array of chars, and initializes it with a specific 7 | * char 8 | * @size: size of the array 9 | * @c: char to initialize the array with 10 | * 11 | * Return: pointer to the array, or NULL if it fails 12 | */ 13 | char *create_array(unsigned int size, char c) 14 | { 15 | char *array; 16 | 17 | if (!size) 18 | return (NULL); 19 | 20 | array = malloc(sizeof(char) * size); 21 | if (!array) 22 | return (NULL); 23 | 24 | memset(array, c, size); 25 | 26 | return (array); 27 | } 28 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strdup - returns a pointer to a newly allocated space in memory 6 | * @str: string to be copied 7 | * 8 | * Return: pointer to the duplicated string 9 | */ 10 | char *_strdup(char *str) 11 | { 12 | char *dup; 13 | int i, len; 14 | 15 | if (!str) 16 | return (NULL); 17 | 18 | for (len = 0; str[len]; len++) /* strlen(str); */ 19 | ; 20 | 21 | dup = malloc(sizeof(char) * (len + 1)); 22 | if (!dup) 23 | return (NULL); 24 | 25 | for (i = 0; i < len; i++) /* strcpy(dup, str); */ 26 | dup[i] = str[i]; 27 | dup[i] = '\0'; 28 | 29 | return (dup); 30 | } 31 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * argstostr - concatenates all the arguments of your program 5 | * @ac: number of arguments 6 | * @av: array of arguments 7 | * 8 | * Return: pointer to a new string, or NULL if it fails 9 | */ 10 | char *argstostr(int ac, char **av) 11 | { 12 | char *str; 13 | int i, j, k, len; 14 | 15 | /* check for NULL */ 16 | if (!ac || !av) 17 | return (NULL); 18 | 19 | /* get length of resulting string */ 20 | for (i = 0, len = 0; i < ac; i++) 21 | { 22 | for (j = 0; av[i][j] != '\0'; j++) 23 | len++; 24 | len++; 25 | } 26 | 27 | /* allocate memory for string */ 28 | str = malloc(sizeof(char) * (len + 1)); 29 | if (str == NULL) 30 | return (NULL); 31 | 32 | /* copy arguments into string */ 33 | for (i = 0, k = 0; i < ac; i++) 34 | { 35 | for (j = 0; av[i][j] != '\0'; j++, k++) 36 | str[k] = av[i][j]; 37 | str[k] = '\n'; 38 | k++; 39 | } 40 | str[k] = '\0'; 41 | 42 | return (str); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - concatenates two strings 6 | * @s1: string 1 7 | * @s2: string 2 8 | * 9 | * Return: pointer to new string 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *new; 14 | int s1_len, s2_len, i, j; 15 | 16 | s1 == NULL ? s1 = "" : s1; 17 | s2 == NULL ? s2 = "" : s2; 18 | 19 | for (s1_len = 0; s1[s1_len]; s1_len++) 20 | ; 21 | for (s2_len = 0; s2[s2_len]; s2_len++) 22 | ; 23 | 24 | new = malloc(sizeof(char) * (s1_len + s2_len + 1)); 25 | if (!new) 26 | return (NULL); 27 | 28 | for (i = 0; i < s1_len; i++) 29 | new[i] = s1[i]; 30 | 31 | for (j = 0; j < s2_len; j++) 32 | new[i + j] = s2[j]; 33 | 34 | new[i + j] = '\0'; 35 | 36 | return (new); 37 | } 38 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * alloc_grid - returns a pointer to a 2 dimensional array of integers 5 | * @width: width of the grid 6 | * @height: height of the grid 7 | * 8 | * Return: pointer to the 2 dimensional array of integers 9 | */ 10 | int **alloc_grid(int width, int height) 11 | { 12 | int **grid; 13 | int i, j; 14 | 15 | if (width <= 0 || height <= 0) 16 | return (NULL); 17 | 18 | grid = malloc(sizeof(int *) * height); 19 | if (!grid) 20 | return (NULL); 21 | 22 | for (i = 0; i < height; i++) 23 | { 24 | grid[i] = malloc(sizeof(int) * width); 25 | if (grid[i] == NULL) 26 | { 27 | for (j = 0; j < i; j++) 28 | free(grid[j]); 29 | free(grid); 30 | return (NULL); 31 | } 32 | for (j = 0; j < width; j++) 33 | grid[i][j] = 0; 34 | /* memset(grid[i], 0, sizeof(int) * width); */ 35 | } 36 | return (grid); 37 | } 38 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * free_grid - frees a 2 dimensional grid previously created by 5 | * your alloc_grid function 6 | * @grid: grid to free 7 | * @height: height of grid 8 | */ 9 | void free_grid(int **grid, int height) 10 | { 11 | int i; 12 | 13 | for (i = 0; i < height; i++) 14 | free(grid[i]); 15 | free(grid); 16 | } 17 | -------------------------------------------------------------------------------- /0x0B-malloc_free/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -pedantic -std=c89 3 | INCLUDES=-I. 4 | 5 | TEST_OUTPUT=test_output 6 | TEST_FILES=test_files 7 | BIN_DIR=bin 8 | 9 | FILE= 10 | 11 | TEST_FILE:=${TEST_FILES}/$(shell echo ${FILE} | sed 's/-[^-]*\./-main./g') 12 | OTHER_FILES:=$(addprefix ${TEST_FILES}/, $(shell ls ${TEST_FILES} | grep -v main)) 13 | 14 | ifeq ($(shell if [ -f ${TEST_FILE} ]; then echo 1; else echo 0; fi), 1) 15 | TEST_FILE:=${TEST_FILE} 16 | else 17 | TEST_FILE:= 18 | endif 19 | 20 | BIN:=${BIN_DIR}/$(shell basename ${FILE} .c) 21 | OUTPUT=${TEST_OUTPUT}/$(shell basename ${FILE} .c).out 22 | 23 | all: ${BIN_DIR} ${TEST_OUTPUT} 24 | @$(CC) $(INCLUDES) $(CFLAGS) -o ${BIN} ${TEST_FILE} ${OTHER_FILES} ${FILE} 25 | @./${BIN} > ${OUTPUT} && ./${BIN} 26 | 27 | ${BIN_DIR}: 28 | @mkdir -p $@ 29 | 30 | ${TEST_OUTPUT}: 31 | @mkdir -p $@ 32 | 33 | clean: 34 | @rm -rf ${BIN_DIR} 35 | @rm -rf ${TEST_OUTPUT} 36 | @rm -f ${BIN} 37 | -------------------------------------------------------------------------------- /0x0B-malloc_free/images/0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x0B-malloc_free/images/0.png -------------------------------------------------------------------------------- /0x0B-malloc_free/images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x0B-malloc_free/images/1.png -------------------------------------------------------------------------------- /0x0B-malloc_free/images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x0B-malloc_free/images/2.png -------------------------------------------------------------------------------- /0x0B-malloc_free/images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x0B-malloc_free/images/3.png -------------------------------------------------------------------------------- /0x0B-malloc_free/images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x0B-malloc_free/images/4.png -------------------------------------------------------------------------------- /0x0B-malloc_free/images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x0B-malloc_free/images/5.png -------------------------------------------------------------------------------- /0x0B-malloc_free/images/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x0B-malloc_free/images/6.png -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | char **strtow(char *str); 11 | 12 | void simple_print_buffer(char *buffer, unsigned int size); 13 | void print_grid(int **grid, int width, int height); 14 | int _putchar(char c); 15 | 16 | #endif /* MAIN_H */ 17 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *a; 13 | 14 | a = create_array(98, 'H'); 15 | if (a == NULL) 16 | { 17 | printf("failed to allocate memory\n"); 18 | return (1); 19 | } 20 | simple_print_buffer(a, 98); 21 | free(a); 22 | 23 | a = create_array(72, 'S'); 24 | if (a == NULL) 25 | { 26 | printf("failed to allocate memory\n"); 27 | return (1); 28 | } 29 | simple_print_buffer(a, 72); 30 | free(a); 31 | 32 | a = create_array(102, 10); 33 | if (a == NULL) 34 | { 35 | printf("failed to allocate memory\n"); 36 | return (1); 37 | } 38 | simple_print_buffer(a, 102); 39 | free(a); 40 | 41 | a = create_array(96, '\0'); 42 | if (a == NULL) 43 | { 44 | printf("failed to allocate memory\n"); 45 | return (1); 46 | } 47 | simple_print_buffer(a, 96); 48 | free(a); 49 | 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - test _strdup 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(void) 11 | { 12 | char *s; 13 | 14 | s = _strdup("ALX is awesome!"); 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 | 23 | s = _strdup("Holberton School!"); 24 | if (s == NULL) 25 | { 26 | printf("failed to allocate memory\n"); 27 | return (1); 28 | } 29 | printf("%s\n", s); 30 | free(s); 31 | 32 | s = _strdup(""); 33 | if (s == NULL) 34 | { 35 | printf("failed to allocate memory\n"); 36 | return (1); 37 | } 38 | printf("%s\n", s); 39 | free(s); 40 | 41 | s = _strdup(NULL); 42 | if (s == NULL) 43 | { 44 | printf("failed to allocate memory\n"); 45 | return (1); 46 | } 47 | printf("%s\n", s); 48 | free(s); 49 | 50 | s = _strdup("First, solve the problem. Then, write the code."); 51 | if (s == NULL) 52 | { 53 | printf("failed to allocate memory\n"); 54 | return (1); 55 | } 56 | printf("%s\n", s); 57 | free(s); 58 | 59 | return (0); 60 | } 61 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char *argstostr(int, char **); 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | 15 | s = argstostr(0, NULL); 16 | if (s == NULL) 17 | { 18 | printf("Failed\n"); 19 | return (1); 20 | } 21 | printf("%s", s); 22 | free(s); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/101-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char **strtow(char *); 5 | 6 | /** 7 | * print_tab - Prints an array of string 8 | * 9 | * @tab: The array to print 10 | */ 11 | void print_tab(char **tab) 12 | { 13 | int i; 14 | 15 | for (i = 0; tab[i] != NULL; ++i) 16 | { 17 | printf("%s\n", tab[i]); 18 | } 19 | } 20 | 21 | /** 22 | * main - check the code . 23 | * 24 | * Return: Always 0. 25 | */ 26 | int main(void) 27 | { 28 | char **tab; 29 | int i; 30 | 31 | tab = strtow(" Talk is cheap. Show me the code. "); 32 | if (tab == NULL) 33 | { 34 | printf("Failed\n"); 35 | return (1); 36 | } 37 | print_tab(tab); 38 | for (i = 0; tab[i] != NULL; i++) 39 | free(tab[i]); 40 | 41 | 42 | tab = strtow("Talk is cheap. Show me the code."); 43 | if (tab == NULL) 44 | { 45 | printf("Failed\n"); 46 | return (1); 47 | } 48 | print_tab(tab); 49 | for (i = 0; tab[i] != NULL; i++) 50 | free(tab[i]); 51 | 52 | tab = strtow(" !@#$^&*(())_Talk ><><.., ][{} 76478 is ~~ cheap :) . Show #$45&*(45 me 873589 the |[]{} -_=+ code.^& "); 53 | if (tab == NULL) 54 | { 55 | printf("Failed\n"); 56 | return (1); 57 | } 58 | print_tab(tab); 59 | for (i = 0; tab[i] != NULL; i++) 60 | free(tab[i]); 61 | 62 | tab = strtow(" "); 63 | if (tab == NULL) 64 | { 65 | printf("Failed\n"); 66 | return (1); 67 | } 68 | print_tab(tab); 69 | for (i = 0; tab[i] != NULL; i++) 70 | free(tab[i]); 71 | 72 | tab = strtow(""); 73 | if (tab == NULL) 74 | { 75 | printf("Failed\n"); 76 | return (1); 77 | } 78 | print_tab(tab); 79 | for (i = 0; tab[i] != NULL; i++) 80 | free(tab[i]); 81 | 82 | return (0); 83 | } 84 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code 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 to allocate memory\n"); 18 | return (1); 19 | } 20 | printf("%s\n", s); 21 | free(s); 22 | 23 | s = str_concat("", "Hello"); 24 | if (s == NULL) 25 | { 26 | printf("failed to allocate memory\n"); 27 | return (1); 28 | } 29 | printf("%s\n", s); 30 | free(s); 31 | 32 | s = str_concat("Hello", ""); 33 | if (s == NULL) 34 | { 35 | printf("failed to allocate memory\n"); 36 | return (1); 37 | } 38 | printf("%s\n", s); 39 | free(s); 40 | 41 | s = str_concat("Hello", NULL); 42 | if (s == NULL) 43 | { 44 | printf("failed to allocate memory\n"); 45 | return (1); 46 | } 47 | printf("%s\n", s); 48 | free(s); 49 | 50 | str_concat(NULL, "Hello"); 51 | if (s == NULL) 52 | { 53 | printf("failed to allocate memory\n"); 54 | return (1); 55 | } 56 | printf("%s\n", s); 57 | free(s); 58 | 59 | 60 | str_concat(NULL, NULL); 61 | if (s == NULL) 62 | { 63 | printf("failed to allocate memory\n"); 64 | return (1); 65 | } 66 | printf("%s\n", s); 67 | free(s); 68 | 69 | return (0); 70 | } 71 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /* todo: fix pointer being freed was not allocated */ 6 | 7 | int **alloc_grid(int width, int height); 8 | void print_grid(int **grid, int width, int height); 9 | 10 | /** 11 | * free_grid - frees a 2 dimensional grid previously created by 12 | * your alloc_grid function 13 | * @grid: grid to free 14 | * @height: height of grid 15 | */ 16 | void free_grid(int **grid, int height) 17 | { 18 | int i; 19 | 20 | for (i = 0; i < height; i++) 21 | free(grid[i]); 22 | free(grid); 23 | } 24 | 25 | /** 26 | * main - test alloc_grid 27 | * 28 | * Return: Always 0. 29 | */ 30 | int main(void) 31 | { 32 | int **grid; 33 | 34 | grid = alloc_grid(6, 4); 35 | if (grid == NULL) 36 | { 37 | printf("failed to allocate memory\n"); 38 | return (1); 39 | } 40 | print_grid(grid, 6, 4); 41 | free_grid(grid, 6); 42 | 43 | grid = alloc_grid(0, 0); 44 | if (grid == NULL) 45 | { 46 | printf("failed to allocate memory\n"); 47 | return (1); 48 | } 49 | print_grid(grid, 0, 0); 50 | free_grid(grid, 0); 51 | 52 | grid = alloc_grid(128, 128); 53 | if (grid == NULL) 54 | { 55 | printf("failed to allocate memory\n"); 56 | return (1); 57 | } 58 | print_grid(grid, 128, 128); 59 | free_grid(grid, 128); 60 | 61 | grid = alloc_grid(48, 48); 62 | if (grid == NULL) 63 | { 64 | printf("failed to allocate memory\n"); 65 | return (1); 66 | } 67 | print_grid(grid, 48, 48); 68 | free_grid(grid, 48); 69 | 70 | grid = alloc_grid(-1, -1); 71 | if (grid == NULL) 72 | { 73 | printf("failed to allocate memory\n"); 74 | return (1); 75 | } 76 | print_grid(grid, -1, -1); 77 | free_grid(grid, -1); 78 | 79 | return (0); 80 | } 81 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void free_grid(int **, int); 5 | 6 | /** 7 | * alloc_grid - returns a pointer to a 2 dimensional array of integers 8 | * @width: width of the grid 9 | * @height: height of the grid 10 | * 11 | * Return: pointer to the 2 dimensional array of integers 12 | */ 13 | int **alloc_grid(int width, int height) 14 | { 15 | int **grid; 16 | int i, j; 17 | 18 | if (width <= 0 || height <= 0) 19 | return (NULL); 20 | 21 | grid = malloc(sizeof(int *) * height); 22 | if (!grid) 23 | return (NULL); 24 | 25 | for (i = 0; i < height; i++) 26 | { 27 | grid[i] = malloc(sizeof(int) * width); 28 | if (grid[i] == NULL) 29 | { 30 | for (j = 0; j < i; j++) 31 | free(grid[j]); 32 | free(grid); 33 | return (NULL); 34 | } 35 | for (j = 0; j < width; j++) 36 | grid[i][j] = 0; 37 | /* memset(grid[i], 0, sizeof(int) * width); */ 38 | } 39 | return (grid); 40 | } 41 | 42 | /** 43 | * main - check the code . 44 | * 45 | * Return: Always 0. 46 | */ 47 | int main(void) 48 | { 49 | int **grid; 50 | int h; 51 | int w; 52 | 53 | h = 100; 54 | w = 200; 55 | grid = alloc_grid(w, h); 56 | if (grid == NULL) 57 | { 58 | return (1); 59 | } 60 | free_grid(grid, h); 61 | 62 | grid = alloc_grid(20, 10); 63 | if (grid == NULL) 64 | return (1); 65 | free_grid(grid, 10); 66 | 67 | grid = alloc_grid(200, 100); 68 | if (grid == NULL) 69 | return (1); 70 | free_grid(grid, 100); 71 | 72 | grid = alloc_grid(1, 1); 73 | if (grid == NULL) 74 | return (1); 75 | free_grid(grid, 1); 76 | 77 | free_grid(NULL, 0); 78 | printf("OK\n"); 79 | 80 | return (0); 81 | } 82 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/print_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * print_grid - prints a grid of integers 5 | * @grid: the grid to print 6 | * @width: width of the grid 7 | * @height: height of the grid 8 | */ 9 | void print_grid(int **grid, int width, int height) 10 | { 11 | int i, j; 12 | 13 | for (i = 0; i < height; i++) 14 | { 15 | for (j = 0; j < width; j++) 16 | { 17 | printf("%d", grid[i][j]); 18 | if (j < width - 1) 19 | printf(", "); 20 | } 21 | printf("\n"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/test_files/simple_print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * simple_print_buffer - prints buffer in hexa 7 | * @buffer: the address of memory to print 8 | * @size: the size of the memory to print 9 | * 10 | * Return: Nothing. 11 | */ 12 | void simple_print_buffer(char *buffer, unsigned int size) 13 | { 14 | unsigned int i; 15 | 16 | i = 0; 17 | while (i < size) 18 | { 19 | if (i % 10) 20 | { 21 | printf(" "); 22 | } 23 | if (!(i % 10) && i) 24 | { 25 | printf("\n"); 26 | } 27 | printf("0x%02x", buffer[i]); 28 | i++; 29 | } 30 | printf("\n"); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * malloc_checked - a function that allocates memory using malloc 6 | * @b: the size of memory to be allocated 7 | * Return: returns a pointer to the allocated memory 8 | */ 9 | void *malloc_checked(unsigned int b) 10 | { 11 | void *ptr = malloc(b); 12 | 13 | if (ptr == NULL) 14 | exit(98); 15 | return (ptr); 16 | } 17 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - a function that concatenates two strings 6 | * @s1: the first string 7 | * @s2: the second string 8 | * @n: the number of bytes of s2 to be concatenated 9 | * Return: returns a pointer to a new string which contains 10 | * s1 followed by the first n bytes of s2, and a=the null-terminator 11 | */ 12 | char *string_nconcat(char *s1, char *s2, unsigned int n) 13 | { 14 | unsigned int i, j; 15 | unsigned int c1 = 0, c2 = 0, c = 0; 16 | char *str; 17 | 18 | if (s1 != NULL) 19 | for (i = 0; s1[i] != '\0'; i++) 20 | c1++; 21 | 22 | if (s2 != NULL) 23 | for (i = 0; s2[i] != '\0'; i++) 24 | if (i < n) 25 | c2++; 26 | 27 | c = c1 + c2 + 1; 28 | 29 | str = malloc(c); 30 | if (str == NULL) 31 | return (NULL); 32 | 33 | for (i = 0; i < c1; i++) 34 | str[i] = s1[i]; 35 | 36 | for (j = 0 ; j < c2; i++, j++) 37 | str[i] = s2[j]; 38 | str[i] = '\0'; 39 | 40 | return (str); 41 | } 42 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _realloc - a function that reallocates a memory block using malloc and free 6 | * @ptr: a pointer to the memory previously allocated 7 | * @old_size: the size, in bytes of the allocated space for ptr 8 | * @new_size: the size, in bytes of the new memory block 9 | * Return: returns a pointer to the newly allocated memory block 10 | */ 11 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 12 | { 13 | unsigned int i; 14 | char *arr; 15 | char *pptr = ptr; 16 | 17 | if (new_size == old_size) 18 | return (ptr); 19 | 20 | if (pptr == NULL) 21 | { 22 | arr = malloc(new_size); 23 | return (arr); 24 | } 25 | if (pptr != NULL && new_size == 0) 26 | { 27 | free(pptr); 28 | return (NULL); 29 | } 30 | if (new_size < old_size) 31 | { 32 | arr = malloc(new_size); 33 | 34 | for (i = 0; i < new_size; i++) 35 | { 36 | arr[i] = pptr[i]; 37 | } 38 | free(pptr); 39 | return (arr); 40 | } 41 | if (new_size > old_size) 42 | { 43 | arr = malloc(new_size); 44 | for (i = 0; i < old_size; i++) 45 | { 46 | arr[i] = pptr[i]; 47 | } 48 | free(pptr); 49 | return (arr); 50 | } 51 | return (pptr); 52 | } 53 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | int is_digit(char *s); 6 | int _strlen(char *s); 7 | void errors(void); 8 | /** 9 | * main - a program that multiplies two positive numbers 10 | * @argc: the number of arguments 11 | * @argv: an array of strings containing each argument 12 | * Return: returns 0 (success) 13 | */ 14 | int main(int argc, char **argv) 15 | { 16 | char *s1, *s2; 17 | int len1, len2, len, i, carry, digit1, digit2, *result, a = 0; 18 | 19 | s1 = argv[1], s2 = argv[2]; 20 | if (argc != 3 || !is_digit(s1) || !is_digit(s2)) 21 | errors(); 22 | len1 = _strlen(s1); 23 | len2 = _strlen(s2); 24 | len = len1 + len2 + 1; 25 | result = malloc(sizeof(int) * len); 26 | if (!result) 27 | return (1); 28 | for (i = 0; i <= len1 + len2; i++) 29 | result[i] = 0; 30 | for (len1 = len1 - 1; len1 >= 0; len1--) 31 | { 32 | digit1 = s1[len1] - '0'; 33 | carry = 0; 34 | for (len2 = _strlen(s2) - 1; len2 >= 0; len2--) 35 | { 36 | digit2 = s2[len2] - '0'; 37 | carry += result[len1 + len2 + 1] + (digit1 * digit2); 38 | result[len1 + len2 + 1] = carry % 10; 39 | carry /= 10; 40 | } 41 | if (carry > 0) 42 | result[len1 + len2 + 1] += carry; 43 | } 44 | for (i = 0; i < len - 1; i++) 45 | { 46 | if (result[i]) 47 | a = 1; 48 | if (a) 49 | _putchar(result[i] + '0'); 50 | } 51 | if (!a) 52 | _putchar('0'); 53 | _putchar('\n'); 54 | free(result); 55 | return (0); 56 | } 57 | 58 | /** 59 | * is_digit - checks if every character of a string is a digit 60 | * @s: the string to be checked 61 | * Return: returns 0 if a non-digit is found, 1 otherwise 62 | */ 63 | int is_digit(char *s) 64 | { 65 | int i = 0; 66 | 67 | for (i = 0; s[i] != '\0'; i++) 68 | { 69 | if (s[i] < '0' || s[i] > '9') 70 | return (0); 71 | } 72 | return (1); 73 | } 74 | 75 | /** 76 | * _strlen - calculates the length of a string (excluding the null-terminator) 77 | * @s: the string to be checked 78 | * Return: returns the length of the string as an int 79 | */ 80 | int _strlen(char *s) 81 | { 82 | int i = 0, c = 0; 83 | 84 | for (i = 0; s[i] != '\0'; i++) 85 | c++; 86 | 87 | return (c); 88 | } 89 | 90 | /** 91 | * errors - prints out "Error" if an error is encountered and exits with 92 | * a status of 98 93 | * Return: returns nothing 94 | */ 95 | void errors(void) 96 | { 97 | printf("Error\n"); 98 | exit(98); 99 | } 100 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _calloc - allocates memory for an array and initializes it to zero 6 | * @nmemb: numbe rof elements in the array 7 | * @size: size of the array 8 | * Return: returns a pointer to the allocated memory 9 | */ 10 | void *_calloc(unsigned int nmemb, unsigned int size) 11 | { 12 | unsigned int i, c; 13 | char *ptr; 14 | 15 | if (nmemb == 0 || size == 0) 16 | return (NULL); 17 | 18 | c = nmemb * size; 19 | 20 | ptr = malloc(c); 21 | if (ptr == NULL) 22 | return (NULL); 23 | 24 | for (i = 0; i < c; i++) 25 | ptr[i] = 0; 26 | 27 | return ((void *) ptr); 28 | } 29 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * array_range - creates an array of integers 6 | * @min: the minimum integer (included) 7 | * @max: the maximum integer (included) 8 | * Return: returns a pointer to the newly created array 9 | */ 10 | int *array_range(int min, int max) 11 | { 12 | int i = 0, j = 0, c = 0; 13 | int *arr; 14 | 15 | if (min > max) 16 | return (NULL); 17 | 18 | c = max - min + 1; 19 | arr = malloc(sizeof(int) * c); 20 | if (arr == NULL) 21 | return (NULL); 22 | 23 | for (i = min; i <= max; i++, j++) 24 | { 25 | arr[j] = i; 26 | } 27 | return (arr); 28 | } 29 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md~: -------------------------------------------------------------------------------- 1 | This folder contains solutions to tasks on 0x0C. C - More malloc, free 2 | -------------------------------------------------------------------------------- /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 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | void *malloc_checked(unsigned int b); 6 | char *string_nconcat(char *s1, char *s2, unsigned int n); 7 | void *_calloc(unsigned int nmemb, unsigned int size); 8 | int *array_range(int min, int max); 9 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 10 | 11 | #endif /*MAIN_H*/ 12 | -------------------------------------------------------------------------------- /0x0D-preprocessor/#3-function_like_macro.h#: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < (0) ? (x) * (-1) : (x)) 5 | 6 | #endif /* 3-FUNCTION_LIKE_MACRO_H */ 7 | -------------------------------------------------------------------------------- /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 /* 0-OBJECT_LIKE_MACRO_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif /* PI_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - a program that prints the name of the file it was compiled from 5 | * Return: returns 0 (success) 6 | */ 7 | int main(void) 8 | { 9 | printf("%s\n", __FILE__); 10 | 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_LIKE_MACRO_H 2 | #define FUNCTION_LIKE_MACRO_H 3 | 4 | #define ABS(x) ((x) < (0) ? (x) * (-1) : (x)) 5 | 6 | #endif /* 3-FUNCTION_LIKE_MACRO_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif /* 4-SUM_H */ 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md~: -------------------------------------------------------------------------------- 1 | This folder contains solutions to tasks on 0x0D. C - Preprocessor 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - initialize a variable of type struct dog 5 | * @d: pointer to struct dog 6 | * @name: name of dog 7 | * @age: age of dog 8 | * @owner: owner of dog 9 | * 10 | * Return: void 11 | */ 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d) 15 | { 16 | d->name = name; 17 | d->age = age; 18 | d->owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * print_dog - prints a struct dog 6 | * @d: pointer to struct dog 7 | * 8 | * Return: void 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (!d) 13 | return; 14 | 15 | printf("Name: %s\n", d->name ? d->name : "(nil)"); 16 | printf("Age: %f\n", d->age); 17 | printf("Owner: %s\n", d->owner ? d->owner : "(nil)"); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * _strlen - returns length of string 6 | * @s: string 7 | * 8 | * Return: length of string 9 | */ 10 | int _strlen(char *s) 11 | { 12 | int i; 13 | 14 | for (i = 0; s[i]; i++) 15 | ; 16 | 17 | return (i); 18 | } 19 | 20 | /** 21 | * _strcpy - copies string 22 | * @dest: destination 23 | * @src: source 24 | * 25 | * Return: pointer to destination 26 | */ 27 | char *_strcpy(char *dest, char *src) 28 | { 29 | int i; 30 | 31 | for (i = 0; src[i]; i++) 32 | dest[i] = src[i]; 33 | dest[i] = '\0'; 34 | 35 | return (dest); 36 | } 37 | 38 | /** 39 | * new_dog - creates a new dog 40 | * @name: name of dog 41 | * @age: age of dog 42 | * @owner: owner of dog 43 | * 44 | * Return: pointer to new dog 45 | */ 46 | dog_t *new_dog(char *name, float age, char *owner) 47 | { 48 | dog_t *new_dog; 49 | 50 | new_dog = malloc(sizeof(dog_t)); 51 | if (!new_dog) 52 | return (NULL); 53 | 54 | new_dog->name = malloc(sizeof(char) * (_strlen(name) + 1)); 55 | if (!new_dog->name) 56 | { 57 | free(new_dog); 58 | return (NULL); 59 | } 60 | new_dog->name = _strcpy(new_dog->name, name); 61 | 62 | new_dog->owner = malloc(sizeof(char) * (_strlen(owner) + 1)); 63 | if (!new_dog->owner) 64 | { 65 | free(new_dog->name); 66 | free(new_dog); 67 | return (NULL); 68 | } 69 | new_dog->owner = _strcpy(new_dog->owner, owner); 70 | 71 | new_dog->age = age; 72 | 73 | return (new_dog); 74 | } 75 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - frees dogs 6 | * @d: pointer to dog 7 | * 8 | * Return: void 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d) 13 | { 14 | free(d->name); 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | In C, structures (also known as structs) are user-defined composite data types that allow you to group together related variables of different data types into a single unit. They provide a way to create complex data structures that can represent real-world entities or organize related data logically. 2 | 3 | Here's an example of how to define a structure: 4 | 5 | ```c 6 | struct Person { 7 | char name[50]; 8 | int age; 9 | float height; 10 | }; 11 | ``` 12 | 13 | In the above code, we define a structure named `Person` that has three members: `name`, `age`, and `height`. Each member can have a different data type. 14 | 15 | Structures are used when you need to represent entities with multiple properties. For example, you might use a structure to represent a person with attributes like name, age, and height. Structures are also useful for organizing related data, creating records, or representing complex data structures like linked lists, trees, or graphs. 16 | 17 | To use a structure, you typically declare variables of that structure type and access its members using the dot (`.`) operator: 18 | 19 | ```c 20 | struct Person person1; 21 | strcpy(person1.name, "John"); 22 | person1.age = 25; 23 | person1.height = 1.75; 24 | ``` 25 | 26 | In the above code, we declare a variable `person1` of type `struct Person` and assign values to its members using the dot operator. 27 | 28 | C also provides the `typedef` keyword, which allows you to create custom type aliases. It can be used to provide a shorter or more descriptive name for a complex type like a structure, union, or pointer. 29 | 30 | Here's an example of how to use `typedef` with a structure: 31 | 32 | ```c 33 | typedef struct { 34 | char name[50]; 35 | int age; 36 | float height; 37 | } Person; 38 | ``` 39 | 40 | In the above code, we define a structure inline without specifying a name, and then immediately create a type alias `Person` using `typedef`. Now, we can use `Person` as a shorthand for `struct Person`: 41 | 42 | ```c 43 | Person person1; 44 | strcpy(person1.name, "John"); 45 | person1.age = 25; 46 | person1.height = 1.75; 47 | ``` 48 | 49 | Using `typedef` can make your code more readable and maintainable, especially when dealing with complex or lengthy type names. It also allows you to abstract away implementation details by using custom type names that convey the purpose or semantics of the data structure. -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * struct dog - struct dog 6 | * @name: name of dog 7 | * @age: age of dog 8 | * @owner: owner of dog 9 | * 10 | * Description: A struct for a dog with name, age, and owner 11 | */ 12 | typedef struct dog 13 | { 14 | char *name; 15 | float age; 16 | char *owner; 17 | } dog_t; 18 | 19 | void init_dog(struct dog *d, char *name, float age, char *owner); 20 | void print_dog(struct dog *d); 21 | dog_t *new_dog(char *name, float age, char *owner); 22 | void free_dog(dog_t *d); 23 | char *_strcpy(char *dest, char *src); 24 | int _strlen(char *s); 25 | 26 | #endif /* DOG_H */ 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - Prints a name. 5 | * @name: The name to be printed. 6 | * @f: A pointer to a function that prints a name. 7 | */ 8 | void print_name(char *name, void (*f)(char *)) 9 | { 10 | if (name == NULL || f == NULL) 11 | return; 12 | 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - executes a func given as a param on each element of array 5 | * @array: array to execute func on 6 | * @size: size of array 7 | * @action: pointer to the func to use 8 | * 9 | * Return: void 10 | */ 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | size_t i; 14 | 15 | if (array && action) 16 | { 17 | for (i = 0; i < size; i++) 18 | { 19 | action(array[i]); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Prints the opcodes of itself. 6 | * @argc: The number of arguments supplied to the program. 7 | * @argv: An array of pointers to the arguments. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int bytes, index; 14 | int (*address)(int, char **) = main; 15 | unsigned char opcode; 16 | 17 | if (argc != 2) 18 | { 19 | printf("Error\n"); 20 | exit(1); 21 | } 22 | 23 | bytes = atoi(argv[1]); 24 | 25 | if (bytes < 0) 26 | { 27 | printf("Error\n"); 28 | exit(2); 29 | } 30 | 31 | for (index = 0; index < bytes; index++) 32 | { 33 | opcode = *(unsigned char *)address; 34 | printf("%.2x", opcode); 35 | 36 | if (index == bytes - 1) 37 | continue; 38 | printf(" "); 39 | 40 | address++; 41 | } 42 | 43 | printf("\n"); 44 | 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - Searches for an integer in an array of integers. 5 | * @array: The array of integers. 6 | * @size: The size of the array. 7 | * @cmp: A pointer to the function to be used to compare values. 8 | * 9 | * Return: If no element matches or size <= 0 - -1. 10 | * Otherwise - The index of the first element for which 11 | * the cmp function does not return 0. 12 | */ 13 | int int_index(int *array, int size, int (*cmp)(int)) 14 | { 15 | int index; 16 | 17 | if (array == NULL || cmp == NULL) 18 | return (-1); 19 | 20 | for (index = 0; index < size; index++) 21 | { 22 | if (cmp(array[index]) != 0) 23 | return (index); 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | /** 4 | * struct op - A struct op. 5 | * @op: The operator. 6 | * @f: The associated function. 7 | */ 8 | typedef struct op 9 | { 10 | char *op; 11 | int (*f)(int a, int b); 12 | } op_t; 13 | 14 | int op_add(int a, int b); 15 | int op_sub(int a, int b); 16 | int op_mul(int a, int b); 17 | int op_div(int a, int b); 18 | int op_mod(int a, int b); 19 | int (*get_op_func(char *s))(int, int); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | * get_op_func - Selects the correct function to perform 6 | * the operation asked by the user. 7 | * @s: The operator passed as argument. 8 | * 9 | * Return: A pointer to the function corresponding 10 | * to the operator given as a parameter. 11 | */ 12 | int (*get_op_func(char *s))(int, int) 13 | { 14 | op_t ops[] = { 15 | {"+", op_add}, 16 | {"-", op_sub}, 17 | {"*", op_mul}, 18 | {"/", op_div}, 19 | {"%", op_mod}, 20 | {NULL, NULL} 21 | }; 22 | int i = 0; 23 | 24 | while (ops[i].op != NULL && *(ops[i].op) != *s) 25 | i++; 26 | 27 | return (ops[i].f); 28 | } 29 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints the result of simple operations. 7 | * @argc: The number of arguments supplied to the program. 8 | * @argv: An array of pointers to the arguments. 9 | * 10 | * Return: Always 0. 11 | */ 12 | int main(int __attribute__((__unused__)) argc, char *argv[]) 13 | { 14 | int num1, num2; 15 | char *op; 16 | 17 | if (argc != 4) 18 | { 19 | printf("Error\n"); 20 | exit(98); 21 | } 22 | 23 | num1 = atoi(argv[1]); 24 | op = argv[2]; 25 | num2 = atoi(argv[3]); 26 | 27 | if (get_op_func(op) == NULL || op[1] != '\0') 28 | { 29 | printf("Error\n"); 30 | exit(99); 31 | } 32 | 33 | if ((*op == '/' && num2 == 0) || 34 | (*op == '%' && num2 == 0)) 35 | { 36 | printf("Error\n"); 37 | exit(100); 38 | } 39 | 40 | printf("%d\n", get_op_func(op)(num1, num2)); 41 | 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | int op_add(int a, int b); 4 | int op_sub(int a, int b); 5 | int op_mul(int a, int b); 6 | int op_div(int a, int b); 7 | int op_mod(int a, int b); 8 | 9 | /** 10 | * op_add - Returns the sum of two numbers. 11 | * @a: The first number. 12 | * @b: The second number. 13 | * 14 | * Return: The sum of a and b. 15 | */ 16 | int op_add(int a, int b) 17 | { 18 | return (a + b); 19 | } 20 | 21 | /** 22 | * op_sub - Returns the difference of two numbers. 23 | * @a: The first number. 24 | * @b: The second number. 25 | * 26 | * Return: The difference of a and b. 27 | */ 28 | int op_sub(int a, int b) 29 | { 30 | return (a - b); 31 | } 32 | 33 | /** 34 | * op_mul - Returns the product of two numbers. 35 | * @a: The first number. 36 | * @b: The second number. 37 | * 38 | * Return: The product of a and b. 39 | */ 40 | int op_mul(int a, int b) 41 | { 42 | return (a * b); 43 | } 44 | 45 | /** 46 | * op_div - Returns the division of two numbers. 47 | * @a: The first number. 48 | * @b: The second number. 49 | * 50 | * Return: The quotient of a and b. 51 | */ 52 | int op_div(int a, int b) 53 | { 54 | return (a / b); 55 | } 56 | 57 | /** 58 | * op_mod - Returns the remainder of the division of two numbers. 59 | * @a: The first number. 60 | * @b: The second number. 61 | * 62 | * Return: The remainder of the division of a by b. 63 | */ 64 | int op_mod(int a, int b) 65 | { 66 | return (a % b); 67 | } 68 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md~: -------------------------------------------------------------------------------- 1 | # 0x0F. C - Function pointers 2 | 3 | This folder contains solutions to tasks on **Function pointers** 4 | 5 | ## General Objectives 6 | 7 | - What are function pointers and how to use them 8 | - What does a function pointer exactly hold 9 | - Where does a function pointer point to in the virtual memory 10 | 11 | ## General Requirements 12 | 13 | * Allowed editors: `vi`, `vim`, `emacs` 14 | * All your files will be compiled on Ubuntu 20.04 LTS using `gcc`, using the options `-Wall` `-Werror` `-Wextra` `-pedantic` `-std=gnu89` 15 | * All your files should end with a new line 16 | * A `README.md` file, at the root of the folder of the project is mandatory 17 | * Your code should use the `Betty` style. It will be checked using `betty-style.pl` and `betty-doc.pl` 18 | * You are not allowed to use global variables 19 | * No more than 5 functions per file 20 | * The only C standard library functions allowed are `malloc`, `free` and `exit`. Any use of functions like `printf`, `puts`, `calloc`, `realloc` etc… is forbidden 21 | * You are allowed to use `_putchar` 22 | * You don’t have to push `_putchar.c`, we will use our file. If you do it won’t be taken into account 23 | * In the following examples, the `main.c` files are shown as examples. You can use them to test your functions, but you don’t have to push them to your repo (if you do we won’t take them into account). We will use our own `main.c` files at compilation. Our `main.c` files might be different from the one shown in the examples 24 | * The prototypes of all your functions and the prototype of the function `_putchar` should be included in your header file called `function_pointers.h` 25 | * Don’t forget to push your header file 26 | * All your header files should be include guarded 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_H_ 2 | #define _FUNCTION_POINTERS_H_ 3 | 4 | #include 5 | 6 | int _putchar(char); 7 | void print_name(char *name, void (*f)(char *)); 8 | void array_iterator(int *array, size_t size, void (*action)(int)); 9 | int int_index(int *array, int size, int (*cmp)(int)); 10 | 11 | #endif /* _FUNCTION_POINTERS_H_ */ 12 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * sum_them_all - returns the sum of all its parameters 5 | * @n: number of parameters 6 | * Return: sum of all parameters 7 | */ 8 | int sum_them_all(const unsigned int n, ...) 9 | { 10 | va_list ap; 11 | unsigned int i; 12 | int sum = 0; 13 | 14 | if (n == 0) 15 | return (0); 16 | 17 | va_start(ap, n); 18 | for (i = 0; i < n; i++) 19 | sum += va_arg(ap, int); 20 | va_end(ap); 21 | 22 | return (sum); 23 | } 24 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | * print_numbers - prints numbers, followed by a new line 6 | * @separator: string to be printed between numbers 7 | * @n: number of integers passed to the function 8 | */ 9 | void print_numbers(const char *separator, const unsigned int n, ...) 10 | { 11 | va_list ap; 12 | unsigned int i; 13 | 14 | va_start(ap, n); 15 | for (i = 0; i < n; i++) 16 | { 17 | printf("%d", va_arg(ap, int)); 18 | if (i < n - 1 && separator != NULL) 19 | printf("%s", separator); 20 | } 21 | va_end(ap); 22 | printf("\n"); 23 | } 24 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | * print_strings - prints strings, followed by a new line 6 | * @separator: string to be printed between the strings 7 | * @n: number of strings passed to the function 8 | */ 9 | void print_strings(const char *separator, const unsigned int n, ...) 10 | { 11 | unsigned int i; 12 | char *str; 13 | va_list args; 14 | 15 | va_start(args, n); 16 | for (i = 0; i < n; i++) 17 | { 18 | str = va_arg(args, char*); 19 | printf("%s%s", str == NULL ? "(nil)" : str, 20 | (i < n - 1 && separator != NULL) ? separator : ""); 21 | } 22 | va_end(args); 23 | printf("\n"); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | * print_char - prints a character 6 | * @ap: va_list containing the character to print 7 | */ 8 | void print_char(va_list ap) 9 | { 10 | printf("%c", va_arg(ap, int)); 11 | } 12 | 13 | /** 14 | * print_int - prints an integer 15 | * @ap: va_list containing the integer to print 16 | */ 17 | void print_int(va_list ap) 18 | { 19 | printf("%d", va_arg(ap, int)); 20 | } 21 | 22 | /** 23 | * print_float - prints a float 24 | * @ap: va_list containing the float to print 25 | */ 26 | void print_float(va_list ap) 27 | { 28 | printf("%f", va_arg(ap, double)); 29 | } 30 | 31 | /** 32 | * print_string - prints a string 33 | * @ap: va_list containing the string to print 34 | */ 35 | void print_string(va_list ap) 36 | { 37 | char *s = va_arg(ap, char*); 38 | 39 | printf("%s", s == NULL ? "(nil)" : s); 40 | } 41 | 42 | /** 43 | * print_all - prints anything 44 | * @format: list of types of arguments passed to the function 45 | */ 46 | void print_all(const char * const format, ...) 47 | { 48 | va_list ap; 49 | int i = 0, j; 50 | char *sep = ""; 51 | 52 | print_t print[] = { 53 | {"c", print_char}, 54 | {"i", print_int}, 55 | {"f", print_float}, 56 | {"s", print_string}, 57 | {NULL, NULL} 58 | }; 59 | 60 | va_start(ap, format); 61 | while (format && format[i]) 62 | { 63 | j = 0; 64 | while (j < 4 && format[i] != print[j].param[0]) 65 | ++j; 66 | if (j < 4) 67 | { 68 | printf("%s", sep); 69 | print[j].f(ap); 70 | sep = ", "; 71 | } 72 | ++i; 73 | } 74 | printf("\n"); 75 | va_end(ap); 76 | } 77 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | Variadic functions in C allow you to define functions that can accept a variable number of arguments. They are useful when you need to handle a flexible number of parameters in a function. Here's a brief explanation of how to use variadic functions in C: 2 | 3 | 1. Include the `` header: 4 | To work with variadic functions, you need to include the `` header, which provides the necessary macros and types. 5 | 6 | 2. Declare the variadic function: 7 | Declare your variadic function with an ellipsis (`...`) in the parameter list to indicate that it can accept a variable number of arguments. For example: 8 | 9 | ````c 10 | #include 11 | 12 | int sum(int count, ...); 13 | ``` 14 | 15 | In this example, the `sum` function takes an integer parameter `count` to specify the number of arguments to be passed. 16 | 17 | 3. Define the variadic function: 18 | Define your variadic function using the `va_list` type and the macros provided by ``. Here's an example of a simple variadic function that calculates the sum of its arguments: 19 | 20 | ````c 21 | #include 22 | 23 | int sum(int count, ...) { 24 | va_list args; 25 | int total = 0; 26 | 27 | va_start(args, count); // Initialize the argument list 28 | 29 | for (int i = 0; i < count; i++) { 30 | int num = va_arg(args, int); // Retrieve the next argument 31 | total += num; 32 | } 33 | 34 | va_end(args); // Clean up the argument list 35 | 36 | return total; 37 | } 38 | ``` 39 | 40 | In this example, `va_list` is used to declare the argument list, `va_start` initializes the list with the last named argument, `va_arg` retrieves the next argument of the specified type, and `va_end` cleans up the argument list. 41 | 42 | 4. Call the variadic function: 43 | You can call the variadic function like any other function, providing the appropriate number and type of arguments. For example: 44 | 45 | ````c 46 | int result = sum(3, 1, 2, 3); // Call the 'sum' function with three arguments 47 | ``` 48 | 49 | In this case, `sum` is called with three arguments: 1, 2, and 3. 50 | 51 | Variadic functions can be powerful when you need to handle a variable number of arguments. However, it's important to ensure that you have enough information to process the arguments correctly, as variadic functions do not provide compile-time type checking. -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | 4 | #include 5 | 6 | /** 7 | * struct print - struct for printing different types 8 | * @param: format specifier 9 | * @f: function to print 10 | */ 11 | typedef struct print 12 | { 13 | char *param; 14 | void (*f)(va_list); 15 | } print_t; 16 | 17 | int sum_them_all(const unsigned int n, ...); 18 | void print_numbers(const char *separator, const unsigned int n, ...); 19 | void print_strings(const char *separator, const unsigned int n, ...); 20 | void print_all(const char * const format, ...); 21 | 22 | #endif /* VARIADIC_FUNCTIONS_H */ 23 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_list - prints all the elements of a list_t list 6 | * @h: pointer to the head of the list 7 | * 8 | * Return: the number of nodes 9 | */ 10 | size_t print_list(const list_t *h) 11 | { 12 | size_t i = 0; 13 | 14 | while (h) 15 | { 16 | if (h->str) 17 | printf("[%u] %s\n", h->len, h->str); 18 | else 19 | printf("[0] (nil)\n"); 20 | h = h->next; 21 | i++; 22 | } 23 | return (i); 24 | } 25 | -------------------------------------------------------------------------------- /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 list_t list 5 | * @h: pointer to the head of the list 6 | * 7 | * Return: the number of nodes 8 | */ 9 | size_t list_len(const list_t *h) 10 | { 11 | size_t i = 0; 12 | 13 | while (h) 14 | { 15 | h = h->next; 16 | i++; 17 | } 18 | return (i); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * premain - prints a string before the main function is executed 5 | */ 6 | __attribute__ ((constructor)) 7 | void premain(void) 8 | { 9 | printf("You're beat! and yet, you must allow,\n"); 10 | printf("I bore my house upon my back!\n"); 11 | } 12 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | ; Declare needed C functions 2 | extern printf 3 | 4 | section .data 5 | msg db "Hello, Holberton", 0 ; Define the string variable named msg 6 | fmt db "%s", 10, 0 ; Define the string variable named fmt and initialize it with “%s\n\0” 7 | 8 | section .text ; Code section 9 | global main ; The standard gcc entry point 10 | main: ; The program label for the entry point 11 | push rbp ; set up stack frame 12 | 13 | ; Pass the arguments to printf 14 | mov rdi, fmt ; First argument: the format string 15 | mov rsi, msg ; Second argument: the string to print 16 | mov rax, 0 ; Move 0 into rax register 17 | 18 | ; Call printf 19 | call printf ; Call printf function 20 | 21 | ; Clean up the stack and return 22 | pop rbp ; restore stack 23 | mov rax,0 ; normal, no error, return value 24 | ret ; return 25 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * _strlen - returns the length of a string 6 | * @s: string to be measured 7 | * 8 | * Return: the length of the string 9 | */ 10 | int _strlen(const char *s) 11 | { 12 | int i = 0; 13 | 14 | while (s[i]) 15 | i++; 16 | return (i); 17 | } 18 | 19 | /** 20 | * add_node - adds a new node at the beginning of a list_t list 21 | * @head: pointer to the head of the list 22 | * @str: string to be duplicated 23 | * 24 | * Return: the address of the new element, or NULL if it failed 25 | */ 26 | list_t *add_node(list_t **head, const char *str) 27 | { 28 | list_t *new; 29 | 30 | new = malloc(sizeof(list_t)); 31 | if (new == NULL) 32 | return (NULL); 33 | new->str = strdup(str); 34 | new->len = _strlen(str); 35 | new->next = *head; 36 | *head = new; 37 | return (new); 38 | } 39 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * _strlen - returns the length of a string 6 | * @s: string to be measured 7 | * 8 | * Return: the length of the string 9 | */ 10 | int _strlen(const char *s) 11 | { 12 | int i = 0; 13 | 14 | while (s[i]) 15 | i++; 16 | return (i); 17 | } 18 | 19 | /** 20 | * add_node_end - adds a new node at the end of a list_t list 21 | * @head: pointer to the head of the list 22 | * @str: string to be duplicated 23 | * 24 | * Return: the address of the new element, or NULL if it failed 25 | */ 26 | list_t *add_node_end(list_t **head, const char *str) 27 | { 28 | list_t *new, *last; 29 | 30 | new = malloc(sizeof(list_t)); 31 | if (new == NULL) 32 | return (NULL); 33 | new->str = strdup(str); 34 | new->len = _strlen(str); 35 | new->next = NULL; 36 | if (*head == NULL) 37 | *head = new; 38 | else 39 | { 40 | last = *head; 41 | while (last->next) 42 | last = last->next; 43 | last->next = new; 44 | } 45 | return (new); 46 | } 47 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list_t list 5 | * @head: pointer to the head of the list 6 | * 7 | * Return: void 8 | */ 9 | void free_list(list_t *head) 10 | { 11 | list_t *tmp; 12 | 13 | while (head) 14 | { 15 | tmp = head; 16 | head = head->next; 17 | free(tmp->str); 18 | free(tmp); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | 6 | /** 7 | * struct list_s - singly linked list 8 | * @str: string - (malloc'ed string) 9 | * @len: length of the string 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | */ 14 | typedef struct list_s 15 | { 16 | char *str; 17 | unsigned int len; 18 | struct list_s *next; 19 | } list_t; 20 | 21 | size_t print_list(const list_t *h); 22 | size_t list_len(const list_t *h); 23 | list_t *add_node(list_t **head, const char *str); 24 | list_t *add_node_end(list_t **head, const char *str); 25 | void free_list(list_t *head); 26 | 27 | #endif /* LISTS_H */ 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | 5 | /** 6 | * print_listint - Prints all the elements of a listint_t list. 7 | * @h: A pointer to the head of the list_t list. 8 | * 9 | * Return: number of nodes in the list_t list. 10 | */ 11 | size_t print_listint(const listint_t *h) 12 | { 13 | size_t nodes = 0; 14 | 15 | while (h) 16 | { 17 | nodes++; 18 | printf("%d\n", h->n); 19 | h = h->next; 20 | } 21 | 22 | return (nodes); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * listint_len - Returns the number of elements 6 | * in a linked listint_t list. 7 | * @h: A pointer to the head of the listint_t list. 8 | * 9 | * Return: number of elements in the listint_t list. 10 | */ 11 | size_t listint_len(const listint_t *h) 12 | { 13 | size_t nodes = 0; 14 | 15 | while (h) 16 | { 17 | nodes++; 18 | h = h->next; 19 | } 20 | 21 | return (nodes); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - Reverses a listint_t list. 5 | * @head: A pointer to the address of 6 | * the head of the list_t list. 7 | * 8 | * Return: A pointer to the first node of the reversed list. 9 | */ 10 | listint_t *reverse_listint(listint_t **head) 11 | { 12 | listint_t *ahead, *behind; 13 | 14 | if (head == NULL || *head == NULL) 15 | return (NULL); 16 | 17 | behind = NULL; 18 | 19 | while ((*head)->next != NULL) 20 | { 21 | ahead = (*head)->next; 22 | (*head)->next = behind; 23 | behind = *head; 24 | *head = ahead; 25 | } 26 | 27 | (*head)->next = behind; 28 | 29 | return (*head); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | size_t looped_listint_len(const listint_t *head); 5 | size_t print_listint_safe(const listint_t *head); 6 | 7 | /** 8 | * looped_listint_len - Counts the number of unique nodes 9 | * in a looped listint_t linked list. 10 | * @head: A pointer to the head of the listint_t to check. 11 | * 12 | * Return: If the list is not looped - 0. 13 | * Otherwise - the number of unique nodes in the list. 14 | */ 15 | size_t looped_listint_len(const listint_t *head) 16 | { 17 | const listint_t *tortoise, *hare; 18 | size_t nodes = 1; 19 | 20 | if (head == NULL || head->next == NULL) 21 | return (0); 22 | 23 | tortoise = head->next; 24 | hare = (head->next)->next; 25 | 26 | while (hare) 27 | { 28 | if (tortoise == hare) 29 | { 30 | tortoise = head; 31 | while (tortoise != hare) 32 | { 33 | nodes++; 34 | tortoise = tortoise->next; 35 | hare = hare->next; 36 | } 37 | 38 | tortoise = tortoise->next; 39 | while (tortoise != hare) 40 | { 41 | nodes++; 42 | tortoise = tortoise->next; 43 | } 44 | 45 | return (nodes); 46 | } 47 | 48 | tortoise = tortoise->next; 49 | hare = (hare->next)->next; 50 | } 51 | 52 | return (0); 53 | } 54 | 55 | /** 56 | * print_listint_safe - Prints a listint_t list safely. 57 | * @head: A pointer to the head of the listint_t list. 58 | * 59 | * Return: The number of nodes in the list. 60 | */ 61 | size_t print_listint_safe(const listint_t *head) 62 | { 63 | size_t nodes, index = 0; 64 | 65 | nodes = looped_listint_len(head); 66 | 67 | if (nodes == 0) 68 | { 69 | for (; head != NULL; nodes++) 70 | { 71 | printf("[%p] %d\n", (void *)head, head->n); 72 | head = head->next; 73 | } 74 | } 75 | 76 | else 77 | { 78 | for (index = 0; index < nodes; index++) 79 | { 80 | printf("[%p] %d\n", (void *)head, head->n); 81 | head = head->next; 82 | } 83 | 84 | printf("-> [%p] %d\n", (void *)head, head->n); 85 | } 86 | 87 | return (nodes); 88 | } 89 | -------------------------------------------------------------------------------- /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 5 | * of a listint_t list. 6 | * @head: A pointer to the address of the 7 | * head of the listint_t list. 8 | * @n: The integer for the new node to contain. 9 | * 10 | * Return: If the function fails - NULL. 11 | * Otherwise - the address of the new element. 12 | */ 13 | listint_t *add_nodeint(listint_t **head, const int n) 14 | { 15 | listint_t *new; 16 | 17 | new = malloc(sizeof(listint_t)); 18 | if (new == NULL) 19 | return (NULL); 20 | 21 | new->n = n; 22 | new->next = *head; 23 | 24 | *head = new; 25 | 26 | return (new); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - Adds a new node at the 5 | * end of a listint_t list. 6 | * @head: A pointer to the address of the 7 | * head of the listint_t list. 8 | * @n: The integer for the new node to contain. 9 | * 10 | * Return: If the function fails - NULL. 11 | * Otherwise - the address of the new element. 12 | */ 13 | listint_t *add_nodeint_end(listint_t **head, const int n) 14 | { 15 | listint_t *new, *last; 16 | 17 | new = malloc(sizeof(listint_t)); 18 | if (new == NULL) 19 | return (NULL); 20 | 21 | new->n = n; 22 | new->next = NULL; 23 | 24 | if (*head == NULL) 25 | *head = new; 26 | 27 | else 28 | { 29 | last = *head; 30 | while (last->next != NULL) 31 | last = last->next; 32 | last->next = new; 33 | } 34 | 35 | return (*head); 36 | } 37 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - Frees a listint_t list. 5 | * @head: A pointer to the head of the listint_t list to be freed. 6 | */ 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *tmp; 10 | 11 | while (head) 12 | { 13 | tmp = head->next; 14 | free(head); 15 | head = tmp; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - Frees a listint_t list. 5 | * @head: A pointer to the address of the 6 | * head of the listint_t list. 7 | * 8 | * Description: Sets the head to NULL. 9 | */ 10 | void free_listint2(listint_t **head) 11 | { 12 | listint_t *tmp; 13 | 14 | if (head == NULL) 15 | return; 16 | 17 | while (*head) 18 | { 19 | tmp = (*head)->next; 20 | free(*head); 21 | *head = tmp; 22 | } 23 | 24 | head = NULL; 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - Deletes the head node of a listint_t list. 5 | * @head: A pointer to the address of the 6 | * head of the listint_t list. 7 | * 8 | * Return: If the linked list is empty - 0. 9 | * Otherwise - The head node's data (n). 10 | */ 11 | int pop_listint(listint_t **head) 12 | { 13 | listint_t *tmp; 14 | int ret; 15 | 16 | if (*head == NULL) 17 | return (0); 18 | 19 | tmp = *head; 20 | ret = (*head)->n; 21 | *head = (*head)->next; 22 | 23 | free(tmp); 24 | 25 | return (ret); 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - Locates a given node of 5 | * a listint_t linked list. 6 | * @head: A pointer to the head of the listint_t list. 7 | * @index: The index of the node to locate - indices start at 0. 8 | * 9 | * Return: If the node does not exist - NULL. 10 | * Otherwise - the located node. 11 | */ 12 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 13 | { 14 | unsigned int node; 15 | 16 | for (node = 0; node < index; node++) 17 | { 18 | if (head == NULL) 19 | return (NULL); 20 | 21 | head = head->next; 22 | } 23 | 24 | return (head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - Calculates the sum of all the 5 | * data (n) of a listint_t list. 6 | * @head: A pointer to the head of the listint_t list. 7 | * 8 | * Return: If the list is empty - 0. 9 | * Otherwise - the sum of all the data. 10 | */ 11 | int sum_listint(listint_t *head) 12 | { 13 | int sum = 0; 14 | 15 | while (head) 16 | { 17 | sum += head->n; 18 | head = head->next; 19 | } 20 | 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - Inserts a new node to a listint_t 5 | * list at a given position. 6 | * @head: A pointer to the address of the 7 | * head of the listint_t list. 8 | * @idx: The index of the listint_t list where the new 9 | * node should be added - indices start at 0. 10 | * @n: The integer for the new node to contain. 11 | * 12 | * Return: If the function fails - NULL. 13 | * Otherwise - the address of the new node. 14 | */ 15 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 16 | { 17 | listint_t *new, *copy = *head; 18 | unsigned int node; 19 | 20 | new = malloc(sizeof(listint_t)); 21 | if (new == NULL) 22 | return (NULL); 23 | 24 | new->n = n; 25 | 26 | if (idx == 0) 27 | { 28 | new->next = copy; 29 | *head = new; 30 | return (new); 31 | } 32 | 33 | for (node = 0; node < (idx - 1); node++) 34 | { 35 | if (copy == NULL || copy->next == NULL) 36 | return (NULL); 37 | 38 | copy = copy->next; 39 | } 40 | 41 | new->next = copy->next; 42 | copy->next = new; 43 | 44 | return (new); 45 | } 46 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | #include 4 | 5 | /** 6 | * struct listint_s - singly linked list 7 | * @n: integer 8 | * @next: points to the next node 9 | * 10 | * Description: more on singly linked list node structure 11 | * for Alx project 12 | */ 13 | typedef struct listint_s 14 | { 15 | int n; 16 | struct listint_s *next; 17 | } listint_t; 18 | 19 | void free_listint(listint_t *head); 20 | void free_listint2(listint_t **head); 21 | int pop_listint(listint_t **head); 22 | size_t print_listint(const listint_t *h); 23 | size_t listint_len(const listint_t *h); 24 | listint_t *add_nodeint(listint_t **head, const int n); 25 | listint_t *add_nodeint_end(listint_t **head, const int n); 26 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 27 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 28 | listint_t *reverse_listint(listint_t **head); 29 | size_t print_listint_safe(const listint_t *head); 30 | size_t free_listint_safe(listint_t **h); 31 | listint_t *find_listint_loop(listint_t *head); 32 | int sum_listint(listint_t *head); 33 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - This is function that converts binary number 5 | * to an unsigned integer 6 | * @b: A pointer to binary string 7 | * Return: unsigned integer converted number 8 | */ 9 | unsigned int binary_to_uint(const char *b) 10 | { 11 | unsigned int base = 1, result = 0, len = 0; 12 | 13 | if (b == NULL) 14 | return (0); 15 | 16 | while (b[len]) 17 | len++; 18 | 19 | while (len) 20 | { 21 | if (b[len - 1] != '0' && b[len - 1] != '1') 22 | return (0); 23 | 24 | if (b[len - 1] == '1') 25 | result += base; 26 | base *= 2; 27 | len--; 28 | } 29 | return (result); 30 | } 31 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - function which print a binary 5 | * of a number. 6 | * @n: unsigned long int. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_binary(unsigned long int n) 11 | { 12 | if (n >> 0) 13 | { 14 | if (n >> 1) 15 | print_binary(n >> 1); 16 | _putchar((n & 1) + '0'); 17 | } 18 | else 19 | { 20 | _putchar('0'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - function check a machine is little or big endian 5 | * Return: 0 big, 1 little 6 | */ 7 | int get_endianness(void) 8 | { 9 | unsigned int i = 1; 10 | char *c = (char *) &i; 11 | 12 | return (*c); 13 | } 14 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - Function that returns the value of a bit at a given index 5 | * @n: the integer to be operated on 6 | * @index: the index 7 | * Return: returns the value of the bit at index or -1 on error 8 | */ 9 | int get_bit(unsigned long int n, unsigned int index) 10 | { 11 | if (index > 63) 12 | return (-1); 13 | 14 | n >>= index; 15 | if ((n & 1) == 1) 16 | return (1); 17 | else 18 | return (0); 19 | 20 | return (-1); 21 | } 22 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - function that sets the value of a bit to 1 at a given index 5 | * @n: the number 6 | * @index: the index 7 | * Return: returns -1 (Error), 1 (Success or worked) 8 | */ 9 | int set_bit(unsigned long int *n, unsigned int index) 10 | { 11 | int mask; 12 | 13 | if (index > 63) 14 | return (-1); 15 | 16 | mask = 1 << index; 17 | 18 | *n = (*n & ~mask) | (1 << index); 19 | 20 | return (1); 21 | } 22 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit -A function that 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 means worked, - has error -1. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | 20 | if (*n & m) 21 | *n ^= m; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits -Function that counts the number of bits to change 5 | * to get from one number to another 6 | * @n: 1st number 7 | * @m: 2nd number 8 | * Return: returns the number of bits to change 9 | */ 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 11 | { 12 | int i, c = 0; 13 | unsigned long int current; 14 | unsigned long int xor = n ^ m; 15 | 16 | for (i = 63; i >= 0; i--) 17 | { 18 | current = xor >> i; 19 | if (current & 1) 20 | c++; 21 | } 22 | 23 | return (c); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | Bit manipulation in C programming allows you to perform operations at the bit level, enabling you to manipulate individual bits within variables. This can be useful for tasks such as setting, clearing, or toggling specific bits, as well as extracting or combining bit patterns. 2 | 3 | Here are some common bit manipulation operations in C: 4 | 5 | 1. Bitwise AND (`&`): 6 | - Performs a bitwise AND operation on each corresponding pair of bits. 7 | - Example: `result = a & b;` (result will have only the common set bits between `a` and `b`) 8 | 9 | 2. Bitwise OR (`|`): 10 | - Performs a bitwise OR operation on each corresponding pair of bits. 11 | - Example: `result = a | b;` (result will have all the set bits from both `a` and `b`) 12 | 13 | 3. Bitwise XOR (`^`): 14 | - Performs a bitwise XOR (exclusive OR) operation on each corresponding pair of bits. 15 | - Example: `result = a ^ b;` (result will have the set bits that are unique to either `a` or `b`) 16 | 17 | 4. Bitwise NOT (`~`): 18 | - Flips the bits of the operand, resulting in the one's complement. 19 | - Example: `result = ~a;` (result will have the complement of `a`) 20 | 21 | 5. Left Shift (`<<`): 22 | - Shifts the bits of the left operand to the left by a specified number of positions. 23 | - Example: `result = a << n;` (result will have the bits of `a` shifted left by `n` positions) 24 | 25 | 6. Right Shift (`>>`): 26 | - Shifts the bits of the left operand to the right by a specified number of positions. 27 | - Example: `result = a >> n;` (result will have the bits of `a` shifted right by `n` positions) 28 | 29 | 7. Bitwise AND with Assignment (`&=`): 30 | - Performs a bitwise AND operation between the operands and assigns the result to the left operand. 31 | - Example: `a &= b;` (equivalent to `a = a & b;`) 32 | 33 | 8. Bitwise OR with Assignment (`|=`): 34 | - Performs a bitwise OR operation between the operands and assigns the result to the left operand. 35 | - Example: `a |= b;` (equivalent to `a = a | b;`) 36 | 37 | 9. Bitwise XOR with Assignment (`^=`): 38 | - Performs a bitwise XOR operation between the operands and assigns the result to the left operand. 39 | - Example: `a ^= b;` (equivalent to `a = a ^ b;`) 40 | 41 | These operations allow you to manipulate individual bits within variables, which can be useful in various scenarios such as implementing flags, bitmasking, or optimizing memory usage. 42 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | int get_endianness(void); 8 | int get_bit(unsigned long int n, unsigned int index); 9 | int set_bit(unsigned long int *n, unsigned int index); 10 | int _putchar(char c); 11 | unsigned int binary_to_uint(const char *b); 12 | void print_binary(unsigned long int n); 13 | int clear_bit(unsigned long int *n, unsigned int index); 14 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 15 | 16 | 17 | #endif /* MAIN_H */ 18 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * read_textfile - function read text file and prints it to output 4 | * @filename: file to be read 5 | * @letters: letters to read and print 6 | * Return: the number of letters printed 7 | */ 8 | ssize_t read_textfile(const char *filename, size_t letters) 9 | { 10 | int ed; 11 | int i, e; 12 | char *bue; 13 | 14 | if (!filename) 15 | return (0); 16 | ed = open(filename, O_RDONLY); 17 | if (ed < 0) 18 | return (0); 19 | bue = malloc(sizeof(char) * letters); 20 | if (!bue) 21 | return (0); 22 | i = read(ed, bue, letters); 23 | if (i < 0) 24 | { 25 | free(bue); 26 | return (0); 27 | } 28 | bue[i] = '\0'; 29 | close(ed); 30 | e = write(STDOUT_FILENO, bue, i); 31 | if (e < 0) 32 | { 33 | free(bue); 34 | return (0); 35 | } 36 | free(bue); 37 | return (e); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - a function that creates a file 5 | * @filename: name of the file 6 | * @text_content: text to write 7 | * 8 | * Return: 1 on success, -1 on failure 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd, x, y = 0; 13 | 14 | if (!filename) 15 | return (-1); 16 | 17 | fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0600); 18 | if (fd < 0) 19 | return (-1); 20 | 21 | if (text_content) 22 | { 23 | while (text_content[y]) 24 | y++; 25 | x = write(fd, text_content, y); 26 | if (x != y) 27 | return (-1); 28 | } 29 | 30 | close(fd); 31 | 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text at the end of a file 5 | * @filename: filename. 6 | * @text_content: added content. 7 | * 8 | * Return: 1 for success. -1 for fail 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) 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/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - entry point 5 | * description: copies the content of a file to another file 6 | * @argc: number of arguments 7 | * @argv: array of arguments 8 | * Return: 0 on success, 97-100 on failure 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int fd_from, fd_to, read_bytes, write_bytes; 13 | char buf[1024]; 14 | 15 | if (argc != 3) 16 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"), exit(97); 17 | 18 | 19 | fd_from = open(argv[1], O_RDONLY); 20 | if (fd_from == -1) 21 | { 22 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 23 | exit(98); 24 | } 25 | 26 | 27 | fd_to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0664); 28 | if (fd_to == -1) 29 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]), exit(99); 30 | 31 | while ((read_bytes = read(fd_from, buf, 1024)) > 0) 32 | { 33 | write_bytes = write(fd_to, buf, read_bytes); 34 | if (write_bytes == -1) 35 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]), exit(99); 36 | } 37 | 38 | if (read_bytes == -1) 39 | { 40 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 41 | exit(98); 42 | } 43 | 44 | if (close(fd_from) == -1) 45 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd_from), exit(100); 46 | 47 | if (close(fd_to) == -1) 48 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd_to), exit(100); 49 | 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | C provides a set of functions for performing file I/O operations, including reading from and writing to files. The key header file you'll need is ``, which stands for "Standard Input/Output." 2 | 3 | Here are the basic file I/O operations you'll commonly encounter: 4 | 5 | 1. **Opening a File**: Before reading from or writing to a file, you need to open it using the `fopen()` function. It takes two arguments: the file name (including the path) and the mode. 6 | 7 | ```c 8 | FILE* file = fopen("filename.txt", "r"); 9 | ``` 10 | 11 | In this example, we are opening the file "filename.txt" in read mode ("r"). The `fopen()` function returns a `FILE` pointer, which you can use for subsequent file operations. 12 | 13 | The most common modes for `fopen()` are: 14 | - `"r"`: Read mode (file must exist). 15 | - `"w"`: Write mode (create a new file or overwrite an existing file). 16 | - `"a"`: Append mode (write data at the end of an existing file or create a new file). 17 | - `"r+"`: Read and write mode. 18 | - `"w+"`: Read and write mode (create a new file or overwrite an existing file). 19 | - `"a+"`: Read and append mode. 20 | 21 | 2. **Closing a File**: Once you finish working with a file, you should close it using the `fclose()` function. 22 | 23 | ```c 24 | fclose(file); 25 | ``` 26 | 27 | 3. **Reading from a File**: To read data from a file, you can use functions like `fgetc()` and `fgets()`. 28 | 29 | - `fgetc()` reads a single character from the file and returns it as an integer. 30 | 31 | ```c 32 | int ch = fgetc(file); 33 | ``` 34 | 35 | - `fgets()` reads a line of text from the file. 36 | 37 | ```c 38 | char buffer[100]; 39 | fgets(buffer, sizeof(buffer), file); 40 | ``` 41 | 42 | 4. **Writing to a File**: To write data to a file, you can use functions like `fputc()` and `fputs()`. 43 | 44 | - `fputc()` writes a single character to the file. 45 | 46 | ```c 47 | fputc('A', file); 48 | ``` 49 | 50 | - `fputs()` writes a string to the file. 51 | 52 | ```c 53 | fputs("Hello, World!", file); 54 | ``` 55 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | ssize_t read_textfile(const char *filename, size_t letters); 13 | int create_file(const char *filename, char *text_content); 14 | int append_text_to_file(const char *filename, char *text_content); 15 | 16 | #endif /*MAIN_H*/ 17 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * print_dlistint - prints all the elements of a list 6 | * @h: pointer to head of list 7 | * Return: number of nodes 8 | */ 9 | size_t print_dlistint(const dlistint_t *h) 10 | { 11 | size_t count = 0; 12 | 13 | while (h) 14 | { 15 | ++count; 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | } 19 | return (count); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c~: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * print_dlistint - prints all the elements of a dlistint_t list 6 | * @h: pointer to head of list 7 | * Return: number of nodes 8 | */ 9 | size_t print_dlistint(const dlistint_t *h) 10 | { 11 | size_t count = 0; 12 | 13 | while (h) 14 | { 15 | ++count; 16 | printf("%d\n", h->n); 17 | h = h->next; 18 | } 19 | return (count); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * dlistint_len - returns the number of elements in a linked dlistint_t list 6 | * @h: head of the list 7 | * 8 | * Return: number of elements in the list 9 | */ 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | size_t count = 0; 13 | 14 | while (h) 15 | { 16 | ++count; 17 | h = h->next; 18 | } 19 | return (count); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * add_dnodeint - adds a new node at the beginning of a dlistint_t list 6 | * @head: pointer to the head of the list 7 | * @n: value of the new node 8 | * 9 | * Return: address of the new element, or NULL if it failed 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | 15 | if (!head) 16 | return (NULL); 17 | 18 | new = malloc(sizeof(dlistint_t)); 19 | if (!new) 20 | return (NULL); 21 | 22 | new->n = n; 23 | new->prev = NULL; 24 | new->next = *head; 25 | if (*head) 26 | (*head)->prev = new; 27 | *head = new; 28 | 29 | return (new); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * add_dnodeint_end - adds a new node at the end of a dlistint_t list 6 | * @head: pointer to the head of the list 7 | * @n: value of the new node 8 | * 9 | * Return: address of the new element, or NULL if it failed 10 | */ 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new, *tmp; 14 | 15 | if (!head) 16 | return (NULL); 17 | 18 | new = malloc(sizeof(dlistint_t)); 19 | if (!new) 20 | return (NULL); 21 | 22 | new->n = n; 23 | new->next = NULL; 24 | 25 | if (!*head) 26 | { 27 | new->prev = NULL; 28 | *head = new; 29 | return (new); 30 | } 31 | 32 | tmp = *head; 33 | while (tmp->next) 34 | tmp = tmp->next; 35 | 36 | tmp->next = new; 37 | new->prev = tmp; 38 | 39 | return (new); 40 | } 41 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * free_dlistint - frees a dlistint_t list 6 | * @head: pointer to the head of the list 7 | * 8 | * Return: void 9 | */ 10 | void free_dlistint(dlistint_t *head) 11 | { 12 | dlistint_t *tmp; 13 | 14 | while (head) 15 | { 16 | tmp = head; 17 | head = head->next; 18 | free(tmp); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - returns the nth node of a dlistint_t linked list 5 | * @head: pointer to the head of the list 6 | * @index: index of the node to return 7 | * 8 | * Return: address of the nth node, or NULL if the node doesn't exist 9 | */ 10 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; head && i < index; i++) 15 | head = head->next; 16 | 17 | return (head); 18 | } 19 | -------------------------------------------------------------------------------- /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) of a dlistint_t list 5 | * @head: pointer to the head of the list 6 | * 7 | * Return: sum of all the data (n) of a dlistint_t list 8 | */ 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 12 | 13 | while (head) 14 | { 15 | sum += head->n; 16 | head = head->next; 17 | } 18 | 19 | return (sum); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * get_dnodeint_at_index - returns the nth node of a dlistint_t linked list 6 | * @head: pointer to the head of the list 7 | * @index: index of the node to return 8 | * 9 | * Return: address of the nth node, or NULL if the node doesn't exist 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | for (i = 0; head && i < index; i++) 16 | head = head->next; 17 | 18 | return (head); 19 | } 20 | 21 | /** 22 | * insert_dnodeint_at_index - inserts a new node at a given position 23 | * @h: pointer to the head of the list 24 | * @idx: index of the list where the new node should be added 25 | * @n: value of the new node 26 | * 27 | * Return: address of the new node, or NULL if it failed 28 | */ 29 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 30 | { 31 | dlistint_t *new, *tmp; 32 | 33 | if (!h) 34 | return (NULL); 35 | 36 | if (idx == 0) 37 | return (add_dnodeint(h, n)); 38 | 39 | tmp = get_dnodeint_at_index(*h, idx - 1); 40 | if (!tmp) 41 | return (NULL); 42 | 43 | if (!tmp->next) 44 | return (add_dnodeint_end(h, n)); 45 | 46 | new = malloc(sizeof(dlistint_t)); 47 | if (!new) 48 | return (NULL); 49 | 50 | new->n = n; 51 | new->prev = tmp; 52 | new->next = tmp->next; 53 | tmp->next->prev = new; 54 | tmp->next = new; 55 | 56 | return (new); 57 | } 58 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 5 | 6 | /** 7 | * delete_dnodeint_at_index - deletes the node at index of a dlistint_t list 8 | * @head: pointer to the head of the list 9 | * @index: index of the node to delete 10 | * 11 | * Return: 1 if it succeeded, -1 if it failed 12 | */ 13 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 14 | { 15 | dlistint_t *tmp; 16 | 17 | if (!head || !*head) 18 | return (-1); 19 | 20 | tmp = get_dnodeint_at_index(*head, index); 21 | if (!tmp) 22 | return (-1); 23 | 24 | if (tmp->prev) 25 | tmp->prev->next = tmp->next; 26 | else 27 | *head = tmp->next; 28 | 29 | if (tmp->next) 30 | tmp->next->prev = tmp->prev; 31 | 32 | free(tmp); 33 | 34 | return (1); 35 | } 36 | 37 | /** 38 | * get_dnodeint_at_index - returns the nth node of a dlistint_t linked list 39 | * @head: pointer to the head of the list 40 | * @index: index of the node to return 41 | * 42 | * Return: address of the nth node, or NULL if the node doesn't exist 43 | */ 44 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 45 | { 46 | unsigned int i; 47 | 48 | for (i = 0; head && i < index; i++) 49 | head = head->next; 50 | 51 | return (head); 52 | } 53 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | 6 | /** 7 | * struct dlistint_s - doubly linked list 8 | * @n: integer 9 | * @prev: points to the previous node 10 | * @next: points to the next node 11 | * 12 | * Description: doubly linked list node structure 13 | * 14 | */ 15 | typedef struct dlistint_s 16 | { 17 | int n; 18 | struct dlistint_s *prev; 19 | struct dlistint_s *next; 20 | } dlistint_t; 21 | 22 | size_t print_dlistint(const dlistint_t *h); 23 | void free_dlistint(dlistint_t *head); 24 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 25 | int sum_dlistint(dlistint_t *head); 26 | size_t dlistint_len(const dlistint_t *h); 27 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 28 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 29 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 30 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -g -fPIC -Wall -Werror -Wextra -pedantic ./*.c -shared -o liball.so 3 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/kirubel-web/alx-low_level_programming/raw/master/0x18-dynamic_libraries/win.so 3 | export LD_PRELOAD=/tmp/win.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/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 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/win.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int rand() 5 | { 6 | static int ct = -1; 7 | 8 | ct++; 9 | if (ct == 0) 10 | return 8; 11 | if (ct == 1) 12 | return 8; 13 | if (ct == 2) 14 | return 7; 15 | if (ct == 3) 16 | return 9; 17 | if (ct == 4) 18 | return 23; 19 | if (ct == 5) 20 | return 74; 21 | return ct * ct % 30000; 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/win.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirubel-web/alx-low_level_programming/366bb6a0bfc7eeef3c56f91ff42b949c9265d6af/0x18-dynamic_libraries/win.so -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Creates a hash. 5 | * @size: the size, in number of nodes, to make the new hash table. 6 | * 7 | * Return: a pointer to the newly created hash table. 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | unsigned int i = 0; 12 | hash_table_t *ht = malloc(sizeof(hash_table_t)); 13 | 14 | if (ht == NULL) 15 | { 16 | fprintf(stderr, "Error: malloc failed\n"); 17 | return (NULL); 18 | } 19 | 20 | ht->size = size; 21 | ht->array = malloc(sizeof(hash_node_t *) * size); 22 | if (ht->array == NULL) 23 | { 24 | fprintf(stderr, "Error: malloc failed\n"); 25 | return (NULL); 26 | } 27 | 28 | for (; i < size; i++) 29 | ht->array[i] = NULL; 30 | 31 | return (ht); 32 | } 33 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - hash function using the djb2 algorithm 5 | * @str: string to hash 6 | * Return: hash value 7 | */ 8 | unsigned long int hash_djb2(const unsigned char *str) 9 | { 10 | unsigned long int hash = 5381; 11 | int c; 12 | 13 | if (str == NULL) 14 | return (0); 15 | 16 | while ((c = *str++)) 17 | hash = ((hash << 5) + hash) + c; 18 | return (hash); 19 | } 20 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - return the index of a key 5 | * @key: key 6 | * @size: size of the array of the hash table 7 | * Return: index at which the key/value pair should be 8 | */ 9 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 10 | { 11 | unsigned long int hash; 12 | 13 | hash = hash_djb2((const unsigned char *)key); 14 | return (hash % size); 15 | } 16 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_set - adds an element to the hash 5 | * @key: key 6 | * @ht: hash table to add or update the key/value to 7 | * @value: value associated with the key 8 | * Return: 1 success, 0 fail 9 | */ 10 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 11 | { 12 | hash_node_t *new_node = NULL, *temp = NULL; 13 | unsigned long int index = 0; 14 | 15 | if (ht == NULL || key == NULL || value == NULL) 16 | return (0); 17 | index = key_index((const unsigned char *)key, ht->size); 18 | temp = ht->array[index]; 19 | while (temp != NULL) 20 | { 21 | if (strcmp(temp->key, key) == 0) 22 | { 23 | free(temp->value); 24 | temp->value = strdup(value); 25 | if (temp->value == NULL) 26 | return (0); 27 | return (1); 28 | } 29 | temp = temp->next; 30 | } 31 | new_node = malloc(sizeof(hash_node_t)); 32 | if (new_node == NULL) 33 | return (0); 34 | new_node->key = strdup(key); 35 | if (new_node->key == NULL) 36 | { 37 | free(new_node); 38 | return (0); 39 | } 40 | new_node->value = strdup(value); 41 | if (new_node->value == NULL) 42 | { 43 | free(new_node->key); 44 | free(new_node); 45 | return (0); 46 | } 47 | new_node->next = ht->array[index]; 48 | ht->array[index] = new_node; 49 | return (1); 50 | } 51 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - print key/values of hash table in order 5 | * @ht: hash table 6 | */ 7 | void hash_table_print(const hash_table_t *ht) 8 | { 9 | unsigned long int idx = 0; 10 | hash_node_t *node; 11 | char *comma = ""; 12 | 13 | if (!ht || !ht->array) 14 | return; 15 | 16 | putchar('{'); 17 | while (idx < ht->size) 18 | { 19 | node = ((ht->array)[idx]); 20 | while (node) 21 | { 22 | printf("%s'%s': '%s'", comma, node->key, node->value); 23 | comma = ", "; 24 | node = node->next; 25 | } 26 | idx++; 27 | } 28 | puts("}"); 29 | } 30 | -------------------------------------------------------------------------------- /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: hash table 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | hash_node_t *temp = NULL, *next = NULL; 10 | unsigned long int i = 0; 11 | 12 | if (ht == NULL) 13 | return; 14 | for (i = 0; i < ht->size; ++i) 15 | { 16 | temp = ht->array[i]; 17 | while (temp != NULL) 18 | { 19 | next = temp->next; 20 | free(temp->key); 21 | free(temp->value); 22 | free(temp); 23 | temp = next; 24 | } 25 | } 26 | free(ht->array); 27 | free(ht); 28 | } 29 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | 4 | all: $(SRC) 5 | $(CC) $(SRC) -o school 6 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall -Wextra -Werror -pedantic 3 | SRC = main.c school.c 4 | OBJ = $(patsubst %.c, %.o, $(SRC)) 5 | NAME = school 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: oclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(NAME) 7 | 8 | .PHONY: all 9 | 10 | $(NAME): $(OBJ) 11 | $(CC) $(OBJ) -o $(NAME) 12 | 13 | %.o: %.c 14 | $(CC) -c $< -o $@ 15 | -------------------------------------------------------------------------------- /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 | .PHONY: clean oclean fclean re 8 | 9 | all: $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | %.o: %.c m.h 13 | $(CC) -c -o $@ $< 14 | 15 | clean: 16 | $(RM) *~ $(NAME) 17 | 18 | oclean: 19 | $(RM) $(OBJ) 20 | 21 | fclean: clean oclean 22 | 23 | re: oclean all 24 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall -Wextra -Werror -pedantic 3 | SRC = main.c school.c 4 | OBJ = $(SRC:%.c=%.o) 5 | NAME = school 6 | RM = rm -f 7 | 8 | .PHONY: clean oclean fclean re 9 | 10 | all: $(OBJ) 11 | $(CC) $(OBJ) -o $(NAME) 12 | 13 | %.o: %.c m.h 14 | $(CC) $(CFLAGS) -c -o $@ $< 15 | 16 | clean: 17 | $(RM) *~ $(NAME) 18 | 19 | oclean: 20 | $(RM) $(OBJ) 21 | 22 | fclean: clean oclean 23 | 24 | re: oclean all 25 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | """ 4 | This Module contains the function island_perimeter. 5 | """ 6 | 7 | 8 | def island_perimeter(grid): 9 | """ 10 | Computes the length of the perimeter of an island. 11 | """ 12 | perimeter = 0 13 | for y, row in enumerate(grid): 14 | for x, cell in enumerate(row): 15 | if cell == 1: 16 | if y == 0 or grid[y - 1][x] == 0: 17 | perimeter += 1 18 | if y == len(grid) - 1 or grid[y + 1][x] == 0: 19 | perimeter += 1 20 | if x == 0 or grid[y][x - 1] == 0: 21 | perimeter += 1 22 | if x == len(row) - 1 or grid[y][x + 1] == 0: 23 | perimeter += 1 24 | 25 | return perimeter 26 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # ALX 2 | 3 | 0x1C. C - Makefiles 4 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - Searches for a value in an array of integers. 5 | * 6 | * @array: A pointer to the first element of the array to search in. 7 | * @size: The number of elements in array. 8 | * @value: The value to search for. 9 | * 10 | * Return: The first index where value is located. 11 | * If value is not present in array or if array is NULL, 12 | * your function must return -1 13 | */ 14 | int linear_search(int *array, size_t size, int value) 15 | { 16 | size_t index; 17 | 18 | if (array == NULL) 19 | return (-1); 20 | 21 | for (index = 0; index < size; index++) 22 | { 23 | printf("Value checked array[%ld] = [%d]\n", index, array[index]); 24 | if (array[index] == value) 25 | return (index); 26 | } 27 | 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - Searches for a value in a sorted array of integers. 5 | * 6 | * @array: A pointer to the first element of the array to search in. 7 | * @size: The number of elements in array. 8 | * @value: The value to search for. 9 | * 10 | * Return: The index where value is located. 11 | * If value is not present in array or if array is NULL 12 | * your function must return -1 13 | */ 14 | int binary_search(int *array, size_t size, int value) 15 | { 16 | size_t index, left, right; 17 | 18 | if (array == NULL) 19 | return (-1); 20 | 21 | for (left = 0, right = size - 1; right >= left;) 22 | { 23 | printf("Searching in array: "); 24 | 25 | for (index = left; index < right; index++) 26 | printf("%d, ", array[index]); 27 | printf("%d\n", array[index]); 28 | 29 | index = left + (right - left) / 2; 30 | 31 | if (array[index] == value) 32 | return (index); 33 | if (array[index] > value) 34 | right = index - 1; 35 | else 36 | left = index + 1; 37 | } 38 | 39 | return (-1); 40 | } 41 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * jump_search - Searches for a value in a sorted array of integers. 5 | * 6 | * @array: A pointer to the first element of the array to search in. 7 | * @size: The number of elements in array. 8 | * @value: The value to search for. 9 | * 10 | * Return: The first index where value is located. 11 | * If value is not present in array or if array is NULL 12 | * your function must return -1 13 | */ 14 | int jump_search(int *array, size_t size, int value) 15 | { 16 | size_t index, jump, step; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | step = sqrt(size); 22 | for (index = jump = 0; jump < size && array[jump] < value;) 23 | { 24 | printf("Value checked array[%ld] = [%d]\n", jump, array[jump]); 25 | index = jump; 26 | jump += step; 27 | } 28 | 29 | printf("Value found between indexes [%ld] and [%ld]\n", index, jump); 30 | 31 | jump = jump < size - 1 ? jump : size - 1; 32 | for (; index < jump && array[index] < value; index++) 33 | printf("Value checked array[%ld] = [%d]\n", 34 | index, array[index]); 35 | printf("Value checked array[%ld] = [%d]\n", index, array[index]); 36 | 37 | return (array[index] == value ? (int)index : -1); 38 | } 39 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - Searches for a value in a sorted array of integers. 5 | * 6 | * @array: A pointer to the first element of the array to search in. 7 | * @size: The number of elements in array. 8 | * @value: The value to search for. 9 | * 10 | * Return: The first index where value is located. 11 | * If value is not present in array or if array is NULL, 12 | * your function must return -1 13 | */ 14 | int interpolation_search(int *array, size_t size, int value) 15 | { 16 | size_t pos, low, high; 17 | 18 | if (array == NULL) 19 | return (-1); 20 | 21 | for (low = 0, high = size - 1; high >= low;) 22 | { 23 | pos = low + (((double)(high - low) / 24 | (array[high] - array[low])) * (value - array[low])); 25 | 26 | if (pos < size) 27 | printf("Value checked array[%ld] = [%d]\n", 28 | pos, array[pos]); 29 | else 30 | { 31 | printf("Value checked array[%ld] is out of range\n", 32 | pos); 33 | break; 34 | } 35 | 36 | if (array[pos] == value) 37 | return (pos); 38 | if (array[pos] > value) 39 | high = pos - 1; 40 | else 41 | low = pos + 1; 42 | } 43 | 44 | return (-1); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * exponential_search - Searches for a value in a sorted array of integers. 5 | * 6 | * @array: A pointer to the first element of the array to search in. 7 | * @size: The number of elements in array. 8 | * @value: The value to search for. 9 | * 10 | * Return: The first index where value is located. 11 | * If value is not present in array or if array is NULL, 12 | * your function must return -1 13 | */ 14 | int exponential_search(int *array, size_t size, int value) 15 | { 16 | size_t index = 0, right; 17 | 18 | if (array == NULL) 19 | return (-1); 20 | 21 | if (array[0] != value) 22 | { 23 | for (index = 1; index < size && array[index] <= value; 24 | index = index * 2) 25 | printf("Value checked array[%ld] = [%d]\n", 26 | index, array[index]); 27 | } 28 | 29 | right = index < size ? index : size - 1; 30 | printf("Value found between indexes [%ld] and [%ld]\n", 31 | index / 2, right); 32 | 33 | return (_binary_search(array, index / 2, right, value)); 34 | } 35 | 36 | /** 37 | * _binary_search - Searches for a value in a sorted array 38 | * of integers using binary search. 39 | * @array: A pointer to the first element of the array to search. 40 | * @left: The starting index of the [sub]array to search. 41 | * @right: The ending index of the [sub]array to search. 42 | * @value: The value to search for. 43 | * 44 | * Return: If the value is not present or the array is NULL, -1. 45 | * Otherwise, the index where the value is located. 46 | */ 47 | int _binary_search(int *array, size_t left, size_t right, int value) 48 | { 49 | size_t index; 50 | 51 | if (array == NULL) 52 | return (-1); 53 | 54 | while (right >= left) 55 | { 56 | printf("Searching in array: "); 57 | for (index = left; index < right; index++) 58 | printf("%d, ", array[index]); 59 | printf("%d\n", array[index]); 60 | 61 | index = left + (right - left) / 2; 62 | if (array[index] == value) 63 | return (index); 64 | if (array[index] > value) 65 | right = index - 1; 66 | else 67 | left = index + 1; 68 | } 69 | 70 | return (-1); 71 | } 72 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * advanced_binary - Searches for a value in a sorted array of integers. 5 | * 6 | * @array: A pointer to the first element of the array to search in. 7 | * @size: The number of elements in array. 8 | * @value: The value to search for. 9 | * 10 | * Return: The index where value is located. 11 | * If value is not present in array or if array is NULL, 12 | * your function must return -1 13 | */ 14 | int advanced_binary(int *array, size_t size, int value) 15 | { 16 | if (array == NULL) 17 | return (-1); 18 | 19 | return (advanced_binary_recursive(array, 0, size - 1, value)); 20 | } 21 | 22 | /** 23 | * advanced_binary_recursive - Searches recursively for a value in a sorted 24 | * array of integers using binary search. 25 | * @array: A pointer to the first element of the [sub]array to search. 26 | * @left: The starting index of the [sub]array to search. 27 | * @right: The ending index of the [sub]array to search. 28 | * @value: The value to search for. 29 | * 30 | * Return: If the value is not present, -1. 31 | * Otherwise, the index where the value is located. 32 | */ 33 | int advanced_binary_recursive(int *array, size_t left, size_t right, int value) 34 | { 35 | size_t i; 36 | 37 | if (right < left) 38 | return (-1); 39 | 40 | printf("Searching in array: "); 41 | for (i = left; i < right; i++) 42 | printf("%d, ", array[i]); 43 | printf("%d\n", array[i]); 44 | 45 | i = left + (right - left) / 2; 46 | if (array[i] == value && (i == left || array[i - 1] != value)) 47 | return (i); 48 | if (array[i] >= value) 49 | return (advanced_binary_recursive(array, left, i, value)); 50 | return (advanced_binary_recursive(array, i + 1, right, value)); 51 | } 52 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * jump_list - Searches for a value in a sorted list of integers. 5 | * 6 | * @list: A pointer to the head of the list to search in. 7 | * @size: The number of nodes in list. 8 | * @value: The value to search for. 9 | * 10 | * Return: A pointer to the first node where value is located. 11 | * If value is not present in head or if head is NULL, 12 | * your function must return NULL 13 | */ 14 | listint_t *jump_list(listint_t *list, size_t size, int value) 15 | { 16 | size_t step, sizes; 17 | listint_t *node, *jump; 18 | 19 | if (list == NULL || size == 0) 20 | return (NULL); 21 | 22 | step = 0; 23 | sizes = sqrt(size); 24 | 25 | for (node = jump = list; jump->index + 1 < size && jump->n < value;) 26 | { 27 | node = jump; 28 | 29 | for (step += sizes; jump->index < step; jump = jump->next) 30 | { 31 | if (jump->index + 1 == size) 32 | break; 33 | } 34 | 35 | printf("Value checked at index [%ld] = [%d]\n", 36 | jump->index, jump->n); 37 | } 38 | 39 | printf("Value found between indexes [%ld] and [%ld]\n", 40 | node->index, jump->index); 41 | for (; node->index < jump->index && node->n < value; node = node->next) 42 | printf("Value checked at index [%ld] = [%d]\n", 43 | node->index, node->n); 44 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 45 | 46 | return (node->n == value ? node : NULL); 47 | } 48 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - Searches for a value in a sorted skip list of integers. 5 | * 6 | * @list: A pointer to the head of the skip list to search in. 7 | * @value: The value to search for. 8 | * 9 | * Return: A pointer on the first node where value is located. 10 | * If value is not present in list or if head is NULL, 11 | * your function must return NULL 12 | */ 13 | skiplist_t *linear_skip(skiplist_t *list, int value) 14 | { 15 | skiplist_t *node, *jump; 16 | 17 | if (list == NULL) 18 | return (NULL); 19 | 20 | for (node = jump = list; jump->next != NULL && jump->n < value;) 21 | { 22 | node = jump; 23 | if (jump->express != NULL) 24 | { 25 | jump = jump->express; 26 | printf("Value checked at index [%ld] = [%d]\n", 27 | jump->index, jump->n); 28 | } 29 | else 30 | { 31 | while (jump->next != NULL) 32 | jump = jump->next; 33 | } 34 | } 35 | 36 | printf("Value found between indexes [%ld] and [%ld]\n", 37 | node->index, jump->index); 38 | 39 | for (; node->index < jump->index && node->n < value; node = node->next) 40 | printf("Value checked at index [%ld] = [%d]\n", 41 | node->index, node->n); 42 | printf("Value checked at index [%ld] = [%d]\n", node->index, node->n); 43 | 44 | return (node->n == value ? node : 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 | # alx 2 | 3 | #### Search Algorithms 4 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS_H 2 | #define SEARCH_ALGOS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct listint_s - singly linked list 10 | * 11 | * @n: Integer 12 | * @index: Index of the node in the list 13 | * @next: Pointer to the next node 14 | * 15 | * Description: singly linked list node structure 16 | */ 17 | typedef struct listint_s 18 | { 19 | int n; 20 | size_t index; 21 | struct listint_s *next; 22 | } listint_t; 23 | 24 | listint_t *jump_list(listint_t *list, size_t size, int value); 25 | 26 | /** 27 | * struct skiplist_s - Singly linked list with an express lane 28 | * 29 | * @n: Integer 30 | * @index: Index of the node in the list 31 | * @next: Pointer to the next node 32 | * @express: Pointer to the next node in the express lane 33 | * 34 | * Description: singly linked list node structure with an express lane 35 | */ 36 | typedef struct skiplist_s 37 | { 38 | int n; 39 | size_t index; 40 | struct skiplist_s *next; 41 | struct skiplist_s *express; 42 | } skiplist_t; 43 | 44 | skiplist_t *linear_skip(skiplist_t *list, int value); 45 | int exponential_search(int *array, size_t size, int value); 46 | int _binary_search(int *array, size_t left, size_t right, int value); 47 | int advanced_binary(int *array, size_t size, int value); 48 | int advanced_binary_recursive(int *array, size_t left, 49 | size_t right, int value); 50 | int linear_search(int *array, size_t size, int value); 51 | int binary_search(int *array, size_t size, int value); 52 | int jump_search(int *array, size_t size, int value); 53 | int interpolation_search(int *array, size_t size, int value); 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repository contains programs written for the low-level programming and 2 | algorithm track at the alx-SoftwareEngineering program facilitated by ALX. I am learning about data 3 | structures, algorithms, and other low-level programming concepts while 4 | working in the C language. --------------------------------------------------------------------------------