├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── .8-24_hours.c.swp ├── 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 ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── .101-keygen.c.swp ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── libmy.a ├── main.c ├── main.h └── quote ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md └── main.h ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── 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 └── 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 └── nrandom.so ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md ├── hash_tables.h └── tests │ ├── 0-main.c │ ├── 100-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ └── 6-main.c ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── m.h ├── main.c ├── school.c └── tests │ └── 5-main.py ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - print quote 6 | * 7 | * Description: prints Dors's quote 8 | * 9 | * Return: Always 1 (Success) 10 | */ 11 | 12 | int main(void) 13 | { 14 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: prints "Programming is like building a multilingual puzzle" 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | puts("\"Programming is like building a multilingual puzzle"); 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Description: prints "with proper grammar, but the outcome is a piece of 7 | * art," 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(void) 12 | { 13 | printf("with proper grammar, but the outcome is a piece of art,\n"); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the size of various types 5 | * 6 | * Description: prints the size of various types on the computer it is compiled 7 | * and run on 8 | * 9 | * Return: Allways 0 (Success) 10 | */ 11 | 12 | int main(void) 13 | { 14 | printf("Size of a char: %d byte(s)\n", (int) sizeof(char)); 15 | printf("Size of an int: %d byte(s)\n", (int) sizeof(int)); 16 | printf("Size of a long int: %d byte(s)\n", (int) sizeof(long int)); 17 | printf("Size of a long long int: %d byte(s)\n", (int) sizeof(long long int)); 18 | printf("Size of a float: %d byte(s)\n", (int) sizeof(float)); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | This project is the first C project as part of the Holberton School curriculum, and covers the very basics of compilation and C. 2 | 3 | File 0-preprocessor is a script that runs a C file through the preprocessor and save the result into another file. 4 | 5 | File 1-compiler is a script that compiles a C file but does not link. 6 | 7 | File 2-assembler is a script that generates the assembly code of a C code and save it in an output file. 8 | 9 | File 3-name is a script that compiles a C file and creates an executable named cisfun. 10 | 11 | File 4-puts.c is a C program that prints exactly "Programming is like building a multilingual puzzle, followed by a new line. 12 | 13 | File 5-printf.c is a C program that prints exactly "with proper grammar, but the outcome is a piece of art,", followed by a new line. 14 | 15 | File 6-size.c is a C program that prints the size of various types on the computer it is compiled and run on. 16 | 17 | File 100-intel is a script that generates the assembly code (Intel syntax) of a C code and save it in an output file. 18 | 19 | File 101-quote.c is a C program that prints exactly "and that piece of art is useful" - Dora Korpar, 2015-10-19", followed by a new line, to the standard error. 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - determine if number is positive or negative 7 | * 8 | * Description: program will assign a random number to the variable n each time 9 | * it is executed. 10 | * 11 | * Return: Always 0 (Success) 12 | */ 13 | 14 | int main(void) 15 | { 16 | int n; 17 | 18 | 19 | srand(time(0)); 20 | n = rand() - RAND_MAX / 2; 21 | if (n == 0) 22 | printf("%d is zero\n", n); 23 | else if (n < 0) 24 | printf("%d is negative\n", n); 25 | else 26 | printf("%d is positive\n", n); 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - print last digit in random number 7 | * 8 | * Description: print last digit in random number 9 | * 10 | * Return: Always 0 (Success) 11 | */ 12 | 13 | int main(void) 14 | { 15 | int n, last; 16 | 17 | srand(time(0)); 18 | n = rand() - RAND_MAX / 2; 19 | last = n % 10; 20 | if (last > 5) 21 | printf("Last digit of %d is %d and is greater than 5\n", n, last); 22 | else if (last == 0) 23 | printf("Last digit of %d is %d and is 0\n", n, last); 24 | else if (last < 6 && last != 0) 25 | printf("Last digit of %d is %d and is less than 6 and not 0\n", n, last); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print double digit combos 5 | * 6 | * Description: print double digit combos 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i, j; 14 | 15 | i = 48; 16 | j = 48; 17 | 18 | while (i < 58) 19 | { 20 | j = i + 1; 21 | while (j < 58) 22 | { 23 | putchar(i); 24 | putchar(j); 25 | 26 | if (i < 56 || j < 57) 27 | { 28 | putchar(44); 29 | putchar(32); 30 | } 31 | j++; 32 | } 33 | 34 | i++; 35 | } 36 | 37 | putchar(10); 38 | 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print triple combos 5 | * 6 | * Description: print triple digit combos 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i, j, k; 14 | 15 | i = 48; 16 | j = 48; 17 | k = 48; 18 | 19 | while (i < 58) 20 | { 21 | j = i + 1; 22 | while (j < 58) 23 | { 24 | k = j + 1; 25 | while (k < 58) 26 | { 27 | putchar(i); 28 | putchar(j); 29 | putchar(k); 30 | if (i < 55 || j < 56 || k < 57) 31 | { 32 | putchar(44); 33 | putchar(32); 34 | } 35 | k++; 36 | } 37 | 38 | j++; 39 | } 40 | 41 | i++; 42 | } 43 | 44 | putchar(10); 45 | 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print pairs of double digit combos 5 | * 6 | * Description: print pairs of double digit combos 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i, j, k, m; 14 | 15 | i = 48; 16 | while (i < 58) 17 | { 18 | j = 48; 19 | while (j < 58) 20 | { 21 | m = j + 1; 22 | k = i; 23 | while (k < 58) 24 | { 25 | while (m < 58) 26 | { 27 | putchar(i); 28 | putchar(j); 29 | putchar(32); 30 | putchar(k); 31 | putchar(m); 32 | if (i < 57 || j < 56 || k < 57 || m < 57) 33 | { 34 | putchar(44); 35 | putchar(32); 36 | } 37 | m++; 38 | } 39 | m = 48; 40 | k++; 41 | } 42 | j++; 43 | } 44 | i++; 45 | } 46 | putchar(10); 47 | return (0); 48 | } 49 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the letters of the alphabet 5 | * 6 | * Description: print the letters of the alphabet 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i = 97; 14 | 15 | while (i < 123) 16 | { 17 | putchar(i); 18 | i++; 19 | } 20 | putchar(10); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the letters of the alphabe 5 | * 6 | * Description: print the letters of the aphabet in lower then in uppercase 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i, j; 14 | 15 | i = 97; 16 | j = 65; 17 | 18 | while (i < 123) 19 | { 20 | putchar(i); 21 | i++; 22 | } 23 | 24 | while (j < 91) 25 | { 26 | putchar(j); 27 | j++; 28 | } 29 | 30 | putchar(10); 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the letters of the alphabet 5 | * 6 | * Description: print the letters of the alphabet except e, q 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { int i = 97; 13 | 14 | while (i < 123) 15 | { 16 | if (i != 101 && i != 113) 17 | { 18 | putchar(i); 19 | } 20 | i++; 21 | } 22 | putchar(10); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print single digit numbers 5 | * 6 | * Descripton: print single digit numbers 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i; 14 | 15 | i = 48; 16 | 17 | while (i < 58) 18 | { 19 | putchar(i); 20 | i++; 21 | } 22 | 23 | putchar(10); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print single digit numbers 5 | * 6 | * Desciption: print single digit numbers 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i; 14 | 15 | i = 48; 16 | 17 | while (i < 58) 18 | { 19 | putchar(i); 20 | i++; 21 | } 22 | 23 | putchar(10); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the latterss of the alphabet 5 | * 6 | * Description: print the letters of the alphabet in reverse 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i = 122; 14 | 15 | while (i > 96) 16 | { 17 | putchar(i); 18 | i--; 19 | } 20 | putchar(10); 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print the letters of the alphabet 5 | * 6 | * Description: print the letters of the alphabet except e, q 7 | * 8 | * Return: lways 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i = 0; 14 | 15 | while (i < 48) 16 | { 17 | if (i < 10) 18 | putchar(i + '0'); 19 | else if (i > 41) 20 | putchar(i - 10 + 'A'); 21 | i++; 22 | } 23 | putchar(10); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print single digit numbers 5 | * 6 | * Description: print single digit numbers with commas 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i; 14 | 15 | i = 48; 16 | 17 | while (i < 58) 18 | { 19 | putchar(i); 20 | if (i != 57) 21 | { 22 | putchar(44); 23 | putchar(32); 24 | } 25 | i++; 26 | } 27 | 28 | putchar(10); 29 | 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | File 0-positive_or_negative.c is a C program that will assign a random number to the variable n each time it is executed, using an if statement. 2 | 3 | File 1-last_digit.c is a C program that will assign a random number to the variable n each time it is executed using another if statement. 4 | 5 | File 2-print_alphabet.c is a C program that prints the alphabet in lowercase, followed by a new line. 6 | 7 | File 3-print_alphabt.c is a C program that prints the alphabet in lowercase, except q and e, followed by a new line. 8 | 9 | File 4-print_alphabets.c is a C program that prints the alphabet in lowercase, and then in uppercase, followed by a new line. 10 | 11 | File 5-print_numbers.c is a C program that prints all single digit numbers of base 10 starting from 0, followed by a new line. 12 | 13 | File 6-print_numberz.c is a C program that prints all single digit numbers of base 10 starting from 0, followed by a new line, using putchar. 14 | 15 | File 7-print_tebahpla.c is a C program that prints the lowercase alphabet in reverse, followed by a new line. 16 | 17 | File 8-print_base16.c is a C program that prints all the numbers of base 16 in lowercase, followed by a new line. 18 | 19 | File 9-print_comb.c is a C program that prints all possible combinations of single-digit numbers. 20 | 21 | File 10-print_comb2.c is a C program that prints the numbers from 00 to 99. 22 | 23 | File 100-print_comb3.c is a program that prints all possible different combinations of two digits. 24 | 25 | File 101-print_comb4.c is a program that prints all possible different combinations of three digits. 26 | 27 | File 102-print_comb5.c is a program that prints all possible combinations of two two-digit numbers. 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.8-24_hours.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x02-functions_nested_loops/.8-24_hours.c.swp -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - prints Putchar 5 | * 6 | * Description: prints _putchar 7 | * 8 | * Return: 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | _putchar('_'); 14 | _putchar('p'); 15 | _putchar('u'); 16 | _putchar('t'); 17 | _putchar('c'); 18 | _putchar('h'); 19 | _putchar('a'); 20 | _putchar('r'); 21 | _putchar('\n'); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - print alphabet of letters 5 | * 6 | * Return: void 7 | */ 8 | 9 | void print_alphabet(void) 10 | { 11 | int i; 12 | 13 | for (i = 'a'; i <= 'z'; i++) 14 | 15 | { 16 | _putchar(i); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * add - add two numbers from input 5 | * @a: first aparamet 6 | * @b: second parameter 7 | * 8 | * Description: adds two numbers 9 | * Return: Always (0). 10 | */ 11 | 12 | int add(int a, int b) 13 | { 14 | return (a + b); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - prints the n times table, starting with 0 5 | * @n: number of the times table 6 | */ 7 | void print_times_table(int n) 8 | { 9 | int i, j, k; 10 | 11 | if (n >= 0 && n <= 15) 12 | { 13 | for (i = 0; i <= n; i++) 14 | { 15 | for (j = 0; j <= n; j++) 16 | { 17 | k = j * i; 18 | if (j == 0) 19 | { 20 | _putchar(k + '0'); 21 | } else if (k < 10 && j != 0) 22 | { 23 | _putchar(','); 24 | _putchar(' '); 25 | _putchar(' '); 26 | _putchar(' '); 27 | _putchar(k + '0'); 28 | } else if (k >= 10 && k < 100) 29 | { 30 | _putchar(','); 31 | _putchar(' '); 32 | _putchar(' '); 33 | _putchar((k / 10) + '0'); 34 | _putchar((k % 10) + '0'); 35 | } else if (k >= 100) 36 | { 37 | _putchar(','); 38 | _putchar(' '); 39 | _putchar((k / 100) + '0'); 40 | _putchar(((k / 10) % 10) + '0'); 41 | _putchar((k % 10) + '0'); 42 | } 43 | } 44 | _putchar('\n'); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - sums nultplies of 3 or 5 5 | * 6 | * Description: multiples between 0 and 1024 7 | * Return: Always(0) Success 8 | */ 9 | 10 | int main(void) 11 | { 12 | int start_num, end_num, total; 13 | 14 | end_num = 1024; 15 | total = 0; 16 | 17 | for (start_num = 0; start_num < end_num; start_num++) 18 | { 19 | if ((start_num % 3 == 0) || (start_num % 5 == 0)) 20 | { 21 | total = total + start_num; 22 | } 23 | else 24 | { 25 | continue; 26 | } 27 | } 28 | 29 | printf("%d", total); 30 | printf("\n"); 31 | 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print the first 50 fibonacci numbers, starting with 1 and 2. 5 | * Numbers must be coma and space separated. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int count = 2; 11 | long int i = 1, j = 2; 12 | long int k; 13 | 14 | printf("%lu, ", i); 15 | while (count <= 50) 16 | { 17 | if (count == 50) 18 | { 19 | printf("%lu\n", j); 20 | } 21 | else 22 | { 23 | printf("%lu, ", j); 24 | } 25 | 26 | k = j; 27 | j += i; 28 | i = k; 29 | count++; 30 | } 31 | 32 | return (0); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print the sum of even Fibonacci numbers up to a fib value 5 | * not exceeding 4,000,000. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | int i = 1, j = 2, total = 0; 11 | int k; 12 | 13 | while (j < 4000000) 14 | { 15 | if (j % 2 == 0) 16 | total += j; 17 | k = j; 18 | j += i; 19 | i = k; 20 | } 21 | printf("%d\n", total); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - finds and prints the first 98 Fibonacci numbers, 4 | * starting with 1 and 2 5 | * followed by a new line 6 | * Return: ALways 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int i, j, k, j1, j2, k1, k2; 11 | 12 | j = 1; 13 | k = 2; 14 | 15 | printf("%lu", j); 16 | 17 | for (i = 1; i < 91; i++) 18 | { 19 | printf(", %lu", k); 20 | k = k + j; 21 | j = k - j; 22 | } 23 | 24 | j1 = j / 1000000000; 25 | j2 = j % 1000000000; 26 | k1 = k / 1000000000; 27 | k2 = k % 1000000000; 28 | 29 | for (i = 92; i < 99; ++i) 30 | { 31 | printf(", %lu", k1 + (k2 / 1000000000)); 32 | printf("%lu", k2 % 1000000000); 33 | k1 = k1 + j1; 34 | j1 = k1 - j1; 35 | k2 = k2 + j2; 36 | j2 = k2 - j2; 37 | } 38 | 39 | printf("\n"); 40 | 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_to_98 - Check Holberton 6 | * @n: A input integer 7 | * Description: function that prints all natural numbers 8 | * from n to 98, followed by a new line. 9 | * Return: Nothing 10 | */ 11 | void print_to_98(int n) 12 | { 13 | if (n <= 98) 14 | { 15 | for (; n <= 98; n++) 16 | { 17 | printf("%d", n); 18 | 19 | if (n == 98) 20 | continue; 21 | printf(", "); 22 | } 23 | printf("\n"); 24 | } 25 | else 26 | { 27 | for (; n >= 98; n--) 28 | { 29 | printf("%d", n); 30 | if (n == 98) 31 | continue; 32 | printf(", "); 33 | } 34 | printf("\n"); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - prints alphabets 10 5 | * 6 | * Description: prints 10 times 7 | * Return: Always(0). 8 | */ 9 | 10 | void print_alphabet_x10(void) 11 | { 12 | int alphabet_count = 0; 13 | 14 | while (alphabet_count < 10) 15 | { 16 | char ch = 'a'; 17 | 18 | while (ch <= 'z') 19 | { 20 | _putchar(ch); 21 | ch++; 22 | } 23 | alphabet_count++; 24 | _putchar('\n'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - prints 1 or 0 depending on input 5 | * @c: first parameter 6 | * 7 | * Description: prints all lowercase letters 8 | * Return: Always(0). 9 | */ 10 | 11 | int _islower(int c) 12 | { 13 | return (c >= 'a' && c <= 'z'); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - Check if character is a alphabet character. 4 | * @c: type int character 5 | * Return: 1 if letter, lowercase or uppercase, and 0 otherwise 6 | */ 7 | 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 11 | return (1); 12 | 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_sign - Print the sign of a number 4 | * @n: type int integer, can be negative or positive 5 | * Description: print +, 0, or - depending on number, along with return 6 | * Return: 1 if +, 0 if 0, and -1 if - 7 | */ 8 | 9 | int print_sign(int n) 10 | { 11 | if (n > 0) 12 | { 13 | _putchar('+'); 14 | return (1); 15 | } 16 | else if (n < 0) 17 | { 18 | _putchar('-'); 19 | return (-1); 20 | } 21 | else 22 | { 23 | _putchar('0'); 24 | return (0); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - compute the absolute value of an integer 4 | * @n: int type number 5 | * Return: absolute value of @n 6 | */ 7 | int _abs(int n) 8 | { 9 | if (n < 0) 10 | { 11 | return (n * -1); 12 | } 13 | else 14 | { 15 | return (n); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_last_digit - print the last digit of a number 4 | * @n: int type number 5 | * Return: return value of last digit 6 | */ 7 | int print_last_digit(int n) 8 | { 9 | int r; 10 | 11 | if (n < 0) 12 | { 13 | r = -1 * (n % 10); 14 | _putchar(r + '0'); 15 | return (r); 16 | } 17 | else 18 | { 19 | r = n % 10; 20 | _putchar(r + '0'); 21 | return (r); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - prints time 5 | * 6 | * Description: prints a list of time 7 | * Return: Always 0. 8 | */ 9 | 10 | void jack_bauer(void) 11 | { 12 | int i, j; 13 | 14 | for (i = 0; i < 24; i++) 15 | { 16 | for (j = 0; j < 60; j++) 17 | { 18 | _putchar(i / 10 + 48); 19 | _putchar(i % 10 + 48); 20 | _putchar(':'); 21 | _putchar(j / 10 + 48); 22 | _putchar(j % 10 + 48); 23 | _putchar('\n'); 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * times_table - a function that prints the 9 times table, starting with 0 4 | * rone = row, cone = column, d = digits of current result 5 | * Return: times table 6 | * add extra space past single digit 7 | */ 8 | void times_table(void) 9 | { 10 | int r, c, d; 11 | 12 | for (r = 0; r <= 9; r++) 13 | { 14 | _putchar('0'); 15 | _putchar(','); 16 | _putchar(' '); 17 | for (c = 1; c <= 9; c++) 18 | { 19 | d = (r * c); 20 | if ((d / 10) > 0) 21 | { 22 | _putchar((d / 10) + '0'); 23 | } 24 | else 25 | { 26 | _putchar(' '); 27 | } 28 | _putchar((d % 10) + '0'); 29 | if (c < 9) 30 | { 31 | _putchar(','); 32 | _putchar(' '); 33 | } 34 | } 35 | _putchar('\n'); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | File 0-holberton.c is a program that prints Holberton, followed by a new line. 2 | 3 | File 1-alphabet.c is a function that prints the alphabet, in lowercase, followed by a new line. 4 | 5 | File 2-print_alphabet_x10.c is a function that prints 10 times the alphabet, in lowercase, followed by a new line. 6 | 7 | File 3-islower.c is a function that checks for lowercase character. 8 | 9 | File 4-isalpha.c is a function that checks for alphabetic character. 10 | 11 | File 5-sign.c is a function that prints the sign of a number. 12 | 13 | File 6-abs.c is a function that computes the absolute value of an integer. 14 | 15 | File 7-print_last_digit.c is a function that prints the last digit of a number. 16 | 17 | File 8-24_hours.c is a function that prints every minute of the day of Jack Bauer, starting from 00:00 to 23:59. 18 | 19 | File 10-add.c is a function that adds two integers and returns the result. 20 | 21 | File 11-print_to_98.c is a function that prints all natural numbers from n to 98, followed by a new line. 22 | 23 | File 100-times_table.c is a function that prints the n times table, starting with 0. 24 | 25 | File 101-natural.c is a program that computes and prints the sum of all the multiples of 3 or 5 below 1024 (excluded), followed by a new line. 26 | 27 | File 102-fibonacci.c is a program that prints the first 50 Fibonacci numbers, starting with 1 and 2, followed by a new line. 28 | 29 | File 103-fibonacci.c is a program that finds and prints the sum of the even-valued terms of the Fibonacci suite under 4000000, followed by a new line. 30 | 31 | File 104-fibonacci.c is a program that finds and prints the first 98 Fibonacci numbers, starting with 1 and 2, followed by a new line. 32 | 33 | File _putchar.c contains the _putchar() function definition. 34 | 35 | File holberton.h is the header file containing all the function prototypes used in this project. 36 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char); 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); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int, int); 12 | void print_to_98(int n); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - Debugging. 5 | * 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | positive_or_negative(i); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | /** 16 | * while (i < 10) 17 | * { 18 | * putchar(i); 19 | * } 20 | */ 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | 16 | if (a >= b && a >= c) 17 | { 18 | largest = a; 19 | } 20 | else if (b >= a && b >= c) 21 | { 22 | largest = b; 23 | } 24 | else 25 | { 26 | largest = c; 27 | } 28 | return (largest); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | */ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 16 | { 17 | if (month >= 3 && day >= 60) 18 | { 19 | day++; 20 | } 21 | printf("Day of the year: %d\n", day); 22 | printf("Remaining days: %d\n", 366 - day); 23 | } 24 | else 25 | { 26 | if (month == 2 && day == 60) 27 | { 28 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 29 | } 30 | else 31 | { 32 | printf("Day of the year: %d\n", day); 33 | printf("Remaining days: %d\n", 365 - day); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | This a a debugging session 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle, followed by a new line 5 | * @size: size of the triangle 6 | */ 7 | void print_triangle(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 1; i <= size; i++) 17 | { 18 | for (j = i; j < size; j++) 19 | { 20 | _putchar(' '); 21 | } 22 | 23 | for (j = 1; j <= i; j++) 24 | { 25 | _putchar('#'); 26 | } 27 | 28 | _putchar('\n'); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - finds and prints the largest prime factor of the number 612852475143 6 | * followed by a new line 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | long int n; 12 | long int max; 13 | long int i; 14 | 15 | n = 612852475143; 16 | max = -1; 17 | 18 | while (n % 2 == 0) 19 | { 20 | max = 2; 21 | n /= 2; 22 | } 23 | 24 | for (i = 3; i <= sqrt(n); i = i + 2) 25 | { 26 | while (n % i == 0) 27 | { 28 | max = i; 29 | n = n / i; 30 | } 31 | } 32 | 33 | if (n > 2) 34 | max = n; 35 | 36 | printf("%ld\n", max); 37 | 38 | return (0); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer n 5 | *@n: integer to be printed 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int n1; 10 | 11 | if (n < 0) 12 | { 13 | n1 = -n; 14 | _putchar('-'); 15 | } else 16 | { 17 | n1 = n; 18 | } 19 | 20 | if (n1 / 10) 21 | { 22 | print_number(n1 / 10); 23 | } 24 | 25 | _putchar((n1 % 10) + '0'); 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two integers 5 | * @a: int to be multiplied to b 6 | * @b: int to be multiplied to a 7 | * Return: the result of the operation 8 | */ 9 | int mul(int a, int b) 10 | { 11 | int c; 12 | 13 | c = a * b; 14 | 15 | return (c); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints the numbers, from 0 to 9, 5 | * followed by a new line 6 | */ 7 | void print_numbers(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | _putchar(i + '0'); 14 | } 15 | 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - prints the numbers, from 0 to 9, 5 | * except 2 and 4, followed by a new line 6 | */ 7 | void print_most_numbers(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | if (i != 2 && i != 4) 14 | { 15 | _putchar(i + '0'); 16 | } 17 | } 18 | 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints 10 times the numbers, from 0 to 14 5 | * followed by a new line 6 | */ 7 | void more_numbers(void) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | for (j = 0; j < 15; j++) 14 | { 15 | if (j >= 10) 16 | _putchar(j / 10 + '0'); 17 | _putchar(j % 10 + '0'); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - draws a straight line in the terminal 5 | * @n: number of times the character _ should be printed 6 | */ 7 | void print_line(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i; 15 | 16 | for (i = 1; i <= n; i++) 17 | { 18 | _putchar('_'); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - draws a diagonal line on the terminal 5 | * @n: number of times the character \ should be printed 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | if (n <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 0; i < n; i++) 17 | { 18 | for (j = 0; j < n; j++) 19 | { 20 | if (j == i) 21 | _putchar('\\'); 22 | else if (j < i) 23 | _putchar(' '); 24 | } 25 | _putchar('\n'); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints a square, followed by a new line; 5 | * @size: size of the square 6 | */ 7 | void print_square(int size) 8 | { 9 | if (size <= 0) 10 | { 11 | _putchar('\n'); 12 | } else 13 | { 14 | int i, j; 15 | 16 | for (i = 0; i < size; i++) 17 | { 18 | for (j = 0; j < size; j++) 19 | { 20 | _putchar('#'); 21 | } 22 | _putchar('\n'); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints the numbers from 1 to 100, followed by a new line 6 | * but for multiples of three prints Fizz instead of the number 7 | * and for the multiples of five prints Buzz 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 1; i <= 100; i++) 15 | { 16 | if (i % 3 == 0 && i % 5 != 0) 17 | { 18 | printf(" Fizz"); 19 | } else if (i % 5 == 0 && i % 3 != 0) 20 | { 21 | printf(" Buzz"); 22 | } else if (i % 3 == 0 && i % 5 == 0) 23 | { 24 | printf(" FizzBuzz"); 25 | } else if (i == 1) 26 | { 27 | printf("%d", i); 28 | } else 29 | { 30 | printf(" %d", i); 31 | } 32 | } 33 | printf("\n"); 34 | 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 |

Nested loops

2 | File 0-isupper.c is a function that checks for uppercase character. 3 | 4 | File 1-isdigit.c is a function that checks for a digit (0 through 9). 5 | 6 | File 2-mul.c is a function that multiplies two integers. 7 | 8 | File 3-print_numbers.c is a function that prints the numbers, from 0 to 9, followed by a new line. 9 | 10 | File 4-print_most_numbers.c is a function that prints the numbers, from 0 to 9, followed by a new line. 11 | 12 | File 5-more_numbers.c is a function that prints 10 times the numbers, from 0 to 14, followed by a new line. 13 | 14 | File 6-print_line.c is a function that draws a straight line in the terminal. 15 | 16 | File 7-print_diagonal.c is a function that draws a diagonal line on the terminal. 17 | 18 | File 8-print_square.c is a function that prints a square, followed by a new line. 19 | 20 | File 9-fizz_buzz.c is a program that prints the numbers from 1 to 100, followed by a new line, but for multiples of three prints Fizz instead of the number and for the multiples of five prints Buzz. 21 | 22 | File 10-print_triangle.c is a function that prints a triangle, followed by a new line. 23 | 24 | File 100-prime_factor.c is a program that finds and prints the largest prime factor of the number 612852475143, followed by a new line. 25 | 26 | File 101-print_number.c is a function that prints an integer using only _putchar(). 27 | 28 | File _putchar.c contains the _putchar() function definition. 29 | 30 | File holberton.h is the header file containing all the function prototypes used in this project. 31 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _isupper(int c); 5 | int _isdigit(int c); 6 | int mul(int a, int b); 7 | void print_numbers(void); 8 | int _putchar(char c); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/.101-keygen.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x05-pointers_arrays_strings/.101-keygen.c.swp -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - function that takes a pointer to an integer parameter 5 | * @n: integer pointer 6 | * Return: 0 Success 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 | /** 4 | * swap_int - swaps the values of two integers 5 | * @a: integer to swap 6 | * @b: integer to swap 7 | */ 8 | 9 | void swap_int(int *a, int *b) 10 | { 11 | int c = *a; 12 | *a = *b; 13 | *b = c; 14 | } 15 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer. 5 | * @s: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | if (*(s + count) == '-') 17 | pn *= -1; 18 | 19 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 20 | { 21 | if (size > 0) 22 | m *= 10; 23 | size++; 24 | } 25 | count++; 26 | } 27 | 28 | for (i = count - size; i < count; i++) 29 | { 30 | oi = oi + ((*(s + i) - 48) * m); 31 | m /= 10; 32 | } 33 | return (oi * pn); 34 | } 35 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - generates keygen. 6 | * Return: 0 Always. 7 | */ 8 | int main(void) 9 | { 10 | int r = 0, c = 0; 11 | time_t t; 12 | 13 | srand((unsigned int) time(&t)); 14 | while (c < 2772) 15 | { 16 | r = rand() % 128; 17 | if ((c + r) > 2772) 18 | break; 19 | c = c + r; 20 | printf("%c", r); 21 | } 22 | printf("%c\n", (2772 - c)); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input stringeturn. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, 5 | * @str: pointer to the string to print 6 | * Return: void 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i]) 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - prints a string, in reverse, followed by a new line. 5 | * @s: input string. 6 | * Return: no return. 7 | */ 8 | void print_rev(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (count >= 0) 13 | { 14 | if (s[count] == '\0') 15 | break; 16 | count++; 17 | } 18 | 19 | for (count--; count >= 0; count--) 20 | _putchar(s[count]); 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverses a string. 5 | * @s: input string. 6 | * Return: no return. 7 | */ 8 | void rev_string(char *s) 9 | { 10 | int count = 0, i, j; 11 | char *str, temp; 12 | 13 | while (count >= 0) 14 | { 15 | if (s[count] == '\0') 16 | break; 17 | count++; 18 | } 19 | str = s; 20 | 21 | for (i = 0; i < (count - 1); i++) 22 | { 23 | for (j = i + 1; j > 0; j--) 24 | { 25 | temp = *(str + j); 26 | *(str + j) = *(str + (j - 1)); 27 | *(str + (j - 1)) = temp; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - prints one char out of 2 of a string. 5 | * @str: input string. 6 | * Return: no return. 7 | */ 8 | void puts2(char *str) 9 | { 10 | int count = 0; 11 | 12 | while (count >= 0) 13 | { 14 | if (str[count] == '\0') 15 | { 16 | _putchar('\n'); 17 | break; 18 | } 19 | if (count % 2 == 0) 20 | _putchar(str[count]); 21 | count++; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - print second half of a string 5 | * @str: char array string type 6 | * Description: If odd number of chars, print (length - 1) / 2 7 | */ 8 | 9 | void puts_half(char *str) 10 | { 11 | int i; 12 | 13 | for (i = 0; str[i] != '\0'; i++) 14 | ; 15 | 16 | i++; 17 | for (i /= 2; str[i] != '\0'; i++) 18 | { 19 | _putchar(str[i]); 20 | 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - prints n elements of an array of integers. 6 | * @a: input array. 7 | * @n: input n elements 8 | * Return: no return. 9 | */ 10 | void print_array(int *a, int n) 11 | { 12 | int i = 0; 13 | 14 | for (; i < n; i++) 15 | { 16 | printf("%d", *(a + i)); 17 | if (i != (n - 1)) 18 | printf(", "); 19 | } 20 | printf("\n"); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcpy - copies the string pointed to by src, 5 | * including the terminating null byte, to the 6 | * buffer pointed to by dest. 7 | * @dest: destination. 8 | * @src: source. 9 | * Return: the pointer to dest. 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int count = 0; 14 | 15 | while (count >= 0) 16 | { 17 | *(dest + count) = *(src + count); 18 | if (*(src + count) == '\0') 19 | break; 20 | count++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 |

C - Pointers

2 | File 0-strcat.c is a function that concatenates two strings. 3 | 4 | File 1-strncat.c is a function that concatenates two strings that will use at most n bytes from src. 5 | 6 | File 2-strncpy.c is a function that copies a string. 7 | 8 | File 3-strcmp.c is a function that compares two strings. 9 | 10 | File 4-rev_array.c a function that reverses the content of an array of integers. 11 | 12 | File 5-string_toupper.c is a function that changes all lowercase letters of a string to uppercase. 13 | 14 | File 6-cap_string.c is a function that capitalizes all words of a string. 15 | 16 | File 7-leet.c is a function that encodes a string into 1337. 17 | 18 | File 8-rot13.c is a function that encodes a string using rot13. 19 | 20 | File _putchar.c is a function that replaces the standard library function putchar(). 21 | 22 | File holberton.h is the header file containing all the function prototypes. 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | void reset_to_98(int *n); 2 | void swap_int(int *a, int *b); 3 | int _strlen(char *s); 4 | void _puts(char *str); 5 | void print_rev(char *s); 6 | void rev_string(char *s); 7 | void puts2(char *str); 8 | void puts_half(char *str); 9 | void _putchar(char); 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 | /** 4 | * *_strcat - function commute srtings 5 | * @dest: param pointer to a char 6 | * @src: param pointer to a char 7 | * Return: return value of dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | while (src[j] != '\0') 23 | { 24 | dest[i] = src[j]; 25 | j++; 26 | i++; 27 | } 28 | 29 | dest[i] = '\0'; 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes used from src. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rot13 - encodes a string using rot13 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *rot13(char *s) 9 | { 10 | int count = 0, i; 11 | char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 12 | char rot13[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; 13 | 14 | while (*(s + count) != '\0') 15 | { 16 | for (i = 0; i < 52; i++) 17 | { 18 | if (*(s + count) == alphabet[i]) 19 | { 20 | *(s + count) = rot13[i]; 21 | break; 22 | } 23 | } 24 | count++; 25 | } 26 | 27 | return (s); 28 | } 29 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int m, d, count; 11 | 12 | if (n < 0) 13 | { 14 | _putchar(45); 15 | m = n * -1; 16 | } 17 | else 18 | { 19 | m = n; 20 | } 21 | 22 | d = m; 23 | count = 1; 24 | 25 | while (d > 9) 26 | { 27 | d /= 10; 28 | count *= 10; 29 | } 30 | 31 | for (; count >= 1; count /= 10) 32 | { 33 | _putchar(((m / count) % 10) + 48); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | p[5] = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * infinite_add - adds two numbers 5 | * @n1: number one. 6 | * @n2: number two. 7 | * @r: buffer that the function will use to store the result. 8 | * @size_r: buffer size: 9 | * Return: the pointer to dest. 10 | */ 11 | 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 13 | { 14 | int c1 = 0, c2 = 0, op, bg, dr1, dr2, add = 0; 15 | 16 | while (*(n1 + c1) != '\0') 17 | c1++; 18 | while (*(n2 + c2) != '\0') 19 | c2++; 20 | if (c1 >= c2) 21 | bg = c1; 22 | else 23 | bg = c2; 24 | if (size_r <= bg + 1) 25 | return (0); 26 | r[bg + 1] = '\0'; 27 | c1--, c2--, size_r--; 28 | dr1 = *(n1 + c1) - 48, dr2 = *(n2 + c2) - 48; 29 | while (bg >= 0) 30 | { 31 | op = dr1 + dr2 + add; 32 | if (op >= 10) 33 | add = op / 10; 34 | else 35 | add = 0; 36 | if (op > 0) 37 | *(r + bg) = (op % 10) + 48; 38 | else 39 | *(r + bg) = '0'; 40 | if (c1 > 0) 41 | c1--, dr1 = *(n1 + c1) - 48; 42 | else 43 | dr1 = 0; 44 | if (c2 > 0) 45 | c2--, dr2 = *(n2 + c2) - 48; 46 | else 47 | dr2 = 0; 48 | bg--, 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: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - prints a buffer 6 | * @b: buffer. 7 | * @size: size of buffer. 8 | * Return: no return. 9 | */ 10 | void print_buffer(char *b, int size) 11 | { 12 | int j, k, l; 13 | 14 | if (size <= 0) 15 | printf("\n"); 16 | else 17 | { 18 | for (j = 0; j < size; j += 10) 19 | { 20 | printf("%.8x:", j); 21 | for (k = j; k < j + 10; k++) 22 | { 23 | if (k % 2 == 0) 24 | printf(" "); 25 | if (k < size) 26 | printf("%.2x", *(b + k)); 27 | else 28 | printf(" "); 29 | } 30 | printf(" "); 31 | for (l = j; l < j + 10; l++) 32 | { 33 | if (l >= size) 34 | break; 35 | if (*(b + l) < 32 || *(b + l) > 126) 36 | printf("%c", '.'); 37 | else 38 | printf("%c", *(b + l)); 39 | } 40 | printf("\n"); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equals, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reverse_array - reverses the content of an array 4 | * of integers. 5 | * @a: array. 6 | * @n: number of elements of the array. 7 | * Return: no return. 8 | */ 9 | 10 | void reverse_array(int *a, int n) 11 | { 12 | int i, j, temp; 13 | 14 | for (i = 0; i < n - 1; i++) 15 | { 16 | for (j = i + 1; j > 0; j--) 17 | { 18 | temp = *(a + j); 19 | *(a + j) = *(a + (j - 1)); 20 | *(a + (j - 1)) = temp; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * string_toupper - changes all lowercase letters of a string to uppercase 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *string_toupper(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if ((*(s + count) >= 97) && (*(s + count) <= 122)) 15 | *(s + count) = *(s + count) - 32; 16 | count++; 17 | } 18 | 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * cap_string - capitalizes all words of a string 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *cap_string(char *s) 9 | { 10 | int count = 0, i; 11 | int sep_words[] = {32, 9, 10, 44, 59, 46, 33, 63, 34, 40, 41, 123, 125}; 12 | 13 | if (*(s + count) >= 97 && *(s + count) <= 122) 14 | *(s + count) = *(s + count) - 32; 15 | count++; 16 | while (*(s + count) != '\0') 17 | { 18 | for (i = 0; i < 13; i++) 19 | { 20 | if (*(s + count) == sep_words[i]) 21 | { 22 | if ((*(s + (count + 1)) >= 97) && (*(s + (count + 1)) <= 122)) 23 | *(s + (count + 1)) = *(s + (count + 1)) - 32; 24 | break; 25 | } 26 | } 27 | count++; 28 | } 29 | return (s); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * leet - encodes a string into 1337 4 | * @s: input string. 5 | * Return: the pointer to dest. 6 | */ 7 | 8 | char *leet(char *s) 9 | { 10 | int count = 0, i; 11 | int low_letters[] = {97, 101, 111, 116, 108}; 12 | int upp_letters[] = {65, 69, 79, 84, 76}; 13 | int numbers[] = {52, 51, 48, 55, 49}; 14 | 15 | while (*(s + count) != '\0') 16 | { 17 | for (i = 0; i < 5; i++) 18 | { 19 | if (*(s + count) == low_letters[i] || *(s + count) == upp_letters[i]) 20 | { 21 | *(s + count) = numbers[i]; 22 | break; 23 | } 24 | } 25 | count++; 26 | } 27 | 28 | return (s); 29 | } 30 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | Hello pointers 2 | -------------------------------------------------------------------------------- /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 | int _putchar(char); 14 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: source adress. 6 | * @to: target adress. 7 | * Return: no return. 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chessboard 5 | * @a: input pointer. 6 | * Return: no return. 7 | */ 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | unsigned int i, m = 0; 11 | 12 | for (i = 0; i < 64; i++) 13 | { 14 | if (i % 8 == 0 && i != 0) 15 | { 16 | m = i; 17 | _putchar('\n'); 18 | } 19 | _putchar(a[i / 8][i - m]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_diagsums - prints the sum of the two 5 | * diagonals of a square matrix of integers 6 | * @a: input pointer. 7 | * @size: size of the matrix 8 | * Return: no return. 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i, sum1 = 0, sum2 = 0; 13 | 14 | for (i = 0; i < (size * size); i++) 15 | { 16 | if (i % (size + 1) == 0) 17 | sum1 += *(a + i); 18 | if (i % (size - 1) == 0 && i != 0 && i < size * size - 1) 19 | sum2 += *(a + i); 20 | } 21 | printf("%d, %d\n", sum1, sum2); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | File 0-memset.c is a function that fills memory with a constant byte. 2 | 3 | File 1-memcpy.c is a function that copies memory area. 4 | 5 | File 2-strchr.c is a function that locates a character in a string. 6 | 7 | File 3-strspn.c is a function that gets the length of a prefix substring. 8 | 9 | File 4-strpbrk.c is a function that searches a string for any of a set of bytes. 10 | 11 | File 5-strstr.c is a function that locates a substring. 12 | 13 | File 7-print_chessboard.c is a function that prints the chessboard. 14 | 15 | File 8-print_diagsums.c is a function that prints the sum of the two diagonals of a square matrix of integers. 16 | 17 | File 9-set_string.c is a function that sets the value of a pointer to a char. 18 | 19 | File holberton.h is the header file with the functions prototypes. 20 | 21 | File _putchar.c is the file that contains the _putchar function. 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _HOLBERTON_H_ 2 | #define _HOLBERTON_H_ 3 | 4 | char *_memset(char *s, char b, unsigned int n); 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn(char *s, char *accept); 8 | char *_strpbrk(char *s, char *accept); 9 | char *_strstr(char *haystack, char *needle); 10 | int _putchar(char); 11 | void print_chessboard(char (*a)[8]); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *to); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - prints a string, followed by a new line. 5 | * @s: string 6 | * Return: no return. 7 | */ 8 | void _puts_recursion(char *s) 9 | { 10 | if (*s != '\0') 11 | { 12 | _putchar(*s); 13 | _puts_recursion(s + 1); 14 | } 15 | else 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints a string in reverse. 5 | * @s: string 6 | * Return: no return. 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 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | 16 | /** 17 | * comparator - compares each character of the string. 18 | * @s: string 19 | * @n1: smallest iterator. 20 | * @n2: biggest iterator. 21 | * Return: . 22 | */ 23 | int comparator(char *s, int n1, int n2) 24 | { 25 | if (*(s + n1) == *(s + n2)) 26 | { 27 | if (n1 == n2 || n1 == n2 + 1) 28 | return (1); 29 | return (0 + comparator(s, n1 + 1, n2 - 1)); 30 | } 31 | return (0); 32 | } 33 | 34 | /** 35 | * is_palindrome - detects if a string is a palindrome. 36 | * @s: string. 37 | * Return: 1 if s is a palindrome, 0 if not. 38 | */ 39 | int is_palindrome(char *s) 40 | { 41 | if (*s == '\0') 42 | return (1); 43 | return (comparator(s, 0, _strlen_recursion(s) - 1)); 44 | } 45 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two strings. 5 | * @s1: string 1. 6 | * @s2: string 2. It can contains a * as a special character. 7 | * Return: 1 if are identical, 0 if not. 8 | */ 9 | int wildcmp(char *s1, char *s2) 10 | { 11 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 12 | return (0); 13 | if (*s1 == '\0' && *s2 == '\0') 14 | return (1); 15 | if (*s1 == *s2) 16 | return (wildcmp(s1 + 1, s2 + 1)); 17 | if (*s2 == '*') 18 | return (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a given number. 5 | * @n: input number. 6 | * Return: factorial of the number. 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n < 0) 11 | return (-1); 12 | else if (n == 0) 13 | return (1); 14 | else 15 | return (n * factorial(n - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - returns the value of x raised to the power of y. 5 | * @x: base. 6 | * @y: exponent. 7 | * Return: value of the exponentiation. 8 | */ 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | return (-1); 13 | else if (y == 0) 14 | return (1); 15 | else 16 | return (x * _pow_recursion(x, y - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * power_operation - returns the natural square root of a number. 5 | * @n: input number. 6 | * @c: iterator. 7 | * Return: square root or -1. 8 | */ 9 | int power_operation(int n, int c) 10 | { 11 | if (c % (n / c) == 0) 12 | { 13 | if (c * (n / c) == n) 14 | return (c); 15 | else 16 | return (-1); 17 | } 18 | return (0 + power_operation(n, c + 1)); 19 | } 20 | /** 21 | * _sqrt_recursion - returns the natural square root of a number. 22 | * @n: input number. 23 | * Return: natural square root. 24 | */ 25 | int _sqrt_recursion(int n) 26 | { 27 | if (n < 0) 28 | return (-1); 29 | if (n == 0) 30 | return (0); 31 | if (n == 1) 32 | return (1); 33 | return (power_operation(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * is_prime - detects if an input number is a prime number. 5 | * @n: input number. 6 | * @c: iterator. 7 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 8 | */ 9 | int is_prime(unsigned int n, unsigned int c) 10 | { 11 | if (n % c == 0) 12 | { 13 | if (n == c) 14 | return (1); 15 | else 16 | return (0); 17 | } 18 | return (0 + is_prime(n, c + 1)); 19 | } 20 | /** 21 | * is_prime_number - detects if an input number is a prime number. 22 | * @n: input number. 23 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 24 | */ 25 | int is_prime_number(int n) 26 | { 27 | if (n == 0) 28 | return (0); 29 | if (n < 0) 30 | return (0); 31 | if (n == 1) 32 | return (0); 33 | return (is_prime(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | File 0-puts_recursion.c is a function that prints a string, followed by a new line. 2 | 3 | File 1-print_rev_recursion.c is a function that prints a string in reverse. 4 | 5 | File 2-strlen_recursion.c is a function that returns the length of a string. 6 | 7 | File 3-factorial.c is a function that returns the factorial of a given number. 8 | 9 | File 4-pow_recursion.c is a function that returns the value of x raised to the power of y. 10 | 11 | File 5-sqrt_recursion.c is a function that returns the natural square root of a number. 12 | 13 | File 6-is_prime_number.c is a function that returns 1 if the input integer is a prime number, otherwise return 0. 14 | 15 | File 7-is_palindrome.c is a function that returns 1 if a string is a palindrome and 0 if not. 16 | 17 | File 100-wildcmp.c is a function that compares two strings and returns 1 if the strings can be considered identical, otherwise return 0. It uses the concept of wildcards. 18 | 19 | File _putchar.c is a file containing the _putchar function. 20 | 21 | File main.h is the header file containing the function prototypes 22 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char); 5 | void _puts_recursion(char *s); 6 | void _print_rev_recursion(char *s); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int x, int y); 10 | int _sqrt_recursion(int n); 11 | int is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - function commute srtings 5 | * @dest: param pointer to a char 6 | * @src: param pointer to a char 7 | * Return: return value of dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | while (src[j] != '\0') 23 | { 24 | dest[i] = src[j]; 25 | j++; 26 | i++; 27 | } 28 | 29 | dest[i] = '\0'; 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes used from src. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer. 5 | * @s: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | if (*(s + count) == '-') 17 | pn *= -1; 18 | 19 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 20 | { 21 | if (size > 0) 22 | m *= 10; 23 | size++; 24 | } 25 | count++; 26 | } 27 | 28 | for (i = count - size; i < count; i++) 29 | { 30 | oi = oi + ((*(s + i) - 48) * m); 31 | m /= 10; 32 | } 33 | return (oi * pn); 34 | } 35 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input stringeturn. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - prints 1 or 0 depending on input 5 | * @c: first parameter 6 | * 7 | * Description: prints all lowercase letters 8 | * Return: Always(0). 9 | */ 10 | 11 | int _islower(int c) 12 | { 13 | return (c >= 'a' && c <= 'z'); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, 5 | * @str: pointer to the string to print 6 | * Return: void 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i]) 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equals, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - Check if character is a alphabet character. 4 | * @c: type int character 5 | * Return: 1 if letter, lowercase or uppercase, and 0 otherwise 6 | */ 7 | 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 11 | return (1); 12 | 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - compute the absolute value of an integer 4 | * @n: int type number 5 | * Return: absolute value of @n 6 | */ 7 | int _abs(int n) 8 | { 9 | if (n < 0) 10 | { 11 | return (n * -1); 12 | } 13 | else 14 | { 15 | return (n); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcpy - copies the string pointed to by src, 5 | * including the terminating null byte, to the 6 | * buffer pointed to by dest. 7 | * @dest: destination. 8 | * @src: source. 9 | * Return: the pointer to dest. 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int count = 0; 14 | 15 | while (count >= 0) 16 | { 17 | *(dest + count) = *(src + count); 18 | if (*(src + count) == '\0') 19 | break; 20 | count++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C - Static libraries 2 | 3 | In this project, I learned what static libraries are and practiced creating and 4 | using them with `ar`, `ranlib`, and `nm`. 5 | 6 | ## Tests :heavy_check_mark: 7 | 8 | * [tests](./tests): Folder of test files. Provided by Holberton School. 9 | 10 | ## Tasks :page_with_curl: 11 | 12 | * **0. A library is not a luxury but one of the necessities of life** 13 | * [libmy.a](./libmy.a): C Static library containing all the functions 14 | listed below: 15 | * `int _putchar(char c);` 16 | * `int _islower(int c);` 17 | * `int _isalpha(int c);` 18 | * `int _abs(int n);` 19 | * `int _isupper(int c);` 20 | * `int _isdigit(int c);` 21 | * `int _strlen(char *s);` 22 | * `void _puts(char *s);` 23 | * `char *_strcpy(char *dest, char *src);` 24 | * `int _atoi(char *s);` 25 | * `char *_strcat(char *dest, char *src);` 26 | * `char *_strncat(char *dest, char *src, int n);` 27 | * `char *_strncpy(char *dest, char *src, int n);` 28 | * `int _strcmp(char *s1, char *s2);` 29 | * `char *_memset(char *s, char b, unsigned int n);` 30 | * `char *_memcpy(char *dest, char *src, unsigned int n);` 31 | * `char *_strchr(char *s, char c);` 32 | * `unsigned int _strspn(char *s, char *accept);` 33 | * `char *_strpbrk(char *s, char *accept);` 34 | * `char *_strstr(char *haystack, char *needle);` 35 | 36 | * [main.h](./main.h): Header file containing the prototypes of all functions 37 | included in `libholberton.a`. 38 | 39 | * **1. Without libraries what have we? We have no past and no future** 40 | * [create_static_lib.sh](./create_static_lib.sh): Bash script that creates a static 41 | library called `liball.a` from all the `.c` files in the current directory. 42 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar -rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int main(void) 4 | { 5 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 6 | return (0); 7 | } 8 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _HOLBERTON_H_ 2 | #define _HOLBERTON_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 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x09-static_libraries/quote -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints its name, followed by a new line. 4 | * @argc: number of command line arguments. 5 | * @argv: array that contains the program command line arguments. 6 | * Return: 0 - success. 7 | */ 8 | int main(int argc __attribute__((unused)), char *argv[]) 9 | { 10 | printf("%s\n", argv[0]); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints the number of arguments passed into it. 4 | * @argc: number of command line arguments. 5 | * @argv: array that contains the program command line arguments. 6 | * Return: 0 - success. 7 | */ 8 | int main(int argc, char *argv[] __attribute__((unused))) 9 | { 10 | printf("%d\n", argc - 1); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - prints the minimum number of coins to make change for an amount. 5 | * of money. 6 | * @argc: number of command line arguments. 7 | * @argv: array that contains the program command line arguments. 8 | * Return: 0 - success. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int cents, ncoins = 0; 13 | 14 | if (argc == 1 || argc > 2) 15 | { 16 | printf("Error\n"); 17 | return (1); 18 | } 19 | 20 | cents = atoi(argv[1]); 21 | 22 | while (cents > 0) 23 | { 24 | if (cents >= 25) 25 | cents -= 25; 26 | else if (cents >= 10) 27 | cents -= 10; 28 | else if (cents >= 5) 29 | cents -= 5; 30 | else if (cents >= 2) 31 | cents -= 2; 32 | else if (cents >= 1) 33 | cents -= 1; 34 | ncoins += 1; 35 | } 36 | printf("%d\n", ncoins); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - prints all arguments it receives. 4 | * @argc: number of command line arguments. 5 | * @argv: array that contains the program command line arguments. 6 | * Return: 0 - success. 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | int i; 11 | 12 | for (i = 0; i < argc; i++) 13 | printf("%s\n", argv[i]); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - multiplies two numbers. 5 | * @argc: number of command line arguments. 6 | * @argv: array that contains the program command line arguments. 7 | * Return: 0 - success. 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | if (argc != 3) 12 | { 13 | printf("Error\n"); 14 | return (1); 15 | } 16 | printf("%d\n", atoi(argv[1]) * atoi(argv[2])); 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - adds positive numbers. 6 | * @argc: number of command line arguments. 7 | * @argv: array that contains the program command line arguments. 8 | * Return: 0 - success. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int i, j, add = 0; 13 | 14 | for (i = 1; i < argc; i++) 15 | { 16 | for (j = 0; argv[i][j] != '\0'; j++) 17 | { 18 | if (!isdigit(argv[i][j])) 19 | { 20 | printf("Error\n"); 21 | return (1); 22 | } 23 | } 24 | add += atoi(argv[i]); 25 | } 26 | printf("%d\n", add); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # C - argc, argv 2 | 3 | In this project, I learned about utilizing arguments passed to C functions 4 | with `argc` and `argv` as well as how to use `__attribute__((unused))` or 5 | `void` to compile functions with unused variables or parameters. 6 | 7 | ## Tasks :page_with_curl: 8 | 9 | * **0. It ain't what they call you, it's what you answer to** 10 | * [0-whatsmyname.c](./0-whatsmyname.c): C program that prints its name, 11 | followed by a new line. 12 | * If the program is renamed, the program will print the new name without having 13 | to be compiled again. 14 | * The path should not be removed before the name of the program. 15 | 16 | * **1. Silence is argument carried out by other means** 17 | * [1-args.c](./1-args.c): C program that prints the number of arguments passed to 18 | it, followed by a new line. 19 | 20 | * **2. The best argument against democracy is a five-minute conversation with the average voter** 21 | * [2-args.c](./2-args.c): C program that prints all arguments it receives, including 22 | the first one. 23 | * Arguments are printed one per line, ending with a new line. 24 | 25 | * **3. Neither irony nor sarcasm is argument** 26 | * [3-mul.c](./3-mul.c): C program that multiplies two numbers and prints the result, 27 | followed by a new line. 28 | * The program assumes the two numbers and result of the multiplication can be 29 | stored in an `int`. 30 | * If the program does not receive two arguments, it prints `Error` followed by 31 | a new line and returns `1`. 32 | 33 | * **4. To infinity and beyond** 34 | * [4-add.c](./4-add.c): C program that adds two positive numbers and prints the result, 35 | followed by a new line. 36 | * The program assumes that the numbers and result of the addition can be stored in an `int`. 37 | * If no number is passed to the program, it prints `0` followed by a new line. 38 | * If one of the numbers contains symbols that are not digits, the program prints `Error` followed by a new line and returns `1`. 39 | 40 | * **5. Minimal Number of Coins for Change** 41 | * [100-change.c](./100-change.c): C program that prints the minimum number of coins to 42 | make change for an amount of money. 43 | * Usage: `./change cents` where `cents` is the amount of cents needed to give back. 44 | * Change can use an unlimited number of coins of values 25, 10, 5, 2 and 1 cent. 45 | * If the number passed as the argument is negative, the program prints `0` 46 | followed by a new line. 47 | * If the number of arguments passed to the program is not exactly one, 48 | it prints `Error` followed by a new line and returns `1`. 49 | -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * create_array - creates an array of chars. 5 | * @size: size of the array. 6 | * @c: storaged char 7 | * 8 | * Return: pointer of an array of chars 9 | */ 10 | char *create_array(unsigned int size, char c) 11 | { 12 | char *cr; 13 | unsigned int i; 14 | 15 | if (size == 0) 16 | return (NULL); 17 | 18 | cr = malloc(sizeof(c) * size); 19 | 20 | if (cr == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | cr[i] = c; 25 | 26 | return (cr); 27 | } 28 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strdup - returns a pointer to a newly allocated space in memory. 5 | * @str: string. 6 | * 7 | * Return: pointer of an array of chars 8 | */ 9 | char *_strdup(char *str) 10 | { 11 | char *strout; 12 | unsigned int i, j; 13 | 14 | if (str == NULL) 15 | return (NULL); 16 | 17 | for (i = 0; str[i] != '\0'; i++) 18 | ; 19 | 20 | strout = (char *)malloc(sizeof(char) * (i + 1)); 21 | 22 | if (strout == NULL) 23 | return (NULL); 24 | 25 | for (j = 0; j <= i; j++) 26 | strout[j] = str[j]; 27 | 28 | return (strout); 29 | } 30 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments of a program. 6 | * @ac: argument count. 7 | * @av: argument vector. 8 | * 9 | * Return: pointer of an array of char 10 | */ 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *aout; 14 | int c, i, j, ia; 15 | 16 | if (ac == 0) 17 | return (NULL); 18 | 19 | for (c = i = 0; i < ac; i++) 20 | { 21 | if (av[i] == NULL) 22 | return (NULL); 23 | 24 | for (j = 0; av[i][j] != '\0'; j++) 25 | c++; 26 | c++; 27 | } 28 | 29 | aout = malloc((c + 1) * sizeof(char)); 30 | 31 | if (aout == NULL) 32 | { 33 | free(aout); 34 | return (NULL); 35 | } 36 | 37 | for (i = j = ia = 0; ia < c; j++, ia++) 38 | { 39 | if (av[i][j] == '\0') 40 | { 41 | aout[ia] = '\n'; 42 | i++; 43 | ia++; 44 | j = 0; 45 | } 46 | if (ia < c - 1) 47 | aout[ia] = av[i][j]; 48 | } 49 | aout[ia] = '\0'; 50 | 51 | return (aout); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * count_word - helper function to count the number of words in a string 6 | * @s: string to evaluate 7 | * 8 | * Return: number of words 9 | */ 10 | int count_word(char *s) 11 | { 12 | int flag, c, w; 13 | 14 | flag = 0; 15 | w = 0; 16 | 17 | for (c = 0; s[c] != '\0'; c++) 18 | { 19 | if (s[c] == ' ') 20 | flag = 0; 21 | else if (flag == 0) 22 | { 23 | flag = 1; 24 | w++; 25 | } 26 | } 27 | 28 | return (w); 29 | } 30 | /** 31 | * **strtow - splits a string into words 32 | * @str: string to split 33 | * 34 | * Return: pointer to an array of strings (Success) 35 | * or NULL (Error) 36 | */ 37 | char **strtow(char *str) 38 | { 39 | char **matrix, *tmp; 40 | int i, k = 0, len = 0, words, c = 0, start, end; 41 | 42 | while (*(str + len)) 43 | len++; 44 | words = count_word(str); 45 | if (words == 0) 46 | return (NULL); 47 | 48 | matrix = (char **) malloc(sizeof(char *) * (words + 1)); 49 | if (matrix == NULL) 50 | return (NULL); 51 | 52 | for (i = 0; i <= len; i++) 53 | { 54 | if (str[i] == ' ' || str[i] == '\0') 55 | { 56 | if (c) 57 | { 58 | end = i; 59 | tmp = (char *) malloc(sizeof(char) * (c + 1)); 60 | if (tmp == NULL) 61 | return (NULL); 62 | while (start < end) 63 | *tmp++ = str[start++]; 64 | *tmp = '\0'; 65 | matrix[k] = tmp - c; 66 | k++; 67 | c = 0; 68 | } 69 | } 70 | else if (c++ == 0) 71 | start = i; 72 | } 73 | 74 | matrix[k] = NULL; 75 | 76 | return (matrix); 77 | } 78 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - concatenates two strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * 9 | * Return: pointer of an array of chars 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *strout; 14 | unsigned int i, j, k, limit; 15 | 16 | if (s1 == NULL) 17 | s1 = ""; 18 | if (s2 == NULL) 19 | s2 = ""; 20 | 21 | for (i = 0; s1[i] != '\0'; i++) 22 | ; 23 | 24 | for (j = 0; s2[j] != '\0'; j++) 25 | ; 26 | 27 | strout = malloc(sizeof(char) * (i + j + 1)); 28 | 29 | if (strout == NULL) 30 | { 31 | free(strout); 32 | return (NULL); 33 | } 34 | 35 | for (k = 0; k < i; k++) 36 | strout[k] = s1[k]; 37 | 38 | limit = j; 39 | for (j = 0; j <= limit; k++, j++) 40 | strout[k] = s2[j]; 41 | 42 | return (strout); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - returns a pointer to a 2 dimensional array of integers. 6 | * @width: width of the array. 7 | * @height: height of the array. 8 | * 9 | * Return: pointer of an array of integers 10 | */ 11 | int **alloc_grid(int width, int height) 12 | { 13 | int **gridout; 14 | int i, j; 15 | 16 | if (width < 1 || height < 1) 17 | return (NULL); 18 | 19 | gridout = malloc(height * sizeof(int *)); 20 | if (gridout == NULL) 21 | { 22 | free(gridout); 23 | return (NULL); 24 | } 25 | 26 | for (i = 0; i < height; i++) 27 | { 28 | gridout[i] = malloc(width * sizeof(int)); 29 | if (gridout[i] == NULL) 30 | { 31 | for (i--; i >= 0; i--) 32 | free(gridout[i]); 33 | free(gridout); 34 | return (NULL); 35 | } 36 | } 37 | 38 | for (i = 0; i < height; i++) 39 | for (j = 0; j < width; j++) 40 | gridout[i][j] = 0; 41 | 42 | return (gridout); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - frees a 2 dimensional grid. 6 | * @grid: multidimensional array of integers. 7 | * @height: height of the grid. 8 | * 9 | * Return: no return 10 | */ 11 | void free_grid(int **grid, int height) 12 | { 13 | if (grid != NULL && height != 0) 14 | { 15 | for (; height >= 0; height--) 16 | free(grid[height]); 17 | free(grid); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * malloc_checked - allocates memory. 5 | * @b: amount of bytes. 6 | * 7 | * Return: pointer to the allocated memory. 8 | * if malloc fails, status value is equal to 98. 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | char *p; 13 | 14 | p = malloc(b); 15 | if (p == NULL) 16 | exit(98); 17 | return (p); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - concatenates two strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * @n: amount of bytes. 9 | * 10 | * Return: pointer to the allocated memory. 11 | * if malloc fails, status value is equal to 98. 12 | */ 13 | char *string_nconcat(char *s1, char *s2, unsigned int n) 14 | { 15 | char *sout; 16 | unsigned int ls1, ls2, lsout, i; 17 | 18 | if (s1 == NULL) 19 | s1 = ""; 20 | 21 | if (s2 == NULL) 22 | s2 = ""; 23 | 24 | for (ls1 = 0; s1[ls1] != '\0'; ls1++) 25 | ; 26 | 27 | for (ls2 = 0; s2[ls2] != '\0'; ls2++) 28 | ; 29 | 30 | if (n > ls2) 31 | n = ls2; 32 | 33 | lsout = ls1 + n; 34 | 35 | sout = malloc(lsout + 1); 36 | 37 | if (sout == NULL) 38 | return (NULL); 39 | 40 | for (i = 0; i < lsout; i++) 41 | if (i < ls1) 42 | sout[i] = s1[i]; 43 | else 44 | sout[i] = s2[i - ls1]; 45 | 46 | sout[i] = '\0'; 47 | 48 | return (sout); 49 | } 50 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _realloc - reallocates a memory block. 5 | * @ptr: pointer to the memory previously allocated. 6 | * @old_size: size, in bytes, of the allocated space of ptr. 7 | * @new_size: new size, in bytes, of the new memory block. 8 | * 9 | * Return: ptr. 10 | * if new_size == old_size, returns ptr without changes. 11 | * if malloc fails, returns NULL. 12 | */ 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 14 | { 15 | if (new_size == 0 && ptr != NULL) 16 | { 17 | free(ptr); 18 | return (NULL); 19 | } 20 | 21 | if (ptr == NULL) 22 | ptr = malloc(new_size); 23 | 24 | if (new_size == old_size) 25 | return (ptr); 26 | 27 | free(ptr); 28 | ptr = malloc(new_size); 29 | 30 | return (ptr); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _calloc - allocates memory for an array. 5 | * @nmemb: number of elements. 6 | * @size: size of bytes. 7 | * 8 | * Return: pointer to the allocated memory. 9 | * if nmemb or size is 0, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | void *_calloc(unsigned int nmemb, unsigned int size) 13 | { 14 | char *p; 15 | unsigned int i; 16 | 17 | if (nmemb == 0 || size == 0) 18 | return (NULL); 19 | 20 | p = malloc(nmemb * size); 21 | 22 | if (p == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; i < (nmemb * size); i++) 26 | p[i] = 0; 27 | 28 | return (p); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * array_range - creates an array of integers. 5 | * @min: minimum value. 6 | * @max: maximum value. 7 | * 8 | * Return: pointer to the newly created array. 9 | * if man > mix, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | int *array_range(int min, int max) 13 | { 14 | int *ar; 15 | int i; 16 | 17 | if (min > max) 18 | return (NULL); 19 | 20 | ar = malloc(sizeof(*ar) * ((max - min) + 1)); 21 | 22 | if (ar == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; min <= max; i++, min++) 26 | ar[i] = min; 27 | 28 | return (ar); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | void *malloc_checked(unsigned int b); 5 | char *string_nconcat(char *s1, char *s2, unsigned int n); 6 | void *_calloc(unsigned int nmemb, unsigned int size); 7 | int *array_range(int min, int max); 8 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _OBJECT_LIKE_MACRO_ 2 | #define _OBJECT_LIKE_MACRO_ 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef _PI_ 2 | #define _PI_ 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_LIKE_MACRO_ 2 | #define _FUNCTION_LIKE_MACRO_ 3 | 4 | #define ABS(x) ((x) < (0) ? ((x) * (-1)) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUM_LIKE_MACRO_ 2 | #define _SUM_LIKE_MACRO_ 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # C - Preprocessor 2 | 3 | In this project, I learned about common predefined macros in C while 4 | practicing implementing my own and include-guarding header files. 5 | 6 | ## Tests :heavy_check_mark: 7 | 8 | * [tests](./tests): Folder of test files. Provided by Holberton School. 9 | 10 | ## Tasks :page_with_curl: 11 | 12 | * **0. Object-like Macro** 13 | * [0-object_like_macro.h](./0-object_like_macro.h): Header file that defines a 14 | macro named `SIZE` as an abbreviation for the token `1024`. 15 | 16 | * **1. Pi** 17 | * [1-pi.h](./1-pi.h): Header file that defines a macro named `PI` as an abbreviation 18 | for the token `3.14159265359`. 19 | 20 | * **2. File name** 21 | * [2-main.c](./2-main.c): C program that prints the name of the file it was 22 | compiled from followed by a new line. 23 | 24 | * **3. Function-like macro** 25 | * [3-function_like_macro.h](./3-function_like_macro.h): Header file that defines a 26 | function-like macro `ABS(x)` that computes the absolute value of a number `x`. 27 | 28 | * **4. SUM** 29 | * [4-sum.h](./4-sum.h): Header file that defines a function-like macro `SUM(x, y)` 30 | that computes the sum of the numbers `x` and `y`. 31 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - initializes a variable of type struct dog. 5 | * @d: struct dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: no return. 11 | */ 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d) 15 | { 16 | d->name = name; 17 | d->age = age; 18 | d->owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * print_dog - prints a struct dog. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (d) 13 | { 14 | if (!(d->name)) 15 | printf("Name: (nil)\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | printf("Age: %f\n", d->age); 20 | 21 | if (!(d->owner)) 22 | printf("Owner: (nil)\n"); 23 | else 24 | printf("Owner: %s\n", d->owner); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * new_dog - creates a new dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: struct dog. 11 | * if fails, returns NULL. 12 | */ 13 | dog_t *new_dog(char *name, float age, char *owner) 14 | { 15 | dog_t *p_dog; 16 | int i, lname, lowner; 17 | 18 | p_dog = malloc(sizeof(*p_dog)); 19 | if (p_dog == NULL || !(name) || !(owner)) 20 | { 21 | free(p_dog); 22 | return (NULL); 23 | } 24 | 25 | for (lname = 0; name[lname]; lname++) 26 | ; 27 | 28 | for (lowner = 0; owner[lowner]; lowner++) 29 | ; 30 | 31 | p_dog->name = malloc(lname + 1); 32 | p_dog->owner = malloc(lowner + 1); 33 | 34 | if (!(p_dog->name) || !(p_dog->owner)) 35 | { 36 | free(p_dog->owner); 37 | free(p_dog->name); 38 | free(p_dog); 39 | return (NULL); 40 | } 41 | 42 | for (i = 0; i < lname; i++) 43 | p_dog->name[i] = name[i]; 44 | p_dog->name[i] = '\0'; 45 | 46 | p_dog->age = age; 47 | 48 | for (i = 0; i < lowner; i++) 49 | p_dog->owner[i] = owner[i]; 50 | p_dog->owner[i] = '\0'; 51 | 52 | return (p_dog); 53 | } 54 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - frees dogs. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d) 13 | { 14 | free(d->name); 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # C - Structures, typedef 2 | 3 | In this project, I learned about using structures and `typedef` in C. 4 | 5 | ## Header File :file_folder: 6 | 7 | * [dog.h](./dog.h): Header file containing definitions and prototypes for all types 8 | and functions written in the project. 9 | 10 | | Type/File | Defintion/Prototype | 11 | | --------------- | ------------------------------------------------------------------------ | 12 | | `struct dog` |
  • `char *name`
  • `float age`
  • `char *owner`
| 13 | | `typedef dog_t` | `struct dog` | 14 | | `1-init_dog.c` | `void init_dog(struct dog *d, char *name, float age, char *owner);` | 15 | | `2-print_dog.c` | `void print_dog(struct dog *d);` | 16 | | `4-new_dog.c` | `dog_t *new_dog(char *name, float age, char *owner);` | 17 | | `5-free_dog.c` | `void free_dog(dog_t *d);` | 18 | 19 | ## Tasks :page_with_curl: 20 | 21 | * **0. Django** 22 | * [dog.h](./dog.h): Header file that defines a new type `struct dog` with the 23 | following elements: 24 | * `char *name` 25 | * `float age` 26 | * `char *owner` 27 | 28 | * **1. A dog is the only thing on earth that loves you more than you love yourself** 29 | * [1-init_dog.c](./1-init_dog.c): C function that initializes a variable of type `struct dog`. 30 | 31 | * **2. A dog will teach you unconditional love. If you can have that in your life, things won't be too bad** 32 | * [2-print_dog.c](./2-print_dog.c): C function that prints a `struct dog`. 33 | * If an element of `d` is `NULL`, the function prints `(nil)` instead of the element. 34 | * If `d` is `NULL`, the function prints nothing. 35 | 36 | * **3. Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to read** 37 | * [dog.h](./dog.h): Header file that defines a new type `dog_t` as a new name for the 38 | type `struct dog`. 39 | 40 | * **4. A door is what a dog is perpetually on the wrong side of** 41 | * [4-new_dog.c](./4-new_dog.c): C function that creates a dog. 42 | * Returns `NULL` if the function fails. 43 | 44 | * **5. How many legs does a dog have if you call his tail a leg? Four. Saying that a tail is a leg doesn't make it a leg** 45 | * [5-free_dog.c](./5-free_dog.c): C function that frees dogs. 46 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOG_H_ 2 | #define _DOG_H_ 3 | 4 | /** 5 | * dog_t - Typedef for struct dog 6 | */ 7 | typedef struct dog dog_t; 8 | 9 | /** 10 | * struct dog - struct that stores some information of a dog 11 | * @name: name of the dog 12 | * @age: age of the dog 13 | * @owner: owner of the dog 14 | * 15 | * Description: struct called "dog" that stores its name, its age 16 | * and the name of its owner. 17 | */ 18 | struct dog 19 | { 20 | char *name; 21 | float age; 22 | char *owner; 23 | }; 24 | 25 | void init_dog(struct dog *d, char *name, float age, char *owner); 26 | void print_dog(struct dog *d); 27 | dog_t *new_dog(char *name, float age, char *owner); 28 | void free_dog(dog_t *d); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - prints a name. 5 | * @name: input name. 6 | * @f: function pointer. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (name && f) 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - executes a function given as a 5 | * parameter on each element of an array. 6 | * @array: input integer array. 7 | * @size: size of the array. 8 | * @action: pointer to the function. 9 | * 10 | * Return: no return. 11 | */ 12 | void array_iterator(int *array, size_t size, void (*action)(int)) 13 | { 14 | unsigned int i; 15 | 16 | if (array && action) 17 | for (i = 0; i < size; i++) 18 | action(array[i]); 19 | } 20 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * @argc: argument count. 7 | * @argv: argument vector. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | char *opc = (char *) main; 14 | int i, nbytes; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | exit(1); 20 | } 21 | 22 | nbytes = atoi(argv[1]); 23 | 24 | if (nbytes < 0) 25 | { 26 | printf("Error\n"); 27 | exit(2); 28 | } 29 | 30 | for (i = 0; i < nbytes; i++) 31 | { 32 | printf("%02x", opc[i] & 0xFF); 33 | if (i != nbytes - 1) 34 | printf(" "); 35 | } 36 | 37 | printf("\n"); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer 5 | * @array: input integer array. 6 | * @size: size of the array. 7 | * @cmp: pointer to the function to be used 8 | * to compare values. 9 | * 10 | * Return: index of the first eement for which the cmp 11 | * function does not return 0. If no elements matches, 12 | * return -1. If size <= 0, return -1. 13 | */ 14 | int int_index(int *array, int size, int (*cmp)(int)) 15 | { 16 | int i; 17 | 18 | if (array && cmp) 19 | { 20 | if (size <= 0) 21 | return (-1); 22 | 23 | for (i = 0; i < size; i++) 24 | if (cmp(array[i])) 25 | return (i); 26 | } 27 | 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H_ 2 | #define _CALC_H_ 3 | 4 | #include 5 | #include 6 | /** 7 | * struct op - Struct op 8 | * 9 | * @op: The operator 10 | * @f: The function associated 11 | */ 12 | typedef struct op 13 | { 14 | char *op; 15 | int (*f)(int a, int b); 16 | } op_t; 17 | 18 | int op_add(int a, int b); 19 | int op_sub(int a, int b); 20 | int op_mul(int a, int b); 21 | int op_div(int a, int b); 22 | int op_mod(int a, int b); 23 | int (*get_op_func(char *s))(int, int); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * get_op_func - selects the correct function to perform 5 | * the operation asked by the user. 6 | * @s: char operator. 7 | * 8 | * Return: pointer to the function that corresponds to the operator. 9 | */ 10 | int (*get_op_func(char *s))(int, int) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | int i = 0; 21 | 22 | while (i < 10) 23 | { 24 | if (s[0] == ops->op[i]) 25 | break; 26 | i++; 27 | } 28 | 29 | return (ops[i / 2].f); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * @argc: argument count. 6 | * @argv: argument vector. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int a, b; 13 | int (*operation)(int, int); 14 | 15 | if (argc != 4) 16 | { 17 | printf("Error\n"); 18 | exit(98); 19 | } 20 | 21 | if (argv[2][1]) 22 | { 23 | printf("Error\n"); 24 | exit(99); 25 | } 26 | 27 | operation = get_op_func(argv[2]); 28 | 29 | if (operation == NULL) 30 | { 31 | printf("Error\n"); 32 | exit(99); 33 | } 34 | 35 | a = atoi(argv[1]); 36 | b = atoi(argv[3]); 37 | 38 | printf("%d\n", operation(a, b)); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * op_add - adds two numbers. 5 | * @a: first number. 6 | * @b: second number. 7 | * 8 | * Return: add. 9 | */ 10 | int op_add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | 15 | /** 16 | * op_sub - subctracts two numbers. 17 | * @a: first number. 18 | * @b: second number. 19 | * 20 | * Return: difference. 21 | */ 22 | int op_sub(int a, int b) 23 | { 24 | return (a - b); 25 | } 26 | 27 | /** 28 | * op_mul - multiplies two numbers. 29 | * @a: first number. 30 | * @b: second number. 31 | * 32 | * Return: multiplication. 33 | */ 34 | int op_mul(int a, int b) 35 | { 36 | return (a * b); 37 | } 38 | 39 | /** 40 | * op_div - divides two numbers. 41 | * @a: first number. 42 | * @b: second number. 43 | * 44 | * Return: division. 45 | */ 46 | int op_div(int a, int b) 47 | { 48 | if (b == 0) 49 | { 50 | printf("Error\n"); 51 | exit(100); 52 | } 53 | return (a / b); 54 | } 55 | 56 | /** 57 | * op_mod - calculates the module of two numbers. 58 | * @a: first number. 59 | * @b: second number. 60 | * 61 | * Return: remainder of the division. 62 | */ 63 | int op_mod(int a, int b) 64 | { 65 | if (b == 0) 66 | { 67 | printf("Error\n"); 68 | exit(100); 69 | } 70 | return (a % b); 71 | } 72 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # Function pointers 2 | Project done during **Full Stack Software Engineering studies** at **ALX**. It aims to learn about function pointers in **C language**. 3 | 4 | ## Technologies 5 | * C files are compiled using `gcc 4.8.4` 6 | * C files are written according to the C90 standard 7 | * Tested on Ubuntu 14.04 LTS 8 | 9 | ## Files 10 | All of the following files are programs written in C: 11 | 12 | | Filename | Description | 13 | | -------- | ----------- | 14 | | `0-print_name.c` | Prints a name | 15 | | `1-array_iterator.c` | Executes a function given as a parameter on each element of an array | 16 | | `2-int_index.c` | Searches for an integer | 17 | | `3-calc.h` | Header file that contains all function prototypes and data structures | 18 | | `3-op_functions.c` | File that contains functions that calculates arithmetic operations | 19 | | `3-get_op_func.c` | Function pointer that returns the pointer of the arithmetic functions | 20 | | `3-main.c` | Entry point | 21 | | `100-main_opcodes.c` | Prints the opcodes of its own main function | 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_ 2 | #define _FUNCTION_POINTERS_ 3 | 4 | #include 5 | 6 | void print_name(char *name, void (*f)(char *)); 7 | void array_iterator(int *array, size_t size, void (*action)(int)); 8 | int int_index(int *array, int size, int (*cmp)(int)); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 0-sum_them_all.c 3 | * Auth: Tolulope Fakunle 4 | */ 5 | 6 | #include "variadic_functions.h" 7 | #include 8 | 9 | /** 10 | * sum_them_all - Returns the sum of all its paramters. 11 | * @n: The number of paramters passed to the function. 12 | * @...: A variable number of paramters to calculate the sum of. 13 | * 14 | * Return: If n == 0 - 0. 15 | * Otherwise - the sum of all parameters. 16 | */ 17 | int sum_them_all(const unsigned int n, ...) 18 | { 19 | va_list nums; 20 | unsigned int index, sum = 0; 21 | 22 | va_start(nums, n); 23 | 24 | for (index = 0; index < n; index++) 25 | sum += va_arg(nums, int); 26 | 27 | va_end(nums); 28 | 29 | return (sum); 30 | } 31 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 1-print_numbers.c 3 | * Auth: Tolulope Fakunle 4 | */ 5 | 6 | #include "variadic_functions.h" 7 | #include 8 | #include 9 | 10 | /** 11 | * print_numbers - Prints numbers, followed by a new line. 12 | * @separator: The string to be printed between numbers. 13 | * @n: The number of integers passed to the function. 14 | * @...: A variable number of numbers to be printed. 15 | */ 16 | void print_numbers(const char *separator, const unsigned int n, ...) 17 | { 18 | va_list nums; 19 | unsigned int index; 20 | 21 | va_start(nums, n); 22 | 23 | for (index = 0; index < n; index++) 24 | { 25 | printf("%d", va_arg(nums, int)); 26 | 27 | if (index != (n - 1) && separator != NULL) 28 | printf("%s", separator); 29 | } 30 | 31 | printf("\n"); 32 | 33 | va_end(nums); 34 | } 35 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 2-print_strings.c 3 | * Auth: Tolulope Fakunle 4 | */ 5 | 6 | #include "variadic_functions.h" 7 | #include 8 | #include 9 | 10 | /** 11 | * print_strings - Prints strings, followed by a new line. 12 | * @separator: The string to be printed between strings. 13 | * @n: The number of strings passed to the function. 14 | * @...: A variable number of strings to be printed. 15 | * 16 | * Description: If separator is NULL, it is not printed. 17 | * If one of the strings if NULL, (nil) is printed instead. 18 | */ 19 | void print_strings(const char *separator, const unsigned int n, ...) 20 | { 21 | va_list strings; 22 | char *str; 23 | unsigned int index; 24 | 25 | va_start(strings, n); 26 | 27 | for (index = 0; index < n; index++) 28 | { 29 | str = va_arg(strings, char *); 30 | 31 | if (str == NULL) 32 | printf("(nil)"); 33 | else 34 | printf("%s", str); 35 | 36 | if (index != (n - 1) && separator != NULL) 37 | printf("%s", separator); 38 | } 39 | 40 | printf("\n"); 41 | 42 | va_end(strings); 43 | } 44 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 3-print_all.c 3 | * Auth: Tolulope Fakunle 4 | */ 5 | 6 | #include "variadic_functions.h" 7 | #include 8 | #include 9 | 10 | void print_char(va_list arg); 11 | void print_int(va_list arg); 12 | void print_float(va_list arg); 13 | void print_string(va_list arg); 14 | void print_all(const char * const format, ...); 15 | 16 | /** 17 | * print_char - Prints a char. 18 | * @arg: A list of arguments pointing to 19 | * the character to be printed. 20 | */ 21 | void print_char(va_list arg) 22 | { 23 | char letter; 24 | 25 | letter = va_arg(arg, int); 26 | printf("%c", letter); 27 | } 28 | 29 | /** 30 | * print_int - Prints an int. 31 | * @arg: A list of arguments pointing to 32 | * the integer to be printed. 33 | */ 34 | void print_int(va_list arg) 35 | { 36 | int num; 37 | 38 | num = va_arg(arg, int); 39 | printf("%d", num); 40 | } 41 | 42 | /** 43 | * print_float - Prints a float. 44 | * @arg: A list of arguments pointing to 45 | * the float to be printed. 46 | */ 47 | void print_float(va_list arg) 48 | { 49 | float num; 50 | 51 | num = va_arg(arg, double); 52 | printf("%f", num); 53 | } 54 | 55 | /** 56 | * print_string - Prints a string. 57 | * @arg: A list of arguments pointing to 58 | * the string to be printed. 59 | */ 60 | void print_string(va_list arg) 61 | { 62 | char *str; 63 | 64 | str = va_arg(arg, char *); 65 | 66 | if (str == NULL) 67 | { 68 | printf("(nil)"); 69 | return; 70 | } 71 | 72 | printf("%s", str); 73 | } 74 | 75 | /** 76 | * print_all - Prints anything, followed by a new line. 77 | * @format: A string of characters representing the argument types. 78 | * @...: A variable number of arguments to be printed. 79 | * 80 | * Description: Any argument not of type char, int, float, 81 | * or char * is ignored. 82 | * If a string argument is NULL, (nil) is printed instead. 83 | */ 84 | void print_all(const char * const format, ...) 85 | { 86 | va_list args; 87 | int i = 0, j = 0; 88 | char *separator = ""; 89 | printer_t funcs[] = { 90 | {"c", print_char}, 91 | {"i", print_int}, 92 | {"f", print_float}, 93 | {"s", print_string} 94 | }; 95 | 96 | va_start(args, format); 97 | 98 | while (format && (*(format + i))) 99 | { 100 | j = 0; 101 | 102 | while (j < 4 && (*(format + i) != *(funcs[j].symbol))) 103 | j++; 104 | 105 | if (j < 4) 106 | { 107 | printf("%s", separator); 108 | funcs[j].print(args); 109 | separator = ", "; 110 | } 111 | 112 | i++; 113 | } 114 | 115 | printf("\n"); 116 | 117 | va_end(args); 118 | } 119 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | 4 | /* 5 | * File: variadic_functions.h 6 | * Auth: Tolulope Fakunle 7 | * Desc: Header file containing prototypes for all functions 8 | * used in the 0x0F-variadic_functions directory. 9 | */ 10 | 11 | #include 12 | 13 | /** 14 | * struct printer - A new struct type defining a printer. 15 | * @symbol: A symbol representing a data type. 16 | * @print: A function pointer to a function that prints 17 | * a data type corresponding to symbol. 18 | */ 19 | typedef struct printer 20 | { 21 | char *symbol; 22 | void (*print)(va_list arg); 23 | 24 | } printer_t; 25 | 26 | int sum_them_all(const unsigned int n, ...); 27 | void print_numbers(const char *separator, const unsigned int n, ...); 28 | void print_strings(const char *separator, const unsigned int n, ...); 29 | void print_all(const char * const format, ...); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_list - prints all the elements of a list_t list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t print_list(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | if (h->str == NULL) 16 | printf("[%d] %s\n", 0, "(nil)"); 17 | else 18 | printf("[%d] %s\n", h->len, h->str); 19 | h = h->next; 20 | nelem++; 21 | } 22 | return (nelem); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * list_len - returns then number of elements in a list. 4 | * @h: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t list_len(const list_t *h) 9 | { 10 | size_t nelem; 11 | 12 | nelem = 0; 13 | while (h != NULL) 14 | { 15 | h = h->next; 16 | nelem++; 17 | } 18 | return (nelem); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * bmain - function executed before main 5 | * Return: no return. 6 | */ 7 | 8 | void __attribute__ ((constructor)) bmain() 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node - adds a new node at the beginning 4 | * of a list_t list. 5 | * @head: head of the linked list. 6 | * @str: string to store in the list. 7 | * Return: address of the head. 8 | */ 9 | 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *new; 13 | size_t nchar; 14 | 15 | new = malloc(sizeof(list_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->str = strdup(str); 20 | 21 | for (nchar = 0; str[nchar]; nchar++) 22 | ; 23 | 24 | new->len = nchar; 25 | new->next = *head; 26 | *head = new; 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - adds a new node at the end 5 | * of a list_t list. 6 | * @head: head of the linked list. 7 | * @str: string to store in the list. 8 | * Return: address of the head. 9 | */ 10 | 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *new, *temp; 14 | size_t nchar; 15 | 16 | new = malloc(sizeof(list_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->str = strdup(str); 21 | 22 | for (nchar = 0; str[nchar]; nchar++) 23 | ; 24 | 25 | new->len = nchar; 26 | new->next = NULL; 27 | temp = *head; 28 | 29 | if (temp == NULL) 30 | { 31 | *head = new; 32 | } 33 | else 34 | { 35 | while (temp->next != NULL) 36 | temp = temp->next; 37 | temp->next = new; 38 | } 39 | 40 | return (*head); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list 5 | * @head: head of the linked list. 6 | * Return: no return. 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *current; 12 | 13 | while ((current = head) != NULL) 14 | { 15 | head = head->next; 16 | free(current->str); 17 | free(current); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # C - Singly linked lists 2 | 3 | In this project, I practiced building and using singly linked lists 4 | in C while learning when and why to use linked lists versus arrays. 5 | 6 | ## Header File :file_folder: 7 | 8 | * [lists.h](./lists.h): Header file containing definitions and prototypes for all 9 | types and functions written for the project. 10 | 11 | | Type/File | Definition/Prototype | 12 | | ------------------ | -------------------------------------------------------------------------------------- | 13 | | `struct list_s` |
  • `char *str`
  • `unsigned int len`
  • `struct list_s *next`
| 14 | | `typedef list_t` | `struct list_s` | 15 | | `0-print_list.c` | `size_t print_list(const list_t *h);` | 16 | | `1-list_len.c` | `size_t list_len(const list_t *h);` | 17 | | `2-add_node.c` | `list_t *add_node(list_t **head, const char *str);` | 18 | | `3-add_node_end.c` | `list_t *add_node_end(list_t **head, const char *str);` | 19 | | `4-free_list.c` | `void free_list(list_t *head)` | 20 | 21 | ## Tasks :page_with_curl: 22 | 23 | * **0. Print list** 24 | * [0-print_list.c](./0-print_list.c): C function that prints all the 25 | elements of a `list_t` list. 26 | * If `str` is `NULL`, the function prints `[0] (nil)`. 27 | 28 | * **1. List length** 29 | * [1-list_len.c](./1-list_len.c): C function that returns the number of elements 30 | in a linked `list_t` list. 31 | 32 | * **2. Add node** 33 | * [2-add_node.c](./2-add_node.c): C function that adds a new node at the 34 | beginning a of a `list_t` list. 35 | * If the function fails - returns `NULL`. 36 | * Otherwise - returns the address of the new element. 37 | 38 | * **3. Add node at the end** 39 | * [3-add_node_end.c](./3-add_node_end.c): C function that adds a new node at 40 | the end of a linked `list_t` list. 41 | * If the function fails - returns `NULL`. 42 | * Otherwise - returns the address of the new element. 43 | 44 | * **4. Free list** 45 | * [4-free_list.c](./4-free_list.c): C function that frees a `list_t` list. 46 | 47 | * **5. The Hare and the Tortoise** 48 | * [100-first.c](./100-first.c): C function that prints `You're beat! and 49 | yet, you must allow,\nI bore my house upon my back!\n` before the `main` 50 | function is executed. 51 | 52 | * **6. Real programmers can write assembly code in any language** 53 | * [101-hello_holberton.asm](./101-hello_holberton.asm): 64-but assembly program 54 | that prints `Hello, Holberton` followed by a new line using only the 55 | `printf` function witout interrupts. 56 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct list_s 18 | { 19 | char *str; 20 | unsigned int len; 21 | struct list_s *next; 22 | } list_t; 23 | 24 | size_t print_list(const list_t *h); 25 | size_t list_len(const list_t *h); 26 | list_t *add_node(list_t **head, const char *str); 27 | list_t *add_node_end(list_t **head, const char *str); 28 | void free_list(list_t *head); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the elements of a list. 5 | * @h: head of a list. 6 | * 7 | * Return: numbers of nodes. 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t nnodes = 0; 12 | 13 | while (h != NULL) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns the number of elements in 5 | * a linked list. 6 | * @h: head of a list. 7 | * 8 | * Return: numbers of nodes. 9 | */ 10 | size_t listint_len(const listint_t *h) 11 | { 12 | size_t nnodes = 0; 13 | 14 | while (h != NULL) 15 | { 16 | h = h->next; 17 | nnodes++; 18 | } 19 | return (nnodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes the node at index 5 | * of a linked list. 6 | * @head: head of a list. 7 | * @index: index of the list where the node is 8 | * deleted. 9 | * 10 | * Return: 1 if it succeeded, -1 if it failed. 11 | */ 12 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 13 | { 14 | unsigned int i; 15 | listint_t *prev; 16 | listint_t *next; 17 | 18 | prev = *head; 19 | 20 | if (index != 0) 21 | { 22 | for (i = 0; i < index - 1 && prev != NULL; i++) 23 | { 24 | prev = prev->next; 25 | } 26 | } 27 | 28 | if (prev == NULL || (prev->next == NULL && index != 0)) 29 | { 30 | return (-1); 31 | } 32 | 33 | next = prev->next; 34 | 35 | if (index != 0) 36 | { 37 | prev->next = next->next; 38 | free(next); 39 | } 40 | else 41 | { 42 | free(prev); 43 | *head = next; 44 | } 45 | 46 | return (1); 47 | } 48 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: pointer to the first node. 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *p; 12 | listint_t *n; 13 | 14 | p = NULL; 15 | n = NULL; 16 | 17 | while (*head != NULL) 18 | { 19 | n = (*head)->next; 20 | (*head)->next = p; 21 | p = *head; 22 | *head = n; 23 | } 24 | 25 | *head = p; 26 | return (*head); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * print_listint_safe - prints a linked list. 28 | * @head: head of a list. 29 | * 30 | * Return: number of nodes in the list. 31 | */ 32 | size_t print_listint_safe(const listint_t *head) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | 37 | hptr = NULL; 38 | while (head != NULL) 39 | { 40 | new = malloc(sizeof(listp_t)); 41 | 42 | if (new == NULL) 43 | exit(98); 44 | 45 | new->p = (void *)head; 46 | new->next = hptr; 47 | hptr = new; 48 | 49 | add = hptr; 50 | 51 | while (add->next != NULL) 52 | { 53 | add = add->next; 54 | if (head == add->p) 55 | { 56 | printf("-> [%p] %d\n", (void *)head, head->n); 57 | free_listp(&hptr); 58 | return (nnodes); 59 | } 60 | } 61 | 62 | printf("[%p] %d\n", (void *)head, head->n); 63 | head = head->next; 64 | nnodes++; 65 | } 66 | 67 | free_listp(&hptr); 68 | return (nnodes); 69 | } 70 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp2(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * free_listint_safe - frees a linked list. 28 | * @h: head of a list. 29 | * 30 | * Return: size of the list that was freed. 31 | */ 32 | size_t free_listint_safe(listint_t **h) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | listint_t *curr; 37 | 38 | hptr = NULL; 39 | while (*h != NULL) 40 | { 41 | new = malloc(sizeof(listp_t)); 42 | 43 | if (new == NULL) 44 | exit(98); 45 | 46 | new->p = (void *)*h; 47 | new->next = hptr; 48 | hptr = new; 49 | 50 | add = hptr; 51 | 52 | while (add->next != NULL) 53 | { 54 | add = add->next; 55 | if (*h == add->p) 56 | { 57 | *h = NULL; 58 | free_listp2(&hptr); 59 | return (nnodes); 60 | } 61 | } 62 | 63 | curr = *h; 64 | *h = (*h)->next; 65 | free(curr); 66 | nnodes++; 67 | } 68 | 69 | *h = NULL; 70 | free_listp2(&hptr); 71 | return (nnodes); 72 | } 73 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - finds the loop in a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: the address of the node where the loop starts. 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *p2; 12 | listint_t *prev; 13 | 14 | p2 = head; 15 | prev = head; 16 | while (head && p2 && p2->next) 17 | { 18 | head = head->next; 19 | p2 = p2->next->next; 20 | 21 | if (head == p2) 22 | { 23 | head = prev; 24 | prev = p2; 25 | while (1) 26 | { 27 | p2 = prev; 28 | while (p2->next != head && p2->next != prev) 29 | { 30 | p2 = p2->next; 31 | } 32 | if (p2->next == head) 33 | break; 34 | 35 | head = head->next; 36 | } 37 | return (p2->next); 38 | } 39 | } 40 | 41 | return (NULL); 42 | } 43 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - add a new node at the beginning 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | 15 | new = malloc(sizeof(listint_t)); 16 | 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->next = *head; 22 | *head = new; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - add a new node at the end 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint_end(listint_t **head, const int n) 12 | { 13 | listint_t *new; 14 | listint_t *temp; 15 | 16 | (void)temp; 17 | 18 | new = malloc(sizeof(listint_t)); 19 | 20 | if (new == NULL) 21 | return (NULL); 22 | 23 | new->n = n; 24 | new->next = NULL; 25 | temp = *head; 26 | if (*head == NULL) 27 | { 28 | *head = new; 29 | } 30 | else 31 | { 32 | while (temp->next != NULL) 33 | { 34 | temp = temp->next; 35 | } 36 | temp->next = new; 37 | } 38 | 39 | return (*head); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *temp; 12 | 13 | while ((temp = head) != NULL) 14 | { 15 | head = head->next; 16 | free(temp); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint2(listint_t **head) 10 | { 11 | listint_t *temp; 12 | listint_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes the head node of 5 | * a linked list 6 | * @head: head of a list. 7 | * 8 | * Return: head node's data. 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | int hnode; 13 | listint_t *h; 14 | listint_t *curr; 15 | 16 | if (*head == NULL) 17 | return (0); 18 | 19 | curr = *head; 20 | 21 | hnode = curr->n; 22 | 23 | h = curr->next; 24 | 25 | free(curr); 26 | 27 | *head = h; 28 | 29 | return (hnode); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the nth node of a linked list 5 | * @head: head of a list. 6 | * @index: index of the node. 7 | * 8 | * Return: nth node. If node does not exist, returns NULL. 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < index && head != NULL; i++) 15 | { 16 | head = head->next; 17 | } 18 | 19 | return (head); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - returns the sum of all the data (n) of 5 | * a linked list. 6 | * @head: head of a list. 7 | * 8 | * Return: sum of all the data (n). 9 | */ 10 | int sum_listint(listint_t *head) 11 | { 12 | int sum; 13 | 14 | sum = 0; 15 | while (head != NULL) 16 | { 17 | sum += head->n; 18 | head = head->next; 19 | } 20 | 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a new node 5 | * at a given position. 6 | * @head: head of a list. 7 | * @idx: index of the list where the new node is 8 | * added. 9 | * @n: integer element. 10 | * 11 | * Return: the address of the new node, or NULL if it 12 | * failed. 13 | */ 14 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 15 | { 16 | unsigned int i; 17 | listint_t *new; 18 | listint_t *h; 19 | 20 | h = *head; 21 | 22 | if (idx != 0) 23 | { 24 | for (i = 0; i < idx - 1 && h != NULL; i++) 25 | { 26 | h = h->next; 27 | } 28 | } 29 | 30 | if (h == NULL && idx != 0) 31 | return (NULL); 32 | 33 | new = malloc(sizeof(listint_t)); 34 | if (new == NULL) 35 | return (NULL); 36 | 37 | new->n = n; 38 | 39 | if (idx == 0) 40 | { 41 | new->next = *head; 42 | *head = new; 43 | } 44 | else 45 | { 46 | new->next = h->next; 47 | h->next = new; 48 | } 49 | 50 | return (new); 51 | } 52 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * @n: integer 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | * for Holberton project 14 | */ 15 | typedef struct listint_s 16 | { 17 | int n; 18 | struct listint_s *next; 19 | } listint_t; 20 | 21 | /** 22 | * struct listp_s - singly linked list 23 | * @p: pointers of nodes 24 | * @next: points to the next node 25 | * 26 | * Description: singly linked list of pointers 27 | */ 28 | typedef struct listp_s 29 | { 30 | void *p; 31 | struct listp_s *next; 32 | } listp_t; 33 | 34 | size_t print_listint(const listint_t *h); 35 | size_t listint_len(const listint_t *h); 36 | listint_t *add_nodeint(listint_t **head, const int n); 37 | listint_t *add_nodeint_end(listint_t **head, const int n); 38 | void free_listint(listint_t *head); 39 | void free_listint2(listint_t **head); 40 | int pop_listint(listint_t **head); 41 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 42 | int sum_listint(listint_t *head); 43 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 44 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 45 | listint_t *reverse_listint(listint_t **head); 46 | size_t print_listint_safe(const listint_t *head); 47 | size_t free_listint_safe(listint_t **h); 48 | listint_t *find_listint_loop(listint_t *head); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to an 5 | * unsigned int. 6 | * @b: binary. 7 | * 8 | * Return: unsigned int. 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int ui; 13 | int len, base_two; 14 | 15 | if (!b) 16 | return (0); 17 | 18 | ui = 0; 19 | 20 | for (len = 0; b[len] != '\0'; len++) 21 | ; 22 | 23 | for (len--, base_two = 1; len >= 0; len--, base_two *= 2) 24 | { 25 | if (b[len] != '0' && b[len] != '1') 26 | { 27 | return (0); 28 | } 29 | 30 | if (b[len] & 1) 31 | { 32 | ui += base_two; 33 | } 34 | } 35 | 36 | return (ui); 37 | } 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary representation 5 | * of a number. 6 | * @n: unsigned long int. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_binary(unsigned long int n) 11 | { 12 | if (n >> 0) 13 | { 14 | if (n >> 1) 15 | print_binary(n >> 1); 16 | _putchar((n & 1) + '0'); 17 | } 18 | else 19 | { 20 | _putchar('0'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * 6 | * Return: 0 if big endian, 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | unsigned int x; 11 | char *c; 12 | 13 | x = 1; 14 | c = (char *) &x; 15 | 16 | return ((int)*c); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given 5 | * index. 6 | * @n: unsigned long int input. 7 | * @index: index of the bit. 8 | * 9 | * Return: value of the bit. 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (n == 0 && index < 64) 16 | return (0); 17 | 18 | for (i = 0; i <= 63; n >>= 1, i++) 19 | { 20 | if (index == i) 21 | { 22 | return (n & 1); 23 | } 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets the value of a bit to 1. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | *n = (*n | m); 20 | 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a bit to 0. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int m; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | m = 1 << index; 19 | 20 | if (*n & m) 21 | *n ^= m; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - returns the number of bits you would 5 | * need to flip to get from one number to another 6 | * @n: number one. 7 | * @m: number two. 8 | * 9 | * Return: number of bits. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned int nbits; 14 | 15 | for (nbits = 0; n || m; n >>= 1, m >>= 1) 16 | { 17 | if ((n & 1) != (m & 1)) 18 | nbits++; 19 | } 20 | 21 | return (nbits); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | unsigned int binary_to_uint(const char *b); 5 | void print_binary(unsigned long int n); 6 | int _putchar(char c); 7 | int get_bit(unsigned long int n, unsigned int index); 8 | int set_bit(unsigned long int *n, unsigned int index); 9 | int clear_bit(unsigned long int *n, unsigned int index); 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 11 | int get_endianness(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - reads a text file and prints the letters 5 | * @filename: filename. 6 | * @letters: numbers of letters printed. 7 | * 8 | * Return: numbers of letters printed. It fails, returns 0. 9 | */ 10 | ssize_t read_textfile(const char *filename, size_t letters) 11 | { 12 | int fd; 13 | ssize_t nrd, nwr; 14 | char *buf; 15 | 16 | if (!filename) 17 | return (0); 18 | 19 | fd = open(filename, O_RDONLY); 20 | 21 | if (fd == -1) 22 | return (0); 23 | 24 | buf = malloc(sizeof(char) * (letters)); 25 | if (!buf) 26 | return (0); 27 | 28 | nrd = read(fd, buf, letters); 29 | nwr = write(STDOUT_FILENO, buf, nrd); 30 | 31 | close(fd); 32 | 33 | free(buf); 34 | 35 | return (nwr); 36 | } 37 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file 5 | * @filename: filename. 6 | * @text_content: content writed in the file. 7 | * 8 | * Return: 1 if it success. -1 if it fails. 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | int nletters; 14 | int rwr; 15 | 16 | if (!filename) 17 | return (-1); 18 | 19 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 20 | 21 | if (fd == -1) 22 | return (-1); 23 | 24 | if (!text_content) 25 | text_content = ""; 26 | 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | 35 | close(fd); 36 | 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text at the end of a file 5 | * @filename: filename. 6 | * @text_content: added content. 7 | * 8 | * Return: 1 if the file exists. -1 if the fails does not exist 9 | * or if it fails. 10 | */ 11 | int append_text_to_file(const char *filename, char *text_content) 12 | { 13 | int fd; 14 | int nletters; 15 | int rwr; 16 | 17 | if (!filename) 18 | return (-1); 19 | 20 | fd = open(filename, O_WRONLY | O_APPEND); 21 | 22 | if (fd == -1) 23 | return (-1); 24 | 25 | if (text_content) 26 | { 27 | for (nletters = 0; text_content[nletters]; nletters++) 28 | ; 29 | 30 | rwr = write(fd, text_content, nletters); 31 | 32 | if (rwr == -1) 33 | return (-1); 34 | } 35 | 36 | close(fd); 37 | 38 | return (1); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * error_file - checks if files can be opened. 6 | * @file_from: file_from. 7 | * @file_to: file_to. 8 | * @argv: arguments vector. 9 | * Return: no return. 10 | */ 11 | void error_file(int file_from, int file_to, char *argv[]) 12 | { 13 | if (file_from == -1) 14 | { 15 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 16 | exit(98); 17 | } 18 | if (file_to == -1) 19 | { 20 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]); 21 | exit(99); 22 | } 23 | } 24 | 25 | /** 26 | * main - check the code for Holberton School students. 27 | * @argc: number of arguments. 28 | * @argv: arguments vector. 29 | * Return: Always 0. 30 | */ 31 | int main(int argc, char *argv[]) 32 | { 33 | int file_from, file_to, err_close; 34 | ssize_t nchars, nwr; 35 | char buf[1024]; 36 | 37 | if (argc != 3) 38 | { 39 | dprintf(STDERR_FILENO, "%s\n", "Usage: cp file_from file_to"); 40 | exit(97); 41 | } 42 | 43 | file_from = open(argv[1], O_RDONLY); 44 | file_to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0664); 45 | error_file(file_from, file_to, argv); 46 | 47 | nchars = 1024; 48 | while (nchars == 1024) 49 | { 50 | nchars = read(file_from, buf, 1024); 51 | if (nchars == -1) 52 | error_file(-1, 0, argv); 53 | nwr = write(file_to, buf, nchars); 54 | if (nwr == -1) 55 | error_file(0, -1, argv); 56 | } 57 | 58 | err_close = close(file_from); 59 | if (err_close == -1) 60 | { 61 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 62 | exit(100); 63 | } 64 | 65 | err_close = close(file_to); 66 | if (err_close == -1) 67 | { 68 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 69 | exit(100); 70 | } 71 | return (0); 72 | } 73 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all the elements of a 5 | * dlistint_t list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | printf("%d\n", h->n); 25 | count++; 26 | h = h->next; 27 | } 28 | 29 | return (count); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements in 5 | * a double linked list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | count++; 25 | h = h->next; 26 | } 27 | 28 | return (count); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * f4 - finds the biggest number 7 | * 8 | * @usrn: username 9 | * @len: length of username 10 | * Return: the biggest number 11 | */ 12 | int f4(char *usrn, int len) 13 | { 14 | int ch; 15 | int vch; 16 | unsigned int rand_num; 17 | 18 | ch = *usrn; 19 | vch = 0; 20 | 21 | while (vch < len) 22 | { 23 | if (ch < usrn[vch]) 24 | ch = usrn[vch]; 25 | vch += 1; 26 | } 27 | 28 | srand(ch ^ 14); 29 | rand_num = rand(); 30 | 31 | return (rand_num & 63); 32 | } 33 | 34 | /** 35 | * f5 - multiplies each char of username 36 | * 37 | * @usrn: username 38 | * @len: length of username 39 | * Return: multiplied char 40 | */ 41 | int f5(char *usrn, int len) 42 | { 43 | int ch; 44 | int vch; 45 | 46 | ch = vch = 0; 47 | 48 | while (vch < len) 49 | { 50 | ch = ch + usrn[vch] * usrn[vch]; 51 | vch += 1; 52 | } 53 | 54 | return (((unsigned int)ch ^ 239) & 63); 55 | } 56 | 57 | /** 58 | * f6 - generates a random char 59 | * 60 | * @usrn: username 61 | * Return: a random char 62 | */ 63 | int f6(char *usrn) 64 | { 65 | int ch; 66 | int vch; 67 | 68 | ch = vch = 0; 69 | 70 | while (vch < *usrn) 71 | { 72 | ch = rand(); 73 | vch += 1; 74 | } 75 | 76 | return (((unsigned int)ch ^ 229) & 63); 77 | } 78 | 79 | /** 80 | * main - Entry point 81 | * 82 | * @argc: arguments count 83 | * @argv: arguments vector 84 | * Return: Always 0 85 | */ 86 | int main(int argc, char **argv) 87 | { 88 | char keygen[7]; 89 | int len, ch, vch; 90 | long alph[] = { 91 | 0x3877445248432d41, 0x42394530534e6c37, 0x4d6e706762695432, 92 | 0x74767a5835737956, 0x2b554c59634a474f, 0x71786636576a6d34, 93 | 0x723161513346655a, 0x6b756f494b646850 }; 94 | (void) argc; 95 | 96 | for (len = 0; argv[1][len]; len++) 97 | ; 98 | /* ----------- f1 ----------- */ 99 | keygen[0] = ((char *)alph)[(len ^ 59) & 63]; 100 | /* ----------- f2 ----------- */ 101 | ch = vch = 0; 102 | while (vch < len) 103 | { 104 | ch = ch + argv[1][vch]; 105 | vch = vch + 1; 106 | } 107 | keygen[1] = ((char *)alph)[(ch ^ 79) & 63]; 108 | /* ----------- f3 ----------- */ 109 | ch = 1; 110 | vch = 0; 111 | while (vch < len) 112 | { 113 | ch = argv[1][vch] * ch; 114 | vch = vch + 1; 115 | } 116 | keygen[2] = ((char *)alph)[(ch ^ 85) & 63]; 117 | /* ----------- f4 ----------- */ 118 | keygen[3] = ((char *)alph)[f4(argv[1], len)]; 119 | /* ----------- f5 ----------- */ 120 | keygen[4] = ((char *)alph)[f5(argv[1], len)]; 121 | /* ----------- f6 ----------- */ 122 | keygen[5] = ((char *)alph)[f6(argv[1])]; 123 | keygen[6] = '\0'; 124 | for (ch = 0; keygen[ch]; ch++) 125 | printf("%c", keygen[ch]); 126 | return (0); 127 | } 128 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds a new node at the beginning 5 | * of a dlistint_t list 6 | * 7 | * @head: head of the list 8 | * @n: value of the element 9 | * Return: the address of the new element 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | dlistint_t *h; 15 | 16 | new = malloc(sizeof(dlistint_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->prev = NULL; 22 | h = *head; 23 | 24 | if (h != NULL) 25 | { 26 | while (h->prev != NULL) 27 | h = h->prev; 28 | } 29 | 30 | new->next = h; 31 | 32 | if (h != NULL) 33 | h->prev = new; 34 | 35 | *head = new; 36 | 37 | return (new); 38 | } 39 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds a new node at the end 5 | * of a dlistint_t list 6 | * 7 | * @head: head of the list 8 | * @n: value of the element 9 | * Return: the address of the new element 10 | */ 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *h; 14 | dlistint_t *new; 15 | 16 | new = malloc(sizeof(dlistint_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->next = NULL; 22 | 23 | h = *head; 24 | 25 | if (h != NULL) 26 | { 27 | while (h->next != NULL) 28 | h = h->next; 29 | h->next = new; 30 | } 31 | else 32 | { 33 | *head = new; 34 | } 35 | 36 | new->prev = h; 37 | 38 | return (new); 39 | } 40 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a dlistint_t list 5 | * 6 | * @head: head of the list 7 | * Return: no return 8 | */ 9 | void free_dlistint(dlistint_t *head) 10 | { 11 | dlistint_t *tmp; 12 | 13 | if (head != NULL) 14 | while (head->prev != NULL) 15 | head = head->prev; 16 | 17 | while ((tmp = head) != NULL) 18 | { 19 | head = head->next; 20 | free(tmp); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - returns the 5 | * nth node of a dlistint_t linked list 6 | * 7 | * @head: head of the list 8 | * @index: index of the nth node 9 | * Return: nth node 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | 18 | while (head->prev != NULL) 19 | head = head->prev; 20 | 21 | i = 0; 22 | 23 | while (head != NULL) 24 | { 25 | if (i == index) 26 | break; 27 | head = head->next; 28 | i++; 29 | } 30 | 31 | return (head); 32 | } 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all the data (n) 5 | * of a doubly linked list 6 | * 7 | * @head: head of the list 8 | * Return: sum of the data 9 | */ 10 | int sum_dlistint(dlistint_t *head) 11 | { 12 | int sum; 13 | 14 | sum = 0; 15 | 16 | if (head != NULL) 17 | { 18 | while (head->prev != NULL) 19 | head = head->prev; 20 | 21 | while (head != NULL) 22 | { 23 | sum += head->n; 24 | head = head->next; 25 | } 26 | } 27 | 28 | return (sum); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - inserts a new node at 5 | * a given position 6 | * 7 | * @h: head of the list 8 | * @idx: index of the new node 9 | * @n: value of the new node 10 | * Return: the address of the new node, or NULL if it failed 11 | */ 12 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 13 | { 14 | dlistint_t *new; 15 | dlistint_t *head; 16 | unsigned int i; 17 | 18 | new = NULL; 19 | if (idx == 0) 20 | new = add_dnodeint(h, n); 21 | else 22 | { 23 | head = *h; 24 | i = 1; 25 | if (head != NULL) 26 | while (head->prev != NULL) 27 | head = head->prev; 28 | while (head != NULL) 29 | { 30 | if (i == idx) 31 | { 32 | if (head->next == NULL) 33 | new = add_dnodeint_end(h, n); 34 | else 35 | { 36 | new = malloc(sizeof(dlistint_t)); 37 | if (new != NULL) 38 | { 39 | new->n = n; 40 | new->next = head->next; 41 | new->prev = head; 42 | head->next->prev = new; 43 | head->next = new; 44 | } 45 | } 46 | break; 47 | } 48 | head = head->next; 49 | i++; 50 | } 51 | } 52 | 53 | return (new); 54 | } 55 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - deletes the node at index of a 5 | * dlistint_t linked list 6 | * 7 | * @head: head of the list 8 | * @index: index of the new node 9 | * Return: 1 if it succeeded, -1 if it failed 10 | */ 11 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 12 | { 13 | dlistint_t *h1; 14 | dlistint_t *h2; 15 | unsigned int i; 16 | 17 | h1 = *head; 18 | 19 | if (h1 != NULL) 20 | while (h1->prev != NULL) 21 | h1 = h1->prev; 22 | 23 | i = 0; 24 | 25 | while (h1 != NULL) 26 | { 27 | if (i == index) 28 | { 29 | if (i == 0) 30 | { 31 | *head = h1->next; 32 | if (*head != NULL) 33 | (*head)->prev = NULL; 34 | } 35 | else 36 | { 37 | h2->next = h1->next; 38 | 39 | if (h1->next != NULL) 40 | h1->next->prev = h2; 41 | } 42 | 43 | free(h1); 44 | return (1); 45 | } 46 | h2 = h1; 47 | h1 = h1->next; 48 | i++; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * for Holberton project 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -fPIC -c *.c 3 | gcc -shared -o liball.so *.o 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp https://github.com/Tolulope05/alx-low_level_programming/raw/master/0x18-dynamic_libraries/nrandom.so 3 | export LD_PRELOAD=/tmp/nrandom.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # 0x18. C - Dynamic libraries 2 | 3 | In this project, i learnt about what dynamic library is, how it works, how to create one and how it works. It also helped me understand the differences between static and shared libraries. 4 | 5 | ## Function Prototypes :floppy_disk: 6 | The following files are scripts written for the projects written in C: 7 | 8 | 9 | ## Table of contents 10 | Files | Description 11 | ----- | ----------- 12 | [libdynamic.so](./libdynamic.so) | C dynamic library containing the function definitions 13 | [main.h](./main.h) | Header files containing the function prototypes 14 | [1-create_dynamic_lib.sh](./1-create_dynamic_lib.sh) | Bash script that creates a dynamic library called liball.so from all the .c files that are in the current directory 15 | [100-operations.so](./100-operations.so) | C dynamic library that contains C functions that can be called from Python 16 | [random.so](./random.so) | C dynamic library to inject in a giga million program 17 | [101-make_me_win.sh](./101-make_me_win.sh) | Bash script to inject the libmask.so library, using LD_PRELOAD, in the giga million program 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_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 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/nrandom.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tolulope05/alx-low_level_programming/2c6b78d96d035389275d1c8ef601053dc8c95877/0x18-dynamic_libraries/nrandom.so -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Creates a hash table. 5 | * @size: The size of the array. 6 | * 7 | * Return: If an error occurs - NULL. 8 | * Otherwise - a pointer to the new hash table. 9 | */ 10 | hash_table_t *hash_table_create(unsigned long int size) 11 | { 12 | hash_table_t *ht; 13 | unsigned long int i; 14 | 15 | ht = malloc(sizeof(hash_table_t)); 16 | if (ht == NULL) 17 | return (NULL); 18 | 19 | ht->size = size; 20 | ht->array = malloc(sizeof(hash_node_t *) * size); 21 | if (ht->array == NULL) 22 | return (NULL); 23 | for (i = 0; i < size; i++) 24 | ht->array[i] = NULL; 25 | 26 | return (ht); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - Hash function implementing the djb2 algorithm. 5 | * @str: The string to hash. 6 | * 7 | * Return: The calculated hash. 8 | */ 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hash; 12 | int c; 13 | 14 | hash = 5381; 15 | while ((c = *str++)) 16 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 17 | 18 | return (hash); 19 | } 20 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - Get the index at which a key/value 5 | * pair should be stored in array of a hash table. 6 | * @key: The key to get the index of. 7 | * @size: The size of the array of the hash table. 8 | * 9 | * Return: The index of the key. 10 | * Description: Uses the djb2 algorithm. 11 | */ 12 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 13 | { 14 | return (hash_djb2(key) % size); 15 | } 16 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_set - Add or update an element in a hash table. 5 | * @ht: A pointer to the hash table. 6 | * @key: The key to add - cannot be an empty string. 7 | * @value: The value associated with key. 8 | * 9 | * Return: Upon failure - 0. 10 | * Otherwise - 1. 11 | */ 12 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 13 | { 14 | hash_node_t *new; 15 | char *value_copy; 16 | unsigned long int index, i; 17 | 18 | if (ht == NULL || key == NULL || *key == '\0' || value == NULL) 19 | return (0); 20 | 21 | value_copy = strdup(value); 22 | if (value_copy == NULL) 23 | return (0); 24 | 25 | index = key_index((const unsigned char *)key, ht->size); 26 | for (i = index; ht->array[i]; i++) 27 | { 28 | if (strcmp(ht->array[i]->key, key) == 0) 29 | { 30 | free(ht->array[i]->value); 31 | ht->array[i]->value = value_copy; 32 | return (1); 33 | } 34 | } 35 | 36 | new = malloc(sizeof(hash_node_t)); 37 | if (new == NULL) 38 | { 39 | free(value_copy); 40 | return (0); 41 | } 42 | new->key = strdup(key); 43 | if (new->key == NULL) 44 | { 45 | free(new); 46 | return (0); 47 | } 48 | new->value = value_copy; 49 | new->next = ht->array[index]; 50 | ht->array[index] = new; 51 | 52 | return (1); 53 | } 54 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - Retrieve the value associated with 5 | * a key in a hash table. 6 | * @ht: A pointer to the hash table. 7 | * @key: The key to get the value of. 8 | * 9 | * Return: If the key cannot be matched - NULL. 10 | * Otherwise - the value associated with key in ht. 11 | */ 12 | char *hash_table_get(const hash_table_t *ht, const char *key) 13 | { 14 | hash_node_t *node; 15 | unsigned long int index; 16 | 17 | if (ht == NULL || key == NULL || *key == '\0') 18 | return (NULL); 19 | 20 | index = key_index((const unsigned char *)key, ht->size); 21 | if (index >= ht->size) 22 | return (NULL); 23 | 24 | node = ht->array[index]; 25 | while (node && strcmp(node->key, key) != 0) 26 | node = node->next; 27 | 28 | return ((node == NULL) ? NULL : node->value); 29 | } 30 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - Prints a hash table. 5 | * @ht: A pointer to the hash table to print. 6 | * 7 | * Description: Key/value pairs are printed in the order 8 | * they appear in the array of the hash table. 9 | */ 10 | void hash_table_print(const hash_table_t *ht) 11 | { 12 | hash_node_t *node; 13 | unsigned long int i; 14 | unsigned char comma_flag = 0; 15 | 16 | if (ht == NULL) 17 | return; 18 | 19 | printf("{"); 20 | for (i = 0; i < ht->size; i++) 21 | { 22 | if (ht->array[i] != NULL) 23 | { 24 | if (comma_flag == 1) 25 | printf(", "); 26 | 27 | node = ht->array[i]; 28 | while (node != NULL) 29 | { 30 | printf("'%s': '%s'", node->key, node->value); 31 | node = node->next; 32 | if (node != NULL) 33 | printf(", "); 34 | } 35 | comma_flag = 1; 36 | } 37 | } 38 | printf("}\n"); 39 | } 40 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - Deletes a hash table. 5 | * @ht: A pointer to a hash table. 6 | */ 7 | void hash_table_delete(hash_table_t *ht) 8 | { 9 | hash_table_t *head = ht; 10 | hash_node_t *node, *tmp; 11 | unsigned long int i; 12 | 13 | for (i = 0; i < ht->size; i++) 14 | { 15 | if (ht->array[i] != NULL) 16 | { 17 | node = ht->array[i]; 18 | while (node != NULL) 19 | { 20 | tmp = node->next; 21 | free(node->key); 22 | free(node->value); 23 | free(node); 24 | node = tmp; 25 | } 26 | } 27 | } 28 | free(head->array); 29 | free(head); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%p\n", (void *)ht); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | shash_table_t *ht; 14 | 15 | ht = shash_table_create(1024); 16 | shash_table_set(ht, "y", "0"); 17 | shash_table_print(ht); 18 | shash_table_set(ht, "j", "1"); 19 | shash_table_print(ht); 20 | shash_table_set(ht, "c", "2"); 21 | shash_table_print(ht); 22 | shash_table_set(ht, "b", "3"); 23 | shash_table_print(ht); 24 | shash_table_set(ht, "z", "4"); 25 | shash_table_print(ht); 26 | shash_table_set(ht, "n", "5"); 27 | shash_table_print(ht); 28 | shash_table_set(ht, "a", "6"); 29 | shash_table_print(ht); 30 | shash_table_set(ht, "m", "7"); 31 | shash_table_print(ht); 32 | shash_table_print_rev(ht); 33 | shash_table_delete(ht); 34 | return (EXIT_SUCCESS); 35 | } 36 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | unsigned long int hash_table_array_size; 15 | 16 | hash_table_array_size = 1024; 17 | s = "cisfun"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 20 | s = "Don't forget to tweet today"; 21 | printf("%lu\n", hash_djb2((unsigned char *)s)); 22 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 23 | s = "98"; 24 | printf("%lu\n", hash_djb2((unsigned char *)s)); 25 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 26 | return (EXIT_SUCCESS); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_set(ht, "betty", "holberton"); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | char *value; 15 | 16 | ht = hash_table_create(1024); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_set(ht, "c", "isfun"); 25 | 26 | value = hash_table_get(ht, "python"); 27 | printf("%s:%s\n", "python", value); 28 | value = hash_table_get(ht, "Jennie"); 29 | printf("%s:%s\n", "Jennie", value); 30 | value = hash_table_get(ht, "N"); 31 | printf("%s:%s\n", "N", value); 32 | value = hash_table_get(ht, "Asterix"); 33 | printf("%s:%s\n", "Asterix", value); 34 | value = hash_table_get(ht, "Betty"); 35 | printf("%s:%s\n", "Betty", value); 36 | value = hash_table_get(ht, "98"); 37 | printf("%s:%s\n", "98", value); 38 | value = hash_table_get(ht, "c"); 39 | printf("%s:%s\n", "c", value); 40 | value = hash_table_get(ht, "javascript"); 41 | printf("%s:%s\n", "javascript", value); 42 | return (EXIT_SUCCESS); 43 | } 44 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_print(ht); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_print(ht); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | char *key; 15 | char *value; 16 | 17 | ht = hash_table_create(1024); 18 | hash_table_set(ht, "c", "fun"); 19 | hash_table_set(ht, "python", "awesome"); 20 | hash_table_set(ht, "Jennie", "and Jay love asm"); 21 | hash_table_set(ht, "N", "queens"); 22 | hash_table_set(ht, "Asterix", "Obelix"); 23 | hash_table_set(ht, "Betty", "Holberton"); 24 | hash_table_set(ht, "98", "Battery Streetz"); 25 | key = strdup("Tim"); 26 | value = strdup("Britton"); 27 | hash_table_set(ht, key, value); 28 | key[0] = '\0'; 29 | value[0] = '\0'; 30 | free(key); 31 | free(value); 32 | hash_table_set(ht, "98", "Battery Street"); 33 | hash_table_set(ht, "hetairas", "Jennie"); 34 | hash_table_set(ht, "hetairas", "Jennie Z"); 35 | hash_table_set(ht, "mentioner", "Jennie"); 36 | hash_table_set(ht, "hetairas", "Jennie Z Chu"); 37 | hash_table_print(ht); 38 | hash_table_delete(ht); 39 | return (EXIT_SUCCESS); 40 | } 41 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | all: $(SRC) 4 | $(CC) $(SRC) -o school 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean: 11 | $(RM) *~ $(NAME) 12 | 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: fclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Return the perimiter of an island. 7 | 8 | The grid represents water by 0 and land by 1. 9 | 10 | Args: 11 | grid (list): A list of list of integers representing an island. 12 | Returns: 13 | The perimeter of the island defined in grid. 14 | """ 15 | width = len(grid[0]) 16 | height = len(grid) 17 | edges = 0 18 | size = 0 19 | 20 | for i in range(height): 21 | for j in range(width): 22 | if grid[i][j] == 1: 23 | size += 1 24 | if (j > 0 and grid[i][j - 1] == 1): 25 | edges += 1 26 | if (i > 0 and grid[i - 1][j] == 1): 27 | edges += 1 28 | return size * 4 - edges * 2 29 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_school 5 | * 6 | * Return: Nothing 7 | */ 8 | void print_school(void) 9 | { 10 | printf("j#0000000000000000000000000000000000000\n"); 11 | printf("j#000000000000000000@Q**g00000000000000\n"); 12 | printf("j#0000000000000000*]++]4000000000000000\n"); 13 | printf("j#000000000000000k]++]++*N#000000000000\n"); 14 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 15 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 16 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 17 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 18 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 19 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 20 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 21 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 22 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 23 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 24 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 25 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 26 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 27 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 28 | printf("j#00000000000w]++++]*0##000000000000000\n"); 29 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 30 | printf("j#00000000000000000we]+]Q00000000000000\n"); 31 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 32 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 33 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 34 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-makefiles/tests/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 5 | * integers using the Linear search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: Always EXIT_SUCCESS 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < (int)size; i++) 20 | { 21 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 22 | if (value == array[i]) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * recursive_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int recursive_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | return ((int)half); 33 | 34 | if (value < array[half]) 35 | return (recursive_search(array, half, value)); 36 | 37 | half++; 38 | 39 | return (recursive_search(array + half, size - half, value) + half); 40 | } 41 | 42 | /** 43 | * binary_search - calls to binary_search to return 44 | * the index of the number 45 | * 46 | * @array: input array 47 | * @size: size of the array 48 | * @value: value to search in 49 | * Return: index of the number 50 | */ 51 | int binary_search(int *array, size_t size, int value) 52 | { 53 | int index; 54 | 55 | index = recursive_search(array, size, value); 56 | 57 | if (index >= 0 && array[index] != value) 58 | return (-1); 59 | 60 | return (index); 61 | } 62 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | int index, m, k, prev; 16 | 17 | if (array == NULL || size == 0) 18 | return (-1); 19 | 20 | m = (int)sqrt((double)size); 21 | k = 0; 22 | prev = index = 0; 23 | 24 | do { 25 | printf("Value checked array[%d] = [%d]\n", index, array[index]); 26 | 27 | if (array[index] == value) 28 | return (index); 29 | k++; 30 | prev = index; 31 | index = k * m; 32 | } while (index < (int)size && array[index] < value); 33 | 34 | printf("Value found between indexes [%d] and [%d]\n", prev, index); 35 | 36 | for (; prev <= index && prev < (int)size; prev++) 37 | { 38 | printf("Value checked array[%d] = [%d]\n", prev, array[prev]); 39 | if (array[prev] == value) 40 | return (prev); 41 | } 42 | 43 | return (-1); 44 | } 45 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in an array of 5 | * integers using the Interpolation search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: index of the number 11 | */ 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t pos, low, high; 15 | double f; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | low = 0; 21 | high = size - 1; 22 | 23 | while (size) 24 | { 25 | f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]); 26 | pos = (size_t)(low + f); 27 | printf("Value checked array[%d]", (int)pos); 28 | 29 | if (pos >= size) 30 | { 31 | printf(" is out of range\n"); 32 | break; 33 | } 34 | else 35 | { 36 | printf(" = [%d]\n", array[pos]); 37 | } 38 | 39 | if (array[pos] == value) 40 | return ((int)pos); 41 | 42 | if (array[pos] < value) 43 | low = pos + 1; 44 | else 45 | high = pos - 1; 46 | 47 | if (low == high) 48 | break; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * _binary_search - Searches for a value in a sorted array 5 | * of integers using binary search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @left: The starting index of the [sub]array to search. 8 | * @right: The ending index of the [sub]array to search. 9 | * @value: The value to search for. 10 | * 11 | * Return: If the value is not present or the array is NULL, -1. 12 | * Otherwise, the index where the value is located. 13 | * 14 | * Description: Prints the [sub]array being searched after each change. 15 | */ 16 | int _binary_search(int *array, size_t left, size_t right, int value) 17 | { 18 | size_t i; 19 | 20 | if (array == NULL) 21 | return (-1); 22 | 23 | while (right >= left) 24 | { 25 | printf("Searching in array: "); 26 | for (i = left; i < right; i++) 27 | printf("%d, ", array[i]); 28 | printf("%d\n", array[i]); 29 | 30 | i = left + (right - left) / 2; 31 | if (array[i] == value) 32 | return (i); 33 | if (array[i] > value) 34 | right = i - 1; 35 | else 36 | left = i + 1; 37 | } 38 | 39 | return (-1); 40 | } 41 | 42 | /** 43 | * exponential_search - Searches for a value in a sorted array 44 | * of integers using exponential search. 45 | * @array: A pointer to the first element of the array to search. 46 | * @size: The number of elements in the array. 47 | * @value: The value to search for. 48 | * 49 | * Return: If the value is not present or the array is NULL, -1. 50 | * Otherwise, the index where the value is located. 51 | * 52 | * Description: Prints a value every time it is compared in the array. 53 | */ 54 | int exponential_search(int *array, size_t size, int value) 55 | { 56 | size_t i = 0, right; 57 | 58 | if (array == NULL) 59 | return (-1); 60 | 61 | if (array[0] != value) 62 | { 63 | for (i = 1; i < size && array[i] <= value; i = i * 2) 64 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 65 | } 66 | 67 | right = i < size ? i : size - 1; 68 | printf("Value found between indexes [%ld] and [%ld]\n", i / 2, right); 69 | return (_binary_search(array, i / 2, right, value)); 70 | } 71 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * rec_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int rec_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | { 33 | if (half > 0) 34 | return (rec_search(array, half + 1, value)); 35 | return ((int)half); 36 | } 37 | 38 | if (value < array[half]) 39 | return (rec_search(array, half + 1, value)); 40 | 41 | half++; 42 | return (rec_search(array + half, size - half, value) + half); 43 | } 44 | 45 | /** 46 | * advanced_binary - calls to rec_search to return 47 | * the index of the number 48 | * 49 | * @array: input array 50 | * @size: size of the array 51 | * @value: value to search in 52 | * Return: index of the number 53 | */ 54 | int advanced_binary(int *array, size_t size, int value) 55 | { 56 | int index; 57 | 58 | index = rec_search(array, size, value); 59 | 60 | if (index >= 0 && array[index] != value) 61 | return (-1); 62 | 63 | return (index); 64 | } 65 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @list: input list 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | listint_t *jump_list(listint_t *list, size_t size, int value) 14 | { 15 | size_t index, k, m; 16 | listint_t *prev; 17 | 18 | if (list == NULL || size == 0) 19 | return (NULL); 20 | 21 | m = (size_t)sqrt((double)size); 22 | index = 0; 23 | k = 0; 24 | 25 | do { 26 | prev = list; 27 | k++; 28 | index = k * m; 29 | 30 | while (list->next && list->index < index) 31 | list = list->next; 32 | 33 | if (list->next == NULL && index != list->index) 34 | index = list->index; 35 | 36 | printf("Value checked at index [%d] = [%d]\n", (int)index, list->n); 37 | 38 | } while (index < size && list->next && list->n < value); 39 | 40 | printf("Value found between indexes "); 41 | printf("[%d] and [%d]\n", (int)prev->index, (int)list->index); 42 | 43 | for (; prev && prev->index <= list->index; prev = prev->next) 44 | { 45 | printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n); 46 | if (prev->n == value) 47 | return (prev); 48 | } 49 | 50 | return (NULL); 51 | } 52 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - searches for a value in a skip list 5 | * 6 | * @list: input list 7 | * @value: value to search in 8 | * Return: index of the number 9 | */ 10 | skiplist_t *linear_skip(skiplist_t *list, int value) 11 | { 12 | skiplist_t *go; 13 | 14 | if (list == NULL) 15 | return (NULL); 16 | 17 | go = list; 18 | 19 | do { 20 | list = go; 21 | go = go->express; 22 | printf("Value checked at index "); 23 | printf("[%d] = [%d]\n", (int)go->index, go->n); 24 | } while (go->express && go->n < value); 25 | 26 | if (go->express == NULL) 27 | { 28 | list = go; 29 | while (go->next) 30 | go = go->next; 31 | } 32 | 33 | printf("Value found between indexes "); 34 | printf("[%d] and [%d]\n", (int)list->index, (int)go->index); 35 | 36 | while (list != go->next) 37 | { 38 | printf("Value checked at index [%d] = [%d]\n", (int)list->index, list->n); 39 | if (list->n == value) 40 | return (list); 41 | list = list->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/README.md: -------------------------------------------------------------------------------- 1 | # Search Algorithms 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SORTING_ALGOS_H_ 2 | #define _SORTING_ALGOS_H_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * 10 | * @n: Integer 11 | * @index: Index of the node in the list 12 | * @next: Pointer to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct listint_s 18 | { 19 | int n; 20 | size_t index; 21 | struct listint_s *next; 22 | } listint_t; 23 | 24 | /** 25 | * struct skiplist_s - Singly linked list with an express lane 26 | * 27 | * @n: Integer 28 | * @index: Index of the node in the list 29 | * @next: Pointer to the next node 30 | * @express: Pointer to the next node in the express lane 31 | * 32 | * Description: singly linked list node structure with an express lane 33 | * for Holberton project 34 | */ 35 | typedef struct skiplist_s 36 | { 37 | int n; 38 | size_t index; 39 | struct skiplist_s *next; 40 | struct skiplist_s *express; 41 | } skiplist_t; 42 | 43 | int linear_search(int *array, size_t size, int value); 44 | int binary_search(int *array, size_t size, int value); 45 | int jump_search(int *array, size_t size, int value); 46 | int interpolation_search(int *array, size_t size, int value); 47 | int exponential_search(int *array, size_t size, int value); 48 | int advanced_binary(int *array, size_t size, int value); 49 | listint_t *jump_list(listint_t *list, size_t size, int value); 50 | skiplist_t *linear_skip(skiplist_t *list, int value); 51 | 52 | #endif 53 | --------------------------------------------------------------------------------