├── .gitmodules ├── 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 ├── 10-print_comb2.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-holberton.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 └── holberton.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── holberton.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 └── holberton.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── holberton.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-print_number.c ├── 101-magic.c ├── 102-infinite_add.c ├── 103-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── 8-rot13.c ├── README.md ├── _putchar.c └── holberton.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── 9-set_string.c ├── README.md └── holberton.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── 7-is_palindrome.c ├── README.md └── holberton.h ├── 0x09-static_libraries ├── README.md ├── create_static_lib.sh ├── holberton.h └── libholberton.a ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── 5-argstostr.c ├── README.md └── holberton.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── holberton.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 ├── 100-hello_holberton.asm ├── 2-main.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 ├── 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 └── holberton.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── holberton.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 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 ├── README.md ├── holberton.h └── libholberton.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 ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py └── README.md ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "printf"] 2 | path = printf 3 | url = https://github.com/Theemiss/printf.git 4 | [submodule "monty"] 5 | path = monty 6 | url = https://github.com/Theemiss/monty 7 | [submodule "simple_shell"] 8 | path = simple_shell 9 | url = https://github.com/Theemiss/simple_shell.git 10 | -------------------------------------------------------------------------------- /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 - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | write(1, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 12 | return (1); 13 | } 14 | -------------------------------------------------------------------------------- /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 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/5-printf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | printf("with proper grammar, but the outcome is a piece of art,\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | printf("Size of a char: %i byte(s)\n", sizeof(char)); 11 | printf("Size of an int: %i byte(s)\n", sizeof(int)); 12 | printf("Size of a long int: %i byte(s)\n", sizeof(long int)); 13 | printf("Size of a long long int: %i byte(s)\n", sizeof(long long int)); 14 | printf("Size of a float: %i byte(s)\n", sizeof(float)); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | First Project In C 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* betty style doc for function main goes there */ 6 | /** 7 | * main - Entry point 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(void) 12 | { 13 | int n; 14 | 15 | srand(time(0)); 16 | n = rand() - RAND_MAX / 2; 17 | if (n > 0) 18 | printf("%i is positive\n", n); 19 | else if (n == 0) 20 | printf("%i is zero\n", n); 21 | else if (n < 0) 22 | printf("%i is negative\n", n); 23 | 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n, r; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | r = n % 10; 17 | 18 | if (r > 5) 19 | printf("Last digit of %i is %i and is greater than 5\n", n, r); 20 | else if (r == 0) 21 | printf("Last digit of %i is %i and is 0\n", n, r); 22 | else if (r < 6) 23 | printf("Last digit of %i is %i and is less than 6 and not 0\n", n, r); 24 | 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/10-print_comb2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | int i, j; 12 | 13 | for (i = 48; i <= 57; i++) 14 | { 15 | j = 48; 16 | while (j <= 57) 17 | { 18 | putchar(i); 19 | putchar(j); 20 | if ((i == 57) && (j == 57)) 21 | { 22 | putchar('\n'); 23 | } 24 | else 25 | { 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | j++; 30 | } 31 | } 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i, j; 11 | 12 | i = 48; 13 | j = 49; 14 | while (i < 58) 15 | { 16 | putchar(i); 17 | putchar(j); 18 | if ((i == 56) && (j == 57)) 19 | { 20 | putchar('\n'); 21 | i++; 22 | j++; 23 | } 24 | else 25 | { 26 | putchar(','); 27 | putchar(' '); 28 | } 29 | if (j < 57) 30 | { 31 | j++; 32 | } 33 | else 34 | { 35 | i++; 36 | j = i + 1; 37 | } 38 | } 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i, j, y; 11 | 12 | i = 48; 13 | j = 49; 14 | y = 50; 15 | while ((i < 56) && (j < 57) && (y < 58)) 16 | { 17 | putchar(i); 18 | putchar(j); 19 | putchar(y); 20 | if ((i == 55) && (j == 56) && (y == 57)) 21 | { 22 | putchar('\n'); 23 | i++; 24 | j++; 25 | y++; 26 | } 27 | else 28 | { 29 | putchar(','); 30 | putchar(' '); 31 | if (y < 57) 32 | { 33 | y++; 34 | } 35 | else if ((j < 56) && (y == 57)) 36 | { 37 | y = 1 + ++j; 38 | } 39 | else 40 | { 41 | j = ++i + 1; 42 | y = j + 1; 43 | } 44 | } 45 | } 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i <= 98; i++) 13 | { 14 | for (j = i + 1; j <= 99; j++) 15 | { 16 | putchar((i / 10) + '0'); 17 | putchar((i % 10) + '0'); 18 | putchar(' '); 19 | putchar((j / 10) + '0'); 20 | putchar((j % 10) + '0'); 21 | if ((i == 98) && (j == 99)) 22 | continue; 23 | putchar(','); 24 | putchar(' '); 25 | } 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | for (c = 'a'; c <= 'z'; ++c) 13 | putchar(c); 14 | putchar('\n'); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | c = 'a'; 13 | while (c <= 'z') 14 | { 15 | putchar(c); 16 | ++c; 17 | } 18 | c = 'A'; 19 | while (c <= 'Z') 20 | { 21 | putchar(c); 22 | ++c; 23 | } 24 | putchar('\n'); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | c = 'a'; 13 | while (c <= 'z') 14 | { 15 | if ((c != 'e') && (c != 'q')) 16 | { 17 | putchar(c); 18 | ++c; 19 | } 20 | else 21 | ++c; 22 | } 23 | putchar('\n'); 24 | 25 | return (0); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int n; 11 | 12 | for (n = 0; n <= 9; n++) 13 | printf("%i", n); 14 | 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | 12 | for (a = 48; a < 58; a++) 13 | putchar(a); 14 | putchar('\n'); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | 12 | for (c = 'z'; c >= 'a'; --c) 13 | putchar(c); 14 | putchar('\n'); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | int n; 12 | 13 | for (n = 48; n < 58; n++) 14 | { 15 | putchar(n); 16 | } 17 | for (c = 'a'; c <= 'f'; c++) 18 | { 19 | putchar(c); 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int a; 11 | 12 | a = 48; 13 | while (a < 58) 14 | { 15 | putchar(a); 16 | if (a != 57) 17 | { 18 | putchar(44); 19 | putchar(32); 20 | a++; 21 | } 22 | else 23 | { 24 | a++; 25 | } 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | Loops 2 | variable 3 | condition -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-holberton.c: -------------------------------------------------------------------------------- 1 | #include"holberton.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | _putchar('H'); 11 | _putchar('o'); 12 | _putchar('l'); 13 | _putchar('b'); 14 | _putchar('e'); 15 | _putchar('r'); 16 | _putchar('t'); 17 | _putchar('o'); 18 | _putchar('n'); 19 | _putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_alphabet - check the code for Holberton School students. 5 | * Description: Prints the alphabet with _putchar 6 | * Return: Always 0. 7 | */ 8 | void print_alphabet(void) 9 | { 10 | char c; 11 | 12 | for (c = 'a'; c <= 'z'; c++) 13 | { 14 | _putchar(c); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * add - add 2 integer. 5 | *@a:integer. 6 | *@b:integer. 7 | * Return: Always 0. 8 | */ 9 | int add(int a, int b) 10 | { 11 | return (a + b); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | *print_times_table - print time table. 5 | * @n: integer. 6 | *Return:void. 7 | */ 8 | void print_times_table(int n) 9 | { 10 | int i, j, k; 11 | 12 | if (n >= 0 && n <= 15) 13 | { 14 | for (i = 0; i <= n; i++) 15 | { 16 | _putchar('0'); 17 | if (n != 0) 18 | _putchar(','); 19 | for (j = 1; j <= n; j++) 20 | { 21 | k = i * j; 22 | if (k < 10) 23 | { 24 | _putchar(' '); 25 | _putchar(' '); 26 | _putchar(' '); 27 | _putchar('0' + k); 28 | } 29 | else if (k < 100) 30 | { 31 | _putchar(' '); 32 | _putchar(' '); 33 | _putchar((k / 10) + '0'); 34 | _putchar((k % 10) + '0'); 35 | } 36 | else 37 | { 38 | _putchar(' '); 39 | _putchar((k / 100) + '0'); 40 | _putchar(((k % 100) / 10) + '0'); 41 | _putchar(((k % 100) % 10) + '0'); 42 | } 43 | if (j < n) 44 | _putchar(','); 45 | } 46 | _putchar('\n'); 47 | } 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - count multiple. 4 | * 5 | * Return:void 6 | */ 7 | int main(void) 8 | { 9 | int a, b, s, x, y; 10 | 11 | s = 0; 12 | for (a = 0; a <= (1024 / 3); a++) 13 | { 14 | y = a * 3; 15 | s = s + y; 16 | } 17 | for (b = 0; b < (1024 / 5); b++) 18 | { 19 | if (!(b % 3 == 0)) 20 | { 21 | x = b * 5; 22 | s = s + x; 23 | } 24 | } 25 | printf("%i\n", s); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - first 50 fibonatchi. 5 | * 6 | * Return: void. 7 | */ 8 | int main(void) 9 | { 10 | long int a, b, s, i; 11 | 12 | a = 1; 13 | b = 2; 14 | s = 3; 15 | printf("%ld, ", a); 16 | printf("%ld, ", b); 17 | for (i = 3; i < 50; i++) 18 | { 19 | printf("%ld, ", s); 20 | a = b; 21 | b = s; 22 | s = a + b; 23 | } 24 | printf("%ld\n", s); 25 | 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - even libber abbaci 4 | * 5 | * Return: void 6 | */ 7 | int main(void) 8 | { 9 | unsigned int i, j, y, k, s; 10 | 11 | i = 1; 12 | j = 2; 13 | y = 3; 14 | k = 2; 15 | for (s = 2; s <= 32; s++) 16 | { 17 | if (y % 2 == 0) 18 | k = k + y; 19 | i = j; 20 | j = y; 21 | y = i + j; 22 | } 23 | printf("%u\n", k); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - first 50 fibonatchi. 5 | * 6 | * Return: void. 7 | */ 8 | int main(void) 9 | { 10 | unsigned long int i, i0, i1, j, j0, j1, e, e0, e1, y, k; 11 | 12 | i = 1; 13 | j = 2; 14 | e = i + j; 15 | 16 | printf("%lu, ", i); 17 | printf("%lu, ", j); 18 | for (y = 3; y < 89; y++) 19 | { 20 | printf("%lu, ", e); 21 | i = j; 22 | j = e; 23 | e = i + j; 24 | } 25 | j0 = j / 1000000000; 26 | j1 = j % 1000000000; 27 | e0 = e / 1000000000; 28 | e1 = e % 1000000000; 29 | for (k = 89; k < 98; k++) 30 | { 31 | printf("%lu%lu, ", e0, e1); 32 | i0 = j0; 33 | i1 = j1; 34 | j0 = e0; 35 | j1 = e1; 36 | e0 = i0 + j0 + ((i1 + j1) / 1000000000); 37 | e1 = (i1 + j1) % 1000000000; 38 | } 39 | printf("%lu%lu\n", e0, e1); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_to_98 - count from n to 98. 6 | *@n:integer. 7 | * Return: Always 0. 8 | */ 9 | void print_to_98(int n) 10 | { 11 | while (n != 98) 12 | { 13 | printf("%i, ", n); 14 | if (n > 98) 15 | n--; 16 | else 17 | n++; 18 | } 19 | printf("98\n"); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_alphabet_x10 - print the alphabet 10 time 5 | * Descreption: using _putchar 6 | * Return: Always 0. 7 | */ 8 | void print_alphabet_x10(void) 9 | { 10 | char c; 11 | int i; 12 | 13 | for (i = 1; i <= 10; i++) 14 | { 15 | for (c = 'a'; c <= 'z'; c++) 16 | { 17 | _putchar(c); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _islower - check lower case. 5 | * @c: charhacter 6 | * Return: Always 0. 7 | */ 8 | int _islower(int c) 9 | { 10 | if ((c >= 97) && (c <= 122)) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isalpha - check charachter. 5 | *@c: charachter 6 | * Return: Always 0. 7 | */ 8 | int _isalpha(int c) 9 | { 10 | if (((c >= 97) && (c <= 122)) || ((c >= 65) && (c <= 90))) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_sign - check sign. 5 | * @n: integer. 6 | * Return: Always 0. 7 | */ 8 | int print_sign(int n) 9 | { 10 | if (n > 0) 11 | { 12 | _putchar('+'); 13 | return (1); 14 | } 15 | else if (n == 0) 16 | { 17 | _putchar('0'); 18 | return (0); 19 | } 20 | else 21 | { 22 | _putchar('-'); 23 | return (-1); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _abs - check abs . 5 | * @x: integer. 6 | * Return: Always 0. 7 | */ 8 | int _abs(int x) 9 | { 10 | if (x > 0) 11 | { 12 | return (x); 13 | } 14 | else if (x < 0) 15 | { 16 | return (x = x * -1); 17 | } 18 | else 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_last_digit - display last digit. 5 | * @x:integer. 6 | * Return: Always 0. 7 | */ 8 | int print_last_digit(int x) 9 | { 10 | int y, e; 11 | 12 | y = x % 10; 13 | if (y < 0) 14 | { 15 | y = -y; 16 | } 17 | e = '0' + y; 18 | _putchar(e); 19 | return (y); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * jack_bauer - display time 24h. 5 | * 6 | * Return: Always 0. 7 | */ 8 | void jack_bauer(void) 9 | { 10 | int i, j, k, y, m, t; 11 | 12 | for (i = 0; i < 24 ; i++) 13 | { 14 | k = i / 10; 15 | y = i % 10; 16 | for (j = 0; j < 60; j++) 17 | { 18 | m = j / 10; 19 | t = j % 10; 20 | _putchar('0' + k); 21 | _putchar('0' + y); 22 | _putchar(':'); 23 | _putchar('0' + m); 24 | _putchar('0' + t); 25 | _putchar('\n'); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * times_table - prints the times table up to 9 5 | * 6 | * Return: void 7 | */ 8 | 9 | void times_table(void) 10 | { 11 | int i, j, y, d, e; 12 | 13 | for (i = 0; i < 10; i++) 14 | { 15 | for (j = 0; j < 10; j++) 16 | { 17 | y = i * j; 18 | d = y / 10; 19 | e = y % 10; 20 | if (j == 0) 21 | { 22 | _putchar('0'); 23 | } 24 | else if (y < 10) 25 | { 26 | _putchar(' '); 27 | _putchar('0' + e); 28 | } 29 | else 30 | { 31 | _putchar('0' + d); 32 | _putchar('0' + e); 33 | } 34 | if (j < 9) 35 | { 36 | _putchar(','); 37 | _putchar(' '); 38 | } 39 | else 40 | { 41 | _putchar('\n'); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | nested loops -------------------------------------------------------------------------------- /0x02-functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int x); 8 | int print_last_digit(int x); 9 | void jack_bauer(void); 10 | int add(int a, int b); 11 | void print_to_98(int n); 12 | void times_table(void); 13 | void print_times_table(int n); 14 | -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop. 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /*while (i < 10)*/ 17 | /*{*/ 18 | /*putchar(i);*/ 19 | /*}*/ 20 | 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a >= b && a >= c) 16 | { 17 | largest = a; 18 | } 19 | else if (b >= a && b >= c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | 28 | return (largest); 29 | } 30 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "holberton.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 > 2) 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 | Dubuging -------------------------------------------------------------------------------- /0x03-debugging/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef HOLBERTON_H 2 | #define HOLBERTON_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 /* HOLBERTON_H */ -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isupper - check uppercase character. 5 | * @c: integer. 6 | * Return: 0 succes. 7 | */ 8 | int _isupper(int c) 9 | { 10 | if ((c >= 65) && (c <= 90)) 11 | { 12 | return (1); 13 | } 14 | else 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _isdigit - check digit. 5 | * @c: integer 6 | * Return: 1 succes 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if ((c >= 48) && (c <= 57)) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_triangle - display a triangele 5 | * @size:integer 6 | * Return: void 7 | */ 8 | 9 | void print_triangle(int size) 10 | { 11 | int i, j, x; 12 | 13 | if (size > 0) 14 | { 15 | 16 | for (i = 1; i <= size; i++) 17 | { 18 | for (j = 0; j <= (size - i - 1); j++) 19 | { 20 | _putchar(' '); 21 | } 22 | for (x = 0; x <= i - 1; x++) 23 | { 24 | _putchar('#'); 25 | } 26 | _putchar('\n'); 27 | } 28 | } 29 | else 30 | { 31 | _putchar('\n'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - factor 4 | * 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | unsigned long int a, i; 10 | 11 | a = 612852475143; 12 | for (i = 3; i < (a / 2); i = i + 2) 13 | { 14 | while ((a % i == 0) && (a != i)) 15 | a = a / i; 16 | } 17 | printf("%lu\n", a); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_number -print number putchar 4 | * @n:integer 5 | * Return: void 6 | */ 7 | void print_number(int n) 8 | { 9 | unsigned int x = n; 10 | 11 | if (n < 0) 12 | { 13 | _putchar('-'); 14 | x = -x; 15 | } 16 | if ((x / 10) > 0) 17 | print_number(x / 10); 18 | 19 | _putchar(x % 10 + '0'); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * mul - multiply 2 nuber 5 | * @a:integer 6 | * @b:integer 7 | * Return: Success integer. 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_numbers - display all number from 0 to 9 followed by new line. 5 | * 6 | *Return: void 7 | */ 8 | void print_numbers(void) 9 | { 10 | int i; 11 | 12 | for (i = 48; i <= 57; i++) 13 | { 14 | _putchar(i); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_most_numbers - print all number from 0 to 9 expext 2 and 4. 4 | * 5 | * Return:void 6 | */ 7 | void print_most_numbers(void) 8 | { 9 | int i; 10 | 11 | for (i = 48; i <= 57; i++) 12 | { 13 | if ((i != 50) && (i != 52)) 14 | _putchar(i); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * more_numbers - print 10 time from 0 to 14 5 | * 6 | *Return:void 7 | */ 8 | void more_numbers(void) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < 10; i++) 13 | { 14 | for (j = 0; j <= 14; j++) 15 | { 16 | if (j >= 10) 17 | { 18 | _putchar('0' + j / 10); 19 | } 20 | _putchar('0' + j % 10); 21 | 22 | 23 | } 24 | _putchar('\n'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_line - print a line 5 | * @n:integer. 6 | * Return:void. 7 | */ 8 | void print_line(int n) 9 | { 10 | int i; 11 | 12 | if (n != 0) 13 | { 14 | for (i = 0; i < n; i++) 15 | { 16 | _putchar('_'); 17 | } 18 | _putchar('\n'); 19 | } 20 | else 21 | { 22 | _putchar('\n'); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_diagonal - print a diagonal line 4 | *@n:integer 5 | *Return:void 6 | */ 7 | void print_diagonal(int n) 8 | { 9 | int i, j; 10 | 11 | if (n > 0) 12 | { 13 | for (i = 0; i < n; i++) 14 | { 15 | for (j = 0; j < i; j++) 16 | { 17 | _putchar(' '); 18 | } 19 | _putchar('\\'); 20 | _putchar('\n'); 21 | } 22 | } 23 | else 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_square - print a square 4 | * @size:integer 5 | * Return:void 6 | */ 7 | void print_square(int size) 8 | { 9 | int i, j; 10 | 11 | if (size > 0) 12 | { 13 | for (i = 0; i < size; i++) 14 | { 15 | for (j = 0; j < size; j++) 16 | { 17 | _putchar('#'); 18 | } 19 | _putchar('\n'); 20 | } 21 | } 22 | else 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Fizz buzz. 4 | * 5 | *Return:void. 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | 11 | for (i = 1; i <= 100; i++) 12 | { 13 | if (((i % 5) == 0) && ((i % 3) == 0)) 14 | { 15 | printf("FizzBuzz"); 16 | } 17 | else if ((i % 5) == 0) 18 | { 19 | printf("Buzz"); 20 | } 21 | else if ((i % 3) == 0) 22 | { 23 | printf("Fizz"); 24 | } 25 | else 26 | { 27 | printf("%i", i); 28 | } 29 | if (i < 100) 30 | printf(" "); 31 | } 32 | printf("\n"); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | More Nested Loops -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | int _isupper(int c); 3 | int _isdigit(int c); 4 | int mul(int a, int b); 5 | void print_numbers(void); 6 | void print_most_numbers(void); 7 | void more_numbers(void); 8 | void print_line(int n); 9 | void print_diagonal(int n); 10 | void print_square(int size); 11 | void print_triangle(int size); 12 | void print_number(int n); -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * reset_to_98 - change the value of integer using pointer 5 | * @n: pointer 6 | * Return:void 7 | */ 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * swap_int - swaping 2 integer using a pointer 5 | * @a:integer 6 | * @b:integer 7 | * Return:void 8 | */ 9 | void swap_int(int *a, int *b) 10 | { 11 | int x; 12 | 13 | x = *a; 14 | *a = *b; 15 | *b = x; 16 | return; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _atoi - convert to a int 5 | * @s:string 6 | * Return:int 7 | */ 8 | 9 | int _atoi(char *s) 10 | { 11 | int i, j, n, x; 12 | 13 | i = n = 0; 14 | x = 1; 15 | while ((s[i] < '0' || s[i] > '9') && (s[i] != '\0')) 16 | { 17 | if (s[i] == '-') 18 | x *= -1; 19 | i++; 20 | } 21 | j = i; 22 | while ((s[j] >= '0') && (s[j] <= '9')) 23 | { 24 | n = (n * 10) + x * ((s[j]) - '0'); 25 | j++; 26 | } 27 | return (n); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - random password generator for 101-crackme 7 | * 8 | * Return: always 0 9 | */ 10 | int main(void) 11 | { 12 | char a[100]; 13 | int r, n, i; 14 | 15 | n = 0; 16 | i = 0; 17 | srand(time(NULL)); 18 | while (n < 2645) 19 | { 20 | r = rand() % 122; 21 | if (r > 32) 22 | { 23 | a[i++] = r; 24 | n += r; 25 | } 26 | } 27 | a[i++] = (2772 - n); 28 | a[i] = '\0'; 29 | printf("%s", a); 30 | 31 | return (0); 32 | } 33 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - lenght of string 5 | * @s:char 6 | * Return:int 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int i; 11 | 12 | for (i = 0; s[i] != '\0'; i++) 13 | { 14 | continue; 15 | } 16 | return (i); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _puts - print a string 5 | * @str:pointer char 6 | * return:void 7 | */ 8 | void _puts(char *str) 9 | { 10 | int i; 11 | 12 | for (i = 0; str[i] != '\0'; i++) 13 | { 14 | _putchar(str[i]); 15 | } 16 | _putchar('\n'); 17 | return; 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_rev - print a string 5 | * @s:pointer char 6 | * return:void 7 | */ 8 | void print_rev(char *s) 9 | { 10 | int i, n; 11 | 12 | for (i = 0; s[i] != '\0'; i++) 13 | { 14 | continue; 15 | } 16 | n = i - 1; 17 | for (i = n; i >= 0 ; i--) 18 | { 19 | _putchar(s[i]); 20 | } 21 | _putchar('\n'); 22 | return; 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * rev_string - reverse a string 5 | * @s:string 6 | * Return:void 7 | */ 8 | void rev_string(char *s) 9 | { 10 | int i, j; 11 | char a[500]; 12 | i = 0; 13 | j = 0; 14 | while (*(s + i)) 15 | { 16 | *(a + i) = *(s + i); 17 | i++; 18 | } 19 | i = i - 1; 20 | while (i >= 0) 21 | { 22 | *(s + i) = *(a + j); 23 | j++; 24 | i--; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * puts2 - print every other character of a string 5 | * @str:pointer char 6 | * return:void 7 | */ 8 | void puts2(char *str) 9 | { 10 | int i, x; 11 | i = 0; 12 | x = 0; 13 | while (str[i++]) 14 | { 15 | x++; 16 | } 17 | for (i = 0; i < x; i += 2) 18 | { 19 | _putchar(*(str + i)); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * puts_half - lenght of string 5 | * @str:char 6 | * Return:int 7 | */ 8 | void puts_half(char *str) 9 | { 10 | int i, j; 11 | 12 | i = 0; 13 | while (str[i]) 14 | i++; 15 | j = i / 2; 16 | if (i % 2) 17 | j += 1; 18 | while (j < i) 19 | { 20 | _putchar(str[j]); 21 | j++; 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_array - display array 6 | * @a:integer 7 | * @n:integer 8 | *Return:void 9 | */ 10 | void print_array(int *a, int n) 11 | { 12 | int i; 13 | for (i = 0; i < n; i++) 14 | { 15 | printf("%d", a[i]); 16 | if (i < (n - 1)) 17 | { 18 | printf(", "); 19 | } 20 | } 21 | printf("\n"); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcpy - copies src to dest. 5 | * @dest:char 6 | * @src:char 7 | * Return:char 8 | */ 9 | char *_strcpy(char *dest, char *src) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (src[i]) 15 | { 16 | dest[i] = src[i]; 17 | i++; 18 | } 19 | dest[i] = '\0'; 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x05. C - Pointers, arrays and strings -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void reset_to_98(int *n); 3 | void swap_int(int *a, int *b); 4 | int _strlen(char *s); 5 | void _puts(char *str); 6 | void print_rev(char *s); 7 | void rev_string(char *s); 8 | void puts2(char *str); 9 | void puts_half(char *str); 10 | void print_array(int *a, int n); 11 | char *_strcpy(char *dest, char *src); 12 | int _atoi(char *s); -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strcat - concat 2 string 5 | * @dest:char 6 | * @src:char 7 | * Return:char 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | char *s = dest; 12 | 13 | while (*dest != '\0') 14 | { 15 | dest++; 16 | } 17 | 18 | while (*src != '\0') 19 | { 20 | *dest = *src; 21 | dest++; 22 | src++; 23 | } 24 | *dest = '\0'; 25 | return (s); 26 | } 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strncat - concatenates two strings 5 | * @dest:char dest 6 | * @src: char src 7 | * @n:length int 8 | * Return:char 9 | */ 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | int i, j; 13 | 14 | for (i = 0; dest[i] != '\0'; i++) 15 | { 16 | continue; 17 | } 18 | for (j = 0; src[j] != '\0' && j < n; j++) 19 | { 20 | dest[i + j] = src[j]; 21 | } 22 | dest[i + j] = '\0'; 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-print_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_number -print number putchar 5 | * @n:integer 6 | * Return: void 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int x = n; 11 | 12 | if (n < 0) 13 | { 14 | _putchar('-'); 15 | x = -x; 16 | } 17 | if ((x / 10) > 0) 18 | print_number(x / 10); 19 | 20 | _putchar(x % 10 + '0'); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-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/102-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * infinite_add - adds two numbers. 5 | * @n1:char 6 | * @n2:char. 7 | * @r:char 8 | * @size_r:int 9 | * Return:char. 10 | */ 11 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 12 | { 13 | int i, j, k, l, m, n; 14 | 15 | for (i = 0; n1[i]; i++) 16 | ; 17 | for (j = 0; n2[j]; j++) 18 | ; 19 | if (i > size_r || j > size_r) 20 | return (0); 21 | m = 0; 22 | for (i -= 1, j -= 1, k = 0; k < size_r - 1; i--, j--, k++) 23 | { 24 | n = m; 25 | if (i >= 0) 26 | n += n1[i] - '0'; 27 | if (j >= 0) 28 | n += n2[j] - '0'; 29 | if (i < 0 && j < 0 && n == 0) 30 | { 31 | break; 32 | } 33 | m = n / 10; 34 | r[k] = n % 10 + '0'; 35 | } 36 | r[k] = '\0'; 37 | if (i >= 0 || j >= 0 || m) 38 | return (0); 39 | for (k -= 1, l = 0; l < k; k--, l++) 40 | { 41 | m = r[k]; 42 | r[k] = r[l]; 43 | r[l] = m; 44 | } 45 | return (r); 46 | } 47 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - Prints a buffer 6 | * @b: char 7 | * @size: int 8 | * Return:void 9 | */ 10 | void print_buffer(char *b, int size) 11 | { 12 | int x, i; 13 | 14 | for (x = 0; x < size; x += 10) 15 | { 16 | printf("%08x: ", x); 17 | 18 | for (i = 0; i < 10; i++) 19 | { 20 | if ((i + x) >= size) 21 | printf(" "); 22 | 23 | else 24 | printf("%02x", *(b + i + x)); 25 | 26 | if ((i % 2) != 0 && i != 0) 27 | printf(" "); 28 | } 29 | 30 | for (i = 0; i < 10; i++) 31 | { 32 | if ((i + x) >= size) 33 | break; 34 | 35 | else if (*(b + i + x) >= 31 && 36 | *(b + i + x) <= 126) 37 | printf("%c", *(b + i + x)); 38 | 39 | else 40 | printf("."); 41 | } 42 | 43 | if (x >= size) 44 | continue; 45 | 46 | printf("\n"); 47 | } 48 | 49 | if (size <= 0) 50 | printf("\n"); 51 | } 52 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strncpy - copie a string 4 | * @dest:char 5 | * @src:char 6 | * @n:int 7 | * Return:char 8 | */ 9 | char *_strncpy(char *dest, char *src, int n) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (i < n && *(src + i)) 15 | { 16 | *(dest + i) = *(src + i); 17 | i++; 18 | } 19 | while (i < n) 20 | { 21 | *(dest + i) = '\0'; 22 | i++; 23 | } 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strcmp - compare 2 string 4 | * @s1:string 5 | * @s2:strmp 6 | * Return:int 7 | */ 8 | int _strcmp(char *s1, char *s2) 9 | { 10 | while (((*s1 != '\0') && (*s2 != '\0')) && (*s1 == *s2)) 11 | { 12 | s1++; 13 | s2++; 14 | } 15 | 16 | if (*s1 == *s2) 17 | { 18 | return (0); 19 | } 20 | 21 | else 22 | { 23 | return (*s1 - *s2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * reverse_array - reverse array 4 | * @a:array 5 | * @n:integer 6 | * Return:void 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int i, c; 11 | 12 | for (i = 0; (i < (n - 1) / 2); i++) 13 | { 14 | c = a[i]; 15 | a[i] = a[n - 1 - i]; 16 | a[n - 1 - i] = c; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * string_toupper - change lowercase to uppercase 5 | * @s:string 6 | * Return:char 7 | */ 8 | char *string_toupper(char *s) 9 | { 10 | 11 | int i; 12 | 13 | i = 0; 14 | while (*(s + i)) 15 | { 16 | if (*(s + i) >= 'a' && *(s + i) <= 'z') 17 | *(s + i) -= 'a' - 'A'; 18 | i++; 19 | } 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * cap_string - capitalizes all words in a string 5 | * @s: string 6 | * Return: address of s 7 | */ 8 | char *cap_string(char *s) 9 | { 10 | int i = 0, j; 11 | char a[] = " \t\n,;.!?\"(){}"; 12 | 13 | while (*(s + i)) 14 | { 15 | if (*(s + i) >= 'a' && *(s + i) <= 'z') 16 | { 17 | if (i == 0) 18 | *(s + i) -= 'a' - 'A'; 19 | else 20 | { 21 | for (j = 0; j <= 12; j++) 22 | { 23 | if (a[j] == *(s + i - 1)) 24 | *(s + i) -= 'a' - 'A'; 25 | } 26 | } 27 | } 28 | i++; 29 | } 30 | return (s); 31 | } 32 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * leet - encodes a string into 1337 5 | * @s: string to encode 6 | * 7 | * Return: address of s 8 | */ 9 | char *leet(char *s) 10 | { 11 | int i, j; 12 | char a[] = "aAeEoOtTlL"; 13 | char b[] = "4433007711"; 14 | 15 | for (i = 0; *(s + i); i++) 16 | { 17 | for (j = 0; j <= 9; j++) 18 | { 19 | if (a[j] == s[i]) 20 | s[i] = b[j]; 21 | } 22 | } 23 | return (s); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/8-rot13.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * rot13 - encodes a string into rot13 5 | * @s: string to encode 6 | * 7 | * Return: address of s 8 | */ 9 | char *rot13(char *s) 10 | { 11 | int i, j; 12 | char a[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 13 | char b[] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"; 14 | 15 | for (i = 0; *(s + i); i++) 16 | { 17 | for (j = 0; j < 52; j++) 18 | { 19 | if (a[j] == *(s + i)) 20 | { 21 | *(s + i) = b[j]; 22 | break; 23 | } 24 | } 25 | } 26 | return (s); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # More pointers, arrays and strings 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *_strcat(char *dest, char *src); 3 | char *_strncat(char *dest, char *src, int n); 4 | char *_strncpy(char *dest, char *src, int n); 5 | int _strcmp(char *s1, char *s2); 6 | void reverse_array(int *a, int n); 7 | char *string_toupper(char *s); 8 | char *cap_string(char *); 9 | char *leet(char *); 10 | char *rot13(char *); 11 | void print_number(int n); 12 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 13 | void print_buffer(char *b, int size); -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _memset - fill memory with a constant byte 4 | * @s:char 5 | * @b:char 6 | * @n:unsigned int 7 | * Return:char 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 "holberton.h" 2 | /** 3 | * _memcpy - copies memory area 4 | * @dest:char 5 | * @src:char 6 | * @n:int 7 | * Return:char pointer 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/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strchr - locate a character in a string 4 | * @s:string 5 | * @c:char 6 | * Return:pointer to char* 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | 11 | do { 12 | 13 | if (*s == c) 14 | { 15 | break; 16 | } 17 | } while (*s++); 18 | 19 | return (s); 20 | } 21 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strspn - get the lenght of prefix substring 4 | * @s:string 5 | * @accept:string 6 | * Return:unsigned int 7 | */ 8 | unsigned int _strspn(char *s, char *accept) 9 | { 10 | unsigned int x; 11 | int i; 12 | 13 | x = 0; 14 | while (*s) 15 | { 16 | for (i = 0; accept[i]; i++) 17 | { 18 | if (*s == accept[i]) 19 | { 20 | x++; 21 | break; 22 | } 23 | else if (accept[i + 1] == '\0') 24 | { 25 | return (x); 26 | } 27 | } 28 | s++; 29 | } 30 | return (x); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strpbrk - searches a string for any of a set of bytes 4 | * @s:string 5 | * @accept:string 6 | * Return:char 7 | */ 8 | char *_strpbrk(char *s, char *accept) 9 | { 10 | int i; 11 | 12 | while (*s) 13 | { 14 | for (i = 0; accept[i]; i++) 15 | { 16 | if (*s == accept[i]) 17 | { 18 | return (s); 19 | } 20 | 21 | } 22 | s++; 23 | } 24 | return ('\0'); 25 | } 26 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strstr - locate a substring 4 | * @haystack:string 5 | * @needle:string 6 | * Return:string 7 | */ 8 | char *_strstr(char *haystack, char *needle) 9 | { 10 | int x; 11 | 12 | if (*needle == 0) 13 | return (haystack); 14 | 15 | while (*haystack) 16 | { 17 | x = 0; 18 | 19 | if (haystack[x] == needle[x]) 20 | { 21 | do { 22 | if (needle[x + 1] == '\0') 23 | return (haystack); 24 | 25 | x++; 26 | 27 | } while (haystack[x] == needle[x]); 28 | } 29 | 30 | haystack++; 31 | } 32 | 33 | return ('\0'); 34 | } 35 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * print_chessboard - print a chessboard 4 | * @a:array 5 | * Return:void 6 | */ 7 | void print_chessboard(char (*a)[8]) 8 | { 9 | int i, j; 10 | 11 | for (i = 0; a[i][7]; i++) 12 | { 13 | for (j = 0; j < 8; j++) 14 | _putchar(a[i][j]); 15 | 16 | _putchar('\n'); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * print_diagsums - print sum of 2 digona 5 | * @a:int 6 | * @size:int size 7 | * Return:void 8 | */ 9 | void print_diagsums(int *a, int size) 10 | { 11 | int i, s1 = 0, s2 = 0; 12 | 13 | for (i = 0; i < size; i++) 14 | { 15 | s1 += a[i]; 16 | a += size; 17 | } 18 | 19 | a -= size; 20 | 21 | for (i = 0; i < size; i++) 22 | { 23 | s2 += a[i]; 24 | a -= size; 25 | } 26 | 27 | printf("%d, %d\n", s1, s2); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/9-set_string.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * set_string - set a pointer to char 4 | * @s:pointer to pointer 5 | * @to:pointer to char 6 | * Return:void; 7 | */ 8 | void set_string(char **s, char *to) 9 | { 10 | *s = to; 11 | } 12 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | # 0x07-pointers_arrays_strings -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *_memset(char *s, char b, unsigned int n); 3 | char *_memcpy(char *dest, char *src, unsigned int n); 4 | char *_strchr(char *s, char c); 5 | unsigned int _strspn(char *s, char *accept); 6 | char *_strpbrk(char *s, char *accept); 7 | char *_strstr(char *haystack, char *needle); 8 | void print_chessboard(char (*a)[8]); 9 | void print_diagsums(int *a, int size); 10 | void set_string(char **s, char *to); -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _puts_recursion - print a string 4 | * @s:string 5 | * Return:void 6 | */ 7 | void _puts_recursion(char *s) 8 | { 9 | if (*s) 10 | { 11 | _putchar(*s); 12 | _puts_recursion(s + 1); 13 | } 14 | else 15 | { 16 | _putchar('\n'); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _print_rev_recursion - print a string 4 | * @s:string 5 | * Return:void 6 | */ 7 | void _print_rev_recursion(char *s) 8 | { 9 | if (*s) 10 | { 11 | _print_rev_recursion(s + 1); 12 | _putchar(*s); 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/100-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * wildcmp - compare two strings with possible wildcards 4 | * @s1: string 5 | * @s2: string 6 | * Return:int 0 or 1 7 | */ 8 | int wildcmp(char *s1, char *s2) 9 | { 10 | if (*s1 == '\0' && *s2 == '\0') 11 | return (1); 12 | else if (*s1 == '\0' && *s2 == '*') 13 | return (wildcmp(s1, (s2 + 1))); 14 | else if (*s1 == '\0' || *s2 == '\0') 15 | return (0); 16 | 17 | if (*s1 == *s2) 18 | return (wildcmp((s1 + 1), (s2 + 1))); 19 | 20 | if (*s2 == '*') 21 | { 22 | if (*(s2 + 1) == '*') 23 | return (wildcmp(s1, (s2 + 1))); 24 | else if (wildcmp(s1, s2 + 1)) 25 | return (1); 26 | else 27 | return (wildcmp(s1 + 1, s2)); 28 | } 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strlen_recursion - length of string 4 | * @s:string 5 | * Return:int 6 | */ 7 | int _strlen_recursion(char *s) 8 | { 9 | int i = 0; 10 | 11 | if (*s) 12 | { 13 | i++; 14 | i += _strlen_recursion(s + 1); 15 | 16 | } 17 | return (i); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * factorial - factorial of int 4 | * @n:int 5 | * Return:int 6 | */ 7 | int factorial(int n) 8 | { 9 | int x; 10 | 11 | if (n == 0) 12 | { 13 | return (1); 14 | } 15 | else if (n < 0) 16 | { 17 | return (-1); 18 | } 19 | else 20 | { 21 | x = n * factorial(n - 1); 22 | } 23 | return (x); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _pow_recursion - power 4 | * @x:int 5 | * @y:int 6 | * Return:int 7 | */ 8 | 9 | int _pow_recursion(int x, int y) 10 | { 11 | if (y < 0) 12 | { 13 | return (-1); 14 | } 15 | else if (y != 0) 16 | return (x * _pow_recursion(x, y - 1)); 17 | 18 | else 19 | { 20 | return (1); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * check - checks for the square root 5 | * @a:int 6 | * @b:int 7 | * 8 | * Return: int 9 | */ 10 | int check(int a, int b) 11 | { 12 | if (a * a == b) 13 | return (a); 14 | if (a * a > b) 15 | return (-1); 16 | return (check(a + 1, b)); 17 | } 18 | 19 | /** 20 | * _sqrt_recursion - returns the natural square root of a number 21 | * @n: integer to find sqrt of 22 | * Return: natural square root or -1 23 | */ 24 | int _sqrt_recursion(int n) 25 | { 26 | if (n == 0) 27 | return (0); 28 | return (check(1, n)); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * check - checks to see if number is prime 5 | * @a:int 6 | * @b:int 7 | * Return:int 8 | */ 9 | int check(int a, int b) 10 | { 11 | if (b < 2 || b % a == 0) 12 | return (0); 13 | else if (a > b / 2) 14 | return (1); 15 | else 16 | return (check(a + 1, b)); 17 | } 18 | 19 | /** 20 | * is_prime_number - states if number is prime 21 | * @n:int 22 | * Return:int 23 | */ 24 | int is_prime_number(int n) 25 | { 26 | if (n == 2) 27 | return (1); 28 | return (check(2, n)); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/7-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | /** 3 | * _strlen_recursion - length of string 4 | * @s:string 5 | * Return:int 6 | */ 7 | int _strlen_recursion(char *s) 8 | { 9 | int i = 0; 10 | 11 | if (*s) 12 | { 13 | i++; 14 | i += _strlen_recursion(s + 1); 15 | 16 | } 17 | return (i); 18 | } 19 | /** 20 | * check_palindrome - checks to see if a string is a palindrome 21 | * @i:int 22 | * @j:int 23 | * @s: string 24 | * Return: int 25 | */ 26 | int check_palindrome(int i, int j, char *s) 27 | { 28 | if (i >= j) 29 | return (1); 30 | else if (s[i] != s[j]) 31 | return (0); 32 | else 33 | return (check_palindrome(i + 1, j - 1, s)); 34 | } 35 | /** 36 | * is_palindrome - states if a string is a palindrome 37 | * @s:string 38 | * Return: int 39 | */ 40 | int is_palindrome(char *s) 41 | { 42 | int i; 43 | 44 | i = _strlen_recursion(s) - 1; 45 | return (check_palindrome(0, i, s)); 46 | } 47 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | # C - Recursion -------------------------------------------------------------------------------- /0x08-recursion/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void _puts_recursion(char *s); 3 | void _print_rev_recursion(char *s); 4 | int _strlen_recursion(char *s); 5 | int factorial(int n); 6 | int _pow_recursion(int x, int y); 7 | int _sqrt_recursion(int n); 8 | int is_prime_number(int n); 9 | int is_palindrome(char *s); 10 | int wildcmp(char *s1, char *s2); -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # Static libraries -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -c *.c 3 | ar -cvq liball.a *.o -------------------------------------------------------------------------------- /0x09-static_libraries/holberton.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 /* _HOLBERTON_H_ */ 26 | -------------------------------------------------------------------------------- /0x09-static_libraries/libholberton.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Theemiss/holbertonschool-low_level_programming/b1a32f5cfe72eabc082198bd0a3b97fbfcb0fc47/0x09-static_libraries/libholberton.a -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - display name of program 4 | * @argc:int 5 | * @argv:string 6 | * Return:int 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | if (argc == 1) 11 | { 12 | printf("%s\n", argv[0]); 13 | } 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - display num of arg 4 | * @argc:int 5 | * @argv:string 6 | * Return:int 7 | */ 8 | int main(int argc, char *argv[] __attribute__((unused))) 9 | { 10 | 11 | printf("%d\n", argc - 1); 12 | 13 | return (0); 14 | } 15 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - print the minimum number of coins to make change 6 | * @argc: int 7 | * @argv: string 8 | * Return: int 9 | */ 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, n, s, ch = 0; 14 | int c[] = {25, 10, 5, 2, 1}; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | return (1); 20 | } 21 | s = atoi(argv[1]); 22 | if (s < 1) 23 | printf("0\n"); 24 | else 25 | { 26 | for (i = 0; i < 5 && s; i++) 27 | { 28 | n = s / c[i]; 29 | ch += n; 30 | s -= n * c[i]; 31 | } 32 | printf("%d\n", ch); 33 | } 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - display all arg 4 | * @argc:int 5 | * @argv:string 6 | * Return:int 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | int i; 11 | 12 | for (i = 0; i < argc; i++) 13 | { 14 | printf("%s\n", argv[i]); 15 | } 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - mul 2 argv 5 | * @argc:int 6 | * @argv:string 7 | * Return:int 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | int x, i, j; 12 | 13 | if (argc != 3) 14 | { 15 | printf("Error\n"); 16 | return (1); 17 | } 18 | else 19 | { 20 | i = atoi(argv[1]); 21 | j = atoi(argv[2]); 22 | x = i * j; 23 | printf("%d\n", x); 24 | } 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - add 6 | * @argc:int 7 | * @argv:string 8 | * Return:int 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int x = 0, i, j; 13 | 14 | for (i = 1; i < argc; i++) 15 | { 16 | for (j = 0; argv[i][j]; j++) 17 | { 18 | if (isdigit(argv[i][j]) == 0) 19 | { 20 | printf("Error\n"); 21 | return (1); 22 | } 23 | } 24 | } 25 | for (i = 1; i < argc; i++) 26 | { 27 | x += atoi(argv[i]); 28 | } 29 | printf("%d\n", x); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # C - argc, argv -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * create_array - allocate space for an array 5 | * @size:unssigned int 6 | * @c:char 7 | * Return:char 8 | */ 9 | char *create_array(unsigned int size, char c) 10 | { 11 | 12 | unsigned int i; 13 | char *x; 14 | 15 | if (size == 0) 16 | return (NULL); 17 | x = malloc(size * sizeof(char)); 18 | if (x == NULL) 19 | { 20 | return (NULL); 21 | } 22 | for (i = 0; i < size; i++) 23 | { 24 | x[i] = c; 25 | } 26 | return (x); 27 | } 28 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * _strdup - Returns a pointer to a newly-allocated space in memory 6 | * containing a copy of the string given as parameter. 7 | * @str:string 8 | * 9 | * Return:char 10 | */ 11 | char *_strdup(char *str) 12 | { 13 | char *s; 14 | int i, l = 0; 15 | 16 | if (str == NULL) 17 | return (NULL); 18 | 19 | for (i = 0; str[i]; i++) 20 | l++; 21 | 22 | s = malloc(sizeof(char) * (l + 1)); 23 | 24 | if (s == NULL) 25 | return (NULL); 26 | 27 | for (i = 0; str[i]; i++) 28 | s[i] = str[i]; 29 | 30 | s[l] = '\0'; 31 | 32 | return (s); 33 | } 34 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-strtow.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * wrdcnt - counts the number of words in a string 6 | * @s: string 7 | * Return: int of number of words 8 | */ 9 | int wrdcnt(char *s) 10 | { 11 | int i, n = 0; 12 | 13 | for (i = 0; s[i]; i++) 14 | { 15 | if (s[i] == ' ') 16 | { 17 | if (s[i + 1] != ' ' && s[i + 1] != '\0') 18 | n++; 19 | } 20 | else if (i == 0) 21 | n++; 22 | } 23 | n++; 24 | return (n); 25 | } 26 | 27 | /** 28 | * strtow - splits a string into words 29 | * @str: string 30 | * Return: pointer to an array of strings 31 | */ 32 | char **strtow(char *str) 33 | { 34 | int i, j, k, l, n = 0, ch = 0; 35 | char **x; 36 | 37 | if (str == NULL || *str == '\0') 38 | return (NULL); 39 | n = wrdcnt(str); 40 | if (n == 1) 41 | return (NULL); 42 | x = (char **)malloc(n * sizeof(char *)); 43 | if (x == NULL) 44 | return (NULL); 45 | x[n - 1] = NULL; 46 | i = 0; 47 | while (str[i]) 48 | { 49 | if (str[i] != ' ' && (i == 0 || str[i - 1] == ' ')) 50 | { 51 | for (j = 1; str[i + j] != ' ' && str[i + j]; j++) 52 | ; 53 | j++; 54 | x[ch] = (char *)malloc(j * sizeof(char)); 55 | j--; 56 | if (x[ch] == NULL) 57 | { 58 | for (k = 0; k < ch; k++) 59 | free(x[k]); 60 | free(x[n - 1]); 61 | free(x); 62 | return (NULL); 63 | } 64 | for (l = 0; l < j; l++) 65 | x[ch][l] = str[i + l]; 66 | x[ch][l] = '\0'; 67 | ch++; 68 | i += j; 69 | } 70 | else 71 | i++; 72 | } 73 | return (x); 74 | } 75 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | /** 4 | * str_concat - concat 2 string 5 | * @s1:string1 6 | * @s2:string2 7 | * Return:char 8 | */ 9 | 10 | char *str_concat(char *s1, char *s2) 11 | { 12 | unsigned int i, j, k, l; 13 | char *s; 14 | 15 | if (s1 == NULL) 16 | i = 0; 17 | else 18 | { 19 | for (i = 0; s1[i]; i++) 20 | ; 21 | } 22 | if (s2 == NULL) 23 | j = 0; 24 | else 25 | { 26 | for (j = 0; s2[j]; j++) 27 | ; 28 | } 29 | k = i + j + 1; 30 | s = malloc(k * sizeof(char)); 31 | if (s == NULL) 32 | return (NULL); 33 | for (l = 0; l < i; l++) 34 | s[l] = s1[l]; 35 | for (l = 0; l < j; l++) 36 | s[l + i] = s2[l]; 37 | s[i + j] = '\0'; 38 | return (s); 39 | } 40 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - creates a 2 dimensional array of integers initialized to 0 6 | * @width: int 7 | * @height:int 8 | * 9 | * Return: a double pointer to the 2D array 10 | */ 11 | int **alloc_grid(int width, int height) 12 | { 13 | int i, j; 14 | int **a; 15 | 16 | if (width <= 0 || height <= 0) 17 | return (NULL); 18 | a = (int **)malloc(sizeof(int *) * height); 19 | if (a == NULL) 20 | return (NULL); 21 | for (i = 0; i < height; i++) 22 | { 23 | a[i] = (int *)malloc(sizeof(int) * width); 24 | if (a[i] == NULL) 25 | { 26 | for (j = 0; j < i; j++) 27 | free(a[j]); 28 | free(a); 29 | return (NULL); 30 | } 31 | for (j = 0; j < width; j++) 32 | { 33 | a[i][j] = 0; 34 | } 35 | } 36 | return (a); 37 | } 38 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | /** 5 | *free_grid - a function that frees a 2 dimensional grid 6 | *@grid : array 7 | *@height : int 8 | *Return: 0 9 | */ 10 | void free_grid(int **grid, int height) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < height; i++) 15 | { 16 | free(grid[i]); 17 | } 18 | free(grid); 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/5-argstostr.c: -------------------------------------------------------------------------------- 1 | 2 | #include "holberton.h" 3 | #include 4 | #include 5 | /** 6 | **argstostr - a function that concatenates all the arguments of your program. 7 | *@ac : int 8 | *@av : array 9 | *Return: array 10 | */ 11 | char *argstostr(int ac, char **av) 12 | { 13 | int x, y, i, j, l = 0, a = 0; 14 | char *s; 15 | 16 | if (ac == 0 || av == NULL) 17 | return (NULL); 18 | for (i = 0; (i < ac); i++) 19 | { 20 | for (j = 0; av[i][j] != '\0'; j++) 21 | l++; 22 | l++; 23 | } 24 | 25 | s = malloc(sizeof(char) * l + 1); 26 | if (s == NULL) 27 | return (NULL); 28 | for (x = 0; x < ac ; x++) 29 | { 30 | for (y = 0; av[x][y] != '\0'; y++) 31 | { 32 | s[a] = av[x][y]; 33 | a++; 34 | } 35 | s[a++] = '\n'; 36 | } 37 | s[a] = '\0'; 38 | return (s); 39 | } 40 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # C - malloc, free -------------------------------------------------------------------------------- /0x0B-malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *create_array(unsigned int size, char c); 3 | char *_strdup(char *str); 4 | char *str_concat(char *s1, char *s2); 5 | int **alloc_grid(int width, int height); 6 | void free_grid(int **grid, int height); 7 | char *argstostr(int ac, char **av); 8 | char **strtow(char *str); -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | /** 5 | * malloc_checked - allocate a space 6 | * @b: unsigned int 7 | * Return: void 8 | */ 9 | void *malloc_checked(unsigned int b) 10 | { 11 | void *x; 12 | 13 | x = malloc(b); 14 | if (x == NULL) 15 | { 16 | exit(98); 17 | } 18 | return (x); 19 | } 20 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | /** 5 | **string_nconcat - a function that concatenates two strings. 6 | *@s1 : pointer to string 7 | *@s2 : pointer to string 8 | *@n : integer 9 | *Return: pointer to string 10 | */ 11 | char *string_nconcat(char *s1, char *s2, unsigned int n) 12 | { 13 | 14 | 15 | 16 | unsigned int x1, x2, i, j; 17 | char *s; 18 | 19 | if (s1 == NULL) 20 | s1 = ""; 21 | if (s2 == NULL) 22 | s2 = ""; 23 | for (x1 = 0; s1[x1] != '\0'; x1++) 24 | ; 25 | for (x2 = 0; s2[x2] != '\0'; x2++) 26 | ; 27 | if (n >= x2) 28 | n = x2; 29 | s = malloc(sizeof(char) * (x1 + n + 1)); 30 | if (s == NULL) 31 | return (NULL); 32 | for (i = 0; s1[i] != '\0'; i++) 33 | s[i] = s1[i]; 34 | for (j = x1; j < x1 + n; j++) 35 | s[j] = s2[j - x1]; 36 | s[j] = '\0'; 37 | return (s); 38 | } 39 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | /** 5 | **_realloc - a function that reallocates a memory block using malloc and free 6 | *@ptr : pointer 7 | *@old_size : int 8 | *@new_size : int 9 | *Return: pointer 10 | */ 11 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 12 | { 13 | char *tmp; 14 | 15 | if (new_size == old_size) 16 | return (ptr); 17 | if (new_size == 0 && ptr != NULL) 18 | { 19 | free(ptr); 20 | return (NULL); 21 | } 22 | if (ptr == NULL) 23 | { 24 | ptr = malloc(new_size); 25 | return (ptr); 26 | } 27 | tmp = malloc(new_size); 28 | free(ptr); 29 | return (tmp); 30 | } 31 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | /** 5 | * _calloc - allocate an array 6 | * @nmemb:int 7 | * @size:int 8 | * Return:void 9 | */ 10 | void *_calloc(unsigned int nmemb, unsigned int size) 11 | { 12 | char *x; 13 | unsigned int i; 14 | 15 | if (size == 0 || nmemb == 0) 16 | return (NULL); 17 | x = malloc(size * nmemb); 18 | if (x == NULL) 19 | return (NULL); 20 | for (i = 0; i < nmemb * size; i++) 21 | { 22 | x[i] = '\0'; 23 | } 24 | return (x); 25 | } 26 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | #include 3 | #include 4 | /** 5 | **array_range - a function that creates an array of integers. 6 | *@min : int 7 | *@max : int 8 | *Return: pointer to array of integer 9 | */ 10 | int *array_range(int min, int max) 11 | { 12 | int *p; 13 | int i, j = 0; 14 | 15 | if (min > max) 16 | return (NULL); 17 | p = malloc((max - min + 1) * sizeof(int)); 18 | if (p == NULL) 19 | return (NULL); 20 | for (i = min; i <= max; i++) 21 | { 22 | p[j] = i; 23 | j++; 24 | } 25 | return (p); 26 | } 27 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # C-More malloc,free -------------------------------------------------------------------------------- /0x0C-more_malloc_free/holberton.h: -------------------------------------------------------------------------------- 1 | void *malloc_checked(unsigned int b); 2 | char *string_nconcat(char *s1, char *s2, unsigned int n); 3 | void *_calloc(unsigned int nmemb, unsigned int size); 4 | int *array_range(int min, int max); 5 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _0_OBJECT_LIKE_MACRO_H_ 2 | #define _0_OBJECT_LIKE_MACRO_H_ 3 | #define SIZE 1024 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI_H 2 | #define PI_H 3 | #define PI 3.14159265359 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print file name 4 | * 5 | * Return:void 6 | */ 7 | int main(void) 8 | { 9 | printf("%s\n", __FILE__); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _3_FUNCTION_LIKE_MACRO_H_ 2 | #define _3_FUNCTION_LIKE_MACRO_H_ 3 | #define ABS(x) ((x) < 0 ? (-(x)) : (x)) 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | #define SUM(x, y) ((x) + (y)) 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # C-Preprocessor -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * init_dog - a function that initialize a variable of type struct dog 5 | * @d:pointer to a struct dog 6 | * @name:pointer to a string 7 | * @age:float 8 | * @owner:pointer to a string 9 | */ 10 | void init_dog(struct dog *d, char *name, float age, char *owner) 11 | { 12 | if (d != NULL) 13 | { 14 | d->name = name; 15 | d->age = age; 16 | d->owner = owner; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | /** 4 | *print_dog - a function that prints a struct dog 5 | *@d : pointer to struct 6 | */ 7 | void print_dog(struct dog *d) 8 | { 9 | if (d == NULL) 10 | return; 11 | if (d->name == NULL) 12 | printf("Name: (nil)\n"); 13 | else 14 | printf("Name: %s\n", d->name); 15 | printf("Age: %.6f\n", d->age); 16 | if (d->owner == NULL) 17 | printf("Owner: (nil)\n"); 18 | else 19 | printf("Owner: %s\n", d->owner); 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | int _strlen(char *s); 4 | char *_strcpy(char *x, char *y); 5 | /** 6 | * new_dog - makes a new dog, memory and all 7 | * @name: name to create mem and assign to for dog 8 | * @age: age to assign to for dog 9 | * @owner: owner to create mem and assign to for dog 10 | * 11 | * Return: pointer to new dog_t (struct dog) 12 | */ 13 | dog_t *new_dog(char *name, float age, char *owner) 14 | { 15 | dog_t *new_d; 16 | 17 | new_d = malloc(sizeof(dog_t)); 18 | if (new_d == NULL) 19 | return (NULL); 20 | if (name == NULL) 21 | new_d->name = NULL; 22 | else 23 | { 24 | new_d->name = malloc(_strlen(name) + 1); 25 | if (new_d->name == NULL) 26 | { 27 | free(new_d); 28 | return (NULL); 29 | } 30 | new_d->name = _strcpy(new_d->name, name); 31 | } 32 | if (owner == NULL) 33 | new_d->owner = NULL; 34 | else 35 | { 36 | new_d->owner = malloc(_strlen(owner) + 1); 37 | if (new_d->owner == NULL) 38 | { 39 | free(new_d->name); 40 | free(new_d); 41 | return (NULL); 42 | } 43 | new_d->owner = _strcpy(new_d->owner, owner); 44 | } 45 | new_d->age = age; 46 | return (new_d); 47 | } 48 | /** 49 | * _strlen - gets len of str 50 | * @s: string to get length of 51 | * 52 | * Return: length of s 53 | */ 54 | int _strlen(char *s) 55 | { 56 | int i = 0; 57 | 58 | for (i = 0; s[i]; i++) 59 | ; 60 | return (i); 61 | } 62 | /** 63 | * _strcpy - copies string from y to x 64 | * 65 | * @x: pointer to destination of string 66 | * @y: pointer to source string to copy from 67 | * 68 | * Return: pointer to dest 69 | */ 70 | char *_strcpy(char *x, char *y) 71 | { 72 | char *a = x; 73 | 74 | while (*y) 75 | *a++ = *y++; 76 | *a = '\0'; 77 | return (x); 78 | } 79 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * free_dog - function that free dog 5 | * @d:pointer 6 | */ 7 | void free_dog(dog_t *d) 8 | { 9 | if (d) 10 | { 11 | free(d->name); 12 | free(d->owner); 13 | free(d); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # C - Structures, typedef -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef DOG_H 3 | #define DOG_H 4 | /** 5 | *struct dog - a new type 6 | *@name : string 7 | *@age : float 8 | *@owner : string 9 | *Description: a new type that contains dog name, age, and owner. 10 | */ 11 | typedef struct dog 12 | { 13 | char *name; 14 | float age; 15 | char *owner; 16 | } dog_t; 17 | void init_dog(struct dog *d, char *name, float age, char *owner); 18 | void print_dog(struct dog *d); 19 | dog_t *new_dog(char *name, float age, char *owner); 20 | void free_dog(dog_t *d); 21 | #endif 22 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | #include 3 | /** 4 | * print_name - print name 5 | * @name : string 6 | * @f: function 7 | * Return: void 8 | */ 9 | void print_name(char *name, void (*f)(char *)) 10 | { 11 | if (name != NULL && f != NULL) 12 | f(name); 13 | } 14 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | /** 3 | * array_iterator - excute a function on array element 4 | * @array: array 5 | * @size: size_t 6 | * @action:function to be excuted 7 | * Return:void 8 | */ 9 | void array_iterator(int *array, size_t size, void (*action)(int)) 10 | { 11 | size_t i; 12 | 13 | i = 0; 14 | if (action != NULL && array != NULL) 15 | { 16 | for (i = 0; i < size; i++) 17 | action(array[i]); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - prints opcode of own main function 5 | * 6 | * @argc: argument count 7 | * @argv: arg value 8 | * Return: int 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int x, i; 13 | unsigned char *p; 14 | 15 | if (argc != 2) 16 | { 17 | printf("Error\n"); 18 | exit(1); 19 | } 20 | x = atoi(argv[1]); 21 | if (x < 0) 22 | { 23 | printf("Error\n"); 24 | exit(2); 25 | } 26 | p = (unsigned char *)main; 27 | i = 0; 28 | if (x > 0) 29 | { 30 | while (i < (x - 1)) 31 | printf("%02hhx ", p[i++]); 32 | printf("%hhx\n", p[i]); 33 | } 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | /** 3 | * int_index - Search for an integer 4 | * @array:int 5 | * @size:int 6 | * @cmp:function 7 | * Return:index of the integer searched for 8 | */ 9 | int int_index(int *array, int size, int (*cmp)(int)) 10 | { 11 | int i, x; 12 | 13 | if (size <= 0) 14 | { 15 | return (-1); 16 | } 17 | if (size && cmp != NULL && array != NULL) 18 | { 19 | for (i = 0; i < size; i++) 20 | { 21 | x = cmp(array[i]); 22 | if (x != 0) 23 | return (i); 24 | } 25 | } 26 | return (-1); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H_ 2 | #define _CALC_H_ 3 | 4 | /** 5 | * struct op - Struct op 6 | * 7 | * @op: The operator 8 | * @f: The function associated 9 | */ 10 | typedef struct op 11 | { 12 | char *op; 13 | int (*f)(int a, int b); 14 | } op_t; 15 | 16 | int op_add(int a, int b); 17 | int op_sub(int a, int b); 18 | int op_mul(int a, int b); 19 | int op_div(int a, int b); 20 | int op_mod(int a, int b); 21 | int (*get_op_func(char *s))(int, int); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | 4 | /** 5 | * get_op_func - selects the correct operation toperform 6 | * @s: operation to perform 7 | * 8 | * Return: pointer to the correct function 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 (ops[i].op != NULL) 23 | { 24 | if (strcmp(s, ops[i].op) == 0) 25 | { 26 | return (ops[i].f); 27 | } 28 | i++; 29 | } 30 | return (NULL); 31 | } 32 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "3-calc.h" 4 | 5 | /** 6 | * main - performs simple operations 7 | * @argc: arg count 8 | * @argv: arg value 9 | * 10 | * Return:int 11 | */ 12 | int main(int argc, char *argv[]) 13 | { 14 | int a, b, c; 15 | int (*fun)(int, int); 16 | 17 | if (argc != 4) 18 | { 19 | printf("Error\n"); 20 | exit(98); 21 | } 22 | a = atoi(argv[1]); 23 | b = atoi(argv[3]); 24 | fun = get_op_func(argv[2]); 25 | if (fun == NULL) 26 | { 27 | printf("Error\n"); 28 | exit(99); 29 | } 30 | c = fun(a, b); 31 | printf("%d\n", c); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | #include 3 | #include 4 | /** 5 | * op_add - add 6 | * @a:integer 7 | * @b:intger 8 | * Return:result 9 | */ 10 | int op_add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | 15 | /** 16 | * op_sub - sub 17 | * @a:integer 18 | * @b:intger 19 | * Return:result 20 | */ 21 | 22 | int op_sub(int a, int b) 23 | { 24 | return (a - b); 25 | } 26 | /** 27 | * op_mul - mul 28 | * @a:integer 29 | * @b:intger 30 | * Return:result 31 | */ 32 | 33 | int op_mul(int a, int b) 34 | { 35 | return (a * b); 36 | } 37 | /** 38 | * op_div - div 39 | * @a:integer 40 | * @b:intger 41 | * Return:result 42 | */ 43 | 44 | int op_div(int a, int b) 45 | { 46 | if (b == 0) 47 | { 48 | printf("Error\n"); 49 | exit(100); 50 | } 51 | return (a / b); 52 | } 53 | /** 54 | * op_mod - mod 55 | * @a:integer 56 | * @b:intger 57 | * Return:result 58 | */ 59 | 60 | int op_mod(int a, int b) 61 | { 62 | if (b == 0) 63 | { 64 | printf("Error\n"); 65 | exit(100); 66 | } 67 | return (a % b); 68 | } 69 | 70 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # C-Function pointers -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_FILE_H_ 2 | #define _FUNCTION_FILE_H_ 3 | #include 4 | void print_name(char *name, void (*f)(char *)); 5 | void array_iterator(int *array, size_t size, void (*action)(int)); 6 | int int_index(int *array, int size, int (*cmp)(int)); 7 | #endif 8 | -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | /** 3 | * sum_them_all - sum of variadic variable 4 | * @n:const 5 | * Return:int 6 | */ 7 | int sum_them_all(const unsigned int n, ...) 8 | { 9 | unsigned int i; 10 | va_list list; 11 | double sum = 0; 12 | 13 | if (n == 0) 14 | { 15 | return (0); 16 | } 17 | if (n == 0) 18 | { 19 | return (0); 20 | } 21 | va_start(list, n); 22 | for (i = 0; i < n; i++) 23 | { 24 | sum += va_arg(list, int); 25 | } 26 | va_end(list); 27 | return (sum); 28 | } 29 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | /** 5 | * print_numbers - print variadic number 6 | * @separator: char 7 | * @n:variadic variable count 8 | * Return :void 9 | */ 10 | void print_numbers(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list list; 13 | unsigned int i; 14 | 15 | va_start(list, n); 16 | for (i = 0; i < n; i++) 17 | { 18 | printf("%d", va_arg(list, int)); 19 | if (separator && i < n - 1) 20 | { 21 | printf("%s", separator); 22 | } 23 | } 24 | printf("\n"); 25 | va_end(list); 26 | } 27 | -------------------------------------------------------------------------------- /0x10-variadic_functions/100-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | section .data 2 | message db "Hello, Holberton",10 3 | section .text 4 | global main 5 | main: 6 | mov rax, 1 7 | mov rdi, 1 8 | mov rsi, message 9 | mov rdx, 17 10 | syscall 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_strings(", ", 2, "Jay", "Django"); 11 | print_strings("", 3,"","belhaj",""); 12 | return (0); 13 | } -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | /** 5 | * print_strings - print strings 6 | * @separator:char 7 | * @n:unsigned int 8 | * Return:void 9 | */ 10 | void print_strings(const char *separator, const unsigned int n, ...) 11 | { 12 | va_list list; 13 | unsigned int i; 14 | char *str; 15 | 16 | va_start(list, n); 17 | for (i = 0; i < n; i++) 18 | { 19 | str = va_arg(list, char *); 20 | if (str) 21 | printf("%s", str); 22 | else 23 | printf("(nil)"); 24 | if (i < n - 1 && separator) 25 | printf("%s", separator); 26 | } 27 | va_end(list); 28 | printf("\n"); 29 | } 30 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | #include 3 | #include 4 | /** 5 | * p_char - print char 6 | * @list:arg 7 | * Return: void 8 | */ 9 | 10 | void p_char(va_list list) 11 | { 12 | printf("%c", va_arg(list, int)); 13 | } 14 | /** 15 | * p_string - print string 16 | * @list:arg 17 | * Return: void 18 | */ 19 | 20 | void p_string(va_list list) 21 | { 22 | char *str; 23 | 24 | str = va_arg(list, char*); 25 | if (str == NULL) 26 | str = "(nil)"; 27 | printf("%s", str); 28 | } 29 | /** 30 | * p_integer - print integer 31 | * @list:arg 32 | * Return: void 33 | */ 34 | 35 | void p_integer(va_list list) 36 | { 37 | printf("%i", va_arg(list, int)); 38 | } 39 | /** 40 | * p_float - print float 41 | * @list:arg 42 | * Return: void 43 | */ 44 | 45 | void p_float(va_list list) 46 | { 47 | printf("%f", va_arg(list, double)); 48 | } 49 | /** 50 | * print_all - print everything 51 | * @format:arg 52 | * Return: void 53 | */ 54 | void print_all(const char * const format, ...) 55 | 56 | { 57 | unsigned int i, j; 58 | t_print t[] = { 59 | {"c", p_char}, 60 | {"s", p_string}, 61 | {"i", p_integer}, 62 | {"f", p_float}, 63 | {NULL, NULL} 64 | }; 65 | va_list valist; 66 | char *s = ""; 67 | 68 | va_start(valist, format); 69 | i = 0; 70 | while (format && format[i]) 71 | { 72 | j = 0; 73 | while (t[j].x != NULL) 74 | { 75 | if (*(t[j].x) == format[i]) 76 | { 77 | printf("%s", s); 78 | t[j].T_func(valist); 79 | s = ", "; 80 | break; 81 | } 82 | j++; 83 | } 84 | i++; 85 | } 86 | va_end(valist); 87 | printf("\n"); 88 | } 89 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # C - Variadic functions 2 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_VARIADIC_H_ 2 | #define _FUNCTION_VARIADIC_H_ 3 | 4 | int sum_them_all(const unsigned int n, ...); 5 | void print_numbers(const char *separator, const unsigned int n, ...); 6 | void print_strings(const char *separator, const unsigned int n, ...); 7 | void print_all(const char * const format, ...); 8 | #include 9 | /** 10 | * struct print - multiple choice print 11 | * @x: char Type of print 12 | * @T_func: funct 13 | */ 14 | typedef struct print 15 | { 16 | char *x; 17 | void (*T_func)(va_list); 18 | } t_print; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_list - prints all the elements of a list_t list 5 | * @h: pointer 6 | * Return: number of nodes in the list 7 | */ 8 | size_t print_list(const list_t *h) 9 | { 10 | size_t len = 0; 11 | int i; 12 | const list_t *c = h; 13 | 14 | for (i = 0; c; i++) 15 | { 16 | if (c->str == NULL) 17 | printf("[0] (nil)\n"); 18 | else 19 | printf("[%d] %s\n", c->len, c->str); 20 | c = c->next; 21 | len++; 22 | } 23 | return (len); 24 | } 25 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * list_len - number of elements in a linked list_t list. 5 | * @h: pointer 6 | * Return: number of nodes in the list 7 | */ 8 | size_t list_len(const list_t *h) 9 | { 10 | size_t len = 0; 11 | int i; 12 | const list_t *c = h; 13 | 14 | for (i = 0; c; i++) 15 | { 16 | c = c->next; 17 | len++; 18 | } 19 | return (len); 20 | } 21 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printfirst(void) __attribute__ ((constructor)); 4 | /** 5 | * printfirst - print before main 6 | * 7 | * Return: void 8 | */ 9 | void printfirst(void) 10 | { 11 | printf("You're beat! and yet, you must allow,\n"); 12 | printf("I bore my house upon my back!\n"); 13 | } 14 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | section .data 2 | message db "Hello, Holberton",10 3 | section .text 4 | global main 5 | main: 6 | mov rax, 1 7 | mov rdi, 1 8 | mov rsi, message 9 | mov rdx, 17 10 | syscall -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * _strlen - lenght of string 5 | * @s:char 6 | * Return:int 7 | */ 8 | unsigned int _strlen(char *s) 9 | { 10 | int i; 11 | 12 | for (i = 0; s[i] != '\0'; i++) 13 | { 14 | continue; 15 | } 16 | return (i); 17 | } 18 | /** 19 | * add_node - adding node 20 | * @head:last node list 21 | * @str:string 22 | * Return:new node created 23 | */ 24 | list_t *add_node(list_t **head, const char *str) 25 | { 26 | list_t *new; 27 | 28 | if (str == NULL) 29 | return (NULL); 30 | new = malloc(sizeof(list_t)); 31 | if (new == NULL) 32 | { 33 | return (NULL); 34 | } 35 | new->str = strdup(str); 36 | if (new->str == NULL) 37 | { 38 | free(new); 39 | return (NULL); 40 | } 41 | new->len = _strlen(new->str); 42 | new->next = *head; 43 | *head = new; 44 | return (new); 45 | } 46 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * _strlen - lenght of string 5 | * @s:char 6 | * Return:int 7 | */ 8 | unsigned int _strlen(char *s) 9 | { 10 | int i; 11 | 12 | for (i = 0; s[i] != '\0'; i++) 13 | { 14 | continue; 15 | } 16 | return (i); 17 | } 18 | /** 19 | * add_node_end - add new node at the end of the list 20 | * @head:pointer to ponter to lined list 21 | * @str : ponter to string inside the list 22 | * Return:pointer to a node at the end of the list 23 | */ 24 | list_t *add_node_end(list_t **head, const char *str) 25 | { 26 | list_t *new, *last; 27 | 28 | if (str == NULL) 29 | return (NULL); 30 | new = malloc(sizeof(list_t)); 31 | if (new == NULL) 32 | return (NULL); 33 | new->str = strdup(str); 34 | if (new->str == NULL) 35 | { 36 | free(new); 37 | return (NULL); 38 | } 39 | new->len = _strlen(new->str); 40 | new->next = NULL; 41 | if (*head == NULL) 42 | { 43 | *head = new; 44 | } 45 | else 46 | { 47 | last = *head; 48 | while (last->next) 49 | { 50 | last = last->next; 51 | } 52 | last->next = new; 53 | } 54 | return (new); 55 | } 56 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - Frees a list_t 5 | * @head: A pointer to the list_t 6 | * Return:void 7 | */ 8 | void free_list(list_t *head) 9 | { 10 | list_t *check, *c; 11 | 12 | c = head; 13 | while (c != NULL) 14 | { 15 | check = c->next; 16 | free(c->str); 17 | free(c); 18 | c = check; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x12-singly_linked_lists -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LIST_H_ 2 | #define _LIST_H_ 3 | #include 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 | unsigned int _strlen(char *s); 25 | size_t print_list(const list_t *h); 26 | size_t list_len(const list_t *h); 27 | list_t *add_node(list_t **head, const char *str); 28 | list_t *add_node_end(list_t **head, const char *str); 29 | void free_list(list_t *head); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - print all element in a list 5 | * @h: pointer in a list 6 | * Return:size of the list 7 | */ 8 | size_t print_listint(const listint_t *h) 9 | { 10 | size_t l = 0; 11 | 12 | while (h) 13 | { 14 | printf("%d\n", h->n); 15 | l++; 16 | h = h->next; 17 | } 18 | return (l); 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - length of a list 5 | * @h:pointer to list element 6 | * Return: size of list 7 | */ 8 | size_t listint_len(const listint_t *h) 9 | { 10 | size_t len = 0; 11 | 12 | while (h) 13 | { 14 | len++; 15 | h = h->next; 16 | } 17 | 18 | return (len); 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | 5 | 6 | 7 | /** 8 | * delete_nodeint_at_index - delete a node from a list at some given index 9 | * @head: header pointer 10 | * @index: index of the node to be deleted 11 | * Return: 1 onSuccess 12 | * Error: -1 13 | */ 14 | 15 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 16 | { 17 | listint_t *t; 18 | listint_t *curr; 19 | unsigned int i = 0; 20 | 21 | if (*head == NULL) 22 | return (-1); 23 | 24 | curr = *head; 25 | 26 | if (index == 0) 27 | { 28 | *head = curr->next; 29 | free(curr); 30 | return (1); 31 | } 32 | 33 | while (i < index && curr->next) 34 | { 35 | t = curr; 36 | curr = curr->next; 37 | i++; 38 | } 39 | if (i < index) 40 | return (-1); 41 | 42 | t->next = curr->next; 43 | free(curr); 44 | 45 | return (1); 46 | } 47 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a list 5 | * @head: a pointer to the given list 6 | * 7 | * Return: a pointer to the newly reversed list 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *back, *next; 12 | 13 | if (*head == NULL || head == NULL) 14 | return (NULL); 15 | if ((*head)->next == NULL) 16 | return (*head); 17 | back = NULL; 18 | while (*head) 19 | { 20 | next = (*head)->next; 21 | (*head)->next = back; 22 | back = *head; 23 | *head = next; 24 | } 25 | *head = back; 26 | return (*head); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - add node list 5 | * @head:pointer to node 6 | * @n:int to refill 7 | * Return: new node 8 | */ 9 | listint_t *add_nodeint(listint_t **head, const int n) 10 | { 11 | listint_t *new; 12 | 13 | new = malloc(sizeof(listint_t)); 14 | if (new == NULL) 15 | return (NULL); 16 | 17 | new->next = *head; 18 | new->n = n; 19 | *head = new; 20 | return (new); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - add node at the end of the list 5 | *@head:pointer to last node 6 | *@n:integer elment in node 7 | * Return:new node 8 | */ 9 | listint_t *add_nodeint_end(listint_t **head, const int n) 10 | { 11 | listint_t *new, *start; 12 | 13 | start = *head; 14 | new = malloc(sizeof(listint_t)); 15 | if (new == NULL) 16 | return (NULL); 17 | new->n = n; 18 | new->next = NULL; 19 | if (*head == NULL) 20 | { 21 | *head = new; 22 | return (new); 23 | } 24 | while (start->next) 25 | { 26 | start = start->next; 27 | } 28 | 29 | start->next = new; 30 | return (new); 31 | } 32 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - free list 5 | * @head:pointer to node element 6 | * Return:void 7 | */ 8 | void free_listint(listint_t *head) 9 | { 10 | listint_t *check, *c; 11 | 12 | c = head; 13 | while (c != NULL) 14 | { 15 | check = c->next; 16 | free(c); 17 | c = check; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - free list 5 | * @head:pointer to list 6 | * Return:void 7 | */ 8 | void free_listint2(listint_t **head) 9 | { 10 | listint_t *c; 11 | 12 | if (head == NULL) 13 | return; 14 | while (*head) 15 | { 16 | c = *head; 17 | *head = (*head)->next; 18 | free(c); 19 | } 20 | head = NULL; 21 | } 22 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * pop_listint - delete the head node 4 | *@head: pointer to list 5 | *Return: head node's data 6 | */ 7 | int pop_listint(listint_t **head) 8 | { 9 | listint_t *t; 10 | int n; 11 | 12 | t = *head; 13 | if (t == NULL) 14 | return (0); 15 | *head = t->next; 16 | n = t->n; 17 | free(t); 18 | return (n); 19 | } 20 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - return nth node index 5 | * @head:pointer to node 6 | * @index:given index int 7 | * Return:Null or index of node 8 | */ 9 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index) 10 | { 11 | size_t x; 12 | 13 | for (x = 0; x < index && head->next; x++) 14 | { 15 | head = head->next; 16 | } 17 | if (x < index) 18 | return (NULL); 19 | return (head); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - sum of element list 5 | * @head:pointer to list 6 | * Return: int 7 | */ 8 | int sum_listint(listint_t *head) 9 | { 10 | int sum = 0; 11 | 12 | if (head == NULL) 13 | return (0); 14 | 15 | while (head->next) 16 | { 17 | sum += head->n; 18 | head = head->next; 19 | } 20 | sum += head->n; 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 at a given index 5 | * @head: a pointer to list 6 | * @index: the given index 7 | * @n: the given number 8 | * 9 | * Return: a pointer to the newly add node 10 | */ 11 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int index, int n) 12 | { 13 | listint_t *new, *t; 14 | unsigned int i = 0; 15 | 16 | new = malloc(sizeof(listint_t)); 17 | if (!new) 18 | return (NULL); 19 | new->n = n; 20 | 21 | if (*head == NULL) 22 | { 23 | *head = new; 24 | new->next = NULL; 25 | return (new); 26 | } 27 | if (index == 0) 28 | { 29 | new->next = *head; 30 | *head = new; 31 | return (new); 32 | } 33 | 34 | t = *head; 35 | while (i < (index - 1) && t->next) 36 | { 37 | t = t->next; 38 | i++; 39 | } 40 | if (i < index - 1) 41 | return (NULL); 42 | 43 | new->next = t->next; 44 | t->next = new; 45 | 46 | return (new); 47 | } 48 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x13. C - More singly linked lists 2 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LIST_H_ 2 | #define _LIST_H_ 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct listint_s - singly linked list 10 | * @n: integer 11 | * @next: points to the next node 12 | * 13 | * Description: singly linked list node structure 14 | * for Holberton project 15 | */ 16 | typedef struct listint_s 17 | { 18 | int n; 19 | struct listint_s *next; 20 | } listint_t; 21 | 22 | size_t print_listint(const listint_t *h); 23 | size_t listint_len(const listint_t *h); 24 | listint_t *add_nodeint(listint_t **head, const int n); 25 | listint_t *add_nodeint_end(listint_t **head, const int n); 26 | void free_listint(listint_t *head); 27 | void free_listint2(listint_t **head); 28 | int pop_listint(listint_t **head); 29 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 30 | int sum_listint(listint_t *head); 31 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 32 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 33 | listint_t *reverse_listint(listint_t **head); 34 | size_t print_listint_safe(const listint_t *head); 35 | size_t free_listint_safe(listint_t **h); 36 | listint_t *find_listint_loop(listint_t *head); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * _strlen - lenght of string 5 | * @s:char 6 | * Return:int 7 | */ 8 | int _strlen(const char *s) 9 | { 10 | int i; 11 | 12 | for (i = 0; s[i] != '\0'; i++) 13 | { 14 | continue; 15 | } 16 | return (i); 17 | } 18 | /** 19 | * binary_to_uint - convert binary to unsigned int 20 | * @b:string from 0 and 1 else NULL 21 | * 22 | * Return:unsigned int 23 | */ 24 | unsigned int binary_to_uint(const char *b) 25 | { 26 | unsigned int k = 1; 27 | unsigned int i = 0; 28 | int c; 29 | unsigned int len; 30 | 31 | if (b == NULL) 32 | return (0); 33 | 34 | len = _strlen(b); 35 | 36 | for (c = len - 1; c >= 0; c--) 37 | { 38 | if (b[c] != '0' && b[c] != '1') 39 | return (0); 40 | if (b[c] == '1') 41 | { 42 | i += k; 43 | } 44 | k *= 2; 45 | } 46 | return (i); 47 | } 48 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * print_binary - prints the binary 5 | * @n : unsigned int 6 | */ 7 | void print_binary(unsigned long int n) 8 | { 9 | if (n > 1) 10 | print_binary(n >> 1); 11 | 12 | _putchar((n & 1) + '0'); 13 | } 14 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * get_endianness - check endianness 5 | * 6 | * Return: 1 or 0 7 | */ 8 | int get_endianness(void) 9 | { 10 | unsigned int i = 1; 11 | char *c = (char *)&i; 12 | 13 | if (*c) 14 | return (1); 15 | else 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given index 5 | * @n: unsigned int 6 | * @index:index unsigned int 7 | * Return: bit at given index else -1 if error accured 8 | */ 9 | int get_bit(unsigned long int n, unsigned int index) 10 | { 11 | if (sizeof(n) * 8 < index) 12 | return (-1); 13 | 14 | n >>= index; 15 | if ((n & 1) == 1) 16 | return (1); 17 | else 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * set_bit - set value to 1 with given index 5 | * @n:pointer to unsigned int 6 | * @index: unsigned int index 7 | * Return: 1 if succes -1 in case of error 8 | */ 9 | int set_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned int x = 1; 12 | 13 | if (sizeof(n) * 8 < index) 14 | return (-1); 15 | 16 | x <<= index; 17 | *n |= x; 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * clear_bit - set value to 1 with given index 5 | * @n:pointer to unsigned int 6 | * @index: unsigned int index 7 | * Return: 1 if succes -1 in case of error 8 | */ 9 | int clear_bit(unsigned long int *n, unsigned int index) 10 | { 11 | unsigned int x = 1; 12 | 13 | if (sizeof(n) * 8 < index) 14 | return (-1); 15 | 16 | x <<= index; 17 | *n &= ~x; 18 | return (1); 19 | } 20 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * flip_bits - returns the number of bits you would need to flip 5 | * @n : unsigned long int 6 | * @m : unsigned long int 7 | * Return: number of bits you would need to flip 8 | */ 9 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 10 | { 11 | unsigned long int t; 12 | int i = 0; 13 | 14 | t = n ^ m; 15 | while (t >= 1) 16 | { 17 | if ((t & 1) == 1) 18 | i++; 19 | t >>= 1; 20 | } 21 | return (i); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # 0x14. C - Bit manipulation -------------------------------------------------------------------------------- /0x14-bit_manipulation/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef _HOLBERTON_H_ 2 | #define _HOLBERTON_H_ 3 | #include 4 | 5 | unsigned int binary_to_uint(const char *b); 6 | void print_binary(unsigned long int n); 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 | int _putchar(char c); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * read_textfile - read text and print it to POSIX 5 | * @filename: name of file char 6 | * @letters:number of letters to read size_t 7 | * Return: read the file display it or error 0 8 | */ 9 | ssize_t read_textfile(const char *filename, size_t letters) 10 | { 11 | int fd, rd, wd; 12 | char *buff = malloc(sizeof(char) * letters); 13 | 14 | if (filename == NULL) 15 | { 16 | return (0); 17 | } 18 | 19 | fd = open(filename, O_RDWR); 20 | if (fd == -1) 21 | { 22 | return (0); 23 | } 24 | rd = read(fd, buff, letters); 25 | if (rd == -1) 26 | { 27 | return (0); 28 | } 29 | wd = write(STDOUT_FILENO, buff, rd); 30 | if (wd == -1) 31 | { 32 | return (0); 33 | } 34 | close(fd); 35 | free(buff); 36 | return (wd); 37 | } 38 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * create_file - create a file and write in it 5 | * @filename:file to be created 6 | * @text_content: content of the file created 7 | * Return: 1 or -1; 8 | */ 9 | int create_file(const char *filename, char *text_content) 10 | { 11 | int fd, len, w; 12 | 13 | if (filename == NULL) 14 | return (-1); 15 | 16 | fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, 00600); 17 | if (fd == -1) 18 | { 19 | return (-1); 20 | } 21 | if (text_content != NULL) 22 | { 23 | for (len = 0; text_content[len] != '\0'; len++) 24 | ; 25 | w = write(fd, text_content, len); 26 | } 27 | if (w == -1) 28 | { 29 | return (-1); 30 | } 31 | close(fd); 32 | return (1); 33 | } 34 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * append_text_to_file - appand a text to the end of file 5 | * @filename: filename 6 | * @text_content: text content of the file 7 | * Return: 1 or -1 8 | */ 9 | int append_text_to_file(const char *filename, char *text_content) 10 | { 11 | int fd, len, w; 12 | 13 | if (filename == NULL) 14 | return (-1); 15 | fd = open(filename, O_APPEND | O_WRONLY); 16 | if (fd == -1) 17 | return (-1); 18 | if (text_content != NULL) 19 | { 20 | for (len = 0; text_content[len] != '\0'; len++) 21 | ; 22 | w = write(fd, text_content, len); 23 | if (w == -1) 24 | return (-1); 25 | } 26 | close(fd); 27 | return (1); 28 | } 29 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "holberton.h" 2 | 3 | /** 4 | * cp - copies src to desinations 5 | * @file_to: the destination file 6 | * @file_from: the source file 7 | * 8 | * Return: integer 9 | */ 10 | int cp(char *file_to, char *file_from) 11 | { 12 | char *buffer[1024]; 13 | int td, fd, fr, fw; 14 | int fc, ftc; 15 | 16 | fd = open(file_from, O_RDONLY); 17 | if (fd < 0) 18 | return (98); 19 | 20 | td = open(file_to, O_CREAT | O_WRONLY | O_TRUNC, 0664); 21 | if (td < 0) 22 | return (99); 23 | 24 | fr = read(fd, buffer, 1024); 25 | if (fr < 0) 26 | return (98); 27 | 28 | while (fr > 0) 29 | { 30 | fw = write(td, buffer, fr); 31 | if (fw < 0) 32 | return (99); 33 | fr = read(fd, buffer, 1024); 34 | if (fr < 0) 35 | return (98); 36 | } 37 | 38 | fc = close(fd); 39 | if (fc < 0) 40 | { 41 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fc); 42 | return (100); 43 | } 44 | ftc = close(td); 45 | if (ftc < 0) 46 | { 47 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", ftc); 48 | return (100); 49 | } 50 | return (0); 51 | } 52 | 53 | /** 54 | * main - the main function 55 | * @ac: the argument count 56 | * @av: the argument vector 57 | * 58 | * Return: always 0 59 | */ 60 | int main(int ac, char **av) 61 | { 62 | int c; 63 | 64 | if (ac != 3) 65 | dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"), exit(97); 66 | 67 | c = cp(av[2], av[1]); 68 | switch (c) 69 | { 70 | case (98): 71 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", av[1]); 72 | exit(98); 73 | case (99): 74 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", av[2]); 75 | exit(99); 76 | case (100): 77 | exit(100); 78 | default: 79 | return (0); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /0x15-file_io/README.md: -------------------------------------------------------------------------------- 1 | # 0x15. C - File I/O -------------------------------------------------------------------------------- /0x15-file_io/holberton.h: -------------------------------------------------------------------------------- 1 | #ifndef _HOLBERTON_H_ 2 | #define _HOLBERTON_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | ssize_t read_textfile(const char *filename, size_t letters); 12 | int create_file(const char *filename, char *text_content); 13 | int append_text_to_file(const char *filename, char *text_content); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - print element in list 5 | * @h:struct list 6 | * Return:list len 7 | */ 8 | size_t print_dlistint(const dlistint_t *h) 9 | { 10 | int i; 11 | size_t len = 0; 12 | 13 | for (i = 0; h; i++) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | len++; 18 | } 19 | return (len); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - Return list len 5 | * @h:struct list 6 | * Return:list len 7 | */ 8 | size_t dlistint_len(const dlistint_t *h) 9 | { 10 | size_t len = 0; 11 | int i = 0; 12 | 13 | for (i = 0; h; i++) 14 | { 15 | len += 1; 16 | h = h->next; 17 | } 18 | return (len); 19 | } 20 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - add new element at begin 5 | * @head:pointer 6 | * @n:element in list 7 | * Return:new element 8 | */ 9 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 10 | { 11 | dlistint_t *new; 12 | 13 | new = malloc(sizeof(dlistint_t)); 14 | if (new == NULL) 15 | return (NULL); 16 | 17 | new->prev = NULL; 18 | new->next = *head; 19 | new->n = n; 20 | if (*head != NULL) 21 | (*head)->prev = new; 22 | *head = new; 23 | return (new); 24 | } 25 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - add element at the end of the list 5 | * @head:list 6 | * @n:element in list 7 | * Return:new element or NULL if failed 8 | */ 9 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 10 | { 11 | dlistint_t *new, *start; 12 | 13 | start = *head; 14 | new = malloc(sizeof(dlistint_t)); 15 | if (new == NULL) 16 | return (NULL); 17 | if (*head == NULL) 18 | { 19 | new->next = NULL; 20 | new->n = n; 21 | new->prev = NULL; 22 | *head = new; 23 | return (new); 24 | } 25 | while (start->next) 26 | { 27 | start = start->next; 28 | } 29 | start->next = new; 30 | new->prev = start; 31 | new->next = NULL; 32 | new->n = n; 33 | return (new); 34 | } 35 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - free a list 5 | * @head:list 6 | * Return:void 7 | */ 8 | void free_dlistint(dlistint_t *head) 9 | { 10 | dlistint_t *start, *check; 11 | 12 | start = head; 13 | while (start != NULL) 14 | { 15 | check = start->next; 16 | free(start); 17 | start = check; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - get node by index 5 | * @head:list 6 | * @index:index of element 7 | * Return: element by index 8 | */ 9 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 10 | { 11 | unsigned int c; 12 | 13 | for (c = 0; c < index && head->next; c++) 14 | { 15 | head = head->next; 16 | } 17 | if (c < index) 18 | return (NULL); 19 | return (head); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - sum of element in list 5 | * @head:list 6 | * Return: int 7 | */ 8 | int sum_dlistint(dlistint_t *head) 9 | { 10 | int sum = 0; 11 | 12 | while (head != NULL) 13 | { 14 | sum += head->n; 15 | head = head->next; 16 | } 17 | return (sum); 18 | } 19 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - insert element at given index 5 | * @h:node 6 | * @idx:index 7 | * @n:element value 8 | * Return: NULL or New element 9 | */ 10 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 11 | { 12 | unsigned int c = 0; 13 | dlistint_t *start, *new; 14 | unsigned int len = len_node(h); 15 | 16 | if (idx == 0) 17 | return (add_dnodeint(h, n)); 18 | start = *h; 19 | while (start) 20 | { 21 | if (c == idx - 1) 22 | break; 23 | else if (c < idx - 1 && start == NULL) 24 | return (NULL); 25 | start = start->next; 26 | c++; 27 | } 28 | new = malloc(sizeof(dlistint_t)); 29 | if (new == NULL) 30 | return (NULL); 31 | if (len == idx) 32 | return (add_dnodeint_end(h, n)); 33 | else if (len < idx) 34 | return (NULL); 35 | start->next->prev = new; 36 | new->next = start->next; 37 | start->next = new; 38 | new->prev = start; 39 | new->n = n; 40 | return (new); 41 | } 42 | /** 43 | * len_node - list len 44 | * 45 | * @node:list 46 | * Return:unsigned int 47 | */ 48 | unsigned int len_node(dlistint_t **node) 49 | { 50 | unsigned int len = 0; 51 | dlistint_t *start; 52 | 53 | start = *node; 54 | while (start != NULL) 55 | { 56 | len += 1; 57 | start = start->next; 58 | } 59 | return (len); 60 | } 61 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - delete node at give index 5 | * @head:list 6 | * @index:given index 7 | * Return: -1 or 0 8 | */ 9 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 10 | { 11 | dlistint_t *start; 12 | unsigned int i; 13 | unsigned int len = len_node(head); 14 | 15 | start = *head; 16 | if (*head == NULL) 17 | return (-1); 18 | 19 | if (index == 0) 20 | { 21 | start = start->next; 22 | free(*head); 23 | *head = start; 24 | if (start != NULL) 25 | start->prev = NULL; 26 | return (1); 27 | } 28 | for (i = 0; i <= index - 1; i++) 29 | { 30 | start = start->next; 31 | if (!start) 32 | return (-1); 33 | } 34 | if (len - 1 == index) 35 | { 36 | start->prev->next = NULL; 37 | free(start); 38 | return (1); 39 | } 40 | start->prev->next = start->next; 41 | start->next->prev = start->prev; 42 | free(start); 43 | return (1); 44 | } 45 | /** 46 | * len_node - list len 47 | * 48 | * @node:list 49 | * Return:unsigned int 50 | */ 51 | unsigned int len_node(dlistint_t **node) 52 | { 53 | unsigned int len = 0; 54 | dlistint_t *start; 55 | 56 | start = *node; 57 | while (start != NULL) 58 | { 59 | len += 1; 60 | start = start->next; 61 | } 62 | return (len); 63 | } 64 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # C - Doubly linked lists -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LIST_H_ 2 | #define _LIST_H_ 3 | #include 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 | unsigned int len_node(dlistint_t **node); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc *.c -c -fPIC && gcc *.o -shared -o liball.so 3 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-operations.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Theemiss/holbertonschool-low_level_programming/b1a32f5cfe72eabc082198bd0a3b97fbfcb0fc47/0x18-dynamic_libraries/100-operations.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C - Dynamic libraries -------------------------------------------------------------------------------- /0x18-dynamic_libraries/holberton.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 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libholberton.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Theemiss/holbertonschool-low_level_programming/b1a32f5cfe72eabc082198bd0a3b97fbfcb0fc47/0x18-dynamic_libraries/libholberton.so -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Create a Hash Table 5 | * @size: The size of the hash table 6 | * Descreption : Alloctaing a Hash Table using Given Size 7 | * Return: NULL Error Occurred Or A Hash Table 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *hash_table; 12 | 13 | hash_table = malloc(sizeof(hash_table_t)); 14 | if (hash_table == NULL) 15 | return (NULL); 16 | hash_table->size = size; 17 | hash_table->array = malloc(sizeof(hash_table->array) * size); 18 | if (hash_table->array == NULL) 19 | return (NULL); 20 | return (hash_table); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * hash_djb2 - implementation of the djb2 algorithm 4 | * @str: string used to generate hash value 5 | * 6 | * Return: hash value 7 | */ 8 | unsigned long int hash_djb2(const unsigned char *str) 9 | { 10 | unsigned long int hash; 11 | int c; 12 | 13 | hash = 5381; 14 | while ((c = *str++)) 15 | { 16 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 17 | } 18 | return (hash); 19 | } 20 | -------------------------------------------------------------------------------- /0x1A-hash_tables/100-sorted_hash_table.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * shash_table_create - creates a sorted hash table 5 | * @size: size of the hash table 6 | * 7 | * Return: pointer to the new table, or NULL on failure 8 | */ 9 | shash_table_t *shash_table_create(unsigned long int size) 10 | { 11 | unsigned long int i; 12 | shash_table_t *sht; 13 | 14 | if (size == 0) 15 | return (NULL); 16 | sht = malloc(sizeof(shash_table_t)); 17 | if (!sht) 18 | return (NULL); 19 | sht->size = size; 20 | sht->array = malloc(sizeof(shash_node_t *) * size); 21 | if (!(sht->array)) 22 | { 23 | free(sht); 24 | return (NULL); 25 | } 26 | for (i = 0; i < size; i++) 27 | sht->array[i] = NULL; 28 | sht->shead = sht->stail = NULL; 29 | return (sht); 30 | } 31 | 32 | /** 33 | * make_shash_node - makes a node for the sorted hash table 34 | * @key: key for the data 35 | * @value: data to be stored 36 | * 37 | * Return: pointer to the new node, or NULL on failure 38 | */ 39 | shash_node_t *make_shash_node(const char *key, const char *value) 40 | { 41 | shash_node_t *shn; 42 | 43 | shn = malloc(sizeof(shash_node_t)); 44 | if (shn == NULL) 45 | return (NULL); 46 | shn->key = strdup(key); 47 | if (shn->key == NULL) 48 | { 49 | free(shn); 50 | return (NULL); 51 | } 52 | shn->value = strdup(value); 53 | if (shn->value == NULL) 54 | { 55 | free(shn->key); 56 | free(shn); 57 | return (NULL); 58 | } 59 | shn->next = shn->snext = shn->sprev = NULL; 60 | return (shn); 61 | } 62 | 63 | /** 64 | * add_node - add a node 65 | * @table: the sorted hash table 66 | * @node: the node to add 67 | * 68 | * Return: void 69 | */ 70 | void add_node(shash_table_t *table, shash_node_t *node) 71 | { 72 | shash_node_t *tmp; 73 | 74 | if (table->shead == NULL && table->stail == NULL) 75 | { 76 | table->shead = table->stail = node; 77 | return; 78 | } 79 | tmp = table->shead; 80 | while (tmp != NULL) 81 | { 82 | if (strcmp(node->key, tmp->key) < 0) 83 | { 84 | node->snext = tmp; 85 | node->sprev = tmp->sprev; 86 | tmp->sprev = node; 87 | if (node->sprev != NULL) 88 | node->sprev->snext = node; 89 | else 90 | table->shead = node; 91 | return; 92 | } 93 | tmp = tmp->snext; 94 | } 95 | node->sprev = table->stail; 96 | table->stail->snext = node; 97 | table->stail = node; 98 | } 99 | 100 | /** 101 | * shash_table_set - sets a key to a value in the hash table 102 | * @ht: sorted hash table 103 | * @key: key to the data 104 | * @value: data to add 105 | * 106 | * Return: 1 on success, 0 otherwise 107 | */ 108 | int shash_table_set(shash_table_t *ht, const char *key, const char *value) 109 | { 110 | unsigned long int index; 111 | char *new_value; 112 | shash_node_t *shn, *tmp; 113 | 114 | if (ht == NULL || ht->array == NULL || ht->size == 0 || 115 | key == NULL || strlen(key) == 0 || value == NULL) 116 | return (0); 117 | index = key_index((const unsigned char *)key, ht->size); 118 | tmp = ht->array[index]; 119 | while (tmp != NULL) 120 | { 121 | if (strcmp(tmp->key, key) == 0) 122 | { 123 | new_value = strdup(value); 124 | if (new_value == NULL) 125 | return (0); 126 | free(tmp->value); 127 | tmp->value = new_value; 128 | return (1); 129 | } 130 | tmp = tmp->next; 131 | } 132 | shn = make_shash_node(key, value); 133 | if (shn == NULL) 134 | return (0); 135 | shn->next = ht->array[index]; 136 | ht->array[index] = shn; 137 | add_node(ht, shn); 138 | return (1); 139 | } 140 | 141 | /** 142 | * shash_table_get - retrieve a value from the hash table 143 | * @ht: hash table 144 | * @key: key to the data 145 | * 146 | * Return: the value associated with key, or NULL on failure 147 | */ 148 | char *shash_table_get(const shash_table_t *ht, const char *key) 149 | { 150 | unsigned long int index; 151 | shash_node_t *tmp; 152 | 153 | if (ht == NULL || ht->array == NULL || ht->size == 0 || 154 | key == NULL || strlen(key) == 0) 155 | return (NULL); 156 | index = key_index((const unsigned char *)key, ht->size); 157 | tmp = ht->array[index]; 158 | while (tmp != NULL) 159 | { 160 | if (strcmp(tmp->key, key) == 0) 161 | return (tmp->value); 162 | tmp = tmp->next; 163 | } 164 | return (NULL); 165 | } 166 | 167 | /** 168 | * shash_table_print - prints a sorted hash table 169 | * @ht: hash table to print 170 | * 171 | * Return: void 172 | */ 173 | void shash_table_print(const shash_table_t *ht) 174 | { 175 | shash_node_t *tmp; 176 | char flag = 0; /* 0 before printing any data, 1 after*/ 177 | 178 | if (ht == NULL || ht->array == NULL) 179 | return; 180 | printf("{"); 181 | tmp = ht->shead; 182 | while (tmp != NULL) 183 | { 184 | if (flag == 1) 185 | printf(", "); 186 | printf("'%s': '%s'", tmp->key, tmp->value); 187 | flag = 1; 188 | tmp = tmp->snext; 189 | } 190 | printf("}\n"); 191 | } 192 | 193 | /** 194 | * shash_table_print_rev - prints a sorted hash table in reverse 195 | * @ht: hash table to print 196 | * 197 | * Return: void 198 | */ 199 | void shash_table_print_rev(const shash_table_t *ht) 200 | { 201 | shash_node_t *tmp; 202 | char flag = 0; /* 0 before printing any data, 1 after*/ 203 | 204 | if (ht == NULL || ht->array == NULL) 205 | return; 206 | printf("{"); 207 | tmp = ht->stail; 208 | while (tmp != NULL) 209 | { 210 | if (flag == 1) 211 | printf(", "); 212 | printf("'%s': '%s'", tmp->key, tmp->value); 213 | flag = 1; 214 | tmp = tmp->sprev; 215 | } 216 | printf("}\n"); 217 | } 218 | 219 | /** 220 | * shash_table_delete - deletes a sorted hash table 221 | * @ht: hash table to delete 222 | * 223 | * Return: void 224 | */ 225 | void shash_table_delete(shash_table_t *ht) 226 | { 227 | unsigned long int i; 228 | shash_node_t *next; 229 | 230 | if (ht == NULL || ht->array == NULL || ht->size == 0) 231 | return; 232 | for (i = 0; i < ht->size; i++) 233 | { 234 | while (ht->array[i] != NULL) 235 | { 236 | next = ht->array[i]->next; 237 | free(ht->array[i]->key); 238 | free(ht->array[i]->value); 239 | free(ht->array[i]); 240 | ht->array[i] = next; 241 | } 242 | } 243 | free(ht->array); 244 | ht->array = NULL; 245 | ht->shead = ht->stail = NULL; 246 | ht->size = 0; 247 | free(ht); 248 | } 249 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | /** 3 | * key_index - gives you the index of a key 4 | * @key: is the key 5 | * @size: size of the array of hash table 6 | * Return: index of the key 7 | */ 8 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 9 | { 10 | unsigned long int hash, index; 11 | 12 | hash = hash_djb2(key); 13 | index = hash % size; 14 | 15 | return (index); 16 | } 17 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_set - add elements to hash table 5 | * @ht: hash table 6 | * @key: key 7 | * @value: value associeted to the key to add 8 | * Return: 0(Fails) 1(Succes) 9 | */ 10 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 11 | { 12 | unsigned long int index, size; 13 | hash_node_t *node, *temp; 14 | 15 | if (!key || !value || !ht) 16 | return (0); 17 | size = ht->size; 18 | index = key_index((const unsigned char *)key, size); 19 | temp = ht->array[index]; 20 | while (temp) 21 | { 22 | if (temp && strcmp(key, temp->key) == 0) 23 | { 24 | free(temp->value); 25 | temp->value = strdup(value); 26 | if (temp->value == NULL) 27 | return (0); 28 | return (1); 29 | } 30 | temp = temp->next; 31 | } 32 | node = malloc(sizeof(hash_node_t)); 33 | if (node == NULL) 34 | return (0); 35 | node->key = strdup(key); 36 | if (node->key == NULL) 37 | return (0); 38 | node->value = strdup(value); 39 | if (node->value == NULL) 40 | return (0); 41 | node->next = ht->array[index]; 42 | ht->array[index] = node; 43 | return (1); 44 | } 45 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - retrieves a value associated with a key 5 | * @ht: hash table 6 | * @key: key 7 | * Return: Value associeted with the key or Null 8 | */ 9 | char *hash_table_get(const hash_table_t *ht, const char *key) 10 | { 11 | unsigned long int index; 12 | hash_node_t *node; 13 | 14 | if (!ht || !key) 15 | return (NULL); 16 | index = key_index((const unsigned char *)key, ht->size); 17 | node = ht->array[index]; 18 | while (node) 19 | { 20 | if (strcmp(node->key, key) == 0) 21 | return (node->value); 22 | node = node->next; 23 | } 24 | return (NULL); 25 | } 26 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - print hash table 5 | * @ht: hash table 6 | * Return: Printed hash table or nth 7 | */ 8 | void hash_table_print(const hash_table_t *ht) 9 | { 10 | unsigned long int i; 11 | int init = 0; 12 | 13 | if (!ht) 14 | return; 15 | printf("{"); 16 | for (i = 0; i <= ht->size; i++) 17 | { 18 | if (ht->array[i]) 19 | { 20 | while (ht->array[i]) 21 | { 22 | if (init != 0) 23 | printf(", "); 24 | printf("'%s': '%s'", ht->array[i]->key, ht->array[i]->value); 25 | init = 1; 26 | ht->array[i] = ht->array[i]->next; 27 | 28 | } 29 | } 30 | } 31 | printf("}\n"); 32 | } 33 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - delete a hash table 5 | * @ht:hash table 6 | * Return: void 7 | */ 8 | void hash_table_delete(hash_table_t *ht) 9 | { 10 | unsigned long int i; 11 | hash_node_t *node; 12 | 13 | if (!ht || !ht->array || !ht->size) 14 | return; 15 | for (i = 0; i < ht->size; i++) 16 | { 17 | while (ht->array[i]) 18 | { 19 | node = ht->array[i]->next; 20 | free(ht->array[i]->key); 21 | free(ht->array[i]->value); 22 | free(ht->array[i]); 23 | ht->array[i] = node; 24 | } 25 | } 26 | free(ht->array); 27 | ht->array = NULL; 28 | ht->size = 0; 29 | free(ht); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | # C - HASH TABLE -------------------------------------------------------------------------------- /0x1A-hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef _HASH_TABLE_H_ 2 | #define _HASH_TABLE_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | /*********** STRUCTS AND LISTS *******/ 8 | 9 | /** 10 | * struct hash_node_s - Node of a hash table 11 | * 12 | * @key: The key, string 13 | * The key is unique in the HashTable 14 | * @value: The value corresponding to a key 15 | * @next: A pointer to the next node of the List 16 | */ 17 | typedef struct hash_node_s 18 | { 19 | char *key; 20 | char *value; 21 | struct hash_node_s *next; 22 | } hash_node_t; 23 | 24 | /** 25 | * struct hash_table_s - Hash table data structure 26 | * 27 | * @size: The size of the array 28 | * @array: An array of size @size 29 | * Each cell of this array is a pointer to the first node of a linked list, 30 | * because we want our HashTable to use a Chaining collision handling 31 | */ 32 | typedef struct hash_table_s 33 | { 34 | unsigned long int size; 35 | hash_node_t **array; 36 | } hash_table_t; 37 | 38 | /****** Hash functions *******/ 39 | 40 | hash_table_t *hash_table_create(unsigned long int size); 41 | unsigned long int hash_djb2(const unsigned char *str); 42 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 43 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 44 | char *hash_table_get(const hash_table_t *ht, const char *key); 45 | void hash_table_print(const hash_table_t *ht); 46 | void hash_table_delete(hash_table_t *ht); 47 | 48 | 49 | /** 50 | * struct shash_node_s - Node of a sorted hash table 51 | * 52 | * @key: The key, string 53 | * The key is unique in the HashTable 54 | * @value: The value corresponding to a key 55 | * @next: A pointer to the next node of the List 56 | * @sprev: A pointer to the previous element of the sorted linked list 57 | * @snext: A pointer to the next element of the sorted linked list 58 | */ 59 | typedef struct shash_node_s 60 | { 61 | char *key; 62 | char *value; 63 | struct shash_node_s *next; 64 | struct shash_node_s *sprev; 65 | struct shash_node_s *snext; 66 | } shash_node_t; 67 | 68 | /** 69 | * struct shash_table_s - Sorted hash table data structure 70 | * 71 | * @size: The size of the array 72 | * @array: An array of size @size 73 | * Each cell of this array is a pointer to the first node of a linked list, 74 | * because we want our HashTable to use a Chaining collision handling 75 | * @shead: A pointer to the first element of the sorted linked list 76 | * @stail: A pointer to the last element of the sorted linked list 77 | */ 78 | typedef struct shash_table_s 79 | { 80 | unsigned long int size; 81 | shash_node_t **array; 82 | shash_node_t *shead; 83 | shash_node_t *stail; 84 | } shash_table_t; 85 | 86 | shash_table_t *shash_table_create(unsigned long int size); 87 | int shash_table_set(shash_table_t *ht, const char *key, const char *value); 88 | char *shash_table_get(const shash_table_t *ht, const char *key); 89 | void shash_table_print(const shash_table_t *ht); 90 | void shash_table_print_rev(const shash_table_t *ht); 91 | void shash_table_delete(shash_table_t *ht); 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc main.c holberton.c -o holberton 3 | -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c holberton.c 3 | all: 4 | $(CC) $(SRC) -o holberton 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c holberton.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = holberton 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | .PHONY: all clean oclean fclean re 7 | all: m.h $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | clean: 10 | $(RM) *~ $(NAME) 11 | oclean: 12 | $(RM) $(OBJ) 13 | fclean: clean oclean 14 | re: oclean all -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c holberton.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = holberton 5 | all:$(OBJ) 6 | $(CC) $(OBJ) -o $(NAME) 7 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c holberton.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = holberton 5 | RM = rm -f 6 | all:$(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | clean: 9 | ${RM} *~ $(NAME) 10 | oclean: 11 | ${RM} $(OBJ) 12 | fclean: 13 | ${RM} *~ $(NAME) 14 | ${RM} $(OBJ) 15 | 16 | re: fclean all $(OBJ) 17 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c holberton.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = holberton 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | all: $(OBJ) 8 | $(CC) $(CFLAGS) $(OBJ) -o $(NAME) 9 | clean: 10 | ${RM} *~ $(NAME) 11 | oclean: 12 | ${RM} $(OBJ) 13 | 14 | fclean: clean oclean 15 | 16 | re: oclean all 17 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | Technical interview preparation: 4 | """ 5 | 6 | 7 | def island_perimeter(grid): 8 | """ 9 | returns the perimeter of the island described in grid: 10 | 11 | grid is a list of list of integers: 12 | 0 represents a water zone 13 | 1 represents a land zone 14 | One cell is a square with side length 1 15 | Grid cells are connected horizontally/vertically (not diagonally). 16 | Grid is rectangular, width and height don’t exceed 100 17 | Grid is completely surrounded by water, and there is one 18 | island (or nothing). 19 | The island doesn’t have “lakes” (water inside that isn’t 20 | connected to the water around the island). 21 | """ 22 | c = 0 23 | for x in range(0, len(grid), 1): 24 | for j in range(0, len(grid[0]), 1): 25 | if grid[x][j] == 1: 26 | c = c + 4 27 | if j - 1 >= 0 and grid[x][j - 1] == 1: 28 | c -= 2 29 | if x - 1 >= 0 and grid[x - 1][j] == 1: 30 | c -= 2 31 | return (c) 32 | -------------------------------------------------------------------------------- /0x1C-makefiles/README.md: -------------------------------------------------------------------------------- 1 | # Makefiles -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - implement linear search algo 5 | * @array: given array to search in 6 | * @size: size of the array 7 | * @value: value to search for 8 | * Return: first index or -1 9 | */ 10 | int linear_search(int *array, size_t size, int value) 11 | { 12 | size_t i; 13 | 14 | if (array == NULL) 15 | return (-1); 16 | for (i = 0; i < size; i++) 17 | { 18 | printf("Value checked array[%lu] = [%d]\n", i, array[i]); 19 | if (array[i] == value) 20 | return (i); 21 | 22 | } 23 | return (-1); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - implement binary search algo 5 | * @array: given array to search in 6 | * @size: size of the array 7 | * @value: value to search for 8 | * Return: first index or -1 9 | */ 10 | int binary_search(int *array, size_t size, int value) 11 | { 12 | size_t i = 0; 13 | size_t left = 0, right = size - 1, midlle; 14 | 15 | if (array == NULL) 16 | return (-1); 17 | while (left <= right) 18 | { 19 | printf("Searching in array: "); 20 | for (i = left; i < right; i++) 21 | { 22 | printf("%i, ", array[i]); 23 | } 24 | printf("%i\n", array[right]); 25 | midlle = (left + right) / 2; 26 | if (array[midlle] < value) 27 | { 28 | left = midlle + 1; 29 | } 30 | else if (array[midlle] > value) 31 | { 32 | right = midlle - 1; 33 | } 34 | else 35 | { 36 | return (midlle); 37 | } 38 | 39 | } 40 | return (-1); 41 | } 42 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * jump_search - implement jump search algo 5 | * @array: given array to search in 6 | * @size: size of the array 7 | * @value: value to search for 8 | * Return: first index or -1 9 | */ 10 | int jump_search(int *array, size_t size, int value) 11 | { 12 | int jump = 0; 13 | int i = 0; 14 | int b = 0; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | jump = sqrt(size); 19 | while (array[i] < value && i < (int) size) 20 | { 21 | b = i; 22 | i = b + jump; 23 | if (b < (int) size) 24 | printf("Value checked array[%d] = [%d]\n", b, array[b]); 25 | } 26 | 27 | if (b >= (int) size && array[size] != value) 28 | b -= jump; 29 | 30 | printf("Value found between indexes [%d] and [%d]\n", b, b + jump); 31 | 32 | while (array[b] < value) 33 | { 34 | if (b >= (int) size) 35 | break; 36 | printf("Value checked array[%d] = [%d]\n", b, array[b]); 37 | b += 1; 38 | } 39 | 40 | if (array[b] == value) 41 | { 42 | printf("Value checked array[%d] = [%d]\n", b, array[b]); 43 | return (b); 44 | } 45 | return (-1); 46 | } 47 | -------------------------------------------------------------------------------- /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 - implement interpolation search algo 5 | * @array: given array to search in 6 | * @size: size of the array 7 | * @value: value to search for 8 | * Return: first index or -1 9 | */ 10 | int interpolation_search(int *array, size_t size, int value) 11 | { 12 | size_t low = 0; 13 | size_t high = size - 1; 14 | size_t pos; 15 | 16 | if (array == NULL && size == 0) 17 | return (-1); 18 | while ((array[high] != array[low])) 19 | { 20 | pos = low + ((value - array[low]) * (high - low) / 21 | (array[high] - array[low])); 22 | 23 | if (pos < 1 || pos > high) 24 | { 25 | printf("Value checked array[%lu] is out of range\n", pos); 26 | break; 27 | } 28 | printf("Value checked array[%lu] = [%d]\n", pos, array[pos]); 29 | if (array[pos] < value) 30 | low = pos + 1; 31 | else if (value < array[pos]) 32 | high = pos - 1; 33 | else 34 | return (pos); 35 | } 36 | 37 | if (value == array[low]) 38 | return (low); 39 | else 40 | return (-1); 41 | } 42 | -------------------------------------------------------------------------------- /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 | # 0x1E. C - Search Algorithms -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SEARCH_H_ 2 | #define _SEARCH_H_ 3 | 4 | /** HEADERS */ 5 | 6 | #include 7 | #include 8 | #include 9 | /** Prototype */ 10 | int linear_search(int *array, size_t size, int value); 11 | int binary_search(int *array, size_t size, int value); 12 | int jump_search(int *array, size_t size, int value); 13 | int interpolation_search(int *array, size_t size, int value); 14 | #endif 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Low Level Programming & Algorithms 2 | 3 | ## Welcome 4 | Thank you For Checking My Project 5 | 6 | ## Table of Contents 7 | * [About](#about) 8 | * [Requirements](#requirements) 9 | * [Table of content](##Project) 10 | * [Author](#Author) 11 | 12 | # About 13 | This repository contains programs written for the low-level programming and algorithm track at Holberton School. In these projects, which occurred throughout 14 | the duration of the Year 1 curriculum, I learned about data structures, algorithms, and other low-level programming concepts while working in the C language. 15 | The specific list of projects contained follows: 16 | 17 | ## Projects 18 | * Ubuntu 14.4 LTS 19 | * gcc 4.8.4 20 | * Your own main function 21 | 22 | ## Table of content 23 | 1. [0x00](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x00-hello_world) - Hello world! 24 | 2. [0x01](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x01-variables_if_else_while) - Variables, if else and loops 25 | 3. [0x02](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x02-functions_nested_loops) - Functions and nested loops 26 | 4. [0x03](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x03-debugging) - Debugging 27 | 5. [0x04](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x04-more_functions_nested_loops) - More functions and nested loops 28 | 6. [0x05](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x05-pointers_arrays_strings) - Pointers, arrays and strings 29 | 7. [0x06](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x06-pointers_arrays_strings) - More pointers, arrays and strings 30 | 8. [0x07](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x07-pointers_arrays_strings) - Even more pointers, arrays and strings 31 | 9. [0x08](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x08-recursion) - Recursion 32 | 10. [0x09](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x09-static_libraries) - Static libraries 33 | 11. [0x0A](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x0A-argc_argv) - Argc and argv 34 | 12. [0x0B](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x0B-malloc_free) - Malloc and free 35 | 13. [0x0C](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x0C-more_malloc_free) - More malloc and free 36 | 14. [0x0D](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x0D-preprocessor) - Preprocessor 37 | 15. [0x0E](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x0E-structures_typedef) - Structures and typedef 38 | 16. [0x0F](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x0F-function_pointers) - Function pointers 39 | 17. [0x10](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x10-variadic_functions) - Variadic functions 40 | 18. [0x11](https://github.com/Theemiss/printf) - Printf 41 | 19. [0x12](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x12-singly_linked_lists) - Singly linked lists 42 | 20. [0x13](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x13-more_singly_linked_lists) - More singly linked lists 43 | 21. [0x14](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x14-bit_manipulation) - Bit manipulation 44 | 22. [0x15](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x15-file_io) - File IO 45 | 23. [0x17](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x17-doubly_linked_lists) - Doubly Linked List 46 | 23. [0x18](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x18-dynamic_libraries) - Dynamic Library 47 | 24. [0x1A](https://github.com/Theemiss/holbertonschool-low_level_programming/tree/master/0x1A-hash_tables) - Hash Table 48 | 49 | 50 | # Author 51 | Ahmed Belhaj <[Theemiss](https://github.com/Theemiss)> 52 | 53 |

54 | Holberton School logo 55 |

--------------------------------------------------------------------------------