├── 0x00-hello_world ├── .1-compiler.swp ├── .101-quote.c.swp ├── 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 ├── 1 ├── 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 ├── main.h └── stdio.h ├── 0x03-debugging ├── 0-main.c ├── 0-main.h ├── 1-main ├── 1-main.c ├── 2-largest_number.c ├── 3-convert_day.c ├── 3-main_b.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-main.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 ├── _putchar.c └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md ├── _putchar.c └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md ├── _putchar.c └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md ├── _putchar.c └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x0F-function_pointers ├── 0-print_name.c ├── 1-array_iterator.c ├── 100-main_opcodes.c ├── 2-int_index.c ├── 3-calc.h ├── 3-get_op_func.c ├── 3-main.c ├── 3-op_functions.c ├── README.md └── function_pointers.h ├── 0x10-variadic_functions ├── 0-sum_them_all.c ├── 1-print_numbers.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 103-find_loop.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md ├── crackme3 └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h └── 0x18-dynamic_libraries ├── 1-create_dynamic_lib.sh ├── 100-operations.so ├── 101-make_me_win.sh ├── README.md ├── libdynamic.so └── main.h /0x00-hello_world/.1-compiler.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x00-hello_world/.1-compiler.swp -------------------------------------------------------------------------------- /0x00-hello_world/.101-quote.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x00-hello_world/.101-quote.c.swp -------------------------------------------------------------------------------- /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 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - prints to string 4 | * * 5 | * * Description: Prints "and that piece of art is useful..." without puts 6 | * * 7 | * * Return: 1 8 | */ 9 | int main(void) 10 | { 11 | char *s = "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n"; 12 | long l = 59; 13 | long fd = 1; 14 | long syscall = 1; 15 | long ret = 0; 16 | 17 | __asm__ ("syscall" 18 | : "=a" (ret) 19 | : "a" (syscall), 20 | "D" (fd), 21 | "S" (s), 22 | "d" (l)); 23 | return (1); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -o cisfun $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * *main - main block 4 | * * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | puts("\"Programming is like building a multilingual puzzle"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - main block 4 | * * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | printf("with proper grammar, but the outcome is a piece of art,\n"); 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - main block 4 | * * Return: 0 5 | */ 6 | int main(void) 7 | { 8 | printf("Size of a char: %i byte(s)\n", sizeof(char)); 9 | printf("Size of an int: %i byte(s)\n", sizeof(int)); 10 | printf("Size of a long int: %i byte(s)\n", sizeof(long int)); 11 | printf("Size of a long long int: %i byte(s)\n", sizeof(long long int)); 12 | printf("Size of a float: %i byte(s)\n", sizeof(float)); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | Hello World Readme file 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * * main - main block 6 | * * Description: Get a random number and print the number 7 | * * and if it is positive, negative, or zero 8 | * * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | if (n > 0) 17 | printf("%i is positive\n", n); 18 | else if (n < 0) 19 | printf("%i is negative\n", n); 20 | else 21 | printf("%i is zero\n", n); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * * main block 6 | * * Desc: check its last digit, compare it with 5 7 | * * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | int last; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | last = n % 10; 17 | if (last > 5) 18 | printf("Last digit of %i is %i and is greater than 5\n", n, last); 19 | else if (last == 0) 20 | printf("Last digit of %i is %i and is 0\n", n, last); 21 | else if (last < 6) 22 | printf("Last digit of %i is %i and is less than 6 and not 0\n", n, last); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * * main - This is the main block 6 | * * Description: Get a number and check its last digit, compare it with 5 7 | * * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | int last; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | last = n % 10; 17 | 18 | if (last > 5) 19 | printf("Last digit of %i is %i and is greater than 5\n", n, last); 20 | else if (last == 0) 21 | printf("Last digit of %i is %i and is 0\n", n, last); 22 | else if (last < 6) 23 | printf("Last digit of %i is %i and is less than 6 and not 0\n", n, last); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * * main - this is the main block 5 | * * Description: print all single digit, base 100 numbers 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int c; 11 | int d = 0; 12 | 13 | while (d < 10) 14 | { 15 | c = 0; 16 | while (c < 10) 17 | { 18 | if (d != c && d < c) 19 | { 20 | putchar('0' + d); 21 | putchar('0' + c); 22 | 23 | if (c + d != 17) 24 | { 25 | putchar(','); 26 | putchar(' '); 27 | } 28 | } 29 | 30 | c++; 31 | } 32 | d++; 33 | } 34 | putchar('\n'); 35 | return (0); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * * main - This is the main block 5 | * * Description: print all possible combinations 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int c; 11 | int d; 12 | int e = 0; 13 | 14 | while (e < 10) 15 | { 16 | d = 0; 17 | while (d < 10) 18 | { 19 | c = 0; 20 | while (c < 10) 21 | { 22 | if (c != d && d != e && e < d && d < c) 23 | { 24 | putchar('0' + e); 25 | putchar('0' + d); 26 | putchar('0' + c); 27 | 28 | if (c + d + e != 9 + 8 + 7) 29 | { 30 | putchar(','); 31 | putchar(' '); 32 | } 33 | } 34 | c++; 35 | } 36 | d++; 37 | } 38 | e++; 39 | } 40 | putchar('\n'); 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - This is the main block 4 | * * Description: create 2 pairs of non-repeating numbers 5 | * * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int j, i; 10 | 11 | for (j = 0; j <= 99; j++) 12 | { 13 | for (i = j; i <= 99; i++) 14 | { 15 | if (i != j) 16 | { 17 | 18 | putchar(j / 10 + 48); 19 | putchar(j % 10 + 48); 20 | putchar(' '); 21 | putchar(i / 10 + 48); 22 | putchar(i % 10 + 48); 23 | 24 | if (j * 100 + i != 9899) 25 | { 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | } 30 | 31 | } 32 | 33 | } 34 | putchar('\n'); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * * main - This is the main block 5 | * * Description: Get a number and check its last digit, compare it with 5 6 | * * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | putchar(c); 15 | c++; 16 | } 17 | 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - This is the main block 4 | * * Description: prints the alphabet in lowercase, 5 | * * then in uppercase. 6 | * * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c = 'a'; 11 | 12 | while (c <= 'z') 13 | { 14 | putchar(c); 15 | c++; 16 | } 17 | 18 | c = 'A'; 19 | 20 | while (c <= 'Z') 21 | { 22 | putchar(c); 23 | c++; 24 | } 25 | 26 | putchar('\n'); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - This is the main block 4 | * * Description: Get a number and check its last digit, compare it with 5 5 | * * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c = 'a'; 10 | 11 | while (c <= 'z') 12 | { 13 | if (c != 'e' && c != 'q') 14 | { 15 | putchar(c); 16 | } 17 | c++; 18 | } 19 | 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * * main - This is the main block 6 | * * Description: print all base 10 single digit numbers. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | char c = '0'; 12 | 13 | while (c <= '9') 14 | { 15 | putchar(c); 16 | c++; 17 | } 18 | 19 | putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * * main - This is the main block 4 | * * Description: print all base 10 single digit numbers. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int c = 0; 10 | 11 | while (c < 10) 12 | { 13 | putchar(48 + c); 14 | c++; 15 | } 16 | 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * * main - This is the main block 6 | * * Description: print all base 10 single digit numbers. 7 | * * Followed by a new line 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | char c = 'z'; 13 | 14 | while (c >= 'a') 15 | { 16 | putchar(c); 17 | c--; 18 | } 19 | 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * * main - This is the main block 5 | * * Description: print all base 10 single digit numbers. 6 | * Return: 0 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 | 19 | for (c = 'a'; c <= 'f'; c++) 20 | { 21 | putchar(c); 22 | } 23 | 24 | putchar('\n'); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * * main - This is the main block 5 | * * Description: prints all base 10 single digit numbers 10 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int c = 0; 11 | 12 | while (c < 10) 13 | { 14 | putchar(48 + c); 15 | if (c != 9) 16 | { 17 | putchar(','); 18 | putchar(' '); 19 | } 20 | c++; 21 | } 22 | putchar('\n'); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | Variables 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Main Func 5 | * Description: Prints the word _putchar, followed by a new line. 6 | * Return: 0. 7 | */ 8 | int main(void) 9 | { 10 | char word[8] = "_putchar"; 11 | int i; 12 | 13 | for (i = 0; i < 8; i++) 14 | { 15 | _putchar(word[i]); 16 | } 17 | _putchar('\n'); 18 | 19 | return (0); 20 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | /** 3 | * print_alphabet - Printing the alphabet in lowercase 4 | * 5 | * Return 0 6 | */ 7 | void print_alphabet(void) 8 | { 9 | char c; 10 | 11 | for (c = 'a'; c <= 'z'; c++) 12 | { 13 | _putchar(c); 14 | } 15 | 16 | _putchar('\n'); 17 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | /** 3 | * maina - Main Desc 4 | * Description - returns the sum of its parameters 5 | * @a: int type number 6 | * @b: int type number 7 | * Return:0 8 | */ 9 | 10 | 11 | int add(int a, int b) 12 | { 13 | return (a + b); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | /** 3 | * main - main func 4 | * Description - prints times table 5 | * @n : times table to use 6 | * Return:void 7 | */ 8 | 9 | void print_times_table(int n) 10 | { 11 | 12 | int a = 0, rep, b; 13 | 14 | if (n < 0 || n > 15) 15 | return; 16 | 17 | while (a <= n) 18 | { 19 | for (b = 0; b <= n; b++) 20 | { 21 | rep = a * b; 22 | if (b == 0) 23 | _putchar('0' + rep); 24 | else if (rep < 10) 25 | { 26 | _putchar(' '); 27 | _putchar(' '); 28 | _putchar('0' + rep); 29 | } 30 | else if (rep < 100) 31 | { 32 | _putchar(' '); 33 | _putchar('0' + rep / 10); 34 | _putchar('0' + rep % 10); 35 | } 36 | else 37 | { 38 | _putchar('0' + rep / 100); 39 | _putchar('0' + (rep - 100) / 10); 40 | _putchar('0' + rep % 10); 41 | } 42 | if (b < n) 43 | { 44 | _putchar(','); 45 | _putchar(' '); 46 | } 47 | } 48 | _putchar('\n'); 49 | a++; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - main block func 5 | * Description: computes and prints the sum of all the multiples of 3 or 6 | * 5 below 1024 (excluded), followed by a new line 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int c = 0; 12 | int sum = 0; 13 | 14 | while (c < 1024) 15 | { 16 | if (c % 3 == 0 || c % 5 == 0) 17 | { 18 | sum += c; 19 | } 20 | 21 | c++; 22 | } 23 | printf("%i\n", sum); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main block 4 | * Description- prints first 50 numbers of the fibonacci sequence 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int i, n; 10 | long f1, f2, next; 11 | 12 | f1 = 1; 13 | f2 = 2; 14 | n = 50; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | if (i == 49) 19 | { 20 | printf("%lu", f1); 21 | } else 22 | { 23 | printf("%lu, ", f1); 24 | next = f1 + f2; 25 | f1 = f2; 26 | f2 = next; 27 | } 28 | } 29 | printf("\n"); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block function 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 | * Main - main block 6 | *Description - returns the lenth of string 7 | *@num : operand number 8 | *Return: number of digits 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 | 26 | return (length); 27 | } 28 | 29 | /** 30 | *main - prints the first 98 fibonaci sequences 31 | *Description - Return: 0 32 | */ 33 | int main(void) 34 | { 35 | 36 | unsigned long f1 = 1, f2 = 2, tmp, mx = 100000000, f1o = 0, f2o = 0, tmpo = 0; 37 | short int i = 1, initial0s; 38 | 39 | while (i <= 98) 40 | { 41 | 42 | if (f1o > 0) 43 | printf("%lu", f1o); 44 | initial0s = numLength(mx) - 1 - numLength(f1); 45 | while (f1o > 0 && initial0s > 0) 46 | { 47 | printf("%i", 0); 48 | initial0s--; 49 | } 50 | printf("%lu", f1); 51 | 52 | tmp = (f1 + f2) % mx; 53 | tmpo = f1o + f2o + (f1 + f2) / mx; 54 | f1 = f2; 55 | f1o = f2o; 56 | f2 = tmp; 57 | f2o = tmpo; 58 | 59 | if (i != 98) 60 | printf(", "); 61 | else 62 | printf("\n"); 63 | i++; 64 | } 65 | return (0); 66 | } 67 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | #include 3 | 4 | /** 5 | * maina - main desc 6 | * description- prints all the natural numbers from n to 98 7 | * 8 | * @n: interger to be tested 9 | * Return: list of numbers 10 | */ 11 | void print_to_98(int n) 12 | { 13 | int i; 14 | 15 | if (n <= 98) 16 | { 17 | for (i = n; i < 98; i++) 18 | { 19 | printf("%i, ", i); 20 | } 21 | } 22 | else 23 | { 24 | for (i = n; i > 98; i--) 25 | { 26 | printf("%i, ", i); 27 | } 28 | } 29 | printf("98\n"); 30 | } 31 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | 3 | /** 4 | * print_alphabet_x10 - prints alphabets 10 times 5 | * Return:void 6 | */ 7 | 8 | void print_alphabet_x10(void) 9 | { 10 | char c; 11 | int i = 0; 12 | 13 | while (i < 10) 14 | { 15 | c = 'a'; 16 | while (c <= 'z') 17 | { 18 | _putchar(c); 19 | c++; 20 | } 21 | _putchar('\n'); 22 | i++; 23 | } 24 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | 3 | /** 4 | * _islower - check for lower case letter 5 | * @c : character to check the case 6 | * Return:0 or 1 7 | */ 8 | 9 | int _islower(int c) 10 | { 11 | return (c >= 97 && c <= 122); 12 | 13 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | 3 | /** 4 | * Main - This is the main function 5 | * Description:- check for lower case letter 6 | * @c : character to check 7 | * Return:0 or 1 8 | */ 9 | 10 | int _isalpha(int c) 11 | { 12 | return ((c >= 97 && c <= 122) || (c >= 65 && c <= 90)); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | /** 3 | * Main - This is the main function 4 | * Description: - prints the sign of a number 5 | * @n: interger to be tested 6 | * Return: 1, 0, -1 7 | */ 8 | int print_sign(int n) 9 | { 10 | if (n > 0) 11 | { 12 | _putchar('+'); 13 | return (1); 14 | } 15 | else if (n == 0) 16 | { 17 | _putchar('0'); 18 | return (0); 19 | } 20 | else 21 | { 22 | _putchar('-'); 23 | return (-1); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | /** 3 | * Main - Main func 4 | * Description - computes the absolute value for an interger 5 | * @n: interger to be tested 6 | * Return: 0 or 1 7 | */ 8 | 9 | int _abs(int n) 10 | 11 | { 12 | return (n < 0 ? -1 * n : n); 13 | } 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | /** 3 | * Main - Maina Func 4 | * Description - return last digit 5 | * @n : number to check 6 | * Return:0 or 1 7 | */ 8 | 9 | int print_last_digit(int n) 10 | { 11 | int nv; 12 | 13 | if (n < 0) 14 | nv = -1 * (n % 10); 15 | else 16 | nv = n % 10; 17 | 18 | _putchar(nv + '0'); 19 | return (nv); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | /** 3 | * Main - Maina Func 4 | * 5 | * Description - prints every minute of the day of Jack Bauer 6 | */ 7 | void jack_bauer(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 24; i++) 12 | { 13 | for (j = 0; j < 60; j++) 14 | { 15 | _putchar(i / 10 + 48); 16 | _putchar(i % 10 + 48); 17 | _putchar(':'); 18 | _putchar(j / 10 + 48); 19 | _putchar(j % 10 + 48); 20 | _putchar('\n'); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | /** 3 | * Main - Main Description 4 | * Description - prints the times table 5 | */ 6 | void times_table(void) 7 | { 8 | int i, j, product; 9 | 10 | for (i = 0; i < 10; i++) 11 | { 12 | for (j = 0; j < 10; j++) 13 | { 14 | if (j == 0) 15 | { 16 | _putchar(48); 17 | continue; 18 | } 19 | product = i * j; 20 | _putchar(','); 21 | _putchar(' '); 22 | if (product >= 10) 23 | { 24 | _putchar(product / 10 + 48); 25 | _putchar(product % 10 + 48); 26 | } 27 | else 28 | { 29 | _putchar(' '); 30 | _putchar(product + 48); 31 | } 32 | } 33 | _putchar('\n'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | Functions and nested loops docs 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int n); 8 | int print_last_digit(int n); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int a, int b); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/stdio.h: -------------------------------------------------------------------------------- 1 | #define stdio_h 2 | 3 | int _putchar(char); 4 | 5 | void print_alphabet(void); 6 | 7 | void print_alphabet_x10(void); 8 | 9 | int _islower(int c); 10 | 11 | int _isalpha(int n); 12 | 13 | int print_sign(int n); 14 | 15 | int _abs(int n); 16 | 17 | int print_last_digit(int n); 18 | 19 | void jack_bauer(void); 20 | 21 | void times_table(void); 22 | 23 | int add(int a, int b); 24 | 25 | void print_to_98(int n); 26 | 27 | void print_times_table(int n); 28 | 29 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - test function if an integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | 14 | positive_or_negative(i); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | 5 | #include 6 | 7 | void positive_or_negative(int i); 8 | int largest_number(int a, int b, int c); 9 | void print_remaining_days(int month, int day, int year); 10 | int convert_day(int month, int day); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /0x03-debugging/1-main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x03-debugging/1-main -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - creates 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 ( < 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: the first integer 6 | * @b: the second integer 7 | * @c: the third integer 8 | * Return: the largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a >= b && a >= c) 16 | { 17 | largest = a; 18 | } 19 | else if (b >= a && b >= c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | return (largest); 28 | } 29 | -------------------------------------------------------------------------------- /0x03-debugging/3-convert_day.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * convert_day - converts day of month to day of year, without accounting 5 | * for leap year 6 | * @month: month in number format 7 | * @day: day of month 8 | * Return: day of year 9 | */ 10 | 11 | int convert_day(int month, int day) 12 | { 13 | switch (month) 14 | { 15 | case 2: 16 | day = 31 + day; 17 | break; 18 | case 3: 19 | day = 59 + day; 20 | break; 21 | case 4: 22 | day = 90 + day; 23 | break; 24 | case 5: 25 | day = 120 + day; 26 | break; 27 | case 6: 28 | day = 151 + day; 29 | break; 30 | case 7: 31 | day = 181 + day; 32 | break; 33 | case 8: 34 | day = 212 + day; 35 | break; 36 | case 9: 37 | day = 243 + day; 38 | break; 39 | case 10: 40 | day = 273 + day; 41 | break; 42 | case 11: 43 | day = 304 + day; 44 | break; 45 | case 12: 46 | day = 334 + day; 47 | break; 48 | default: 49 | break; 50 | } 51 | return (day); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /0x03-debugging/3-main_b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - takes a date and prints how many days are left in the year, taking 6 | * leap years into account 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int month; 13 | int day; 14 | int year; 15 | 16 | month = 2; 17 | day = 29; 18 | year = 2000; 19 | 20 | printf("Date: %02d/%02d/%04d\n", month, day, year); 21 | 22 | day = convert_day(month, day); 23 | 24 | print_remaining_days(month, day, year); 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes date and prints remaining days in the year 6 | * taking leap year into the accoun 7 | * @month:month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if ((year % 4 == 0) && (year % 400 == 0 || year != 0)) 16 | { 17 | if (month >= 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | printf("Day of the year: %d\n", day); 22 | rintf("Remaining days: %d\n", 366 - day); 23 | } 24 | else 25 | { 26 | if (month == 2 && day == 60) 27 | { 28 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 29 | } 30 | else 31 | { 32 | printf("Day of the year: %d\n", day); 33 | printf("Remaining days: %d\n", 365 - day); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | Debug Readme file 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int); 7 | int largest_number(int a, int b, int c); 8 | void print_remaining_days(int month, int day, int year); 9 | int convert_day(int month, int day); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - A func that checks for uppercase character. 5 | * @c: An input character 6 | * Return: 1 if c is uppercase or 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - A function that checks for a digit in the range (0 and 9). 5 | * @c: An input integer 6 | * Return: 1 if c is a digit or 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= '0') 11 | { 12 | if (c <= '9') 13 | return (1); 14 | } 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_triangle(2); 11 | print_triangle(10); 12 | print_triangle(1); 13 | print_triangle(0); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - Function prints an upright triangle of a given size. 5 | * @size: The triangle's size 6 | * Return - Returns nothing 7 | */ 8 | void print_triangle(int size) 9 | { 10 | int j, k; 11 | 12 | if (size <= 0) 13 | { 14 | _putchar('\n'); 15 | return; 16 | } 17 | 18 | for (j = 1; j <= size; ++j) 19 | { 20 | for (k = 0; k < size - j; ++k) 21 | _putchar(' '); 22 | for (k = 0; k < j; ++k) 23 | _putchar('#'); 24 | _putchar('\n'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * largest_prime_factor - Function finds the largest prime factor 6 | * of a given long number 7 | * @n: the long number 8 | * 9 | * Return: returns the largest prime factor of the number n 10 | */ 11 | long largest_prime_factor(long n) 12 | { 13 | long p_factor; 14 | 15 | while (n % 2 == 0) 16 | { 17 | p_factor = 2; 18 | n /= 2; 19 | } 20 | 21 | for (p_factor = 3; p_factor <= sqrt(n); p_factor += 2) 22 | { 23 | while (n % p_factor == 0) 24 | { 25 | n /= p_factor; 26 | } 27 | } 28 | 29 | if (n > 2) 30 | return (n); 31 | else 32 | return (p_factor); 33 | } 34 | 35 | /** 36 | * main - Entry function to check the code 37 | * 38 | * Return: Returns always 0. 39 | */ 40 | 41 | int main(void) 42 | { 43 | long n = 612852475143; 44 | 45 | long largest_factor = largest_prime_factor(n); 46 | 47 | printf("%ld\n", largest_factor); 48 | 49 | return (0); 50 | } 51 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - Function prints an integer provided as argument 5 | * @n: The integer to be printed 6 | * Return: Returns nothing 7 | */ 8 | void print_number(int n) 9 | { 10 | if (n < 0) 11 | { 12 | _putchar('-'); 13 | n *= -1; 14 | } 15 | 16 | if (n / 10 != 0) 17 | print_number(n / 10); 18 | 19 | _putchar(n % 10 + '0'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - a multiplies two integers. 5 | * @a: An input integer 6 | * @b: An input integer 7 | * Return: Always 0 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints the numbers, from 0 to 9, 5 | * followed by a new line. 6 | * Return: Always 0 7 | */ 8 | void print_numbers(void) 9 | { 10 | int i = '0'; 11 | 12 | for (; i <= '9'; i++) 13 | _putchar(i); 14 | _putchar('\n'); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints the numbers, from 0 to 9. 5 | * Do not print 2 and 4. 6 | * followed by a new line. 7 | * Return: Always 0 8 | */ 9 | void print_most_numbers(void) 10 | { 11 | int i = '0'; 12 | 13 | for (; i <= '9'; i++) 14 | { 15 | if (i != '2' && i != '4') 16 | _putchar(i); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - Function prints a numbers 10 times from 0 to 14 5 | * followed by a new line 6 | * Return: Returns nothing 7 | */ 8 | void more_numbers(void) 9 | { 10 | int j, k; 11 | 12 | for (k = 0; k < 10; k++) 13 | { 14 | for (j = 0; j <= 14; j++) 15 | { 16 | if (j >= 10) 17 | _putchar(j / 10 + '0'); 18 | _putchar(j % 10 + '0'); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - Function prints a line of underscore characters 5 | * @n: Number of times the character '_' should be printed 6 | */ 7 | void print_line(int n) 8 | { 9 | int j; 10 | 11 | if (n <= 0) 12 | { 13 | _putchar('\n'); 14 | return; 15 | } 16 | 17 | for (j = 0; j < n; j++) 18 | _putchar('_'); 19 | 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - Function prints a diagonal line on the terminal 5 | * @n: The number of times the char '\' should be printed 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | int j, k; 10 | 11 | if (n <= 0) 12 | { 13 | _putchar('\n'); 14 | return; 15 | } 16 | 17 | for (j = 0; j < n; j++) 18 | { 19 | for (k = 0; k < j; k++) 20 | _putchar(' '); 21 | _putchar('\\'); 22 | _putchar('\n'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - Function prints a square made of '#' chars 5 | * @size: The square size 6 | * Return - Returns nothing 7 | */ 8 | void print_square(int size) 9 | { 10 | int j, k; 11 | 12 | if (size <= 0) 13 | { 14 | _putchar('\n'); 15 | return; 16 | } 17 | 18 | for (j = 0; j < size; j++) 19 | { 20 | for (k = 0; k < size; k++) 21 | _putchar('#'); 22 | _putchar('\n'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Function's entry point 5 | * 6 | * Return: Returns always 0 7 | */ 8 | int main(void) 9 | { 10 | int j; 11 | 12 | for (j = 1; j <= 100; j++) 13 | { 14 | if (j % 3 == 0 && j % 5 == 0) 15 | printf("FizzBuzz"); 16 | else if (j % 3 == 0) 17 | printf("Fizz"); 18 | else if (j % 5 == 0) 19 | printf("Buzz"); 20 | else 21 | printf("%d", j); 22 | 23 | if (j < 100) 24 | printf(" "); 25 | else 26 | printf("\n"); 27 | } 28 | 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # More Functions and nested loops 2 | 3 | - Functions and nested loops directory. 4 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _isupper(int c); 6 | int _isdigit(int c); 7 | int mul(int a, int b); 8 | void print_numbers(void); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif /* MAIN_H */ 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - Reset number to 98 with pointer 5 | * @n: - A pointer to the integer value 6 | * Return: return void 7 | */ 8 | 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | } 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * swap_int - Function swaps integer values of 2 ints 4 | * @a: first integer pointer 5 | * @b: second integer pointer 6 | * return: void 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | int old_a = *a; 12 | *a = *b; 13 | *b = old_a; 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _atoi - should convert a string to an integer type. 4 | * @s: the provided string for conversion. 5 | * Return: Returns integer value 6 | * of the converted string. 7 | */ 8 | 9 | int _atoi(char *s) 10 | { 11 | 12 | int state = 1; 13 | unsigned int digit = 0; 14 | 15 | do { 16 | 17 | if (*s == '-') 18 | state *= -1; 19 | 20 | else if (*s >= '0' && *s <= '9') 21 | digit = (digit * 10) + (*s - '0'); 22 | 23 | else if (digit > 0) 24 | break; 25 | 26 | } while (*s++); 27 | 28 | return (digit * state); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - functions generates passwords 7 | * randomly for 101-crackme program 8 | * Return: function returns zero 9 | */ 10 | int main(void) 11 | { 12 | int summation; 13 | char k; 14 | 15 | srand(time(NULL)); 16 | while (summation <= 2645) 17 | { 18 | k = rand() % 128; 19 | summation += k; 20 | putchar(k); 21 | } 22 | putchar(2772 - summation); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen - Function obtains the length 4 | * of a given string 5 | * @s: string pointer 6 | * Return: return the length 7 | */ 8 | 9 | int _strlen(char *s) 10 | { 11 | int character = 0; 12 | 13 | while (s[character]) 14 | character++; 15 | return (character); 16 | } 17 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts - Function prints a string followed 4 | * by a new line 5 | * @str: String pointer of the input 6 | * Return: return void 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int j = 0; 12 | 13 | while (str[j]) 14 | { 15 | _putchar(str[j]); 16 | j++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_rev - Prints the given string in reverse 4 | * followed by a new line 5 | * @s:string pointer 6 | * Return: return void 7 | */ 8 | 9 | void print_rev(char *s) 10 | { 11 | int k = 0; 12 | 13 | while (s[k]) 14 | { 15 | k++; 16 | } 17 | while (k--) 18 | { 19 | _putchar(s[k]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rev_string - Reverses a string 4 | * @s: String pointer 5 | * Return: return void 6 | */ 7 | 8 | void rev_string(char *s) 9 | { 10 | int length, i, semi; 11 | char temp; 12 | 13 | for (length = 0; s[length] != '\0'; length++) 14 | ; 15 | 16 | i = 0; 17 | 18 | semi = length / 2; 19 | 20 | while (semi--) 21 | { 22 | temp = s[length - i - 1]; 23 | s[length - i - 1] = s[i]; 24 | s[i] = temp; 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * puts2 - Prints every other line 4 | * of a string 5 | * @str: Points to string 6 | * Return: return void 7 | */ 8 | 9 | void puts2(char *str) 10 | { 11 | int j = 0; 12 | 13 | while (str[j] != '\0') 14 | { 15 | if (j % 2 == 0) 16 | { 17 | _putchar(str[j]); 18 | } 19 | j++; 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * puts_half - Function prints second half of 4 | * every string 5 | * @str: An array string 6 | * Description: Function prints (length - 1) / 2 7 | * if the chars of the string are odd numbers 8 | */ 9 | 10 | void puts_half(char *str) 11 | { 12 | int h; 13 | 14 | for (h = 0; str[h] != '\0'; h++) 15 | h++; 16 | 17 | for (h /= 2; str[h] != '\0'; h++) 18 | { 19 | _putchar(str[h]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - for an array of integers, 6 | * print `n` elements 7 | * @a: an array pointer of type int 8 | * @n: an int integer 9 | * Description: Numbers have to be space 10 | * or comma separated 11 | * Numbers should be displayed in the same order they are stored in array. 12 | * Only _putchar can be used for printing. 13 | */ 14 | 15 | void print_array(int *a, int n) 16 | { 17 | int character; 18 | 19 | character = 0; 20 | for (n--; n >= 0; n--, character++) 21 | { 22 | printf("%d", a[character]); 23 | if (n > 0) 24 | { 25 | printf(", "); 26 | } 27 | } 28 | printf("\n"); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strcpy - function copies the string pointed to by src 4 | * including the terminating null byte (\0) 5 | * @src: char type string 6 | * @dest: char type string 7 | * Description: Copy the string pointed to 8 | * by `src` pointer to 9 | * buffer pointed to by `dest` 10 | * Return: Returns the pointer to `dest` 11 | */ 12 | 13 | char *_strcpy(char *dest, char *src) 14 | { 15 | int k = -1; 16 | 17 | do { 18 | k++; 19 | dest[k] = src[k]; 20 | } while (src[k] != '\0'); 21 | 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | Pointers, Arrays and Strings directory 2 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void reset_to_98(int *n); 3 | void swap_int(int *a, int *b); 4 | int _strlen(char *s); 5 | void _puts(char *str); 6 | void print_rev(char *s); 7 | void rev_string(char *s); 8 | void puts2(char *str); 9 | void puts_half(char *str); 10 | void print_array(int *a, int n); 11 | char *_strcpy(char *dest, char *src); 12 | int _atoi(char *s); 13 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strcat - Functions concatenates two strings 4 | * @dest: destination pointer 5 | * @src: source pointer 6 | * Return: Returns void 7 | */ 8 | 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int index = -1, counter; 12 | 13 | for (counter = 0; dest[counter] != '\0'; counter++) 14 | ; 15 | 16 | do { 17 | index++; 18 | dest[counter] = src[index]; 19 | counter++; 20 | } while (src[index] != '\0'); 21 | 22 | return (dest); 23 | } -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strncat - Function concatenates two strings. 6 | * @dest: Input string 7 | * @src: Input string 8 | * @n: Input integer 9 | * return: Returns a pointer to the resulting string 10 | */ 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int srclen = 0, i = 0; 14 | char *temp = dest, *start = src; 15 | 16 | while (*src) 17 | { 18 | srclen++; 19 | src++; 20 | } 21 | 22 | while (*dest) 23 | dest++; 24 | 25 | if (n > srclen) 26 | n = srclen; 27 | 28 | src = start; 29 | 30 | for (; i < n; i++) 31 | *dest++ = *src++; 32 | 33 | *dest = '\0'; 34 | return (temp); 35 | } 36 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rot13 - Function encodes a given string using rot13 4 | * @s: Input string 5 | * Return: Returns encoded string 6 | */ 7 | char *rot13(char *s) 8 | { 9 | int j; 10 | 11 | char rot13[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 12 | char ROT13[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; 13 | char *ptr = s; 14 | 15 | while (*s) 16 | { 17 | for (j = 0; j <= 52; j++) 18 | { 19 | if (*s == rot13[j]) 20 | { 21 | *s = ROT13[j]; 22 | break; 23 | } 24 | } 25 | s++; 26 | } 27 | return (ptr); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int _putchar(char c); 4 | 5 | /** 6 | * print_number - Prints an integer using recursion 7 | * @n: The integer to print 8 | * 9 | * Description: Recursively divides the number to print each digit 10 | */ 11 | void print_number(int n) 12 | { 13 | if (n < 0) 14 | { 15 | _putchar('-'); /* Handle negative numbers */ 16 | if (n < -9) 17 | print_number(-(n / 10)); 18 | _putchar('0' - (n % 10)); 19 | } 20 | else 21 | { 22 | if (n > 9) 23 | print_number(n / 10); 24 | _putchar('0' + (n % 10)); 25 | } 26 | } 27 | 28 | /** 29 | * _putchar - Writes the character c to stdout 30 | * @c: The character to print 31 | * 32 | * Return: On success 1. 33 | * On error, -1 is returned, and errno is set appropriately. 34 | */ 35 | int _putchar(char c) 36 | { 37 | return (write(1, &c, 1)); 38 | } 39 | -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * infinite_add - Function adds two numbers/ integers 5 | * @n1: The fisrt number 6 | * @n2: The second number 7 | * @r: Buffer used to store the result 8 | * @size_r: Buffer size 9 | * Return: Returns results, or 0 if results can't be stored 10 | */ 11 | 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 13 | { 14 | int c1 = 0, c2 = 0, total, wide, numb1, numb2, modul = 0; 15 | 16 | while (n1[c1] != '\0') 17 | c1++; 18 | while (n2[c2] != '\0') 19 | c2++; 20 | if (c1 >= c2) 21 | wide = c1; 22 | else 23 | wide = c2; 24 | if (size_r <= wide + 1) 25 | return (0); 26 | r[wide + 1] = '\0'; 27 | c1--, c2--, size_r--; 28 | numb1 = n1[c1] - 48, numb2 = n2[c2] - 48; 29 | while (wide >= 0) 30 | { 31 | total = numb1 + numb2 + modul; 32 | if (total >= 10) 33 | modul = total / 10; 34 | else 35 | modul = 0; 36 | if (total > 0) 37 | r[wide] = (total % 10) + 48; 38 | else 39 | r[wide] = '0'; 40 | if (c1 > 0) 41 | c1--, numb1 = *(n1 + c1) - 48; 42 | else 43 | numb1 = 0; 44 | if (c2 > 0) 45 | c2--, numb2 = n2[c2] - 48; 46 | else 47 | numb2 = 0; 48 | wide--, size_r--; 49 | } 50 | if (*(r) == '0') 51 | return (r + 1); 52 | else 53 | return (r); 54 | } 55 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x06-pointers_arrays_strings/104-print_buffer.c -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strncpy - Function copies the string 4 | * that is pointed to by src. 5 | * @dest: buffer pointer. 6 | * @src: source string pointer. 7 | * @n: src length. 8 | * Return: Returns char 9 | */ 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int a; 13 | 14 | for (a = 0; a < n && src[a] != '\0'; a++) 15 | { 16 | dest[a] = src[a]; 17 | } 18 | for (; a < n; a++) 19 | dest[a] = '\0'; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - Function compares two strings. 5 | * @s1: input string 6 | * @s2: input string 7 | * Return: Function returns the difference (s1 - s2) 8 | */ 9 | int _strcmp(char *s1, char *s2) 10 | { 11 | while (*s1 && *s2) 12 | { 13 | if (*s1 != *s2) 14 | return (*s1 - *s2); 15 | s1++, s2++; 16 | } 17 | return (*s1 - *s2); 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - Function reverses an array 5 | * @a: Input array 6 | * @n: Number of array elements 7 | * Return: Returns nothing 8 | */ 9 | void reverse_array(int *a, int n) 10 | { 11 | int j = 0; 12 | int aux; 13 | 14 | while (j < n--) 15 | { 16 | aux = a[j]; 17 | a[j++] = a[n]; 18 | a[n] = aux; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *string_toupper - Function changes all lowercase letters 4 | * of a string to uppercase 5 | * 6 | * @str: The string to return 7 | * Return: Returns a string 8 | */ 9 | char *string_toupper(char *str) 10 | { 11 | int j; 12 | 13 | for (j = 0; str[j] != '\0'; j++) 14 | { 15 | if (str[j] >= 97 && str[j] <= 122) 16 | { 17 | str[j] = str[j] - 32; 18 | } 19 | } 20 | return (str); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * isLower - Function determines whether an ascii is in lowercase 6 | * @c: A character 7 | * Return: Returns 1 if true, or 0 if false 8 | */ 9 | int isLower(char c) 10 | { 11 | return (c >= 97 && c <= 122); 12 | } 13 | 14 | /** 15 | * isDelimiter - Function determines whether an ascii is a delimiter 16 | * @c: Character 17 | * Return: Returns 1 if true, or 0 if false 18 | */ 19 | int isDelimiter(char c) 20 | { 21 | int j; 22 | char delimiter[] = " \t\n,.!?\"(){}"; 23 | 24 | for (j = 0; j < 12; j++) 25 | if (c == delimiter[j]) 26 | return (1); 27 | return (0); 28 | } 29 | 30 | /** 31 | * cap_string - Function changes all words of a string 32 | * to capitalized 33 | * @s: Input string 34 | * Return: Returns a string with capitalized words 35 | */ 36 | char *cap_string(char *s) 37 | { 38 | char *pntr = s; 39 | int fndDelimit = 1; 40 | 41 | while (*s) 42 | { 43 | if (isDelimiter(*s)) 44 | fndDelimit = 1; 45 | else if (isLower(*s) && fndDelimit) 46 | { 47 | *s -= 32; 48 | fndDelimit = 0; 49 | } 50 | else 51 | fndDelimit = 0; 52 | s++; 53 | } 54 | return (pntr); 55 | } 56 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - Function encodes a string into 1337 4 | * @c: String 5 | * Return: Returns an encoded string 6 | */ 7 | char *leet(char *c) 8 | { 9 | char *cp = c; 10 | char key[] = {'A', 'E', 'O', 'T', 'L'}; 11 | int value[] = {4, 3, 0, 7, 1}; 12 | unsigned int j; 13 | 14 | while (*c) 15 | { 16 | for (j = 0; j < sizeof(key) / sizeof(char); j++) 17 | { 18 | /*Lower case letters and apper case letters have a difference of 32*/ 19 | if (*c == key[j] || *c == key[j] + 32) 20 | { 21 | *c = 48 + value[j]; 22 | } 23 | } 24 | c++; 25 | } 26 | 27 | return (cp); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # Pointers, Arrays, and Strings 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - Function writes the character c to stdout 5 | * @c: Character to print 6 | * 7 | * Return: 1 On success 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | 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 *leet(char *); 9 | char *rot13(char *); 10 | void print_number(int n); 11 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 12 | void print_buffer(char *b, int size); 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - copies memory area and fills memory 5 | * with constant bytes 6 | * @s: the starting address of memory to be filled 7 | * @b: the desired value 8 | * @n: the number of bytes to be filled 9 | * 10 | * return: returns a changed array with a new value for n bytes 11 | */ 12 | 13 | char *_memset(char *s, char b, unsigned int n) 14 | { 15 | unsigned int a; 16 | 17 | for (a = 0; a < n; a++) 18 | { 19 | s[a] = b; 20 | } 21 | return (s); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - function copies memory area 5 | * @dest: memory stored 6 | * @src: copied memory 7 | * @n: the number of bytes to be copied 8 | * 9 | * Return: copied mem with n bytes changed 10 | */ 11 | 12 | char *_memcpy(char *dest, char *src, unsigned int n) 13 | { 14 | unsigned int j; 15 | 16 | for (j = 0; j < n; j++) 17 | { 18 | dest[j] = src[j]; 19 | } 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - function sets the value of a pointer to a char. 5 | * @s: double pointer for source address. 6 | * @to: pointer for target address. 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 single character in a string. 5 | * @s: a pointer to the string. 6 | * @c: the character being located. 7 | * return: Pointer for first occurrence of 'c' 8 | * or NULL if 'c' is not found. 9 | */ 10 | 11 | char *_strchr(char *s, char c) 12 | { 13 | unsigned int x = 0; 14 | 15 | for (; s[x] != '\0'; x++) 16 | if (s[x] == c) 17 | return (s + x); 18 | if (s[x] == c) 19 | return (s + x); 20 | return ('\0'); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - function gets length of a prefix substring. 5 | * @s: pointer for initial segment string. 6 | * @accept: pointer for string of accepted bytes. 7 | * return: returns the number of accepted bytes 8 | * in the initial segment of 's'. 9 | */ 10 | 11 | unsigned int _strspn(char *s, char *accept) 12 | { 13 | unsigned int a, b; 14 | int found; 15 | 16 | for (a = 0; s[a] != '\0'; a++) 17 | { 18 | found = 1; 19 | for (b = 0; accept[b] != '\0'; b++) 20 | { 21 | if (s[a] == accept[b]) 22 | { 23 | found = 0; 24 | break; 25 | } 26 | } 27 | if (found == 1) 28 | break; 29 | } 30 | return (a); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - function searches a string for any of a set of bytes. 5 | * @s: pointer for searched string 6 | * @accept: pointer for set of bytes. 7 | * return: returns pointer for first occurrence 8 | * of a matching byte in 's', 9 | * or NULL if none is found. 10 | */ 11 | 12 | char *_strpbrk(char *s, char *accept) 13 | { 14 | unsigned int x, y; 15 | 16 | for (x = 0; s[x] != '\0'; x++) 17 | { 18 | for (y = 0; accept[y] != '\0'; y++) 19 | { 20 | if (s[x] == accept[y]) 21 | return (s + x); 22 | } 23 | } 24 | return ('\0'); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - function locates a substring in a string. 5 | * @haystack: Pointer for string to search in. 6 | * @needle: pointer for substring to search for. 7 | * Return: returns pointer for beginning of 8 | * the located substring, 9 | * if the substring is not found, returns NULL 10 | */ 11 | 12 | char *_strstr(char *haystack, char *needle) 13 | { 14 | unsigned int k, p; 15 | 16 | if (*needle == '\0') 17 | return (haystack); 18 | for (k = 0; haystack[k] != '\0'; k++) 19 | { 20 | for (p = 0; needle[p] != '\0'; p++) 21 | { 22 | if (haystack[k + p] != needle[p]) 23 | break; 24 | } 25 | if (needle[p] == '\0') 26 | return (haystack + k); 27 | } 28 | return ('\0'); 29 | } 30 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - function prints the chessboard 5 | * @a: a 2D array 6 | */ 7 | 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | unsigned int k, j = 0; 11 | 12 | for (k = 0; k < 64; k++) 13 | { 14 | if (k % 8 == 0 && k != 0) 15 | { 16 | j = k; 17 | _putchar('\n'); 18 | } 19 | _putchar(a[k / 8][k - j]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - function prints the sum of 6 | * the two diagonals of any square matrix. 7 | * @a: pointer to the first element of the matrix. 8 | * @size: the size of the matrix 9 | * return: returns void. 10 | */ 11 | 12 | void print_diagsums(int *a, int size) 13 | { 14 | int j, sum1 = 0, sum2 = 0; 15 | 16 | for (j = 0; j < (size * size); j++) 17 | { 18 | if (j % (size + 1) == 0) 19 | sum1 += a[j]; 20 | if (j % (size - 1) == 0 && j != 0 && j < size * size - 1) 21 | sum2 += a[j]; 22 | } 23 | printf("%d, %d\n", sum1, sum2); 24 | } 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # Doc file for more pointers, Arrays, and strings 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - function will write the character c to stdout 5 | * @c: Character to print 6 | * 7 | * return: 1 on success and 8 | * -1 on error. Errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | char *_memcpy(char *dest, char *src, unsigned int n); 5 | char *_memset(char *s, char b, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn(char *s, char *accept); 8 | char *_strpbrk(char *s, char *accept); 9 | char *_strstr(char *haystack, char *needle); 10 | void print_chessboard(char (*a)[8]); 11 | void print_diagsums(int *a, int size); 12 | void set_string(char **s, char *to); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts_recursion - function prints a string, 4 | * followed by a new line 5 | * @s: string pointer 6 | * return: returns void 7 | */ 8 | 9 | void _puts_recursion(char *s) 10 | { 11 | if (*s == '\0') 12 | { 13 | _putchar('\n'); 14 | } 15 | else 16 | { 17 | _putchar(*s); 18 | _puts_recursion(s + 1); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _print_rev_recursion - prints string on reverse 4 | * @s: string pointer 5 | * *Return: void 6 | */ 7 | 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s > '\0') 11 | { 12 | _print_rev_recursion(s + 1); 13 | _putchar(*s); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * last_index - function returns the last index of a string 4 | * @s: string pointer 5 | * Return: int 6 | */ 7 | int is_palindrome(char *s); 8 | int check(char *s, int start, int end, int pair); 9 | int last_index(char *s) 10 | { 11 | int n = 0; 12 | 13 | if (*s > '\0') 14 | n += last_index(s + 1) + 1; 15 | 16 | return (n); 17 | } 18 | 19 | /** 20 | * is_palindrome - function checks if a string is a palindrome or not 21 | * @s: string being checked 22 | * Return: 0 or 1 23 | */ 24 | 25 | int is_palindrome(char *s) 26 | { 27 | int end = last_index(s); 28 | 29 | return (check(s, 0, end - 1, end % 2)); 30 | } 31 | 32 | /** 33 | * check - palindrome checker 34 | * @s: string 35 | * @start: first int to the left 36 | * @end: first int to the right 37 | * @pair: int 38 | * Return: 0 or 1 39 | */ 40 | 41 | 42 | int check(char *s, int start, int end, int pair) 43 | { 44 | 45 | if ((start == end && pair != 0) || (start == end + 1 && pair == 0)) 46 | return (1); 47 | else if (s[start] != s[end]) 48 | return (0); 49 | else 50 | return (check(s, start + 1, end - 1, pair)); 51 | } 52 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * wildcmp - function checks the code for Holberton School students. 6 | * @s1: string value 7 | * @s2: string value 8 | * @a: int value 9 | * @b: int value 10 | * @wildChar: int value 11 | * Return: returns 0 always. 12 | */ 13 | int checker(char *s1, char *s2, int a, int b, int wildChar); 14 | int wildcmp(char *s1, char *s2) 15 | { 16 | return (checker(s1, s2, 0, 0, -1)); 17 | } 18 | 19 | /** 20 | * checkLast - check the last char of s2 when s1 ends 21 | * @str: string value 22 | * @i: int value 23 | * Return: 0 or 1 24 | */ 25 | int checkLast(char *str, int i) 26 | { 27 | if (str[i] == '*') 28 | return (checkLast(str, i + 1)); 29 | else if (str[i] == '\0') 30 | return (1); 31 | 32 | return (0); 33 | 34 | } 35 | /** 36 | * checker - a helper function 37 | * @s1: string 38 | * @s2: string 39 | * @a: int value 40 | * @b: int value 41 | * @wildChar: int 42 | * Return: returns 0 always. 43 | */ 44 | int checker(char *s1, char *s2, int a, int b, int wildChar) 45 | { 46 | 47 | if (s1[a] != '\0') 48 | { 49 | if (s2[b] == '\0') 50 | return (0); 51 | else if (s2[b] == '*') 52 | { 53 | if (s2[b + 1] == '*') 54 | return (checker(s1, s2, a, b + 1, b)); 55 | else if (s2[b + 1] == s1[a]) 56 | return (checker(s1, s2, a, b + 1, b)); 57 | else if (s1[a + 1] != s2[b + 1]) 58 | return (checker(s1, s2, a + 1, b, b)); 59 | else if (s1[a + 1] == s2[b + 1]) 60 | return (checker(s1, s2, a + 1, b + 1, b)); 61 | } 62 | else if ((s1[a] == s2[b]) || (s2[b] == '*' && s2[b + 1] == s1[a + 1])) 63 | return (checker(s1, s2, a + 1, b + 1, wildChar)); 64 | 65 | if (wildChar == -1) 66 | return (0); 67 | 68 | return (checker(s1, s2, a, wildChar, wildChar)); 69 | 70 | } 71 | if (s2[b] != '\0') 72 | return (checkLast(s2, b)); 73 | 74 | return (1); 75 | 76 | } 77 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen_recursion - function returns the 4 | * string, and length. 5 | * @s: string pointer 6 | * Return: int 7 | */ 8 | 9 | int _strlen_recursion(char *s) 10 | { 11 | int x = 0; 12 | 13 | if (*s > '\0') 14 | { 15 | x += _strlen_recursion(s + 1) + 1; 16 | } 17 | 18 | return (x); 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * factorial - function returns the 4 | * factorial of a given number 5 | * @n: input number 6 | * Return: factorial of input 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n < 0) 11 | return (-1); 12 | if (n == 1) 13 | return (1); 14 | return (n * factorial(n - 1)); 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _pow_recursion - The function will search 4 | * a string for any of a set of bytes. 5 | * @x: the base 6 | * @y: the exponent 7 | * Return: Returns pointer to the byte in `s` 8 | * if it matches one of the bytes in `accept` 9 | * or NULL if such a byte is not found. 10 | */ 11 | 12 | int _pow_recursion(int x, int y) 13 | { 14 | 15 | if (y < 0) 16 | return (-1); 17 | else if (y == 1) 18 | return (x); 19 | else if (y == 0) 20 | return (1); 21 | 22 | return (x * _pow_recursion(x, y - 1)); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _sqrt_recursion - function finds the natural square root by recursion 4 | * @n: int 5 | * @val: square root 6 | * Return: returns an int 'n' 7 | */ 8 | int square(int n, int val); 9 | int _sqrt_recursion(int n) 10 | { 11 | return (square(n, 1)); 12 | } 13 | 14 | /** 15 | * square - find square root 16 | * @n: input int 17 | * @val: square root value of int `n` 18 | * Return: int 19 | */ 20 | 21 | int square(int n, int val) 22 | { 23 | 24 | if (val * val == n) 25 | return (val); 26 | else if (val * val < n) 27 | return (square(n, val + 1)); 28 | else 29 | return (-1); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * is_prime_number - function checks if n is prime 5 | * @resp: int value 6 | * @n: int value 7 | * Return: 0 or 1 8 | */ 9 | 10 | int check_prime(int n, int resp); 11 | int is_prime_number(int n) 12 | { 13 | return (check_prime(n, 2)); 14 | } 15 | 16 | /** 17 | * check_prime - check if n is divisible by all numbers less than it. 18 | * @n: int value 19 | * @resp: int value 20 | * Return: int 21 | */ 22 | 23 | int check_prime(int n, int resp) 24 | { 25 | 26 | if (resp >= n && n > 1) 27 | return (1); 28 | else if (n % resp == 0 || n <= 1) 29 | return (0); 30 | else 31 | return (check_prime(n, resp + 1)); 32 | } 33 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | Recursion in C programming 2 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void _puts_recursion(char *s); 3 | void _print_rev_recursion(char *s); 4 | int _strlen_recursion(char *s); 5 | int factorial(int n); 6 | int _pow_recursion(int x, int y); 7 | int _sqrt_recursion(int n); 8 | int is_prime_number(int n); 9 | int is_palindrome(char *s); 10 | int wildcmp(char *s1, char *s2); 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isupper - function checks if super 4 | * @c: Character to check 5 | * Return: return true or false as output 6 | */ 7 | 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_memset - empty function 4 | * @s: Parameter 5 | * @b: Parameter 6 | * @n: Parameter 7 | * Return: returns empty 8 | */ 9 | 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | return(0); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strcat - empty function 4 | * @dest: Parameter 5 | * @src: Parameter 6 | * Return: returns empty 7 | */ 8 | char *_strcat(char *dest, char *src) 9 | { 10 | return(0); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isdigit- empty function 4 | * @c: Parameter 5 | * Return: returns empty 6 | */ 7 | 8 | int _isdigit(int c) 9 | { 10 | return(0); 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * *_memcpy - empty function 4 | * @dest: Parameter 5 | * @src: Parameter 6 | * @n: Parameter 7 | * Return: returns empty 8 | */ 9 | 10 | char *_memcpy(char *dest, char *src, unsigned int n) 11 | { 12 | return(0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strncat - empty function 4 | * @dest: Parameter 5 | * @src: Parameter 6 | * @n: Parameter 7 | * Return: returns empty 8 | */ 9 | 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _atoi - empty function 4 | * @s: Parameter 5 | * Return: return 6 | */ 7 | 8 | int _atoi(char *s) 9 | 10 | { 11 | return(0); 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strchr - empty function 4 | * @s: Parameter 5 | * @c: Parameter 6 | * Return: returns empty 7 | */ 8 | 9 | char *_strchr(char *s, char c) 10 | { 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen - empty function 4 | * @s: Parameter 5 | * Return: returns empty 6 | */ 7 | 8 | int _strlen(char *s) 9 | { 10 | return(0); 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strncpy - empty function 4 | * @dest: Parameter 5 | * @src: Parameter 6 | * @n: Parameter 7 | * Return: returns empty 8 | */ 9 | char *_strncpy(char *dest, char *src, int n) 10 | { 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _islower - empty function 4 | * @c: Parameter 5 | * Return: returns empty 6 | */ 7 | 8 | int _islower(int c) 9 | { 10 | return(0); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts - empty function 4 | * @s: Parameter 5 | * Return: no return 6 | */ 7 | void _puts(char *s) 8 | { 9 | 0; 10 | } 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - empty function 4 | * @s1: Parameter 5 | * @s2: Parameter 6 | * Return: return 7 | */ 8 | 9 | int _strcmp(char *s1, char *s2) 10 | { 11 | return(0); 12 | } 13 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strspn - empty function 4 | * @s: Parameter 5 | * @accept: Parameter 6 | * Return: return 7 | */ 8 | 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - empty function 4 | * @c: Parameter 5 | * Return: return 6 | */ 7 | 8 | 9 | int _isalpha(int c) 10 | { 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strpbrk - empty function 4 | * @s: Parameter 5 | * @accept: Parameter 6 | * Return: return 7 | */ 8 | 9 | char *_strpbrk(char *s, char *accept) 10 | { 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strstr - empty function 4 | * @haystack: Parameter 5 | * @needle: Parameter 6 | * Return: return 7 | */ 8 | 9 | 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | return(0); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - empty function 4 | * @n: Parameter 5 | * Return: return 6 | */ 7 | 8 | int _abs(int n) 9 | { 10 | return(0); 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strcpy - empty function 4 | * @dest: Parameter 5 | * @src: Parameter 6 | * Return: return 7 | */ 8 | 9 | char *_strcpy(char *dest, char *src) 10 | { 11 | return(0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C Libraries 2 | 3 | ## A library is not a luxury but one of the necessities of life 4 | 5 | mandatory 6 | Create the static library libmy.a containing all the functions listed below: 7 | 8 | int \_putchar(char c); 9 | 10 | int \_islower(int c); 11 | 12 | int \_isalpha(int c); 13 | 14 | int \_abs(int n); 15 | 16 | int \_isupper(int c); 17 | 18 | int \_isdigit(int c); 19 | 20 | int \_strlen(char \*s); 21 | 22 | void \_puts(char \*s); 23 | 24 | char *\_strcpy(char*dest, char *src); 25 | 26 | int \_atoi(char*s); 27 | 28 | char *\_strcat(char*dest, char \*src); 29 | 30 | char *\_strncat(char*dest, char \*src, int n); 31 | 32 | char *\_strncpy(char*dest, char *src, int n); 33 | 34 | int \_strcmp(char*s1, char _s2); 35 | 36 | char_\_memset(char_s, char b, unsigned int n); 37 | 38 | char_\_memcpy(char *dest, char*src, unsigned int n); 39 | 40 | char *\_strchr(char*s, char c); 41 | 42 | unsigned int \_strspn(char *s, char*accept); 43 | 44 | char *\_strpbrk(char*s, char _accept); 45 | 46 | char_\_strstr(char *haystack, char*needle); 47 | 48 | If you haven’t coded all of the above functions create empty ones with the right prototype. 49 | Don’t forget to push your main.h file to your repository. It should at least contain all the prototypes of the above functions. 50 | 51 | julien@ubuntu:~/0x09. Static Librairies$ ar -t libmy.a 52 | 0-isupper.o 53 | 0-memset.o 54 | 0-strcat.o 55 | 1-isdigit.o 56 | 1-memcpy.o 57 | 1-strncat.o 58 | 100-atoi.o 59 | 2-strchr.o 60 | 2-strlen.o 61 | 2-strncpy.o 62 | 3-islower.o 63 | 3-puts.o 64 | 3-strcmp.o 65 | 3-strspn.o 66 | 4-isalpha.o 67 | 4-strpbrk.o 68 | 5-strstr.o 69 | 6-abs.o 70 | 9-strcpy.o 71 | \_putchar.o 72 | julien@ubuntu:~/0x09. Static Librairies$ nm libmy.a 73 | 74 | 0-isupper.o: 75 | 0000000000000000 T \_isupper 76 | 77 | 0-memset.o: 78 | 0000000000000000 T \_memset 79 | 80 | 0-strcat.o: 81 | 0000000000000000 T \_strcat 82 | 83 | 1-isdigit.o: 84 | 0000000000000000 T \_isdigit 85 | 86 | 1-memcpy.o: 87 | 0000000000000000 T \_memcpy 88 | 89 | 1-strncat.o: 90 | 0000000000000000 T \_strncat 91 | 92 | 100-atoi.o: 93 | 0000000000000000 T \_atoi 94 | 95 | 2-strchr.o: 96 | 0000000000000000 T \_strchr 97 | 98 | 2-strlen.o: 99 | 0000000000000000 T \_strlen 100 | 101 | 2-strncpy.o: 102 | 0000000000000000 T \_strncpy 103 | 104 | 3-islower.o: 105 | 0000000000000000 T \_islower 106 | 107 | 3-puts.o: 108 | U \_putchar 109 | 0000000000000000 T \_puts 110 | 111 | 3-strcmp.o: 112 | 0000000000000000 T \_strcmp 113 | 114 | 3-strspn.o: 115 | 0000000000000000 T \_strspn 116 | 117 | 4-isalpha.o: 118 | 0000000000000000 T \_isalpha 119 | 120 | 4-strpbrk.o: 121 | 0000000000000000 T \_strpbrk 122 | 123 | 5-strstr.o: 124 | 0000000000000000 T \_strstr 125 | 126 | 6-abs.o: 127 | 0000000000000000 T \_abs 128 | 129 | 9-strcpy.o: 130 | 0000000000000000 T \_strcpy 131 | 132 | \_putchar.o: 133 | 0000000000000000 T \_putchar 134 | U write 135 | julien@ubuntu:~/0x09. Static Librairies$ cat main.c 136 | 137 | ## include "main.h" 138 | 139 | int main(void) 140 | { 141 | \_puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 142 | return (0); 143 | } 144 | julien@ubuntu:~/0x09. Static Librairies$ gcc -std=gnu89 main.c -L. -lmy -o quote 145 | julien@ubuntu:~/0x09. Static Librairies$ ./quote 146 | "At the end of the day, my goal was to be the best hacker" - Kevin Mitnick 147 | julien@ubuntu:~/0x09. Static Librairies$ 148 | Repo: 149 | 150 | GitHub repository: alx-low_level_programming 151 | Directory: 0x09-static_libraries 152 | File: libmy.a, main.h 153 | 154 | 1. Without libraries what have we? We have no past and no future 155 | mandatory 156 | Create a script called create_static_lib.sh that creates a static library called liball.a from all the .c files that are in the current directory. 157 | 158 | julien@ubuntu:~/0x09. Static Librairies$ ls _.c 159 | 0-isupper.c 0-strcat.c 1-isdigit.c 1-strncat.c 2-strlen.c 3-islower.c 3-strcmp.c 4-isalpha.c 5-strstr.c 9-strcpy.c \_putchar.c 160 | 0-memset.c 100-atoi.c 1-memcpy.c 2-strchr.c 2-strncpy.c 3-puts.c 3-strspn.c 4-strpbrk.c 6-abs.c 161 | julien@ubuntu:~/0x09. Static Librairies$ ./create_static_lib.sh 162 | julien@ubuntu:~/0x09. Static Librairies$ ls _.a 163 | liball.a 164 | julien@ubuntu:~/0x09. Static Librairies$ ar -t liball.a 165 | 0-isupper.o 166 | 0-memset.o 167 | 0-strcat.o 168 | 100-atoi.o 169 | 1-isdigit.o 170 | 1-memcpy.o 171 | 1-strncat.o 172 | 2-strchr.o 173 | 2-strlen.o 174 | 2-strncpy.o 175 | 3-islower.o 176 | 3-puts.o 177 | 3-strcmp.o 178 | 3-strspn.o 179 | 4-isalpha.o 180 | 4-strpbrk.o 181 | 5-strstr.o 182 | 6-abs.o 183 | 9-strcpy.o 184 | \_putchar.o 185 | julien@ubuntu:~/0x09. Static Librairies$ 186 | Repo: 187 | 188 | GitHub repository: alx-low_level_programming 189 | Directory: 0x09-static_libraries 190 | File: create_static_lib.sh 191 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - function will write the character c to stdout 5 | * @c: Character to print 6 | * 7 | * Return: 1 on success and 8 | * -1 on error. 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/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/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/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif /*"MAIN_H*/ 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - function prints program name 5 | * followed by new line 6 | * @argc: int 7 | * @argv: list 8 | * Return: 0 9 | */ 10 | 11 | int main(int argc, char const *argv[]) 12 | { 13 | (void)argc; 14 | printf("%s\n", argv[0]); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | *main- function prints number of arguments being passed. 4 | *@argc: the number of command line arguments. 5 | *@argv: a pointer to array of command line arguments. 6 | *Return: 0-success, other than 0 on failure. 7 | */ 8 | 9 | int main(int argc, char *argv[] __attribute__((unused))) 10 | { 11 | printf("%d\n", argc - 1); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - function prints the min number of coins 5 | *to make change for a certain amount of money. 6 | *@argc: the number of command line args. 7 | *@argv: a pointer to the array of command line args. 8 | *Return: returns 0 on success, and others on failure. 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | if (argc == 2) 14 | { 15 | int j, mincents = 0, cash = atoi(argv[1]); 16 | int cents[] = {25, 10, 5, 2, 1}; 17 | 18 | for (j = 0; j < 5; j++) 19 | { 20 | if (cash >= cents[j]) 21 | { 22 | mincents += cash / cents[j]; 23 | cash = cash % cents[j]; 24 | if (cash % cents[j] == 0) 25 | { 26 | break; 27 | } 28 | } 29 | } 30 | printf("%d\n", mincents); 31 | } 32 | else 33 | { 34 | printf("Error\n"); 35 | return (1); 36 | } 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - function prints number of arguments passed into program 5 | * @argc: int 6 | * @argv: list 7 | * Return: 0 (Zero) 8 | */ 9 | 10 | int main(int argc, char const *argv[]) 11 | { 12 | int j = 0; 13 | 14 | while (argc--) 15 | { 16 | printf("%s\n", argv[j]); 17 | j++; 18 | } 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | *main - function prints sum of two numbers. 5 | *@argc: number of command line arguments. 6 | *@argv: a pointer to the array of cmd arguments. 7 | *Return: 0 on success, other on failure. 8 | */ 9 | 10 | int main(int argc, char *argv[]) 11 | { 12 | int total; 13 | 14 | if (argc == 3) 15 | { 16 | total = atoi(argv[1]) * atoi(argv[2]); 17 | printf("%d\n", total); 18 | } 19 | else 20 | { 21 | printf("Error\n"); 22 | return (1); 23 | } 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * isInteger - function checks if s is integer 5 | * @s: string to be checked 6 | * Return: returns 0 or 1 7 | */ 8 | 9 | int isInteger(const char *s) 10 | { 11 | int g = 0; 12 | while (s[g] != '\0') 13 | { 14 | if (s[g] < '0' || s[g] > '9') 15 | return (1); 16 | g++; 17 | } 18 | return (0); 19 | } 20 | 21 | /** 22 | * main - function adds positive nums 23 | * @argc: int 24 | * @argv: list 25 | * Return: returns 0 26 | */ 27 | 28 | int main(int argc, char const *argv[]) 29 | { 30 | int total = 0; 31 | while (--argc) 32 | { 33 | if (isInteger(argv[argc])) 34 | { 35 | printf("Error\n"); 36 | return (1); 37 | } 38 | total += atoi(argv[argc]); 39 | } 40 | 41 | printf("%i\n", total); 42 | 43 | return (0); 44 | } 45 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # Argv README file 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * *create_array - Function creates a chars array 5 | * and initializes the arr with a char. 6 | * @size: array size 7 | * @c: char to initialize 8 | * Return: Returns pointer to the initialized arr or NULL 9 | */ 10 | 11 | char *create_array(unsigned int size, char c) 12 | { 13 | char *k = malloc(size); 14 | 15 | if (size == 0 || k == 0) 16 | return (0); 17 | 18 | while (size--) 19 | k[size] = c; 20 | 21 | return (k); 22 | } 23 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * *_strdup - Function returns a pointer to a new space in memory 7 | * which stores a copy of the string (parameter). 8 | * @str: string parameter 9 | * Return: Returns 0 10 | */ 11 | 12 | char *_strdup(char *str) 13 | { 14 | int i = 0; 15 | int size = 0; 16 | char *y; 17 | 18 | if (str == NULL) 19 | return (NULL); 20 | 21 | for (; str[size] != '\0'; size++) 22 | ; 23 | 24 | /*+1 on the size puts the end of string character*/ 25 | y = malloc(size * sizeof(*str) + 1); 26 | 27 | if (y == 0) 28 | return (NULL); 29 | else 30 | { 31 | for (; i < size; i++) 32 | y[i] = str[i]; 33 | } 34 | return (y); 35 | } 36 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * _strlen - Function finds length of a given string 6 | * @s: a string 7 | * Return: Returns an integer 8 | */ 9 | 10 | 11 | int _strlen(char *s) 12 | { 13 | int sizing = 0; 14 | for (; s[sizing] != '\0'; sizing++) 15 | ; 16 | return (sizing); 17 | } 18 | 19 | /** 20 | * *argstostr - Function description 21 | * @ac: integer 22 | * @av: vector arguments 23 | * Return: Returns a string 24 | */ 25 | 26 | char *argstostr(int ac, char **av) 27 | { 28 | int j = 0, nc = 0, k = 0, dev = 0; 29 | char *s; 30 | 31 | if (ac == 0 || av == NULL) 32 | return (NULL); 33 | 34 | for (; j < ac; j++, nc++) 35 | nc += _strlen(av[j]); 36 | 37 | s = malloc(sizeof(char) * nc + 1); 38 | if (s == 0) 39 | return (NULL); 40 | 41 | for (j = 0; j < ac; j++) 42 | { 43 | for (k = 0; av[j][k] != '\0'; k++, dev++) 44 | s[dev] = av[j][k]; 45 | 46 | s[dev] = '\n'; 47 | dev++; 48 | } 49 | s[dev] = '\0'; 50 | 51 | return (s); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * wrdcnt - Function counts the number of words in a string 6 | * @s: string 7 | * Return: Returns number of words (integer) 8 | */ 9 | int wrdcnt(char *s) 10 | { 11 | int j, n = 0; 12 | 13 | for (j = 0; s[j]; j++) 14 | { 15 | if (s[j] == ' ') 16 | { 17 | if (s[j + 1] != ' ' && s[j + 1] != '\0') 18 | n++; 19 | } 20 | else if (j == 0) 21 | n++; 22 | } 23 | n++; 24 | return (n); 25 | } 26 | 27 | /** 28 | * strtow - Function splits a string into words 29 | * @str: string 30 | * Return: Returns pointer to a strings array 31 | */ 32 | char **strtow(char *str) 33 | { 34 | int a, b, c, m, n = 0, wc = 0; 35 | char **w; 36 | 37 | if (str == NULL || *str == '\0') 38 | return (NULL); 39 | n = wrdcnt(str); 40 | if (n == 1) 41 | return (NULL); 42 | w = (char **)malloc(n * sizeof(char *)); 43 | if (w == NULL) 44 | return (NULL); 45 | w[n - 1] = NULL; 46 | a = 0; 47 | while (str[a]) 48 | { 49 | if (str[a] != ' ' && (a == 0 || str[a - 1] == ' ')) 50 | { 51 | for (b = 1; str[a + b] != ' ' && str[a + b]; b++) 52 | ; 53 | b++; 54 | w[wc] = (char *)malloc(b * sizeof(char)); 55 | b--; 56 | if (w[wc] == NULL) 57 | { 58 | for (c = 0; c < wc; c++) 59 | free(w[c]); 60 | free(w[n - 1]); 61 | free(w); 62 | return (NULL); 63 | } 64 | for (m = 0; m < b; m++) 65 | w[wc][m] = str[a + m]; 66 | w[wc][m] = '\0'; 67 | wc++; 68 | a += b; 69 | } 70 | else 71 | a++; 72 | } 73 | return (w); 74 | } 75 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include 4 | 5 | /** 6 | * _strlen - Function finds length of a string 7 | * @s: Given string 8 | * Return: Returns an integer 9 | */ 10 | 11 | 12 | int _strlen(char *s) 13 | { 14 | int size = 0; 15 | for (; s[size] != '\0'; size++) 16 | ; 17 | return (size); 18 | } 19 | 20 | /** 21 | * *str_concat - Function concatenates two strings 22 | * @s1: first string 23 | * @s2: second string 24 | * Return: Returns pointer 25 | */ 26 | 27 | char *str_concat(char *s1, char *s2) 28 | { 29 | int size_str_1, size_str_2, i; 30 | char *m; 31 | 32 | if (s1 == NULL) 33 | s1 = "\0"; 34 | if (s2 == NULL) 35 | s2 = "\0"; 36 | 37 | size_str_1 = _strlen(s1); 38 | size_str_2 = _strlen(s2); 39 | m = malloc((size_str_1 + size_str_2) *sizeof(char) + 1); 40 | if (m == 0) 41 | return (0); 42 | 43 | for (i = 0; i <= size_str_1 + size_str_2; i++) 44 | { 45 | if (i < size_str_1) 46 | m[i] = s1[i]; 47 | else 48 | m[i] = s2[i - size_str_1]; 49 | } 50 | m[i] = '\0'; 51 | return (m); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * **alloc_grid - Functions returns a pointer to a 2D array of ints. 5 | * @width: integer 6 | * @height: integer 7 | * Return: Returns a 2D array 8 | */ 9 | 10 | int **alloc_grid(int width, int height) 11 | { 12 | int **sect, i, j; 13 | sect = malloc(sizeof(*sect) * height); 14 | 15 | if (width <= 0 || height <= 0 || sect == 0) 16 | return (NULL); 17 | 18 | else 19 | { 20 | for (i = 0; i < height; i++) 21 | { 22 | sect[i] = malloc(sizeof(**sect) * width); 23 | if (sect[i] == 0) 24 | { 25 | while (i--) /*Free everything in case malloc fails*/ 26 | free(sect[i]); 27 | free(sect); 28 | return (NULL); 29 | } 30 | 31 | for (j = 0; j < width; j++) 32 | sect[i][j] = 0; 33 | } 34 | } 35 | 36 | return (sect); 37 | } 38 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * free_grid - Function frees the grid 5 | * @grid: A grid of memories 6 | * @height: integer 7 | * Return: Returns void 8 | */ 9 | 10 | void free_grid(int **grid, int height) 11 | { 12 | int j = 0; 13 | for (; j < height; j++) 14 | free(grid[j]); 15 | free(grid); 16 | } 17 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # Memory Allocation 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #if !defined(MAIN_H) 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | char *create_array(unsigned int size, char c); 9 | char *_strdup(char *str); 10 | char *str_concat(char *s1, char *s2); 11 | int **alloc_grid(int width, int height); 12 | void free_grid(int **grid, int height); 13 | char *argstostr(int ac, char **av); 14 | char **strtow(char *str); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | /** 5 | * *malloc_checked - the function allocates memory 6 | * function exits if it fails 7 | * @b: int 8 | * Return: returns pointer to the array initialized or NULL 9 | */ 10 | 11 | void *malloc_checked(unsigned int b) 12 | { 13 | int *mem = malloc(b); 14 | 15 | if (mem == 0) 16 | exit(98); 17 | 18 | return (mem); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * string_nconcat - function concatenates any two strings 7 | * @s1: pointer to the first string. 8 | * @s2: pointer to the 2nd string. 9 | * @n: Number of to concatenate bytes from n2. 10 | * 11 | * Return: returns pointer to the memory space 12 | * containing the concatenated string. 13 | */ 14 | char *string_nconcat(char *s1, char *s2, unsigned int n) 15 | { 16 | char *str; 17 | unsigned int a, b, s1_length, s2_length; 18 | 19 | /*Check if the strings passed are null*/ 20 | if (s1 == NULL) 21 | s1 = ""; 22 | if (s2 == NULL) 23 | s2 = ""; 24 | /*Compute strings length*/ 25 | 26 | for (s1_length = 0; s1[s1_length] != '\0'; s1_length++) 27 | ; 28 | 29 | for (s2_length = 0; s2[s2_length] != '\0'; s2_length++) 30 | ; 31 | /*Case 1 & 2 Memory reservation*/ 32 | str = malloc(s1_length + n + 1); 33 | if (str == NULL) 34 | { 35 | return (NULL); 36 | } 37 | /*Copy into str the first string*/ 38 | for (a = 0; s1[a] != '\0'; a++) 39 | str[a] = s1[a]; 40 | /*copy into str the second string*/ 41 | for (b = 0; b < n; b++) 42 | { 43 | str[a] = s2[b]; 44 | a++; 45 | } 46 | 47 | str[a] = '\0'; 48 | return (str); 49 | } 50 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * *_realloc - functions allocates a memory block 5 | * using malloc and free. 6 | * @ptr: pointer to memory allocated with malloc(old_size). 7 | * @old_size: size in bytes, of the allocated space for ptr 8 | * @new_size: size in bytes of the new memory block. 9 | * 10 | * Return: NULL if new_size = 0 and ptr is not NULL. 11 | */ 12 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 13 | { 14 | void *pos; 15 | unsigned int i; 16 | 17 | if (new_size == old_size) 18 | return (ptr); 19 | if (new_size == 0 && ptr != NULL) 20 | { 21 | free(ptr); 22 | return (NULL); 23 | } 24 | if (ptr == NULL) 25 | { 26 | pos = malloc(new_size); 27 | if (pos == NULL) 28 | return (NULL); 29 | return (pos); 30 | } 31 | if (new_size > old_size) 32 | { 33 | pos = malloc(new_size); 34 | if (pos == NULL) 35 | return (NULL); 36 | for (i = 0; i < old_size && i < new_size; i++) 37 | *((char *)pos + i) = *((char *)ptr + i); 38 | free(ptr); 39 | } 40 | return (pos); 41 | } 42 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _puts - function prints a string 4 | * followed by a new line 5 | * @str: pointer 6 | * Return: returns void 7 | */ 8 | 9 | 10 | void _puts(char *str) 11 | { 12 | int a = 0; 13 | while (str[a]) 14 | { 15 | _putchar(str[a]); 16 | a++; 17 | } 18 | 19 | } 20 | 21 | /** 22 | * _atoi - function convert a string to an int 23 | * @s: char type string 24 | * Return: returns integer converted 25 | */ 26 | 27 | int _atoi(const char *s) 28 | { 29 | int sign = 1; 30 | unsigned long int rtn = 0, fNum, i; 31 | 32 | for (fNum = 0; !(s[fNum] >= 48 && s[fNum] <= 57); fNum++) 33 | { 34 | if (s[fNum] == '-') 35 | { 36 | sign *= -1; 37 | } 38 | } 39 | 40 | for (i = fNum; s[i] >= 48 && s[i] <= 57; i++) 41 | { 42 | rtn *= 10; 43 | rtn += (s[i] - 48); 44 | } 45 | 46 | return (sign * rtn); 47 | } 48 | 49 | /** 50 | * print_int - function prints an integer 51 | * @n: an int 52 | * Return: returns 0 53 | */ 54 | 55 | void print_int(unsigned long int n) 56 | { 57 | 58 | unsigned long int div = 1, a, rtn; 59 | 60 | for (a = 0; n / div > 9; a++, div *= 10) 61 | ; 62 | 63 | for (; div >= 1; n %= div, div /= 10) 64 | { 65 | rtn = n / div; 66 | _putchar('0' + rtn); 67 | } 68 | 69 | } 70 | 71 | /** 72 | * main - function print the multiplication result 73 | * followed by a new line 74 | * @argc: an int 75 | * @argv: a list 76 | * Return: returns 0 77 | */ 78 | 79 | int main(int argc, char const *argv[]) 80 | { 81 | (void)argc; 82 | 83 | if (argc != 3) 84 | { 85 | _puts("Error "); 86 | exit(98); 87 | } 88 | print_int(_atoi(argv[1]) * _atoi(argv[2])); 89 | _putchar('\n'); 90 | 91 | return (0); 92 | } 93 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_memset - function fills memory 4 | * with a constant byte 5 | * @s: a pointer to put the constant 6 | * @b: a constant 7 | * @n: maximum bytes to use 8 | * Return: returns s 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | char *pointer = s; 14 | 15 | while (n--) 16 | *s++ = b; 17 | 18 | return (pointer); 19 | } 20 | 21 | /** 22 | * *_calloc - function allocates memory 23 | * using malloc for an array, 24 | * @nmemb: the array length 25 | * @size: the size of each element 26 | * Return: returns a pointer 27 | */ 28 | 29 | void *_calloc(unsigned int nmemb, unsigned int size) 30 | { 31 | void *mem; 32 | 33 | if (size == 0 || nmemb == 0) 34 | return (NULL); 35 | mem = malloc(sizeof(int) * nmemb); 36 | 37 | if (mem == 0) 38 | return (NULL); 39 | 40 | _memset(mem, 0, nmemb * sizeof(int)); 41 | 42 | return (mem); 43 | } 44 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * array_range - function creates an array of integers 5 | * @min: the starting int 6 | * @max: maximum int 7 | * Return: returns an array of integers 8 | */ 9 | int *array_range(int min, int max) 10 | { 11 | int len, j; 12 | int *pointr; 13 | 14 | if (min > max) 15 | return (NULL); 16 | len = max - min + 1; 17 | pointr = malloc(sizeof(int) * len); 18 | if (!pointr) 19 | return (NULL); 20 | for (j = 0; j < len; j++) 21 | pointr[j] = min++; 22 | return (pointr); 23 | } 24 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | More malloc 2 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | #include 4 | 5 | void _puts(char *str); 6 | void *malloc_checked(unsigned int b); 7 | char *string_nconcat(char *s1, char *s2, unsigned int n); 8 | void *_calloc(unsigned int nmemb, unsigned int size); 9 | int *array_range(int min, int max); 10 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 11 | int _putchar(char c); 12 | int _atoi(const char *s); 13 | void print_int(unsigned long int n); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef SIZE 2 | #define SIZE 1024 3 | #endif 4 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI 2 | #define PI 3.14159265359 3 | #endif 4 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - function prints the name 5 | * of the file that was compiled 6 | * Return: returns an int 7 | */ 8 | 9 | int main(void) 10 | { 11 | printf("%s\n", __FILE__); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef ABS_DEF 2 | #define ABS_DEF 3 | #define ABS(x) (x > 0 ? (x) : (x) * -1) 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_DEF 2 | #define SUM_DEF 3 | #define SUM(x, y) (x + y) 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | Preprocessors in C Programming 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * init_dog - function initialize a variable 6 | * of type `struct dog` 7 | * @d: struct dog 8 | * @age: integer for age 9 | * @name: string for name 10 | * @owner: string for owners 11 | */ 12 | 13 | void init_dog(struct dog *d, char *name, float age, char *owner) 14 | { 15 | if (d != NULL) 16 | { 17 | d->name = name; 18 | d->age = age; 19 | d->owner = owner; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_dog - function prints dog data 7 | * @d: dog struct 8 | */ 9 | 10 | void print_dog(struct dog *d) 11 | { 12 | if (d != NULL) 13 | { 14 | printf("Name: %s\nAge: %f\nOwner: %s\n", 15 | (*d).name != NULL ? d->name : "(nil)", 16 | (*d).age, 17 | (*d).owner != NULL ? d->owner : "(nil)"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | #include 4 | /** 5 | * new_dog - function creates new data structure for dog 6 | * @name: dog name 7 | * @age: dog age 8 | * @owner: dog owner 9 | * 10 | * Return: Pointer to new dog 11 | **/ 12 | dog_t *new_dog(char *name, float age, char *owner) 13 | { 14 | dog_t *new_dog; 15 | int name_len, owner_len; 16 | 17 | new_dog = malloc(sizeof(dog_t)); 18 | if (new_dog == NULL) 19 | return (NULL); 20 | 21 | name_len = _strlen(name); 22 | new_dog->name = malloc(sizeof(char) * name_len + 1); 23 | if (new_dog->name == NULL) 24 | { 25 | free(new_dog); 26 | return (NULL); 27 | } 28 | new_dog->name = _strcpy(new_dog->name, name); 29 | owner_len = _strlen(owner); 30 | new_dog->owner = malloc(sizeof(char) * owner_len + 1); 31 | if (new_dog->owner == NULL) 32 | { 33 | free(new_dog->name); 34 | free(new_dog); 35 | return (NULL); 36 | } 37 | 38 | new_dog->owner = _strcpy(new_dog->owner, owner); 39 | new_dog->age = age; 40 | 41 | return (new_dog); 42 | } 43 | 44 | /** 45 | * _strlen - function determines the length of a string 46 | * @s: string pointer 47 | * Return: the length 48 | */ 49 | int _strlen(char *s) 50 | { 51 | int a; 52 | 53 | for (a = 0; s[a] != '\0'; a++) 54 | ; 55 | return (a); 56 | } 57 | 58 | /** 59 | * _strcpy - function copies a pointed string 60 | * @dest: destination string pointer 61 | * @src: source string pointer 62 | * Return: destiny string address 63 | */ 64 | char *_strcpy(char *dest, char *src) 65 | { 66 | int j = 0; 67 | 68 | while (src[j] != '\0') 69 | { 70 | dest[j] = src[j]; 71 | j++; 72 | } 73 | 74 | dest[j] = '\0'; 75 | return (dest); 76 | } 77 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - function of free dog memory 6 | * @d: dog 7 | */ 8 | 9 | 10 | void free_dog(dog_t *d) 11 | { 12 | if (d == NULL) 13 | return; 14 | 15 | free(d->owner); 16 | free(d->name); 17 | free(d); 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # Structures type def Directory 2 | 3 | Structures type def example problems 4 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef DOG_H 2 | #define DOG_H 3 | 4 | /** 5 | * struct dog - dog structure definition 6 | * @name: string character 7 | * @age: int 8 | * @owner: string char 9 | */ 10 | 11 | typedef struct dog 12 | { 13 | char *name; 14 | float age; 15 | char *owner; 16 | } dog_t; 17 | 18 | void init_dog(struct dog *d, char *name, float age, char *owner); 19 | void print_dog(struct dog *d); 20 | dog_t *new_dog(char *name, float age, char *owner); 21 | void free_dog(dog_t *d); 22 | char *_strcpy(char *dest, char *src); 23 | int _strlen(char *s); 24 | #endif 25 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - function prints name 5 | * @name: string 6 | * @f: function 7 | */ 8 | 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | if (f != NULL) 12 | f(name); 13 | } 14 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - function executes a function provided as a parameter 5 | * on each element of an array. 6 | * @array: an integer array 7 | * @size: array size 8 | * @action: a function pointer 9 | */ 10 | 11 | void array_iterator(int *array, size_t size, void (*action)(int)) 12 | { 13 | size_t i; 14 | 15 | if (array != NULL && action != NULL) 16 | { 17 | for (i = 0; i < size; i++) 18 | action(array[i]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - main function's entry point 6 | * 7 | * @argc: the argument count 8 | * 9 | * @argv: The argument vector 10 | * 11 | * Return: Returns 0 always 12 | */ 13 | int main(int argc, char *argv[]) 14 | { 15 | int j, k; 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | k = atoi(argv[1]); 22 | if (k < 0) 23 | { 24 | printf("Error\n"); 25 | exit(2); 26 | } 27 | for (j = 0; j < k; j++) 28 | { 29 | printf("%02hhx", *((char *)main + j)); 30 | if (j < k - 1) 31 | { 32 | printf(" "); 33 | } 34 | else 35 | { 36 | printf("\n"); 37 | } 38 | } 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - function searches for an integer 5 | * @array: an integers array 6 | * @size: size of array 7 | * @cmp: a function pointer 8 | * Return: returns the index of first element 9 | * that matches `cmp`, or -1 if none is found 10 | */ 11 | 12 | int int_index(int *array, int size, int (*cmp)(int)) 13 | { 14 | int j; 15 | 16 | if (size < 1 || array == NULL || cmp == NULL) 17 | return (-1); 18 | 19 | for (j = 0; j < size; j++) 20 | { 21 | if (cmp(array[j])) 22 | return (j); 23 | } 24 | 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef CALC_H 2 | #define CALC_H 3 | /** 4 | * struct op - The Struct op 5 | * 6 | * @op: an operator 7 | * @f: a function 8 | */ 9 | typedef struct op 10 | { 11 | char *op; 12 | int (*f)(int a, int b); 13 | } op_t; 14 | 15 | int op_add(int a, int b); 16 | int op_sub(int a, int b); 17 | int op_mul(int a, int b); 18 | int op_div(int a, int b); 19 | int op_mod(int a, int b); 20 | int (*get_op_func(char *s))(int, int); 21 | #endif 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | * get_op_func - function selects the correct operation function 6 | * requested by a user 7 | * @s: operator argument 8 | * Return: returns function pointer corresponding to the given operator 9 | */ 10 | int (*get_op_func(char *s))(int, int) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | int j = 0; 21 | 22 | while (ops[j].op != NULL && *(ops[j].op) != *s) 23 | j++; 24 | return (ops[j].f); 25 | } 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | /** 5 | * main - Function prints the results of simple operations. 6 | * @argc: Number of arguments supplied as input. 7 | * @argv: A pointers array to the arguments. 8 | * 9 | * Return: Returns always 0. 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int first_num, second_num; 14 | char *op; 15 | 16 | if (argc != 4) 17 | { 18 | printf("Error\n"); 19 | exit(98); 20 | } 21 | 22 | first_num = atoi(argv[1]); 23 | op = argv[2]; 24 | second_num = atoi(argv[3]); 25 | 26 | if (get_op_func(op) == NULL || op[1] != '\0') 27 | { 28 | printf("Error\n"); 29 | exit(99); 30 | } 31 | if ((*op == '/' && second_num == 0) || 32 | (*op == '%' && second_num == 0)) 33 | { 34 | printf("Error\n"); 35 | exit(100); 36 | } 37 | printf("%d\n", get_op_func(op)(first_num, second_num)); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * op_add - The addition operator 5 | * @a: first integer 6 | * @b: second integer 7 | * Return: Returns sum of 2 numbers 8 | */ 9 | int op_add(int a, int b) 10 | { 11 | return (a + b); 12 | } 13 | 14 | /** 15 | * op_sub - The subtraction operator 16 | * @a: first integer 17 | * @b: second integer 18 | * Return: Returns difference of 2 numbers 19 | */ 20 | int op_sub(int a, int b) 21 | { 22 | return (a - b); 23 | } 24 | 25 | /** 26 | * op_mul - The product of 2 numbers 27 | * @a: first integer 28 | * @b: second integer 29 | * Return: Returns product of provided numbers 30 | */ 31 | int op_mul(int a, int b) 32 | { 33 | return (a * b); 34 | } 35 | 36 | /** 37 | * op_div - The division of 2 numbers 38 | * @a: first integer 39 | * @b: second integer 40 | * Return: Returns result of the division of the provided numbers 41 | */ 42 | int op_div(int a, int b) 43 | { 44 | return (a / b); 45 | } 46 | 47 | /** 48 | * op_mod - The Modulus. Getting remainder after division of 2 numbers 49 | * @a: first integer 50 | * @b: second integer 51 | * Return: Returns the remainder of division of the given numbers 52 | */ 53 | int op_mod(int a, int b) 54 | { 55 | return (a % b); 56 | } 57 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # Function Pointers Directory 2 | 3 | - Function pointers examples. 4 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTION_POINTERS_H 2 | #define FUNCTION_POINTERS_H 3 | #include 4 | void print_name(char *name, void (*f)(char *)); 5 | void array_iterator(int *array, size_t size, void (*action)(int)); 6 | int int_index(int *array, int size, int (*cmp)(int)); 7 | 8 | #endif /* FUNCTION_POINTERS_H_FILE */ 9 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | 4 | /** 5 | * sum_them_all - function sum all provided arguments 6 | * @n: an integer - number of the undefined arguments 7 | * Return: Returns sum of args 8 | * 0 is returned when an error is encountered 9 | */ 10 | 11 | int sum_them_all(const unsigned int n, ...) 12 | { 13 | va_list list; 14 | unsigned int j, sum = 0; 15 | 16 | va_start(list, n); 17 | if (n != 0) 18 | for (j = 0; j < n; sum += va_arg(list, unsigned int), j++) 19 | ; 20 | va_end(list); 21 | 22 | return (sum); 23 | } 24 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * print_numbers - Function prints all given numbers with separator 8 | * @separator: separates numbers 9 | * @n: integer - number of undefined arguments 10 | */ 11 | 12 | void print_numbers(const char *separator, const unsigned int n, ...) 13 | { 14 | va_list list; 15 | unsigned int idx; 16 | 17 | 18 | va_start(list, n); 19 | 20 | 21 | for (idx = 0; idx < n; idx++) 22 | { 23 | printf("%d", va_arg(list, int)); 24 | 25 | 26 | if (idx != (n - 1) && separator != NULL) 27 | printf("%s", separator); 28 | } 29 | 30 | 31 | printf("\n"); 32 | 33 | 34 | va_end(list); 35 | } 36 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * print_strings - Function prints all strings with separator 8 | * @separator: separates strings 9 | * @n: integer - number of undefined arguments 10 | */ 11 | 12 | void print_strings(const char *separator, const unsigned int n, ...) 13 | { 14 | va_list list; 15 | unsigned int j; 16 | char *c; 17 | va_start(list, n); 18 | 19 | for (j = 0; j < n; j++) 20 | { 21 | c = va_arg(list, char*); 22 | if (c != NULL) 23 | printf("%s", c); 24 | else 25 | printf("(nil)"); 26 | 27 | if (j != n - 1 && separator != NULL) 28 | printf("%s", separator); 29 | } 30 | va_end(list); 31 | 32 | putchar('\n'); 33 | } 34 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * print_str - Function prints string 7 | * @list: arguments from print_all 8 | */ 9 | void print_str(va_list list) 10 | { 11 | char *s = va_arg(list, char *); 12 | 13 | s == NULL ? printf("(nil)") : printf("%s", s); 14 | 15 | } 16 | 17 | /** 18 | * print_float - Function prints float 19 | * @list: arguments from print_all 20 | */ 21 | void print_float(va_list list) 22 | { 23 | printf("%f", va_arg(list, double)); 24 | } 25 | 26 | /** 27 | * print_int - Function prints int 28 | * @list: arguments from print_all 29 | */ 30 | void print_int(va_list list) 31 | { 32 | printf("%d", va_arg(list, int)); 33 | } 34 | 35 | /** 36 | * print_char - Function prints int 37 | * @list: arguments from print_all 38 | */ 39 | void print_char(va_list list) 40 | { 41 | printf("%c", va_arg(list, int)); 42 | } 43 | 44 | /** 45 | * print_all - Function prints any type 46 | * @format: arguments to print 47 | */ 48 | 49 | void print_all(const char * const format, ...) 50 | { 51 | va_list list; 52 | int i = 0, j = 0; 53 | char *sept = ""; 54 | 55 | printTypeStruct printType[] = { 56 | { "i", print_int }, 57 | { "c", print_char }, 58 | { "s", print_str }, 59 | { "f", print_float }, 60 | {NULL, NULL} 61 | }; 62 | 63 | 64 | va_start(list, format); 65 | 66 | while (format && format[i]) 67 | { 68 | j = 0; 69 | while (j < 4) 70 | { 71 | if (*printType[j].type == format[i]) 72 | { 73 | printf("%s", sept); 74 | printType[j].printer(list); 75 | sept = ", "; 76 | break; 77 | } 78 | j++; 79 | } 80 | i++; 81 | } 82 | 83 | printf("\n"); 84 | va_end(list); 85 | } 86 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | Variadic functions documentation 2 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_H 2 | #define VARIADIC_H 3 | #include 4 | 5 | int sum_them_all(const unsigned int n, ...); 6 | void print_numbers(const char *separator, const unsigned int n, ...); 7 | void print_strings(const char *separator, const unsigned int n, ...); 8 | void print_all(const char * const format, ...); 9 | int _putchar(char c); 10 | 11 | void print_int(va_list list); 12 | void print_float(va_list list); 13 | void print_char(va_list list); 14 | void print_str(va_list list); 15 | 16 | /** 17 | * struct printTypeStruct - outline of printTypeStruct structure 18 | * @type: type 19 | * @printer: printer function 20 | */ 21 | typedef struct printTypeStruct 22 | { 23 | char *type; 24 | void (*printer)(va_list); 25 | } printTypeStruct; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_list - Function prints elements of linked list 6 | * @h: linked list 7 | * 8 | * Return: Returns number of nodes 9 | */ 10 | 11 | size_t print_list(const list_t *h) 12 | { 13 | size_t num_nodes = 0; 14 | const list_t *current = h; 15 | 16 | while (current != NULL) 17 | { 18 | printf("[%d] %s\n", current->len, 19 | current->str != NULL ? current->str : "(nil)"); 20 | current = current->next; 21 | num_nodes++; 22 | } 23 | return (num_nodes); 24 | } 25 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * list_len - Function returns the number of elements 6 | * in a linked list_t list 7 | * @h: linked list 8 | * 9 | * Return: Returns number of elements 10 | */ 11 | 12 | size_t list_len(const list_t *h) 13 | { 14 | size_t num_element = 0; 15 | const list_t *current = h; 16 | 17 | while (current != NULL) 18 | { 19 | current = current->next; 20 | num_element++; 21 | } 22 | return (num_element); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * premain - runs before main 5 | */ 6 | void __attribute__ ((constructor)) premain() 7 | { 8 | printf( 9 | "You're beat! and yet, you must allow,\nI bore my house upon my back!\n" 10 | ); 11 | } 12 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | global main 2 | extern printf 3 | 4 | section .text 5 | main: 6 | push rbp 7 | mov rdi, format 8 | mov rsi, message 9 | mov rax, 0 10 | call printf 11 | pop rbp 12 | mov rax, 0 13 | ret 14 | message: db "Hello, Holberton", 0 15 | format: db "%s", 10, 0 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * *add_node - Function adds a node to a linked list head 6 | * @head: Linked list head 7 | * @str: String element of the linked list 8 | * Return: Returns number of elements 9 | */ 10 | 11 | list_t *add_node(list_t **head, const char *str) 12 | { 13 | list_t *nw_node; 14 | int len_str; 15 | 16 | nw_node = (list_t *) malloc(sizeof(list_t)); 17 | 18 | if (nw_node == NULL) 19 | { 20 | free(nw_node); 21 | return (NULL); 22 | } 23 | 24 | nw_node->str = strdup(str); 25 | if (nw_node->str == NULL) 26 | { 27 | free(nw_node); 28 | return (NULL); 29 | } 30 | for (len_str = 0; str[len_str] != '\0'; len_str++) 31 | ; 32 | 33 | nw_node->len = len_str; 34 | nw_node->next = *head; 35 | *head = nw_node; 36 | 37 | return (nw_node); 38 | } 39 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * add_node_end - Function adds a node at end of a linked list 7 | * @head: The character to print 8 | * @str: string for the new node 9 | * 10 | * Return: new node 11 | */ 12 | list_t *add_node_end(list_t **head, const char *str) 13 | { 14 | list_t *current; 15 | list_t *nw_node; 16 | int c; 17 | 18 | current = *head; 19 | while (current && current->next != NULL) 20 | current = current->next; 21 | 22 | for (c = 0; str[c] != '\0'; c++) 23 | ; 24 | 25 | nw_node = malloc(sizeof(list_t)); 26 | if (nw_node == NULL) 27 | { 28 | free(nw_node); 29 | return (NULL); 30 | } 31 | nw_node->str = strdup(str); 32 | if (nw_node->str == NULL) 33 | { 34 | free(nw_node); 35 | return (NULL); 36 | } 37 | nw_node->len = c; 38 | nw_node->next = NULL; 39 | 40 | if (current) 41 | current->next = nw_node; 42 | else 43 | *head = nw_node; 44 | return (nw_node); 45 | } 46 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - Function frees a list_t list 5 | * @head: Linked list head 6 | */ 7 | void free_list(list_t *head) 8 | { 9 | list_t *current; 10 | list_t *nxxt; 11 | 12 | current = head; 13 | 14 | while (current != NULL) 15 | { 16 | nxxt = current->next; 17 | free(current->str); 18 | free(current); 19 | current = nxxt; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # Singly linked Lists 2 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | /** 6 | * struct list_s - singly linked list 7 | * @str: string - (malloc'ed string) 8 | * @len: length of the string 9 | * @next: points to the next node 10 | * 11 | * Description: singly linked list node structure 12 | * for Holberton project 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 | 28 | #endif 29 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * print_listint - Function prints elements of linked lists 6 | * @h: The head 7 | * Return: Returns a string length 8 | */ 9 | 10 | size_t print_listint(const listint_t *h) 11 | { 12 | size_t no_nodes = 0; 13 | 14 | const listint_t *curr_v = h; 15 | 16 | while (curr_v != NULL) 17 | { 18 | printf("%i\n", curr_v->n); 19 | curr_v = curr_v->next; 20 | no_nodes++; 21 | } 22 | return (no_nodes); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * listint_len - Function returns no. of elements in a linked list 4 | * @h: The head 5 | * Return: Returns no. of elements 6 | */ 7 | size_t listint_len(const listint_t *h) 8 | { 9 | size_t no_nodes = 0; 10 | 11 | while (h) 12 | { 13 | no_nodes++; 14 | h = h->next; 15 | } 16 | 17 | return (no_nodes); 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | /** 4 | * delete_nodeint_at_index - Function deletes a node at a certain position 5 | * @head: The linked list head 6 | * @index: The index to delete 7 | * Return: Returns 1 if successful, or -1 if failed 8 | */ 9 | 10 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 11 | { 12 | listint_t *curr; 13 | listint_t *tmp; 14 | 15 | if (head == NULL || (*head) == NULL) 16 | return (-1); 17 | 18 | curr = *head; 19 | if (index == 0) 20 | { 21 | if ((*head)->next) 22 | (*head) = (*head)->next; 23 | else 24 | (*head) = NULL; 25 | free(curr); 26 | return (1); 27 | } 28 | 29 | while (index != 1) 30 | { 31 | if (curr->next == NULL) 32 | return (-1); 33 | 34 | curr = curr->next; 35 | --index; 36 | } 37 | tmp = curr->next; 38 | if (curr->next->next) 39 | curr->next = curr->next->next; 40 | else 41 | curr->next = NULL; 42 | free(tmp); 43 | 44 | return (1); 45 | } 46 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * *reverse_listint - Function reverses order of nodes 4 | * @head: Head pointer 5 | * Return: Returns always successful 6 | */ 7 | listint_t *reverse_listint(listint_t **head) 8 | { 9 | listint_t *fwd; 10 | listint_t *backward = NULL; 11 | 12 | if (head == NULL) 13 | return (NULL); 14 | fwd = *head; 15 | while (fwd != NULL) 16 | { 17 | fwd = fwd->next; 18 | fwd = (*head)->next; 19 | (*head)->next = backward; 20 | backward = *head; 21 | *head = fwd; 22 | } 23 | *head = backward; 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * _r - Function reallocates memory to the nodes in a linked list 7 | * for an array of pointers 8 | * @list: Old list to append 9 | * @size: new list size 10 | * @new: new node to be added to the list 11 | * 12 | * Return: Returns pointer to the new list 13 | */ 14 | const listint_t **_r(const listint_t **list, size_t size, const listint_t *new) 15 | { 16 | const listint_t **new_lst; 17 | size_t j; 18 | 19 | new_lst = malloc(size * sizeof(listint_t *)); 20 | if (new_lst == NULL) 21 | { 22 | free(list); 23 | exit(98); 24 | } 25 | for (j = 0; j < size - 1; j++) 26 | new_lst[j] = list[j]; 27 | new_lst[j] = new; 28 | free(list); 29 | return (new_lst); 30 | } 31 | 32 | /** 33 | * print_listint_safe - Function prints a linked list of listint_t 34 | * @head: The pointer to the list start 35 | * 36 | * Return: Returns number of nodes in the list 37 | */ 38 | size_t print_listint_safe(const listint_t *head) 39 | { 40 | size_t j, num = 0; 41 | const listint_t **list = NULL; 42 | 43 | while (head != NULL) 44 | { 45 | for (j = 0; j < num; j++) 46 | { 47 | if (head == list[j]) 48 | { 49 | printf("-> [%p] %d\n", (void *)head, head->n); 50 | free(list); 51 | return (num); 52 | } 53 | } 54 | num++; 55 | list = _r(list, num, head); 56 | printf("[%p] %d\n", (void *)head, head->n); 57 | head = head->next; 58 | } 59 | free(list); 60 | return (num); 61 | } 62 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | #include 4 | 5 | /** 6 | * _ra - Function reallocates memory to the nodes 7 | * in a linked list for a pointers' array 8 | * @list: The old list to append 9 | * @size: New list size 10 | * @new: The new node to add to list 11 | * 12 | * Return: Returns a pointer to the new list 13 | */ 14 | listint_t **_ra(listint_t **list, size_t size, listint_t *new) 15 | { 16 | listint_t **new_lst; 17 | size_t j; 18 | 19 | new_lst = malloc(size * sizeof(listint_t *)); 20 | if (new_lst == NULL) 21 | { 22 | free(list); 23 | exit(98); 24 | } 25 | for (j = 0; j < size - 1; j++) 26 | new_lst[j] = list[j]; 27 | new_lst[j] = new; 28 | free(list); 29 | return (new_lst); 30 | } 31 | 32 | /** 33 | * free_listint_safe - frees a listint_t linked list. 34 | * @head: double pointer to the start of the list 35 | * 36 | * Return: the number of nodes in the list 37 | */ 38 | size_t free_listint_safe(listint_t **head) 39 | { 40 | size_t j, number = 0; 41 | listint_t **list = NULL; 42 | listint_t *next; 43 | 44 | if (head == NULL || *head == NULL) 45 | return (number); 46 | while (*head != NULL) 47 | { 48 | for (j = 0; j < number; j++) 49 | { 50 | if (*head == list[j]) 51 | { 52 | *head = NULL; 53 | free(list); 54 | return (number); 55 | } 56 | } 57 | number++; 58 | list = _ra(list, number, *head); 59 | next = (*head)->next; 60 | free(*head); 61 | *head = next; 62 | } 63 | free(list); 64 | return (number); 65 | } 66 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - Function finds the loop contained in 5 | * a linked list of a listint_t 6 | * @head: A pointer for the head of the listint_t list 7 | * 8 | * Return: Returns NULL if there is no loop or else 9 | * the address of the node where the loop starts. 10 | */ 11 | listint_t *find_listint_loop(listint_t *head) 12 | { 13 | listint_t *slow_tort, *fast_hare; 14 | 15 | if (head == NULL || head->next == NULL) 16 | return (NULL); 17 | 18 | slow_tort = head->next; 19 | fast_hare = (head->next)->next; 20 | 21 | while (fast_hare) 22 | { 23 | if (slow_tort == fast_hare) 24 | { 25 | slow_tort = head; 26 | 27 | while (slow_tort != fast_hare) 28 | { 29 | slow_tort = slow_tort->next; 30 | fast_hare = fast_hare->next; 31 | } 32 | 33 | return (slow_tort); 34 | } 35 | 36 | slow_tort = slow_tort->next; 37 | fast_hare = (fast_hare->next)->next; 38 | } 39 | 40 | return (NULL); 41 | } 42 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | **add_nodeint - Function adds a node at the start of a list 5 | *@head: The head 6 | *@n: An int 7 | *Return: Returns null or a new element's address 8 | */ 9 | 10 | listint_t *add_nodeint(listint_t **head, const int n) 11 | { 12 | listint_t *new_ad; 13 | 14 | new_ad = malloc(sizeof(listint_t)); 15 | if (new_ad == NULL) 16 | return (NULL); 17 | new_ad->n = n; 18 | new_ad->next = *head; 19 | *head = new_ad; 20 | return (new_ad); 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | **add_nodeint_end - Function adds a node at the end of a list_t 5 | *@head: The head 6 | *@n: An int 7 | *Return: Returns null or a new_ad element's address 8 | */ 9 | 10 | listint_t *add_nodeint_end(listint_t **head, const int n) 11 | { 12 | listint_t *new_ad; 13 | listint_t *tmp = *head; 14 | 15 | new_ad = malloc(sizeof(listint_t)); 16 | 17 | if (!new_ad) 18 | return (NULL); 19 | new_ad->n = n; 20 | 21 | if (tmp == NULL) 22 | *head = new_ad; 23 | else 24 | { 25 | while (tmp->next != NULL) 26 | { 27 | tmp = tmp->next; 28 | } 29 | tmp->next = new_ad; 30 | } 31 | return (new_ad); 32 | } 33 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | *free_listint - Function frees a memory location 4 | *@head: Head of the linked listint_t 5 | */ 6 | 7 | void free_listint(listint_t *head) 8 | { 9 | listint_t *tmp; 10 | 11 | if (head == NULL) 12 | 13 | return; 14 | 15 | while (head) 16 | 17 | { 18 | tmp = head->next; 19 | free(head); 20 | head = tmp; 21 | } 22 | free(head); 23 | } 24 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - Function frees a listint_t list 5 | * Description: The head will point to NULL at the end. 6 | * Therefore, the tail always points to NULL, we will 7 | * move a temp pointer, and free its memory if its not NULL 8 | * Untill we reach the tail 9 | * @head: The head of the linked list 10 | */ 11 | 12 | void free_listint2(listint_t **head) 13 | { 14 | listint_t *curr; 15 | 16 | if (head == NULL) 17 | return; 18 | 19 | while (*head != NULL) 20 | { 21 | curr = *head; 22 | *head = (*head)->next; 23 | free(curr); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - Function deletes a listint_t linked list's head node 5 | * @head: The head of a linked list 6 | * Return: Returns the head node’s data --> n. 7 | */ 8 | 9 | int pop_listint(listint_t **head) 10 | { 11 | listint_t *curr; 12 | int p; 13 | 14 | if (head == NULL) 15 | return (0); 16 | 17 | curr = *head; 18 | if (curr == NULL) 19 | return (0); 20 | 21 | p = curr->n; 22 | *head = curr->next; 23 | free(curr); 24 | return (p); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - Function returns nth node of a listint_t linked list. 5 | * @head: The head of linked list 6 | * @index: node index to return 7 | * Return: Returns a node 8 | */ 9 | 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | listint_t *curr; 13 | 14 | if (head == NULL) 15 | return (0); 16 | 17 | curr = head; 18 | 19 | while (index != 0) 20 | { 21 | curr = curr->next; 22 | index--; 23 | if (curr == NULL) 24 | return (0); /*Out of range*/ 25 | } 26 | 27 | return (curr); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - Function returns sum of all the given data 5 | * (n) of a listint_t linked list 6 | * @head: The linked list head 7 | * Return: Returns sum 8 | */ 9 | 10 | int sum_listint(listint_t *head) 11 | { 12 | listint_t *curr; 13 | int sum = 0; 14 | 15 | if (head == NULL) 16 | return (0); 17 | 18 | curr = head; 19 | 20 | while (curr != NULL) 21 | { 22 | sum += curr->n; 23 | curr = curr->next; 24 | } 25 | 26 | return (sum); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - Function inserts a new node at a certain position 5 | * @head: The head of linked list 6 | * @idx: The index to insert the node 7 | * @n: The new node data 8 | * Return: Returns new node 9 | */ 10 | 11 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 12 | { 13 | listint_t *curr; 14 | listint_t *new; 15 | 16 | if (head == NULL) 17 | return (0); 18 | new = malloc(sizeof(listint_t)); 19 | if (!new) 20 | return (0); 21 | 22 | new->next = NULL; 23 | new->n = n; 24 | 25 | if (idx == 0) 26 | { 27 | new->next = *head; 28 | (*head) = new; 29 | return (new); 30 | } 31 | 32 | curr = *head; 33 | 34 | while (idx != 1) 35 | { 36 | curr = curr->next; 37 | --idx; 38 | if (curr == NULL) 39 | { 40 | free(new); 41 | return (NULL); 42 | } 43 | } 44 | new->next = curr->next; 45 | curr->next = new; 46 | 47 | return (new); 48 | } 49 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # More singly linked lists 2 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | #include 4 | #include 5 | #include 6 | /** 7 | * struct listint_s - singly linked list 8 | * @n: integer 9 | * @next: points to the next node 10 | * 11 | * Description: singly linked list node structure 12 | * 13 | */ 14 | typedef struct listint_s 15 | { 16 | int n; 17 | struct listint_s *next; 18 | } listint_t; 19 | size_t print_listint(const listint_t *h); 20 | size_t listint_len(const listint_t *h); 21 | listint_t *add_nodeint(listint_t **head, const int n); 22 | listint_t *add_nodeint_end(listint_t **head, const int n); 23 | void free_listint(listint_t *head); 24 | void free_listint2(listint_t **head); 25 | int pop_listint(listint_t **head); 26 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 27 | int sum_listint(listint_t *head); 28 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 29 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 30 | listint_t *reverse_listint(listint_t **head); 31 | size_t print_listint_safe(const listint_t *head); 32 | size_t free_listint_safe(listint_t **h); 33 | listint_t *find_listint_loop(listint_t *head); 34 | #endif 35 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - Function converts a binary number to unsigned int 5 | * @b: pointer to a string of a binary numer 6 | * 7 | * Return: Returns unsigned int with decimal value of 8 | * binary number on success, or 0 if error 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | int j; 13 | unsigned int numb; 14 | 15 | numb = 0; 16 | if (!b) 17 | return (0); 18 | for (j = 0; b[j] != '\0'; j++) 19 | { 20 | if (b[j] != '0' && b[j] != '1') 21 | return (0); 22 | } 23 | for (j = 0; b[j] != '\0'; j++) 24 | { 25 | numb <<= 1; 26 | if (b[j] == '1') 27 | numb += 1; 28 | } 29 | return (numb); 30 | } 31 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow - Function prints binary (base ^ power) 5 | * @base: The base 6 | * @power: The power 7 | * 8 | * Return: Returns binary 9 | */ 10 | unsigned long int _pow(unsigned int base, unsigned int power) 11 | { 12 | unsigned long int numb; 13 | unsigned int j; 14 | 15 | numb = 1; 16 | for (j = 1; j <= power; j++) 17 | numb *= base; 18 | return (numb); 19 | } 20 | 21 | /** 22 | * print_binary - Function prints a number in binary form 23 | * @n: Input number 24 | * 25 | * Return: Returns void 26 | */ 27 | void print_binary(unsigned long int n) 28 | { 29 | unsigned long int factor, checker; 30 | char flag; 31 | 32 | flag = 0; 33 | factor = _pow(2, sizeof(unsigned long int) * 8 - 1); 34 | while (factor != 0) 35 | { 36 | checker = n & factor; 37 | if (checker == factor) 38 | { 39 | flag = 1; 40 | _putchar('1'); 41 | } 42 | else if (flag == 1 || factor == 1) 43 | { 44 | _putchar('0'); 45 | } 46 | factor >>= 1; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - Function checks the endianness 5 | * 6 | * Return: Returns 0 if big endian, or 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | int a; 11 | char *b; 12 | 13 | a = 1; 14 | b = (char *)&a; 15 | return (*b); 16 | } 17 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - Function returns a bit value at a given index 5 | * @n: Input number to check 6 | * @index: Index to check 7 | * 8 | * Return: Returns the bit value, or -1 on fail 9 | */ 10 | int get_bit(unsigned long int n, unsigned int index) 11 | { 12 | unsigned long int factor, checker; 13 | 14 | if (index > (sizeof(unsigned long int) * 8 - 1)) 15 | return (-1); 16 | factor = 1 << index; 17 | checker = n & factor; 18 | if (checker == factor) 19 | return (1); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - Function sets the value of a bit to 1 at an index. 5 | * @n: Input number to set 6 | * @index: The index at which to set bit 7 | * 8 | * Return: Returns 1 if it successful, or -1 if failed 9 | */ 10 | int set_bit(unsigned long int *n, unsigned int index) 11 | { 12 | unsigned long int set; 13 | 14 | if (index > (sizeof(unsigned long int) * 8 - 1)) 15 | return (-1); 16 | set = 1 << index; 17 | *n = *n | set; 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - Function sets the value of a bit to 0 at an index. 5 | * @n: Input number to set 6 | * @index: Returns index of the number to set bit 7 | * 8 | * Return: Returns 1 on success, or -1 if on error 9 | */ 10 | 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned long int mySet; 14 | 15 | if (index > (sizeof(unsigned long int) * 8 - 1)) 16 | return (-1); 17 | mySet = ~(1 << index); 18 | *n = *n & mySet; 19 | return (1); 20 | } 21 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - Function prints no of different bits between two numbers 5 | * @n: first number 6 | * @m: second number 7 | * 8 | * Return: Returns no. of bits needed to flip 9 | * to change from one no. to another. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned long int diff, checker; 14 | unsigned int count, j; 15 | 16 | count = 0; 17 | diff = n ^ m; 18 | checker = 1; 19 | 20 | for (j = 0; j < (sizeof(unsigned long int) * 8); j++) 21 | { 22 | if (checker == (diff & checker)) 23 | count++; 24 | checker <<= 1; 25 | } 26 | return (count); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # Bit Manipulation 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/crackme3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x14-bit_manipulation/crackme3 -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H 2 | #define _MAIN_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int _putchar(char c); 9 | 10 | unsigned int binary_to_uint(const char *b); 11 | 12 | void print_binary(unsigned long int n); 13 | 14 | int get_bit(unsigned long int n, unsigned int index); 15 | 16 | int set_bit(unsigned long int *n, unsigned int index); 17 | 18 | int clear_bit(unsigned long int *n, unsigned int index); 19 | 20 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 21 | 22 | int get_endianness(void); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | /** 6 | * read_textfile - Function reads a file_text file and 7 | * prints it to the POSIX standard output 8 | * @filename: The file 9 | * @letters: Number of letters 10 | * Description: Function opens a file, reads up to `letters` characters 11 | * from it, and writes those characters to the standard output. It ensures 12 | * that the resources are freed and files are closed in case of any errors. 13 | * Return: Returns the actual number of letters it could read and print, or 0 if 14 | * an operation fails. 15 | */ 16 | ssize_t read_textfile(const char *filename, size_t letters) 17 | { 18 | int fd; 19 | 20 | ssize_t nread, nwritten; 21 | char *buffer; 22 | 23 | if (filename == NULL) 24 | return (0); 25 | 26 | buffer = malloc(sizeof(char) * letters); 27 | if (buffer == NULL) 28 | return (0); 29 | 30 | fd = open(filename, O_RDONLY); 31 | if (fd == -1) 32 | { 33 | free(buffer); 34 | return (0); 35 | } 36 | 37 | nread = read(fd, buffer, letters); 38 | if (nread == -1) 39 | { 40 | close(fd); 41 | free(buffer); 42 | return (0); 43 | } 44 | 45 | nwritten = write(STDOUT_FILENO, buffer, nread); 46 | close(fd); 47 | free(buffer); 48 | 49 | if (nwritten != nread) 50 | return (0); 51 | 52 | return (nread); 53 | } 54 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strlen - String Length 4 | * @s: input char 5 | * Return: Returns length of a string 6 | **/ 7 | int _strlen(char *s) 8 | { 9 | int j = 0; 10 | 11 | while (s[j]) 12 | { 13 | j++; 14 | } 15 | return (j); 16 | } 17 | /** 18 | * create_file - Function creates a file 19 | * @filename: The file 20 | * @text_content: File content 21 | * Return: Returns 1 on success, or -1 on failure 22 | */ 23 | int create_file(const char *filename, char *text_content) 24 | { 25 | ssize_t letters_num; 26 | int file; 27 | 28 | if (!filename) 29 | return (1); 30 | file = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 31 | if (file == -1) 32 | { 33 | return (-1); 34 | } 35 | if (text_content) 36 | { 37 | letters_num = write(file, text_content, _strlen(text_content)); 38 | if (letters_num == -1) 39 | { 40 | close(file); 41 | return (-1); 42 | } 43 | } 44 | close(file); 45 | return (1); 46 | } 47 | -------------------------------------------------------------------------------- /0x15-file_io/100-elf_header.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - main function 4 | * @argc: argument count 5 | * @argv: argument vector 6 | * Return: Returns 0 if success 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | register int fd, r, c; 11 | Elf64_Ehdr *header; 12 | 13 | if (argc != 2) 14 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"), exit(98); 15 | header = malloc(sizeof(Elf64_Ehdr)); 16 | if (!header) 17 | dprintf(STDERR_FILENO, "Malloc error\n"), exit(98); 18 | fd = open(argv[1], O_RDONLY); 19 | if (fd < 0) 20 | { 21 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 22 | exit(98); 23 | } 24 | r = read(fd, header, sizeof(Elf64_Ehdr)); 25 | if (r < 0) 26 | { 27 | free(header); 28 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 29 | exit(98); 30 | } 31 | validate_elf(header->e_ident); 32 | print_magic(header->e_ident); 33 | print_class(header->e_ident); 34 | print_data(header->e_ident); 35 | print_version(header->e_ident); 36 | print_osabi(header->e_ident); 37 | printf(" ABI Version: "); 38 | printf("%i\n", header->e_ident[EI_ABIVERSION]); 39 | print_type(header->e_type, header->e_ident); 40 | print_entry(header->e_entry, header->e_ident); 41 | free(header); 42 | c = close(fd); 43 | if (c) 44 | dprintf(STDERR_FILENO, "Error: Can't close fd\n"), exit(98); 45 | return (0); 46 | } 47 | 48 | /** 49 | * validate_elf - helper func checking if input is valid elf file 50 | * @e_ident: pointer to char array 51 | */ 52 | void validate_elf(unsigned char *e_ident) 53 | { 54 | if (e_ident[0] == 0x7f && e_ident[1] == 'E' && 55 | e_ident[2] == 'L' && e_ident[3] == 'F') 56 | { 57 | printf("ELF Header:\n"); 58 | } else 59 | dprintf(STDERR_FILENO, "Error: Not valid ELF\n"), exit(98); 60 | } 61 | 62 | /** 63 | * print_magic - Function prints ELF's magic number 64 | * @e_ident: pointer to char array 65 | */ 66 | void print_magic(unsigned char *e_ident) 67 | { 68 | register int i; 69 | 70 | printf(" Magic: "); 71 | for (i = 0; i < EI_NIDENT - 1; i++) 72 | printf("%02x ", e_ident[i]); 73 | printf("%02x\n", e_ident[i]); 74 | } 75 | /** 76 | * print_class - Function prints ELF's class 77 | * @e_ident: pointer to char array 78 | */ 79 | void print_class(unsigned char *e_ident) 80 | { 81 | printf(" Class: "); 82 | switch (e_ident[EI_CLASS]) 83 | { 84 | case ELFCLASSNONE: 85 | printf("This class is invalid\n"); 86 | break; 87 | case ELFCLASS32: 88 | printf("ELF32\n"); 89 | break; 90 | case ELFCLASS64: 91 | printf("ELF64\n"); 92 | break; 93 | default: 94 | printf("\n", e_ident[EI_CLASS]); 95 | } 96 | } 97 | /** 98 | * print_data - Function prints ELF's data 99 | * @e_ident: pointer to char array 100 | */ 101 | void print_data(unsigned char *e_ident) 102 | { 103 | printf(" Data: "); 104 | switch (e_ident[EI_DATA]) 105 | { 106 | case ELFDATANONE: 107 | printf("Unknown data format\n"); 108 | break; 109 | case ELFDATA2LSB: 110 | printf("2's complement, little endian\n"); 111 | break; 112 | case ELFDATA2MSB: 113 | printf("2's complement, big endian\n"); 114 | break; 115 | default: 116 | printf("\n", e_ident[EI_DATA]); 117 | } 118 | } 119 | /** 120 | * print_version - Function prints ELF's version 121 | * @e_ident: pointer to char array 122 | */ 123 | void print_version(unsigned char *e_ident) 124 | { 125 | printf(" Version: "); 126 | if (e_ident[EI_VERSION] == EV_CURRENT) 127 | printf("%i (current)\n", EV_CURRENT); 128 | else 129 | printf("%i\n", e_ident[EI_VERSION]); 130 | } 131 | 132 | /** 133 | * print_osabi - Function prints ELF's osabi 134 | * @e_ident: pointer to char array 135 | */ 136 | void print_osabi(unsigned char *e_ident) 137 | { 138 | printf(" OS/ABI: "); 139 | switch (e_ident[EI_OSABI]) 140 | { 141 | case ELFOSABI_SYSV: 142 | printf("UNIX - System V\n"); 143 | break; 144 | case ELFOSABI_HPUX: 145 | printf("UNIX - HP-UX\n"); 146 | break; 147 | case ELFOSABI_NETBSD: 148 | printf("UNIX - NetBSD\n"); 149 | break; 150 | case ELFOSABI_LINUX: 151 | printf("UNIX - Linux\n"); 152 | break; 153 | case ELFOSABI_SOLARIS: 154 | printf("UNIX - Solaris\n"); 155 | break; 156 | case ELFOSABI_IRIX: 157 | printf("UNIX - IRIX\n"); 158 | break; 159 | case ELFOSABI_FREEBSD: 160 | printf("UNIX - FreeBSD\n"); 161 | break; 162 | case ELFOSABI_TRU64: 163 | printf("UNIX - TRU64\n"); 164 | break; 165 | case ELFOSABI_ARM: 166 | printf("ARM\n"); 167 | break; 168 | case ELFOSABI_STANDALONE: 169 | printf("Standalone App\n"); 170 | break; 171 | default: 172 | printf("\n", e_ident[EI_OSABI]); 173 | } 174 | } 175 | 176 | /** 177 | * print_type - Function prints ELF's type 178 | * @e_type: e_type address 179 | * @e_ident: pointer to char array 180 | */ 181 | void print_type(unsigned int e_type, unsigned char *e_ident) 182 | { 183 | if (e_ident[EI_DATA] == ELFDATA2MSB) 184 | e_type = e_type >> 8; 185 | 186 | printf(" Type: "); 187 | switch (e_type) 188 | { 189 | case ET_NONE: 190 | printf("NONE (Unknown type)\n"); 191 | break; 192 | case ET_REL: 193 | printf("REL (Relocatable file)\n"); 194 | break; 195 | case ET_EXEC: 196 | printf("EXEC (Executable file)\n"); 197 | break; 198 | case ET_DYN: 199 | printf("DYN (Shared object file)\n"); 200 | break; 201 | case ET_CORE: 202 | printf("CORE (Core file)\n"); 203 | break; 204 | default: 205 | printf("\n", e_type); 206 | } 207 | } 208 | 209 | /** 210 | * print_entry - Function prints ELF's entry 211 | * @e_entry: The e_entry address 212 | * @e_ident: pointer to char array 213 | */ 214 | void print_entry(unsigned int e_entry, unsigned char *e_ident) 215 | { 216 | if (e_ident[EI_DATA] == ELFDATA2MSB) 217 | e_entry = lit_to_big_endian(e_entry); 218 | 219 | printf(" Entry point address: "); 220 | printf("%#x\n", (unsigned int)e_entry); 221 | } 222 | 223 | /** 224 | * lit_to_big_endian - Function converts little endian hexes to big hexes 225 | * @x: input u_int 226 | * Return: output u_int 227 | */ 228 | unsigned int lit_to_big_endian(unsigned int x) 229 | { 230 | return (((x >> 24) & 0x000000ff) | 231 | ((x >> 8) & 0x0000ff00) | 232 | ((x << 8) & 0x00ff0000) | 233 | ((x << 24) & 0xff000000)); 234 | } 235 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * append_text_to_file - Function appends text at the file end 4 | * @filename: Name of the data file 5 | * @text_content: The null terminated string to append at the file end. 6 | * Return: Returns 1 on success, -1 on failure 7 | */ 8 | int append_text_to_file(const char *filename, char *text_content) 9 | { 10 | int leng; 11 | int file_d; 12 | int bytes_written; 13 | 14 | if (filename == NULL) 15 | return (-1); 16 | 17 | if (text_content == NULL) 18 | return (1); 19 | 20 | file_d = open(filename, O_WRONLY | O_APPEND); 21 | if (file_d == -1) 22 | return (-1); 23 | 24 | leng = 0; 25 | while (text_content[leng]) 26 | leng++; 27 | 28 | bytes_written = write(file_d, text_content, leng); 29 | if (bytes_written == -1) 30 | { 31 | close(file_d); 32 | return (-1); 33 | } 34 | 35 | close(file_d); 36 | return (1); 37 | } 38 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - Function copies info from file_from to file_to file 4 | * @av: An array of arguments 5 | * @ac: An array of arguments 6 | * Return: Returns always 0. 7 | */ 8 | int main(int ac, char **av) 9 | { 10 | int file_from, file_to; 11 | ssize_t l_read = 1024, l_write, close_file; 12 | char content[1024]; 13 | 14 | if (ac != 3) 15 | { dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"); 16 | exit(97); } 17 | file_from = open(av[1], O_RDONLY); 18 | if (file_from == -1) 19 | { dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", av[1]), 20 | exit(98); } 21 | file_to = open(av[2], O_CREAT | O_WRONLY | O_TRUNC, 0664); 22 | if (file_to == -1) 23 | { dprintf(STDERR_FILENO, "Error: Can't write to %s\n", av[2]); 24 | exit(99); } 25 | while (l_read == 1024) 26 | { 27 | l_read = read(file_from, content, 1024); 28 | if (l_read == -1) 29 | { dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", av[1]); 30 | exit(98); } 31 | l_write = write(file_to, content, l_read); 32 | if (l_write == -1) 33 | { dprintf(STDERR_FILENO, "Error: Can't write to %s\n", av[2]); 34 | exit(99); } 35 | } 36 | close_file = close(file_from); 37 | if (close_file == -1) 38 | { dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 39 | exit(100); } 40 | close_file = close(file_to); 41 | if (close_file == -1) 42 | { dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_to); 43 | exit(100); } 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | # File Input and Output 2 | 3 | - File Input Output. -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 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 | int _putchar(char c); 16 | void print_magic(unsigned char *e_ident); 17 | void print_class(unsigned char *e_ident); 18 | void print_data(unsigned char *e_ident); 19 | void print_version(unsigned char *e_ident); 20 | void print_osabi(unsigned char *e_ident); 21 | void print_type(unsigned int e_type, unsigned char *e_ident); 22 | void validate_elf(unsigned char *e_ident); 23 | unsigned int lit_to_big_endian(unsigned int x); 24 | void print_entry(unsigned int e_type, unsigned char *e_ident); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * print_dlistint - Function prints all the elements of a doubly linked list 7 | * @h: The doubly linked list to be printed 8 | * Return: Returns number of nodes of the doubly linked list 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | int leng = 0; 13 | 14 | while (h != NULL) 15 | { 16 | printf("%d\n", h->n); 17 | ++leng; 18 | h = h->next; 19 | } 20 | 21 | return (leng); 22 | } 23 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * dlistint_len - Function counts the number of 6 | * elements in a doubly linked list 7 | * @h: Doubly linked list 8 | * 9 | * Return: Returns number of elements in the doubly linked list 10 | */ 11 | size_t dlistint_len(const dlistint_t *h) 12 | { 13 | int leng = 0; 14 | 15 | while (h != NULL) 16 | { 17 | ++leng; 18 | h = h->next; 19 | } 20 | 21 | return (leng); 22 | } 23 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | echo -n "z" > 100-password -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Function generates and prints 7 | * passwords for the crackme5 executable. 8 | * @argc: Number of arguments supplied to the program. 9 | * @argv: Array of pointers to the arguments. 10 | * 11 | * Return: Always 0. 12 | */ 13 | int main(int __attribute__((__unused__)) argc, char *argv[]) 14 | { 15 | char passwd[7], *codex; 16 | int leng = strlen(argv[1]), i, tmp; 17 | 18 | codex = "A-CHRDw87lNS0E9B2TibgpnMVys5XzvtOGJcYLU+4mjW6fxqZeF3Qa1rPhdKIouk"; 19 | 20 | tmp = (leng ^ 59) & 63; 21 | passwd[0] = codex[tmp]; 22 | 23 | tmp = 0; 24 | for (i = 0; i < leng; i++) 25 | tmp += argv[1][i]; 26 | passwd[1] = codex[(tmp ^ 79) & 63]; 27 | 28 | tmp = 1; 29 | for (i = 0; i < leng; i++) 30 | tmp *= argv[1][i]; 31 | passwd[2] = codex[(tmp ^ 85) & 63]; 32 | 33 | tmp = 0; 34 | for (i = 0; i < leng; i++) 35 | { 36 | if (argv[1][i] > tmp) 37 | tmp = argv[1][i]; 38 | } 39 | srand(tmp ^ 14); 40 | passwd[3] = codex[rand() & 63]; 41 | 42 | tmp = 0; 43 | for (i = 0; i < leng; i++) 44 | tmp += (argv[1][i] * argv[1][i]); 45 | passwd[4] = codex[(tmp ^ 239) & 63]; 46 | 47 | for (i = 0; i < argv[1][0]; i++) 48 | tmp = rand(); 49 | passwd[5] = codex[(tmp ^ 229) & 63]; 50 | 51 | passwd[6] = '\0'; 52 | printf("%s", passwd); 53 | return (0); 54 | } 55 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * add_dnodeint - Function adds a new node at 7 | * the beginning of a doubly linked list 8 | * @head: Head of the doubly linked list 9 | * @n: The int number to place in the new node 10 | * 11 | * Return: Teturns new head of the doubly linked list 12 | */ 13 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 14 | { 15 | dlistint_t *new_node = NULL; 16 | 17 | new_node = malloc(sizeof(dlistint_t)); 18 | if (new_node == NULL) 19 | return (NULL); 20 | 21 | new_node->n = n; 22 | if (*head) 23 | { 24 | new_node->next = *head; 25 | new_node->prev = (*head)->prev; 26 | (*head)->prev = new_node; 27 | *head = new_node; 28 | return (*head); 29 | } 30 | 31 | new_node->next = *head; 32 | new_node->prev = NULL; 33 | *head = new_node; 34 | return (*head); 35 | } 36 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * add_dnodeint_end - Function adds a new node at 6 | * the end of a doubly linked list 7 | * @head: Head of the doubly linked list 8 | * @n: Int number to place in the new node 9 | * 10 | * Return: Returns new head of the list 11 | */ 12 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 13 | { 14 | dlistint_t *current_node = NULL, *new_node = NULL; 15 | 16 | new_node = malloc(sizeof(dlistint_t)); 17 | if (new_node == NULL) 18 | return (NULL); 19 | 20 | new_node->n = n; 21 | if (*head) 22 | { 23 | current_node = *head; 24 | while (current_node->next != NULL) 25 | current_node = current_node->next; 26 | 27 | new_node->next = NULL; 28 | new_node->prev = current_node; 29 | current_node->next = new_node; 30 | return (new_node); 31 | } 32 | 33 | new_node->next = *head; 34 | new_node->prev = NULL; 35 | *head = new_node; 36 | return (*head); 37 | } 38 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * free_dlistint - Function frees a doubly linked list 7 | * @head: Head of the list 8 | * 9 | * Return: Returns nothing 10 | */ 11 | void free_dlistint(dlistint_t *head) 12 | { 13 | dlistint_t *current_node = head, *next = NULL; 14 | 15 | while (current_node != NULL) 16 | { 17 | next = current_node->next; 18 | free(current_node); 19 | current_node = next; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * get_dnodeint_at_index - Function gets a node from a doubly linked list 6 | * @head: The head of the list 7 | * @index: Index to find in the list 8 | * 9 | * Return: Returns the specific node of the list 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | dlistint_t *current_node = head; 14 | unsigned int iteration_times = 0; 15 | 16 | if (head) 17 | { 18 | while (current_node != NULL) 19 | { 20 | if (iteration_times == index) 21 | return (current_node); 22 | 23 | current_node = current_node->next; 24 | ++iteration_times; 25 | } 26 | } 27 | 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lists.h" 3 | 4 | /** 5 | * sum_dlistint - Function finds sum of all 6 | * data (n) in a doubly linked list 7 | * @head: Head of the list 8 | * 9 | * Return: Returns sum of all data 10 | */ 11 | int sum_dlistint(dlistint_t *head) 12 | { 13 | dlistint_t *current_node = head; 14 | int sum = 0; 15 | 16 | if (head) 17 | { 18 | while (current_node != NULL) 19 | { 20 | sum += current_node->n; 21 | current_node = current_node->next; 22 | } 23 | } 24 | 25 | return (sum); 26 | } 27 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * insert_dnodeint_at_index - Function inserts a new node at a given position 7 | * @h: Head of the doubly linked list 8 | * @idx: The index in which insert the new node 9 | * @n: Number to insert in the new node 10 | * 11 | * Return: Returns address of the new node, or NULL if it failed 12 | */ 13 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 14 | { 15 | dlistint_t *current_node = NULL, *new_node = NULL; 16 | unsigned int iteration_times = 0, leng = 0; 17 | 18 | if (h == NULL) 19 | return (NULL); 20 | 21 | if (*h == NULL && idx == 0) 22 | return (add_dnodeint(h, n)); 23 | 24 | leng = dlistint_len(*h); 25 | if (idx == 0) 26 | return (add_dnodeint(h, n)); 27 | else if (leng == idx) 28 | return (add_dnodeint_end(h, n)); 29 | 30 | current_node = *h; 31 | while (current_node != NULL) 32 | { 33 | if (iteration_times == idx) 34 | { 35 | new_node = create_node(n, current_node, current_node->prev); 36 | current_node->prev = new_node; 37 | current_node = new_node; 38 | current_node->prev->next = new_node; 39 | return (new_node); 40 | } 41 | 42 | current_node = current_node->next; 43 | ++iteration_times; 44 | } 45 | 46 | return (current_node); 47 | } 48 | 49 | /** 50 | * dlistint_len - Function counts the number of 51 | * elements in a doubly linked list 52 | * @h: Doubly linked list 53 | * 54 | * Return: Returns number of elements in the list 55 | */ 56 | size_t dlistint_len(const dlistint_t *h) 57 | { 58 | int lenth = 0; 59 | 60 | while (h != NULL) 61 | { 62 | ++lenth; 63 | h = h->next; 64 | } 65 | 66 | return (lenth); 67 | } 68 | 69 | /** 70 | * create_node - Creates a new node with values 71 | * @n: Number of the new node 72 | * @next: Next node of the new node 73 | * @prev: Previous node of the new node 74 | * 75 | * Return: Returns address of the new node created 76 | */ 77 | dlistint_t *create_node(unsigned int n, void *next, void *prev) 78 | { 79 | dlistint_t *new_node = NULL; 80 | 81 | new_node = malloc(sizeof(dlistint_t)); 82 | if (new_node == NULL) 83 | return (NULL); 84 | 85 | new_node->n = n; 86 | new_node->next = next; 87 | new_node->prev = prev; 88 | return (new_node); 89 | } 90 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lists.h" 4 | 5 | /** 6 | * delete_dnodeint_at_index - Function deletes the node at index 7 | * @head: Head of the node 8 | * @index: Index of the node that should be deleted 9 | * 10 | * Return: Returns 1 on success or -1 on failure 11 | */ 12 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 13 | { 14 | dlistint_t *current_node = NULL, *temp = NULL; 15 | unsigned int leng = 0; 16 | 17 | if (head && *head) 18 | { 19 | leng = dlistint_len(*head); 20 | if (index > leng) 21 | return (-1); 22 | 23 | if (index == 0) 24 | return (delete_first_dnode(head)); 25 | 26 | current_node = get_dnodeint_at_index(*head, index); 27 | if (current_node) 28 | { 29 | temp = current_node; 30 | if (leng - 1 == index) 31 | current_node->prev->next = current_node->next; 32 | else 33 | { 34 | current_node->prev->next = current_node->next; 35 | current_node->next->prev = current_node->prev; 36 | } 37 | 38 | free(temp); 39 | return (1); 40 | } 41 | } 42 | 43 | return (-1); 44 | } 45 | 46 | /** 47 | * delete_first_dnode - Removes the first node of a doubly linked list 48 | * @head: Head of the list 49 | * 50 | * Return: 1 if deleted 51 | */ 52 | int delete_first_dnode(dlistint_t **head) 53 | { 54 | dlistint_t *current_node = *head, *temp = NULL; 55 | 56 | temp = current_node; 57 | if (current_node->next) 58 | { 59 | current_node = current_node->next; 60 | current_node->prev = temp->prev; 61 | *head = current_node; 62 | } 63 | else 64 | { 65 | *head = NULL; 66 | } 67 | 68 | free(temp); 69 | return (1); 70 | } 71 | 72 | /** 73 | * get_dnodeint_at_index - Gets a node from a doubly linked list 74 | * @head: Head of the list 75 | * @index: Index to find in the list 76 | * 77 | * Return: Returns specific node of the list 78 | */ 79 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 80 | { 81 | dlistint_t *current_node = head; 82 | unsigned int iter_times = 0; 83 | 84 | if (head) 85 | { 86 | while (current_node != NULL) 87 | { 88 | if (iter_times == index) 89 | return (current_node); 90 | 91 | current_node = current_node->next; 92 | ++iter_times; 93 | } 94 | } 95 | 96 | return (NULL); 97 | } 98 | 99 | /** 100 | * dlistint_len - Counts the number of elements in a doubly linked list 101 | * @h: The double linked list to count 102 | * 103 | * Return: Number of elements in the list 104 | */ 105 | size_t dlistint_len(const dlistint_t *h) 106 | { 107 | int lnth = 0; 108 | 109 | while (h != NULL) 110 | { 111 | ++lnth; 112 | h = h->next; 113 | } 114 | 115 | return (lnth); 116 | } 117 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # Doubly linked Lists 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | /** 5 | * struct dlistint_s - doubly linked list 6 | * @n: integer 7 | * @prev: points to the previous node 8 | * @next: points to the next node 9 | * 10 | * Description: doubly linked list node structure 11 | * for Holberton project 12 | */ 13 | typedef struct dlistint_s 14 | { 15 | int n; 16 | struct dlistint_s *prev; 17 | struct dlistint_s *next; 18 | } dlistint_t; 19 | 20 | size_t print_dlistint(const dlistint_t *h); 21 | size_t dlistint_len(const dlistint_t *h); 22 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 23 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 24 | void free_dlistint(dlistint_t *head); 25 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 26 | int sum_dlistint(dlistint_t *head); 27 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 28 | dlistint_t *create_node(unsigned int n, void *next, void *prev); 29 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 30 | int delete_first_dnode(dlistint_t **head); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -fPIC -c *.c 3 | gcc -shared *.o -o liball.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P .. https://raw.githubusercontent.com/monoprosito/holbertonschool-low_level_programming/master/0x18-dynamic_libraries/libgiga.so 3 | export LD_PRELOAD="$PWD/../libgiga.so" 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mathenge-Alex/low_level_programming/22efdb447d57ec25bf9c16b9d8a1e4a100fba659/0x18-dynamic_libraries/README.md -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | int _islower(int c); 9 | int _isalpha(int c); 10 | int _abs(int n); 11 | int _isupper(int c); 12 | int _isdigit(int c); 13 | int _strlen(char *s); 14 | void _puts(char *s); 15 | char *_strcpy(char *dest, char *src); 16 | int _atoi(char *s); 17 | char *_strcat(char *dest, char *src); 18 | char *_strncat(char *dest, char *src, int n); 19 | char *_strncpy(char *dest, char *src, int n); 20 | int _strcmp(char *s1, char *s2); 21 | char *_memset(char *s, char b, unsigned int n); 22 | char *_memcpy(char *dest, char *src, unsigned int n); 23 | char *_strchr(char *s, char c); 24 | unsigned int _strspn(char *s, char *accept); 25 | char *_strpbrk(char *s, char *accept); 26 | char *_strstr(char *haystack, char *needle); 27 | 28 | #endif /* MAIN_H */ 29 | --------------------------------------------------------------------------------