├── .github └── workflows │ └── c-cpp.yml ├── 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 └── a.out ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c ├── README.md └── a.out ├── 0x02-functions_nested_loops ├── .c ├── 0-putchar ├── 0-putchar.c ├── 1-alphabet.c ├── 1-main.c ├── 10-add.c ├── 10-main.c ├── 11-main.c ├── 11-print_to_98.c ├── 2-main.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 3-main.c ├── 4-isalpha.c ├── 4-main.c ├── 5-main.c ├── 5-sign.c ├── 6-abs.c ├── 6-main.c ├── 7-main.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 8-main.c ├── 9-main.c ├── 9-times_table.c ├── README.md ├── _putchar.c ├── cler ├── main.h └── main.h.gch ├── 0x03-debugging ├── 0-main ├── 0-main.c ├── 1-main ├── 1-main.c ├── 2-largest_number.c ├── 2-main ├── 2-main.c ├── 3-convert_day.c ├── 3-main_a ├── 3-main_a.c ├── 3-main_b.c ├── 3-print_remaining_days.c ├── README.md ├── main.h └── positive_or_negative.c ├── 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 ├── _putchar.c └── main.h ├── 0x05-pointers_arrays_strings ├── 0-reset_to_98.c ├── 1-swap.c ├── 100-atoi.c ├── 101-keygen.c ├── 2-strlen.c ├── 3-puts.c ├── 4-print_rev.c ├── 5-rev_string.c ├── 6-puts2.c ├── 7-puts_half.c ├── 8-print_array.c ├── 9-strcpy.c ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 101-print_number.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md └── main.h ├── 0x09-static_libraries ├── README.md ├── create_static_lib.sh ├── liball.a ├── libmy.a └── main.h ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c ├── README.md ├── _putchar.c ├── add ├── args ├── main.h ├── mul ├── mynewnameis └── nargs ├── 0x0B-malloc_free ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 2-main.c ├── 1-init_dog.c ├── 2-main.c ├── 2-print_dog.c ├── 3-main.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md ├── c └── dog.h ├── 0x0F-function_pointers ├── 0-main.c ├── 0-print_name.c ├── 1-array_iterator.c ├── 1-main.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 ├── a └── function_pointers.h ├── 0x10-variadic_functions ├── 3-main.c ├── 0-main.c ├── 0-sum_them_all.c ├── 1-main.c ├── 1-print_numbers.c ├── 2-main.c ├── 2-print_strings.c ├── 3-print_all.c ├── README.md └── variadic_functions.h ├── 0x12-singly_linked_lists ├── 0-main.c ├── 0-print_list.c ├── 1-list_len.c ├── 1-main.c ├── 100-first.c ├── 100-main.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 2-main.c ├── 3-add_node_end.c ├── 3-main.c ├── 4-free_list.c ├── 4-main.c ├── README.md └── lists.h ├── 0x13-more_singly_linked_lists ├── 0-print_listint.c ├── 1-listint_len.c ├── 10-delete_nodeint.c ├── 100-reverse_listint.c ├── 101-print_listint_safe.c ├── 102-free_listint_safe.c ├── 103-find_loop.c ├── 2-add_nodeint.c ├── 3-add_nodeint_end.c ├── 4-free_listint.c ├── 5-free_listint2.c ├── 6-pop_listint.c ├── 7-get_nodeint.c ├── 8-sum_listint.c ├── 9-insert_nodeint.c ├── README.md └── lists.h ├── 0x14-bit_manipulation ├── 0-binary_to_uint.c ├── 1-print_binary.c ├── 100-get_endianness.c ├── 101-password ├── 2-get_bit.c ├── 3-set_bit.c ├── 4-clear_bit.c ├── 5-flip_bits.c ├── README.md └── main.h ├── 0x15-file_io ├── 0-read_textfile.c ├── 1-create_file.c ├── 100-elf_header.c ├── 2-append_text_to_file.c ├── 3-cp.c ├── README.md └── main.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 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 ├── 0-main.c ├── 0-memset.c ├── 1-create_dynamic_lib.sh ├── README.md ├── abs.c ├── atoi.c ├── isalpha.c ├── isdigit.c ├── islower.c ├── isupper.c ├── liball.so ├── libdynamic.so ├── main.h ├── memcpy.c ├── putchar.c ├── puts.c ├── strcat.c ├── strchr.c ├── strcmp.c ├── strcpy.c ├── strlen.c ├── strncat.c ├── strncpy.c ├── strpbrk.c ├── strspn.c └── strstr.c ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md ├── hash_tables.h └── tests │ ├── 0-main.c │ ├── 2-main.c │ ├── 3-main.c │ ├── 4-main.c │ ├── 5-main.c │ └── 6-main.c ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── m.h ├── main.c ├── school.c └── tests │ └── 5-main.py ├── 0x1E-search_algorithms ├── .gitignore ├── 0-linear.c ├── 1-binary.c ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md └── search_algos.h └── README.md /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: configure 17 | run: ./configure 18 | - name: make 19 | run: make 20 | - name: make check 21 | run: make check 22 | - name: make distcheck 23 | run: make distcheck 24 | -------------------------------------------------------------------------------- /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 | #include 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | char str[] = "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n"; 12 | size_t len = strlen(str); 13 | fwrite(str, sizeof(char), len, stdout); 14 | return (1); 15 | } 16 | -------------------------------------------------------------------------------- /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 | * main - Entry point 4 | * 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | puts("\"Programming is like building a multilingual puzzle"); 10 | return (0); 11 | } 12 | -------------------------------------------------------------------------------- /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: %d byte(s)\n", sizeof(char)); 11 | printf("Size of an int: %d byte(s)\n", sizeof(int)); 12 | printf("Size of a long int: %d byte(s)\n", sizeof(long int)); 13 | printf("Size of a long long int: %d byte(s)\n", sizeof(long long int)); 14 | printf("Size of a float: %d byte(s)\n", sizeof(float)); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | # 0x00. C -Hello, World 2 | 3 | ## Tasks 4 | 5 | * [0-preprocessor](./0-preprocessor). 6 | : is a script that runs a C file through the preprocessor and save the result into another file. 7 | 8 | * [1-compiler](./1-compiler). 9 | : is a script that compiles a C file but does not link 10 | 11 | * [2-assembler](./2-assembler). 12 | : is a script that generates the assembly code of a C code and save it in an output file. 13 | 14 | * [3-name](./3-name). 15 | : is a script that compiles a C file and creates an executable named cisfun. 16 | 17 | * [4-puts.c](./4-puts.c). 18 | : is a C program that prints exactly "Programming is like building a multilingual puzzle, followed by a new line. 19 | 20 | * [5-printf.c](./5-printf.c). 21 | : is a C program that prints exactly "with proper grammar, but the outcome is a piece of art,", followed by a new l 22 | 23 | * [6-size.c](./6-size.c). 24 | is a C program that prints the size of various types on the computer it is compiled and run on. 25 | 26 | * [100-intel](./100-intel). 27 | : is a script that generates the assembly code (Intel syntax) of a C code and save it in an output file. 28 | 29 | * [101-quote.c](./101-quote.c). 30 | : is a C program that prints exactly "and that piece of art is useful" - Dora Korpar, 2015-10-19", followed by a n 31 | -------------------------------------------------------------------------------- /0x00-hello_world/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x00-hello_world/a.out -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 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 | 18 | if (n > 0) 19 | { 20 | printf("%d is positive\n", n); 21 | } 22 | else if (n < 0) 23 | { 24 | printf("%d is negative\n", n); 25 | } 26 | else 27 | { 28 | printf("%d is zero\n", n); 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - Entry point 6 | * check if is greater than 5 or 7 | * less than 6 and not 0 8 | * Return: 0 (SUCCESS) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | /* your code goes there */ 17 | printf("Last digit of "); 18 | if ((n % 10) == 0) 19 | printf("%d is 0 and is 0", n); 20 | else if ((n % 10) > 5) 21 | printf("%d is %d and is greater than 5", n, (n % 10)); 22 | else 23 | printf("%d is %d and is less than 6 and not 0", n, (n % 10)); 24 | 25 | printf("\n"); 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints comb of numb 00 to 89. 5 | * Return: ALways 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int a, b; 10 | 11 | for (a = 48; a <= 56; a++) 12 | { 13 | for (b = 49; b <= 57; b++) 14 | { 15 | if (b > a) 16 | { 17 | putchar(a); 18 | putchar(b); 19 | if (a != 56 || b != 57) 20 | { 21 | putchar(','); 22 | putchar(' '); 23 | } 24 | } 25 | } 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers between 012 to 789. 5 | * Return: Always 0 (Success) 6 | */ 7 | int main(void) 8 | { 9 | int n, m, l; 10 | 11 | for (n = 48; n < 58; n++) 12 | { 13 | for (m = 49; m < 58; m++) 14 | { 15 | for (l = 50; l < 58; l++) 16 | { 17 | if (l > m && m > n) 18 | { 19 | putchar(n); 20 | putchar(m); 21 | putchar(l); 22 | if (n != 55 || m != 56) 23 | { 24 | putchar(','); 25 | putchar(' '); 26 | } 27 | } 28 | } 29 | } 30 | } 31 | putchar('\n'); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints numbers between 00 to 99. 5 | * 6 | * Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | int i, e, g, h, op1, op2; 11 | 12 | i = e = g = h = 48; 13 | while (h < 58) 14 | { 15 | g = 48; 16 | while (g < 58) 17 | { 18 | e = 48; 19 | while (e < 58) 20 | { 21 | i = 48; 22 | while (i < 58) 23 | { 24 | op1 = (h * 10) + g; 25 | op2 = (e * 10) + i; 26 | if (op1 < op2) 27 | { 28 | putchar(h); 29 | putchar(g); 30 | putchar(' '); 31 | putchar(e); 32 | putchar(i); 33 | if (h == 57 && g == 56 && e == 57 && i == 57) 34 | break; 35 | putchar(','); 36 | putchar(' '); 37 | } 38 | i++; 39 | } 40 | e++; 41 | } 42 | g++; 43 | } 44 | h++; 45 | } 46 | putchar('\n'); 47 | return (0); 48 | } 49 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * lets play what I call the alphabet game 6 | * Return: 0 (SUCCESS) 7 | */ 8 | 9 | int main(void) 10 | { 11 | char a; 12 | 13 | a = 'a'; 14 | while (a <= 'z') 15 | { 16 | putchar(a); 17 | a++; 18 | } 19 | putchar('\n'); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * lets play what I call the alphabet game again 6 | * but this time uppercase and lowercase 7 | * Return: 0 (SUCCESS) 8 | */ 9 | 10 | int main(void) 11 | { 12 | char low; 13 | char app; 14 | 15 | low = 'a'; 16 | app = 'A'; 17 | while (low <= 'z') 18 | { 19 | putchar(low); 20 | low++; 21 | } 22 | while (app <= 'Z') 23 | { 24 | putchar(app); 25 | app++; 26 | } 27 | putchar('\n'); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - Prints the alphabet without q and e. 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 97; i < 123; i++) 15 | { 16 | if (i != 101 && i != 113) 17 | { 18 | putchar(i); 19 | } 20 | } 21 | putchar('\n'); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * lets print numbers 6 | * Return: 0 (SUCCESS) 7 | */ 8 | 9 | int main(void) 10 | { 11 | puts("0123456789"); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * lets print numbers 6 | * Return: 0 (SUCCESS) 7 | */ 8 | 9 | int main(void) 10 | { 11 | int i; 12 | i = '0'; 13 | while (i <= '9') 14 | { 15 | putchar(i++); 16 | } 17 | putchar('\n'); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * lets write the alphabets with the while loop 6 | * Return: 0 (SUCCESS) 7 | */ 8 | 9 | int main(void) 10 | { 11 | char z; 12 | z = 'z'; 13 | while (z >= 'a') 14 | { 15 | putchar(z); 16 | z--; 17 | } 18 | putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * lets write the alphabets with the while loop 6 | * Return: 0 (SUCCESS) 7 | */ 8 | 9 | int main(void) 10 | { 11 | char a; 12 | char b; 13 | 14 | a = '0'; 15 | b = 'a'; 16 | while (a <= '9') 17 | putchar(a++); 18 | while (b <= 'f') 19 | putchar(b++); 20 | putchar('\n'); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * while loop to print numbers from 0 to 9 6 | * with printing (,) 7 | * Return: 0 (SUCCESS) 8 | */ 9 | 10 | int main(void) 11 | { 12 | int a; 13 | 14 | a = '0'; 15 | while (a <= '9') 16 | { 17 | putchar(a++); 18 | if (a <= '9') 19 | { 20 | putchar(','); 21 | putchar(' '); 22 | } 23 | } 24 | putchar('\n'); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | # 0x01. C - Variables, if, else, while 2 | 3 | ## Tasks 4 | 5 | * [0-positive_or_negative.c](./0-positive_or_negative.c) : is a C program that will assign a random number to the variable n each time it is executed, using an if statement. 6 | 7 | * [1-last_digit.c](./1-last_digit.c) : is a C program that will assign a random number to the variable n each time it is executed using another if statement. 8 | 9 | * [2-print_alphabet.c](./2-print_alphabet.c): is a C program that will assign a random number to the variable n each time it is executed using another if statement. 10 | 11 | * [3-print_alphabets.c](./3-print_alphabets.c):is a C program that prints the alphabet in lowercase, except q and e, followed by a new line. 12 | 13 | * [4-print_alphabt.c](./4-print_alphabt.c):is a C program that prints the alphabet in lowercase, and then in uppercase, followed by a new line. 14 | 15 | * [5-print_numbers.c](./5-print_numbers.c):is a C program that prints all single digit numbers of base 10 starting from 0, followed by a new line. 16 | 17 | * [6-print_numberz.c](./6-print_numberz.c):is a C program that prints all single digit numbers of base 10 starting from 0, followed by a new line, using putchar. 18 | 19 | * [7-print_tebahpla.c](./7-print_tebahpla.c):is a C program that prints the lowercase alphabet in reverse, followed by a new line. 20 | 21 | * [8-print_base16.c](./8-print_base16.c):is a C program that prints all the numbers of base 16 in lowercase, followed by a new line. 22 | 23 | * [9-print_comb.c](./9-print_comb.c):is a C program that prints all possible combinations of single-digit numbers. 24 | 25 | * [100-print_comb3.c](./100-print_comb3.c):is a C program that prints the numbers from 00 to 99. 26 | 27 | * [101-print_comb4.c](./101-print_comb4.c):is a program that prints all possible different combinations of three digits. 28 | 29 | * [102-print_comb5.c](./102-print_comb5.c)": is a program that prints all possible combinations of two two-digit numbers. 30 | 31 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x01-variables_if_else_while/a.out -------------------------------------------------------------------------------- /0x02-functions_nested_loops/.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x02-functions_nested_loops/.c -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x02-functions_nested_loops/0-putchar -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - Entry point 4 | * lets print _putchar 5 | * Return: 0 (SUCCESS) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i = 0; 11 | char str[8] = "_putchar"; 12 | 13 | while (i < 8) 14 | _putchar(str[i++]); 15 | _putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet - alphabet function 5 | * 6 | **/ 7 | void print_alphabet(void) 8 | { 9 | int i = 0; 10 | char str[26] = "abcdefghijklmnopqrstuvwxyz"; 11 | 12 | while (i < 26) 13 | _putchar(str[i++]); 14 | _putchar('\n'); 15 | } 16 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_alphabet(); 11 | return (0); 12 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * add - add two integers 4 | * @a: two integer arguments 5 | * @b: two integer arguments 6 | * Return: sum 7 | */ 8 | 9 | int add(int a, int b) 10 | { 11 | return (a + b); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | 13 | n = add(89, 9); 14 | printf("%d\n", n); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-main.c: -------------------------------------------------------------------------------- 1 | 2 | #include "main.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | print_to_98(0); 12 | print_to_98(98); 13 | print_to_98(111); 14 | print_to_98(81); 15 | print_to_98(-10); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/11-print_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_to_98 - print numbers n to 98 6 | * @n: integer argument 7 | */ 8 | 9 | void print_to_98(int n) 10 | { 11 | while (n < 98) 12 | { 13 | printf("%d, ", n); 14 | n++; 15 | } 16 | 17 | while (n > 98) 18 | { 19 | printf("%d, ", n); 20 | n--; 21 | } 22 | if (n == 98) 23 | printf("%d", n); 24 | printf("\n"); 25 | } 26 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_alphabet_x10(); 11 | return (0); 12 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - print lowercase alphabet 10 times 5 | */ 6 | 7 | void print_alphabet_x10(void) 8 | { 9 | int i; 10 | int j = 0; 11 | 12 | while (j < 10) 13 | { 14 | i = 'a'; 15 | while (i <= 'z') 16 | { 17 | _putchar(i); 18 | i++; 19 | } 20 | _putchar('\n'); 21 | j++; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - find lower caracter 5 | * 6 | * @c: the charactere to check 7 | * 8 | * Return: 1 if true. 9 | **/ 10 | 11 | int _islower(int c) 12 | { 13 | if (c >= 'a' && c <= 'z') 14 | return (1); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = _islower('H'); 13 | _putchar(r + '0'); 14 | r = _islower('o'); 15 | _putchar(r + '0'); 16 | r = _islower(108); 17 | _putchar(r + '0'); 18 | _putchar('\n'); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks for alphabet character 5 | * @c: character to check 6 | * Return: 1 if lower or uppercase, 0 if not alphabet character 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = _isalpha('H'); 13 | _putchar(r + '0'); 14 | r = _isalpha('o'); 15 | _putchar(r + '0'); 16 | r = _isalpha(108); 17 | _putchar(r + '0'); 18 | r = _isalpha(';'); 19 | _putchar(r + '0'); 20 | _putchar('\n'); 21 | return (0); 22 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = print_sign(98); 13 | _putchar(','); 14 | _putchar(' '); 15 | _putchar(r + '0'); 16 | _putchar('\n'); 17 | r = print_sign(0); 18 | _putchar(','); 19 | _putchar(' '); 20 | _putchar(r + '0'); 21 | _putchar('\n'); 22 | r = print_sign(0xff); 23 | _putchar(','); 24 | _putchar(' '); 25 | _putchar(r + '0'); 26 | _putchar('\n'); 27 | r = print_sign(-1); 28 | _putchar(','); 29 | _putchar(' '); 30 | _putchar(r + '0'); 31 | _putchar('\n'); 32 | return (0); 33 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - print sign of number 5 | * @n: number to check 6 | * Return: 1 if greater than 0, 0 if 0, -1 if less than 0 7 | */ 8 | 9 | int print_sign(int n) 10 | { 11 | if (n > 0) 12 | { 13 | _putchar('+'); 14 | return (1); 15 | } 16 | else if (n == 0) 17 | { 18 | _putchar('0'); 19 | return (0); 20 | } 21 | else 22 | { 23 | _putchar('-'); 24 | return (-1); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - compute absolute value of integer 5 | * @n: integer argument 6 | * Return: absolute value 7 | */ 8 | 9 | int _abs(int n) 10 | { 11 | if (n > 0) 12 | return (n); 13 | else if (n < 0) 14 | return (-n); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | r = _abs(-1); 13 | printf("%d\n", r); 14 | r = _abs(0); 15 | printf("%d\n", r); 16 | r = _abs(1); 17 | printf("%d\n", r); 18 | r = _abs(-98); 19 | printf("%d\n", r); 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | int r; 11 | 12 | print_last_digit(98); 13 | print_last_digit(0); 14 | r = print_last_digit(-1024); 15 | _putchar('0' + r); 16 | _putchar('\n'); 17 | return (0); 18 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_last_digit - print last digit of integer 5 | * @n: integer argument 6 | * Return: last digit of number 7 | */ 8 | int print_last_digit(int n) 9 | { 10 | int last_digit; 11 | if (n == INT_MIN) 12 | { 13 | last_digit = 8; 14 | _putchar(last_digit + '0'); 15 | return (last_digit); 16 | } 17 | if (n < 0) 18 | n = -n; 19 | last_digit = n % 10; 20 | _putchar(last_digit + '0'); 21 | return (last_digit); 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer - print every minute of a day 5 | * Description: print minutes 00:00 to 23:59 6 | */ 7 | 8 | void jack_bauer(void) 9 | { 10 | int hours = 0; 11 | int min = 0; 12 | 13 | while (hours < 24) 14 | { 15 | while (min < 60) 16 | { 17 | _putchar('0' + (hours / 10)); 18 | _putchar('0' + (hours % 10)); 19 | _putchar(':'); 20 | _putchar('0' + (min / 10)); 21 | _putchar('0' + (min % 10)); 22 | _putchar('\n'); 23 | min++; 24 | } 25 | min = 0; 26 | hours++; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | jack_bauer(); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | times_table(); 11 | return (0); 12 | } -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * times_table - print multiplication table 5 | */ 6 | 7 | void times_table(void) 8 | { 9 | int row; 10 | int column; 11 | int product; 12 | 13 | for (row = 0; row <= 9; row++) 14 | { 15 | for (column = 0; column <= 9; column++) 16 | { 17 | product = (row * column); 18 | 19 | if (column == 0) 20 | { 21 | _putchar('0' + product); 22 | } 23 | else if (product <= 9) 24 | { 25 | _putchar(','); 26 | _putchar(' '); 27 | _putchar(' '); 28 | _putchar('0' + product); 29 | } 30 | else if (product > 9) 31 | { 32 | _putchar(','); 33 | _putchar(' '); 34 | _putchar('0' + (product / 10)); 35 | _putchar('0' + (product % 10)); 36 | } 37 | } 38 | _putchar('\n'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | ## 0x02. C - Functions, nested loops ## 2 | File 0-holberton.c is a program that prints Holberton, followed by a new line. 3 | 4 | File 1-alphabet.c is a function that prints the alphabet, in lowercase, followed by a new line. 5 | 6 | File 2-print_alphabet_x10.c is a function that prints 10 times the alphabet, in lowercase, followed by a new line. 7 | 8 | File 3-islower.c is a function that checks for lowercase character. 9 | 10 | File 4-isalpha.c is a function that checks for alphabetic character. 11 | 12 | File 5-sign.c is a function that prints the sign of a number. 13 | 14 | File 6-abs.c is a function that computes the absolute value of an integer. 15 | 16 | File 7-print_last_digit.c is a function that prints the last digit of a number. 17 | 18 | File 8-24_hours.c is a function that prints every minute of the day of Jack Bauer, starting from 00:00 to 23:59. 19 | 20 | File 10-add.c is a function that adds two integers and returns the result. 21 | 22 | File 11-print_to_98.c is a function that prints all natural numbers from n to 98, followed by a new line. 23 | 24 | File 100-times_table.c is a function that prints the n times table, starting with 0. 25 | 26 | File 101-natural.c is a program that computes and prints the sum of all the multiples of 3 or 5 below 1024 (excluded), followed by a new line. 27 | 28 | File 102-fibonacci.c is a program that prints the first 50 Fibonacci numbers, starting with 1 and 2, followed by a new line. 29 | 30 | File 103-fibonacci.c is a program that finds and prints the sum of the even-valued terms of the Fibonacci suite under 4000000, followed by a new line. 31 | 32 | File 104-fibonacci.c is a program that finds and prints the first 98 Fibonacci numbers, starting with 1 and 2, followed by a new line. 33 | 34 | File _putchar.c contains the _putchar() function definition. 35 | 36 | 37 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * lets print a character 6 | * Return: 1 (SUCCESS) 7 | */ 8 | 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/cler: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * main - Entry point 4 | * lets print _putchar 5 | * Return: 0 (SUCCESS) 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i = 0; 11 | char str[8] = "_putchar"; 12 | 13 | while (i < 8) 14 | _putchar(str[i++]); 15 | _putchar('\n'); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | void jack_bauer(void); 9 | void print_alphabet(void); 10 | void print_alphabet_x10(void); 11 | int _islower(int c); 12 | int _isalpha(int c); 13 | int print_sign(int n); 14 | int _abs(int n); 15 | int print_last_digit(int n); 16 | void times_table(void); 17 | int add(int a, int b); 18 | void print_to_98(int n); 19 | #endif 20 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x02-functions_nested_loops/main.h.gch -------------------------------------------------------------------------------- /0x03-debugging/0-main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x03-debugging/0-main -------------------------------------------------------------------------------- /0x03-debugging/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * main - tests function that prints if integer is positive or negative 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x03-debugging/1-main -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | /* 16 | *while (i < 10) 17 | *{ 18 | *putchar(i); 19 | *} 20 | */ 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | if (a >= b && a >= c) 15 | { 16 | largest = a; 17 | } 18 | else if (b >= a && b >= c) 19 | { 20 | largest = b; 21 | } 22 | else 23 | { 24 | largest = c; 25 | } 26 | return (largest); 27 | } 28 | -------------------------------------------------------------------------------- /0x03-debugging/2-main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x03-debugging/2-main -------------------------------------------------------------------------------- /0x03-debugging/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - prints the largest of 3 integers 6 | * Return: 0 7 | */ 8 | 9 | int main(void) 10 | { 11 | int a, b, c; 12 | int largest; 13 | a = 972; 14 | b = -98; 15 | c = 0; 16 | largest = largest_number(a, b, c); 17 | printf("%d is the largest number\n", largest); 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x03-debugging/3-convert_day.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * convert_day - converts day of month to day of year, without accounting 5 | * for leap year 6 | * @month: month in number format 7 | * @day: day of month 8 | * Return: day of year 9 | */ 10 | 11 | int convert_day(int month, int day) 12 | { 13 | switch (month) 14 | { 15 | case 2: 16 | day = 31 + day; 17 | break; 18 | case 3: 19 | day = 59 + day; 20 | break; 21 | case 4: 22 | day = 90 + day; 23 | break; 24 | case 5: 25 | day = 120 + day; 26 | break; 27 | case 6: 28 | day = 151 + day; 29 | break; 30 | case 7: 31 | day = 181 + day; 32 | break; 33 | case 8: 34 | day = 212 + day; 35 | break; 36 | case 9: 37 | day = 243 + day; 38 | break; 39 | case 10: 40 | day = 273 + day; 41 | break; 42 | case 11: 43 | day = 304 + day; 44 | break; 45 | case 12: 46 | day = 334 + day; 47 | break; 48 | default: 49 | break; 50 | } 51 | return (day); 52 | } -------------------------------------------------------------------------------- /0x03-debugging/3-main_a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x03-debugging/3-main_a -------------------------------------------------------------------------------- /0x03-debugging/3-main_a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - takes a date and prints how many days are left in the year, taking 6 | * leap years into account 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int month; 13 | int day; 14 | int year; 15 | 16 | month = 4; 17 | day = 01; 18 | year = 1997; 19 | 20 | printf("Date: %02d/%02d/%04d\n", month, day, year); 21 | 22 | day = convert_day(month, day); 23 | 24 | print_remaining_days(month, day, year); 25 | 26 | return (0); 27 | } -------------------------------------------------------------------------------- /0x03-debugging/3-main_b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * main - takes a date and prints how many days are left in the year, taking 6 | * leap years into account 7 | * Return: 0 8 | */ 9 | 10 | int main(void) 11 | { 12 | int month; 13 | int day; 14 | int year; 15 | 16 | month = 2; 17 | day = 29; 18 | year = 2000; 19 | 20 | printf("Date: %02d/%02d/%04d\n", month, day, year); 21 | 22 | day = convert_day(month, day); 23 | 24 | print_remaining_days(month, day, year); 25 | 26 | return (0); 27 | } -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * print_remaining_days - takes a date and prints how many days are 5 | * left in the year, taking leap years into account 6 | * @month: month in number format 7 | * @day: day of month 8 | * @year: year 9 | * Return: void 10 | */ 11 | 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) 16 | { 17 | if (month >= 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | 23 | printf("Day of the year: %d\n", day); 24 | printf("Remaining days: %d\n", 366 - day); 25 | } 26 | else 27 | { 28 | if (month == 2 && day == 60) 29 | { 30 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 31 | } 32 | else 33 | { 34 | printf("Day of the year: %d\n", day); 35 | printf("Remaining days: %d\n", 365 - day); 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | ## Debugging session ^^ ## 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | void print_remaining_days(int month, int day, int year); 9 | int convert_day(int month, int day); 10 | 11 | 12 | #endif /* MAIN_H */ 13 | -------------------------------------------------------------------------------- /0x03-debugging/positive_or_negative.c: -------------------------------------------------------------------------------- 1 | 2 | /* betty style doc for function main goes there */ 3 | #include 4 | #include 5 | #include 6 | /** 7 | * main - Entry point 8 | * 9 | * Return: Always 0 (Success) 10 | */ 11 | int positive_or_negative( int n) 12 | { 13 | 14 | if (n > 0) 15 | { 16 | printf("%d is positive\n", n); 17 | } 18 | else if (n < 0) 19 | { 20 | printf("%d is negative\n", n); 21 | } 22 | else 23 | { 24 | printf("%d is zero\n", n); 25 | } 26 | return (0); 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: character to be checked 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit 0 through 9 5 | * @c: digit to be checked 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - print traingle with #'s with given size 5 | * @size: size of triangle to draw 6 | */ 7 | 8 | void print_triangle(int size) 9 | { 10 | int height; 11 | int width; 12 | int draw; 13 | 14 | if (size <= 0) 15 | _putchar('\n'); 16 | 17 | for (height = 1; height <= size; height++) 18 | { 19 | for (width = 1; width <= (size - height); width++) 20 | _putchar(' '); 21 | 22 | for (draw = 1; draw <= height; draw++) 23 | _putchar('#'); 24 | 25 | _putchar('\n'); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print largest prime factor of 612852475143 5 | * Return: 0 on success 6 | */ 7 | 8 | int main(void) 9 | { 10 | unsigned long num = 612852475143; 11 | unsigned long divisor = 2; 12 | 13 | while (divisor < num) 14 | { 15 | if (num % divisor == 0) 16 | { 17 | num /= divisor; 18 | divisor = 2; 19 | } 20 | else 21 | divisor++; 22 | } 23 | printf("%lu\n", num); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - print an integer, without using long, arrays, or pointers 5 | * @n: number to be printed 6 | */ 7 | 8 | void print_number(int n) 9 | { 10 | unsigned int tens, digit, positive = n; 11 | double t_beg = 1; 12 | 13 | if (n == 0) 14 | _putchar('0'); 15 | else 16 | { 17 | if (n < 0) 18 | { 19 | positive = n * -1; 20 | _putchar('-'); 21 | } 22 | 23 | while (t_beg <= positive) 24 | t_beg *= 10; 25 | tens = t_beg / 10; 26 | 27 | while (tens >= 1) 28 | { 29 | digit = positive / tens; 30 | _putchar(digit + '0'); 31 | positive = (positive - (tens * digit)); 32 | tens /= 10; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two integers 5 | * @a: one integer 6 | * @b: second integer 7 | * Return: product of a and b 8 | */ 9 | 10 | int mul(int a, int b) 11 | { 12 | return (a * b); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - print 0 to 9 5 | */ 6 | 7 | void print_numbers(void) 8 | { 9 | char i; 10 | 11 | for (i = '0'; i <= '9'; i++) 12 | _putchar(i); 13 | 14 | _putchar('\n'); 15 | } 16 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_most_numbers - print 0 to 9, skip 2 and 4 5 | */ 6 | 7 | void print_most_numbers(void) 8 | { 9 | char i; 10 | 11 | for (i = '0'; i <= '9'; i++) 12 | { 13 | if (i != '2' && i != '4') 14 | _putchar(i); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - print 0 to 14 ten times 5 | */ 6 | 7 | void more_numbers(void) 8 | { 9 | char i; 10 | int counter; 11 | 12 | for (counter = 1; counter <= 10; counter++) 13 | { 14 | for (i = 0; i <= 14; i++) 15 | { 16 | if (i / 10 > 0) 17 | _putchar((i / 10) + '0'); 18 | _putchar((i % 10) + '0'); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/6-print_line.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_line - print the character "_" n times to draw a line 5 | * @n: number of characters to draw 6 | */ 7 | 8 | void print_line(int n) 9 | { 10 | int times = n; 11 | 12 | for (times = n; times > 0; times--) 13 | _putchar('_'); 14 | 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - print character "\" n times 5 | * @n: number of characters to draw 6 | */ 7 | 8 | void print_diagonal(int n) 9 | { 10 | int draw; 11 | int space; 12 | 13 | if (n > 0) 14 | { 15 | for (draw = 1; draw <= n; draw++) 16 | { 17 | for (space = 1; space < draw; space++) 18 | _putchar(' '); 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 "main.h" 2 | 3 | /** 4 | * print_square - print a square of #'s given size 5 | * @size: size to draw 6 | */ 7 | 8 | void print_square(int size) 9 | { 10 | int row, column; 11 | 12 | if (size > 0) 13 | { 14 | for (row = 1; row <= size; row++) 15 | { 16 | for (column = 1; column <= size; column++) 17 | { 18 | _putchar('#'); 19 | } 20 | _putchar('\n'); 21 | } 22 | } 23 | else 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - print 1 to 100, 5 | * multiples of 3 with Fizz, 6 | * multiples of 5 with Buzz 7 | * and multiple of both 3 and 5 with FizzBuzz 8 | * Return: 0 on success 9 | */ 10 | 11 | int main(void) 12 | { 13 | int i; 14 | 15 | for (i = 1; i <= 100; i++) 16 | { 17 | if (i % 3 == 0 && i % 5 == 0) 18 | printf("FizzBuzz"); 19 | else if (i % 3 == 0) 20 | printf("Fizz"); 21 | else if (i % 5 == 0) 22 | printf("Buzz"); 23 | else 24 | printf("%d", i); 25 | 26 | if (i < 100) 27 | printf(" "); 28 | } 29 | printf("\n"); 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | 0-isupper.c : function that checks for uppercase character. 2 | 1-isdigit.c : a function that checks for a digit 0 - 9 3 | 2-mul.c : a function that multiplies two integers 4 | 3-print_numbers.c : a function that prints the numbers 0-9 5 | 4-print_most_numbers.c : a function that prints the numbers 0-9 and skips 2 and 4 6 | 5-more_numbers.c : a function that prints 10 times the numbers 0-14 7 | 6-print_line.c : a function that draws a straight line in the terminal, print "_" n times 8 | 7-print_diagonal.c : a function that draws a diagonal line on the terminal, print "\" n times 9 | 8-print_square.c : a function that prints a square, using # 10 | 9-fizz_buzz.c : a program that prints the numbers from 1 to 100, "Fizz" for multiples of three, "Buzz" for multiples of five, and "FizzBuzz" for numbers which are multiples of both three and five 11 | 10-print_triangle.c : a function that prints a triangle with # based on size argument 12 | 100-prime_factor.c : a program that finds and prints the largest prime factor of the number 612852475143 13 | 101-print_number.c : a function that prints an integer 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | 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_triangle(int size); 13 | void print_number(int n); 14 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - Updates the value it points to 98 5 | * @n: pointer to n 6 | * 7 | * Return: void 8 | */ 9 | void reset_to_98(int *n) 10 | { 11 | *n = 98; 12 | } 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - Swaps the values of two integers 5 | * 6 | * @a: An integer to swap 7 | * @b: Another integer to swap 8 | * 9 | * Return: void 10 | */ 11 | void swap_int(int *a, int *b) 12 | { 13 | int c; 14 | 15 | c = *a; 16 | *a = *b; 17 | *b = c; 18 | } 19 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _atoi - convert a string to an integer. 4 | * @s: pointer to string 5 | * 6 | * Return: integer value of string, or 0 if no digits are found 7 | */ 8 | int _atoi(char *s) 9 | { 10 | int sign = 1; 11 | int result = 0; 12 | while (*s) 13 | { 14 | if (*s == '-') 15 | sign = -sign; 16 | else if (*s >= '0' && *s <= '9') 17 | result = result * 10 + (*s - '0'); 18 | else if (result > 0) 19 | break; 20 | s++; 21 | } 22 | return (sign *result); 23 | } 24 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * main - entry point 7 | * 8 | * Return: always 0 9 | */ 10 | int main(void) 11 | { 12 | char password[65]; 13 | int i, sum, diff; 14 | 15 | srand(time(NULL)); 16 | 17 | for (i = 0, sum = 0; sum < (2772 - 122); i++) 18 | { 19 | password[i] = rand() % 94 + 33; 20 | sum += password[i]; 21 | } 22 | diff = 2772 - sum; 23 | password[i] = diff; 24 | password[i + 1] = '\0'; 25 | 26 | printf("%s", password); 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string 5 | * @s: String to count 6 | * 7 | * Return: String length 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int i = 0; 12 | 13 | for (; *s != '\0'; s++) 14 | { 15 | i++; 16 | } 17 | 18 | return (i); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - Prints a string 5 | * @str: The string to print 6 | * 7 | * Return: void 8 | */ 9 | void _puts(char *str) 10 | { 11 | for (; *str != '\0'; str++) 12 | { 13 | _putchar(*str); 14 | } 15 | 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_rev - Prints a string in reverse 6 | * @s: The string to print 7 | * 8 | * Return: void 9 | */ 10 | void print_rev(char *s) 11 | { 12 | int i = 0; 13 | 14 | while (s[i] != '\0') 15 | { 16 | i++; 17 | } 18 | 19 | for (i -= 1; i >= 0; i--) 20 | { 21 | _putchar(s[i]); 22 | } 23 | 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverses a string 5 | * @s: string to be reversed 6 | */ 7 | void rev_string(char *s) 8 | { 9 | char tmp; 10 | int i, len, len1; 11 | len = 0; 12 | len1 = 0; 13 | while (s[len] != '\0') 14 | { 15 | len++; 16 | } 17 | len1 = len - 1; 18 | for (i = 0; i < len / 2; i++) 19 | { 20 | tmp = s[i]; 21 | s[i] = s[len1]; 22 | s[len1--] = tmp; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/6-puts2.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts2 - Prints every other character of a string 5 | * @str: The string to be treated 6 | * 7 | * Return: void 8 | */ 9 | void puts2(char *str) 10 | { 11 | int i; 12 | int j = 0; 13 | 14 | while (str[j] != '\0') 15 | { 16 | j++; 17 | } 18 | 19 | for (i = 0; i < j; i += 2) 20 | { 21 | _putchar(str[i]); 22 | } 23 | 24 | _putchar('\n'); 25 | } 26 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/7-puts_half.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * puts_half - Prints half of a string 5 | * @str: The string to print 6 | * 7 | * Return: void 8 | */ 9 | void puts_half(char *str) 10 | { 11 | int j = 0; 12 | int k; 13 | 14 | while (str[j] != '\0') 15 | { 16 | j++; 17 | } 18 | 19 | if (j % 2 == 1) 20 | { 21 | k = (j - 1) / 2; 22 | k += 1; 23 | } 24 | else 25 | { 26 | k = j / 2; 27 | } 28 | 29 | for (; k < j; k++) 30 | { 31 | _putchar(str[k]); 32 | } 33 | 34 | _putchar('\n'); 35 | } 36 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/8-print_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_array - Prints n elements of an array of integers 6 | * @a: Array of integers 7 | * @n: Number of elements of the array to be printed 8 | * 9 | * Return: void 10 | */ 11 | void print_array(int *a, int n) 12 | { 13 | int j; 14 | 15 | for (j = 0; j < n; j++) 16 | { 17 | printf("%d", a[j]); 18 | 19 | if (j != (n - 1)) 20 | { 21 | printf(", "); 22 | } 23 | } 24 | 25 | printf("\n"); 26 | } 27 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcpy - Copy a string 5 | * @dest: Destination value 6 | * @src: Source value 7 | * 8 | * Return: the pointer to dest 9 | */ 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i; 13 | 14 | for (i = 0; src[i] != '\0'; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | 19 | dest[i++] = '\0'; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0-reset_to_98.c : function that takes a pointer to an int as parameter and updates the value it points to to 98. 2 | 1-swap.c : function that swaps the values of two integers. 3 | 2-strlen.c : function that returns the length of a string. 4 | 3-puts.c : function that prints a string, followed by a new line, to stdout. 5 | 4-print_rev.c : function that prints a string, in reverse, followed by a new line. 6 | 5-rev_string.c : function that reverses a string. 7 | 6-puts2.c : function that prints every other character of a string, starting with the first character, followed by a new line. 8 | 7-puts_half.c : function that prints half of a string, followed by a new line. 9 | 8-print_array.c : function that prints n elements of an array of integers, followed by a new line. 10 | 9-strcpy.c : function that copies the string pointed to by src, including the terminating null byte (\0), to the buffer pointed to by dest. 11 | 100-atoi.c : function that convert a string to an integer. 12 | 101-keygen.c : program that generates random valid passwords for the program 101-crackme. 13 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | #include 6 | 7 | int _putchar(char c); 8 | void reset_to_98(int *n); 9 | void swap_int(int *a, int *b); 10 | int _strlen(char *s); 11 | void _puts(char *str); 12 | void print_rev(char *s); 13 | void rev_string(char *s); 14 | void puts2(char *str); 15 | void puts_half(char *str); 16 | void print_array(int *a, int n); 17 | char *_strcpy(char *dest, char *src); 18 | int _atoi(char *s); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strcat - function that concatenates two strings. 4 | *@dest: pointer to destination string. 5 | *@src: pointer to source string. 6 | * 7 | *Return: pointer to destination string. 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int length, i; 12 | 13 | length = 0; 14 | while (dest[length] != '\0') 15 | { 16 | length++; 17 | } 18 | for (i = 0; src[i] != '\0'; i++, length++) 19 | { 20 | dest[length] = src[i]; 21 | } 22 | dest[length] = '\0'; 23 | return (dest); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strncat - concatenates n bytes of two strings 4 | * @dest: pointer destination 5 | * @src: pointer source 6 | * @n: number of bytes 7 | * Return: void 8 | */ 9 | 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | 13 | int dest_len, i; 14 | 15 | for (dest_len = 0; dest[dest_len] != '\0'; dest_len++) 16 | ; 17 | 18 | for (i = 0; i < n && src[i] != '\0'; i++) 19 | dest[dest_len + i] = src[i]; 20 | 21 | /*should end with a end of string char*/ 22 | dest[dest_len + i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * rot13 - encodes a string using rot13. 4 | * @str: pointer to the string. 5 | * Return: @str 6 | */ 7 | char *rot13(char *str) 8 | { 9 | int i; 10 | 11 | i = 0; 12 | while (str[i]) 13 | { 14 | while ((str[i] <= 'z' && str[i] >= 'a') || (str[i] <= 'Z' && str[i] >= 'A')) 15 | { 16 | if ((str[i] >= 'n' && str[i] <= 'z') || (str[i] >= 'N' && str[i] <= 'Z')) 17 | { 18 | str[i] -= 13; 19 | break; 20 | } 21 | str[i] += 13; 22 | i++; 23 | } 24 | i++; 25 | } 26 | return (str); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *print_number - prints an integer. 4 | *only using the putchar function. 5 | *noarrays and pointers. 6 | *@n: integer to be printed. 7 | * 8 | *Return: void. 9 | */ 10 | void print_number(int n) 11 | { 12 | int i, j, digit, digits, power; 13 | unsigned int temp, numchar, number; 14 | 15 | digit = 0; 16 | if (n < 0) 17 | { 18 | _putchar('-'); 19 | temp = -n; 20 | } 21 | else 22 | { 23 | temp = n; 24 | } 25 | 26 | number = temp; 27 | while (number >= 10) 28 | { 29 | number = number / 10; 30 | digit++; 31 | } 32 | digits = digit + 1; 33 | power = 1; 34 | i = 1; 35 | 36 | while (i < digits) 37 | { 38 | power = power * 10; 39 | i++; 40 | } 41 | j = power; 42 | while (j >= 1) 43 | { 44 | numchar = (temp / j) % 10; 45 | _putchar(numchar + '0'); 46 | j = j / 10; 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p - 2 + 4) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * infinite_add - adds two numbers 4 | * @n1: first number 5 | * @n2: second number 6 | * @r: buffer for result 7 | * @size_r: buffer size 8 | * 9 | * Return: address of r or 0 10 | */ 11 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 12 | { 13 | int i, j, k, l, m, n; 14 | for (i = 0; n1[i]; i++) 15 | ; 16 | for (j = 0; n2[j]; j++) 17 | ; 18 | if (i > size_r || j > size_r) 19 | return (0); 20 | m = 0; 21 | for (i -= 1, j -= 1, k = 0; k < size_r - 1; i--, j--, k++) 22 | { 23 | n = m; 24 | if (i >= 0) 25 | n += n1[i] - '0'; 26 | if (j >= 0) 27 | n += n2[j] - '0'; 28 | if (i < 0 && j < 0 && n == 0) 29 | { 30 | break; 31 | } 32 | m = n / 10; 33 | r[k] = n % 10 + '0'; 34 | } 35 | r[k] = '\0'; 36 | if (i >= 0 || j >= 0 || m) 37 | return (0); 38 | for (k -= 1, l = 0; l < k; k--, l++) 39 | { 40 | m = r[k]; 41 | r[k] = r[l]; 42 | r[l] = m; 43 | } 44 | return (r); 45 | } 46 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * isPrintableASCII - determines if n is a printable ASCII char 6 | * @n: integer 7 | * Return: 1 if true, 0 if false 8 | */ 9 | int isPrintableASCII(int n) 10 | { 11 | return (n >= 32 && n <= 126); 12 | } 13 | 14 | /** 15 | * printHexes - print hex values for string b in formatted form 16 | * @b: string to print 17 | * @start: starting position 18 | * @end: ending position 19 | */ 20 | void printHexes(char *b, int start, int end) 21 | { 22 | int i = 0; 23 | 24 | while (i < 10) 25 | { 26 | if (i < end) 27 | printf("%02x", *(b + start + i)); 28 | else 29 | printf(" "); 30 | if (i % 2) 31 | printf(" "); 32 | i++; 33 | } 34 | } 35 | 36 | /** 37 | * printASCII - print ascii values for string b, 38 | * formatted to replace nonprintable chars with '.' 39 | * @b: string to print 40 | * @start: starting position 41 | * @end: ending position 42 | */ 43 | void printASCII(char *b, int start, int end) 44 | { 45 | int ch, i = 0; 46 | 47 | while (i < end) 48 | { 49 | ch = *(b + i + start); 50 | if (!isPrintableASCII(ch)) 51 | ch = 46; 52 | printf("%c", ch); 53 | i++; 54 | } 55 | } 56 | 57 | /** 58 | * print_buffer - prints a buffer 59 | * @b: string 60 | * @size: size of buffer 61 | */ 62 | void print_buffer(char *b, int size) 63 | { 64 | int start, end; 65 | 66 | if (size > 0) 67 | { 68 | for (start = 0; start < size; start += 10) 69 | { 70 | end = (size - start < 10) ? size - start : 10; 71 | printf("%08x: ", start); 72 | printHexes(b, start, end); 73 | printASCII(b, start, end); 74 | printf("\n"); 75 | } 76 | } 77 | else 78 | printf("\n"); 79 | } 80 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strncpy - find the length of a string 4 | * @dest: pointer to the string 5 | * @src: pointer 6 | * @n: int 7 | * Return: destination 8 | */ 9 | 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n && src[i] != '\0'; i++) 16 | dest[i] = src[i]; 17 | 18 | for ( ; i < n; i++) 19 | dest[i] = '\0'; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare two strings 5 | * @s1: one string 6 | * @s2: one string 7 | * Return: int that tells num spaces in between 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0; 13 | 14 | while (*(s1 + i) == *(s2 + i) && *(s1 + i)) 15 | i++; 16 | 17 | if (*(s2 + i)) 18 | return (*(s1 + i)-*(s2 + i)); 19 | else 20 | return (0); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * reverse_array - prints reverse array. 4 | * @a: array to be compared. 5 | * @n: size of array. 6 | * Return: reversed array. 7 | */ 8 | void reverse_array(int *a, int n) 9 | { 10 | int swap, begin, end; 11 | 12 | begin = 0; 13 | end = n - 1; 14 | while (begin < end) 15 | { 16 | swap = *(a + begin); 17 | *(a + begin) = *(a + end); 18 | *(a + end) = swap; 19 | begin++; 20 | end--; 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * string_toupper - change lowercase letters to uppercase. 4 | * @s: analized string. 5 | * 6 | * Return: String with all letters Uppercased. 7 | */ 8 | char *string_toupper(char *s) 9 | { 10 | int i = 0; 11 | 12 | while (*(s + i) != '\0') 13 | { 14 | if (*(s + i) >= 'a' && *(s + i) <= 'z') 15 | { 16 | *(s + i) = *(s + i) - 32; 17 | } 18 | i++; 19 | } 20 | return (s); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *cap_string - capitalizes every first letter of a word in a string. 4 | *separators of words are: space, tabulation, 5 | * new line, ,, ;, ., !, ?, ", (, ), {, and }. 6 | *@s: pointer to string. 7 | * 8 | *Return: pointer to s. 9 | */ 10 | char *cap_string(char *s) 11 | { 12 | int i, j; 13 | int a[] = {32, 9, 10, 44, 59, 46, 33, 63, 34, 40, 41, 123, 125}; 14 | 15 | i = 0; 16 | while (*(s + i) != '\0') 17 | { 18 | if (*(s + i) >= 'a' && *(s + i) <= 'z') 19 | { 20 | if (i == 0) 21 | { 22 | *(s + i) = *(s + i)-32; 23 | } 24 | else 25 | { 26 | for (j = 0; j <= 12; j++) 27 | { 28 | if (a[j] == *(s + i - 1)) 29 | { 30 | *(s + i) = *(s + i)-32; 31 | } 32 | } 33 | } 34 | } 35 | i++; 36 | } 37 | return (s); 38 | } 39 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *leet - encodes a string into 1337. 4 | *letters a and A are replaced by 4. 5 | *Letters e and E are replaced by 3 6 | *Letters o and O are replaced by 0 7 | *Letters t and T are replaced by 7 8 | *Letters l and L are replaced by 1 9 | *@s: pointer to string. 10 | * 11 | *Return: pointer to s. 12 | */ 13 | char *leet(char *s) 14 | { 15 | char a[] = "aeotlAEOTL"; 16 | char n[] = "4307143071"; 17 | int i = 0; 18 | int j; 19 | while (*(s + i) != '\0') 20 | { 21 | for (j = 0; j <= 9; j++) 22 | { 23 | if (*(s + i) == a[j]) 24 | { 25 | *(s + i) = n[j]; 26 | } 27 | } 28 | i++; 29 | } 30 | return (s); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | 0x06-pointers_arrays_strings 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | int _putchar(char c); 5 | char *_strcat(char *dest, char *src); 6 | char *_strncat(char *dest, char *src, int n); 7 | char *_strncpy(char *dest, char *src, int n); 8 | int _strcmp(char *s1, char *s2); 9 | void reverse_array(int *a, int n); 10 | char *string_toupper(char *); 11 | char *cap_string(char *); 12 | char *leet(char *); 13 | char *rot13(char *); 14 | void print_number(int n); 15 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 16 | void print_buffer(char *b, int size); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | for (i = 0; i < n; i++) 13 | *(dest + i) = *(src + i); 14 | 15 | return (dest); 16 | } 17 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - sets the value of a pointer to a char 5 | * @s: source adress. 6 | * @t: target adress. 7 | * Return: no return. 8 | */ 9 | void set_string(char **s, char *t) 10 | { 11 | *s = t; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s1: initial segment. 6 | * @s2: s2ed bytes. 7 | * Return: the number of s2ed bytes. 8 | */ 9 | unsigned int _strspn(char *s1, char *s2) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s1 + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(s2 + j) != '\0'; j++) 17 | { 18 | if (*(s1 + i) == *(s2 + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @s2: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in s2, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *s2) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(s2 + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(s2 + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * str2 in the string str1. 6 | * @str1: entire string. 7 | * @str2: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *str1, char *str2) 12 | { 13 | char *bstr1; 14 | char *pstr2; 15 | 16 | while (*str1 != '\0') 17 | { 18 | bstr1 = str1; 19 | pstr2 = str2; 20 | 21 | while (*str1 != '\0' && *pstr2 != '\0' && *str1 == *pstr2) 22 | { 23 | str1++; 24 | pstr2++; 25 | } 26 | if (!*pstr2) 27 | return (bstr1); 28 | str1 = bstr1 + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - prints the chessboard 5 | * @a: input pointer. 6 | * Return: no return. 7 | */ 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | unsigned int i, m = 0; 11 | 12 | for (i = 0; i < 64; i++) 13 | { 14 | if (i % 8 == 0 && i != 0) 15 | { 16 | m = i; 17 | _putchar('\n'); 18 | } 19 | _putchar(a[i / 8][i - m]); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_diagsums - prints the sum of the two 5 | * diagonals of a square matrix of integers 6 | * @a: input pointer. 7 | * @size: size of the matrix 8 | * Return: no return. 9 | */ 10 | void print_diagsums(int *a, int size) 11 | { 12 | int i, sum1 = 0, sum2 = 0; 13 | 14 | for (i = 0; i < (size * size); i++) 15 | { 16 | if (i % (size + 1) == 0) 17 | sum1 += *(a + i); 18 | if (i % (size - 1) == 0 && i != 0 && i < size * size - 1) 19 | sum2 += *(a + i); 20 | } 21 | printf("%d, %d\n", sum1, sum2); 22 | } 23 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | File 0-memset.c is a function that fills memory with a constant byte. 2 | 3 | File 1-memcpy.c is a function that copies memory area. 4 | 5 | File 2-strchr.c is a function that locates a character in a string. 6 | 7 | File 3-strspn.c is a function that gets the length of a prefix substring. 8 | 9 | File 4-strpbrk.c is a function that searches a string for any of a set of bytes. 10 | 11 | File 5-strstr.c is a function that locates a substring. 12 | 13 | File 7-print_chessboard.c is a function that prints the chessboard. 14 | 15 | File 8-print_diagsums.c is a function that prints the sum of the two diagonals of a square matrix of integers. 16 | 17 | File 9-set_string.c is a function that sets the value of a pointer to a char. 18 | 19 | File holberton.h is the header file with the functions prototypes. 20 | 21 | File _putchar.c is the file that contains the _putchar function. 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _HOLBERTON_H_ 2 | #define _HOLBERTON_H_ 3 | 4 | char *_memset(char *s, char b, unsigned int n); 5 | char *_memcpy(char *dest, char *src, unsigned int n); 6 | char *_strchr(char *s, char c); 7 | unsigned int _strspn(char *s, char *s2); 8 | char *_strpbrk(char *s1, char *s2); 9 | char *_strstr(char *str1, char *str2); 10 | int _putchar(char); 11 | void print_chessboard(char (*a)[8]); 12 | void print_diagsums(int *a, int size); 13 | void set_string(char **s, char *t); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - prints a string, followed by a new line. 5 | * @str: string 6 | * Return: no return. 7 | */ 8 | void _puts_recursion(char *str) 9 | { 10 | if (*str != '\0') 11 | { 12 | _putchar(*str); 13 | _puts_recursion(str + 1); 14 | } 15 | else 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/1-print_rev_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _print_rev_recursion - prints a string in reverse. 5 | * @s: string 6 | * Return: no return. 7 | */ 8 | void _print_rev_recursion(char *s) 9 | { 10 | if (*s != '\0') 11 | { 12 | _print_rev_recursion(s + 1); 13 | _putchar(*s); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | 16 | /** 17 | * comparator - compares each character of the string. 18 | * @s: string 19 | * @n1: smallest iterator. 20 | * @n2: biggest iterator. 21 | * Return: . 22 | */ 23 | int comparator(char *s, int n1, int n2) 24 | { 25 | if (*(s + n1) == *(s + n2)) 26 | { 27 | if (n1 == n2 || n1 == n2 + 1) 28 | return (1); 29 | return (0 + comparator(s, n1 + 1, n2 - 1)); 30 | } 31 | return (0); 32 | } 33 | 34 | /** 35 | * is_palindrome - detects if a string is a palindrome. 36 | * @s: string. 37 | * Return: 1 if s is a palindrome, 0 if not. 38 | */ 39 | int is_palindrome(char *s) 40 | { 41 | if (*s == '\0') 42 | return (1); 43 | return (comparator(s, 0, _strlen_recursion(s) - 1)); 44 | } 45 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two strings. 5 | * @s1: string 1. 6 | * @s2: string 2. It can contains a * as a special character. 7 | * Return: 1 if are identical, 0 if not. 8 | */ 9 | int wildcmp(char *s1, char *s2) 10 | { 11 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 12 | return (0); 13 | if (*s1 == '\0' && *s2 == '\0') 14 | return (1); 15 | if (*s1 == *s2) 16 | return (wildcmp(s1 + 1, s2 + 1)); 17 | if (*s2 == '*') 18 | return (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen_recursion - returns the length of a string. 5 | * @s: string 6 | * Return: the length of a string. 7 | */ 8 | int _strlen_recursion(char *s) 9 | { 10 | if (*s == '\0') 11 | return (0); 12 | else 13 | return (1 + _strlen_recursion(s + 1)); 14 | } 15 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - returns the factorial of a given number. 5 | * @n: input number. 6 | * Return: factorial of the number. 7 | */ 8 | int factorial(int n) 9 | { 10 | if (n < 0) 11 | return (-1); 12 | else if (n == 0) 13 | return (1); 14 | else 15 | return (n * factorial(n - 1)); 16 | } 17 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - returns the value of x raised to the power of y. 5 | * @i: base. 6 | * @j: exponent. 7 | * Return: value of the exponentiation. 8 | */ 9 | int _pow_recursion(int i, int j) 10 | { 11 | if (j < 0) 12 | return (-1); 13 | else if (j == 0) 14 | return (1); 15 | else 16 | return (i * _pow_recursion(i, j - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * power_operation - returns the natural square root of a number. 5 | * @n: input number. 6 | * @c: iterator. 7 | * Return: square root or -1. 8 | */ 9 | int power_operation(int n, int c) 10 | { 11 | if (c % (n / c) == 0) 12 | { 13 | if (c * (n / c) == n) 14 | return (c); 15 | else 16 | return (-1); 17 | } 18 | return (0 + power_operation(n, c + 1)); 19 | } 20 | /** 21 | * _sqrt_recursion - returns the natural square root of a number. 22 | * @n: input number. 23 | * Return: natural square root. 24 | */ 25 | int _sqrt_recursion(int n) 26 | { 27 | if (n < 0) 28 | return (-1); 29 | if (n == 0) 30 | return (0); 31 | if (n == 1) 32 | return (1); 33 | return (power_operation(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * is_prime - detects if an input number is a prime number. 5 | * @a: input number. 6 | * @b: iterator. 7 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 8 | */ 9 | int is_prime(unsigned int a, unsigned int b) 10 | { 11 | if (a % b == 0) 12 | { 13 | if (a == b) 14 | return (1); 15 | else 16 | return (0); 17 | } 18 | return (0 + is_prime(a, b + 1)); 19 | } 20 | /** 21 | * is_prime_number - detects if an input number is a prime number. 22 | * @n: input number. 23 | * Return: 1 if n is a prime number. 0 if n is not a prime number. 24 | */ 25 | int is_prime_number(int n) 26 | { 27 | if (n == 0) 28 | return (0); 29 | if (n < 0) 30 | return (0); 31 | if (n == 1) 32 | return (0); 33 | return (is_prime(n, 2)); 34 | } 35 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | *File 0-puts_recursion.c* is a function that prints a string, followed by a new line. 2 | 3 | *File 1-print_rev_recursion.c* is a function that prints a string in reverse. 4 | 5 | *File 2-strlen_recursion.c* is a function that returns the length of a string. 6 | 7 | *File 3-factorial.c *is a function that returns the factorial of a given number. 8 | 9 | *File 4-pow_recursion.c *is a function that returns the value of x raised to the power of y. 10 | 11 | *File 5-sqrt_recursion.c* is a function that returns the natural square root of a number. 12 | 13 | *File 6-is_prime_number.c *is a function that returns 1 if the input integer is a prime number, otherwise return 0. 14 | 15 | *File 7-is_palindrome.c * is a function that returns 1 if a string is a palindrome and 0 if not. 16 | 17 | *File 100-wildcmp.c *is a function that compares two strings and returns 1 if the strings can be considered identical, otherwise return 0. It uses the concept of wildcards. 18 | 19 | *File _putchar.c *is a file containing the _putchar function. 20 | 21 | *File main.h* is the header file containing the function prototypes 22 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _putchar(char); 5 | void _puts_recursion(char *str); 6 | void _print_rev_recursion(char *s); 7 | int _strlen_recursion(char *s); 8 | int factorial(int n); 9 | int _pow_recursion(int i, int j); 10 | int _sqrt_recursion(int n); 11 | int is_prime_number(int n); 12 | int is_palindrome(char *s); 13 | int wildcmp(char *s1, char *s2); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | 0x09-static_libraries 2 | -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c *.c 3 | ar crs liball.a *.o 4 | -------------------------------------------------------------------------------- /0x09-static_libraries/liball.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x09-static_libraries/liball.a -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main_H 2 | #define main_H 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Prints the name of the program 5 | * @argc: Number of command line arguments 6 | * @argv: Array name 7 | * Return: 0 8 | */ 9 | int main(int argc, char *argv[]) 10 | { 11 | printf("%s\n", argv[argc * 0]); 12 | return (0); 13 | } 14 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints the number of arguments passed into main 4 | * @argc: Number of command line arguments 5 | * @argv: Array name 6 | * Return: 0 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | int i; 11 | 12 | if (argc == 1) 13 | printf("%d\n", argc - 1); 14 | else 15 | { 16 | for (i = 0; *argv; i++, argv++) 17 | ; 18 | 19 | printf("%d\n", i - 1); 20 | } 21 | 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /** 4 | * main - prints the minimum number of coins to make change for an amount. 5 | * of money. 6 | * @argc: number of command line arguments. 7 | * @argv: array that contains the program command line arguments. 8 | * Return: 0 - success. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int cents, ncoins = 0; 13 | 14 | if (argc == 1 || argc > 2) 15 | { 16 | printf("Error\n"); 17 | return (1); 18 | } 19 | 20 | cents = atoi(argv[1]); 21 | 22 | while (cents > 0) 23 | { 24 | if (cents >= 25) 25 | cents -= 25; 26 | else if (cents >= 10) 27 | cents -= 10; 28 | else if (cents >= 5) 29 | cents -= 5; 30 | else if (cents >= 2) 31 | cents -= 2; 32 | else if (cents >= 1) 33 | cents -= 1; 34 | ncoins += 1; 35 | } 36 | printf("%d\n", ncoins); 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - Prints the number of arguments passed into main 4 | * @argc: Number of command line arguments 5 | * @argv: Array name 6 | * Return: 0 7 | */ 8 | int main(int argc, char *argv[]) 9 | { 10 | int i; 11 | for (i = 0; i < argc; i++) 12 | { 13 | printf("%s\n", argv[i]); 14 | } 15 | return (0); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "main.h" 4 | 5 | /** 6 | * main - Program that takes first two integer arguments and prints the product 7 | * @argc: Number of command line arguments 8 | * @argv: Array name 9 | * Return: 1 if not enough arguments passed in, 0 otherwise 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | int i, j; 14 | if (argc != 0 && argc != 3) 15 | { 16 | printf("Error\n"); 17 | return (1); 18 | } 19 | else 20 | { 21 | i = atoi(argv[1]); 22 | j = atoi(argv[2]); 23 | printf("%d\n", i * j); 24 | return (0); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | #include 4 | #include 5 | #include 6 | 7 | /** 8 | * main - adds positive numbers 9 | * @argc: Number of command line arguments 10 | * @argv: Array name 11 | * Return: 0 12 | */ 13 | int main(int argc, char *argv[]) 14 | { 15 | int i, j; 16 | int sum; 17 | if (argc == 1) 18 | { 19 | printf("0\n"); 20 | return (0); 21 | } 22 | for (i = 1; i < argc; i++) 23 | { 24 | int arg_len = strlen(argv[i]); 25 | for (j = 0; j < arg_len; j++) 26 | { 27 | if (!isdigit(argv[i][j])) 28 | { 29 | printf("Error\n"); 30 | return (1); 31 | } 32 | else if (isdigit(argv[i][j]) < 0) 33 | { 34 | printf("Error\n"); 35 | return (1); 36 | } 37 | } 38 | } 39 | for (i = 0; i < argc; i++) 40 | { 41 | sum += atoi(argv[i]); 42 | } 43 | printf("%d\n", sum); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | 0x0A-argc_argv 2 | -------------------------------------------------------------------------------- /0x0A-argc_argv/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x0A-argc_argv/add: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x0A-argc_argv/add -------------------------------------------------------------------------------- /0x0A-argc_argv/args: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x0A-argc_argv/args -------------------------------------------------------------------------------- /0x0A-argc_argv/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int _putchar(char c); 5 | int _atoi(char *s); 6 | int _isdigit(int c); 7 | int _isalpha(int c); 8 | int _strlen(char *s); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0A-argc_argv/mul: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x0A-argc_argv/mul -------------------------------------------------------------------------------- /0x0A-argc_argv/mynewnameis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x0A-argc_argv/mynewnameis -------------------------------------------------------------------------------- /0x0A-argc_argv/nargs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x0A-argc_argv/nargs -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * create_array - creates an array of chars. 5 | * @size: size of the array. 6 | * @c: storaged char 7 | * 8 | * Return: pointer of an array of chars 9 | */ 10 | char *create_array(unsigned int size, char c) 11 | { 12 | char *char1; 13 | unsigned int i; 14 | 15 | if (size == 0) 16 | return (NULL); 17 | 18 | char1 = malloc(sizeof(c) * size); 19 | 20 | if (char1 == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | char1[i] = c; 25 | 26 | return (char1); 27 | } 28 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strdup - returns a pointer to a newly allocated space in memory. 5 | * @str: string. 6 | * 7 | * Return: pointer of an array of chars 8 | */ 9 | char *_strdup(char *str) 10 | { 11 | char *strout; 12 | unsigned int i, j; 13 | 14 | if (str == NULL) 15 | return (NULL); 16 | 17 | for (i = 0; str[i] != '\0'; i++) 18 | ; 19 | 20 | strout = (char *)malloc(sizeof(char) * (i + 1)); 21 | 22 | if (strout == NULL) 23 | return (NULL); 24 | 25 | for (j = 0; j <= i; j++) 26 | strout[j] = str[j]; 27 | 28 | return (strout); 29 | } 30 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments of a program. 6 | * @ac: argument count. 7 | * @av: argument vector. 8 | * 9 | * Return: pointer of an array of char 10 | */ 11 | char *argstostr(int ac, char **av) 12 | { 13 | char *aout; 14 | int c, i, j, ia; 15 | 16 | if (ac == 0) 17 | return (NULL); 18 | 19 | for (c = i = 0; i < ac; i++) 20 | { 21 | if (av[i] == NULL) 22 | return (NULL); 23 | 24 | for (j = 0; av[i][j] != '\0'; j++) 25 | c++; 26 | c++; 27 | } 28 | 29 | aout = malloc((c + 1) * sizeof(char)); 30 | 31 | if (aout == NULL) 32 | { 33 | free(aout); 34 | return (NULL); 35 | } 36 | 37 | for (i = j = ia = 0; ia < c; j++, ia++) 38 | { 39 | if (av[i][j] == '\0') 40 | { 41 | aout[ia] = '\n'; 42 | i++; 43 | ia++; 44 | j = 0; 45 | } 46 | if (ia < c - 1) 47 | aout[ia] = av[i][j]; 48 | } 49 | aout[ia] = '\0'; 50 | 51 | return (aout); 52 | } 53 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * count_word - helper function to count the number of words in a string 6 | * @s: string to evaluate 7 | * 8 | * Return: number of words 9 | */ 10 | int count_word(char *s) 11 | { 12 | int flag, c, i; 13 | 14 | flag = 0; 15 | i = 0; 16 | 17 | for (c = 0; s[c] != '\0'; c++) 18 | { 19 | if (s[c] == ' ') 20 | flag = 0; 21 | else if (flag == 0) 22 | { 23 | flag = 1; 24 | i++; 25 | } 26 | } 27 | 28 | return (i); 29 | } 30 | /** 31 | * **strtow - splits a string into words 32 | * @str: string to split 33 | * 34 | * Return: pointer to an array of strings (Success) 35 | * or NULL (Error) 36 | */ 37 | char **strtow(char *str) 38 | { 39 | char **matrix, *tmp; 40 | int i, k = 0, len = 0, words, c = 0, start, end; 41 | 42 | while (*(str + len)) 43 | len++; 44 | words = count_word(str); 45 | if (words == 0) 46 | return (NULL); 47 | 48 | matrix = (char **) malloc(sizeof(char *) * (words + 1)); 49 | if (matrix == NULL) 50 | return (NULL); 51 | 52 | for (i = 0; i <= len; i++) 53 | { 54 | if (str[i] == ' ' || str[i] == '\0') 55 | { 56 | if (c) 57 | { 58 | end = i; 59 | tmp = (char *) malloc(sizeof(char) * (c + 1)); 60 | if (tmp == NULL) 61 | return (NULL); 62 | while (start < end) 63 | *tmp++ = str[start++]; 64 | *tmp = '\0'; 65 | matrix[k] = tmp - c; 66 | k++; 67 | c = 0; 68 | } 69 | } 70 | else if (c++ == 0) 71 | start = i; 72 | } 73 | 74 | matrix[k] = NULL; 75 | 76 | return (matrix); 77 | } 78 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * str_concat - concatenates two strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * 9 | * Return: pointer of an array of chars 10 | */ 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *strout; 14 | unsigned int i, j, k, limit; 15 | 16 | if (s1 == NULL) 17 | s1 = ""; 18 | if (s2 == NULL) 19 | s2 = ""; 20 | 21 | for (i = 0; s1[i] != '\0'; i++) 22 | ; 23 | 24 | for (j = 0; s2[j] != '\0'; j++) 25 | ; 26 | 27 | strout = malloc(sizeof(char) * (i + j + 1)); 28 | 29 | if (strout == NULL) 30 | { 31 | free(strout); 32 | return (NULL); 33 | } 34 | 35 | for (k = 0; k < i; k++) 36 | strout[k] = s1[k]; 37 | 38 | limit = j; 39 | for (j = 0; j <= limit; k++, j++) 40 | strout[k] = s2[j]; 41 | 42 | return (strout); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * alloc_grid - returns a pointer to a 2 dimensional array of integers. 6 | * @width: width of the array. 7 | * @height: height of the array. 8 | * 9 | * Return: pointer of an array of integers 10 | */ 11 | int **alloc_grid(int width, int height) 12 | { 13 | int **grid; 14 | int i, j; 15 | 16 | if (width < 1 || height < 1) 17 | return (NULL); 18 | 19 | grid = malloc(height * sizeof(int *)); 20 | if (grid == NULL) 21 | { 22 | free(grid); 23 | return (NULL); 24 | } 25 | 26 | for (i = 0; i < height; i++) 27 | { 28 | grid[i] = malloc(width * sizeof(int)); 29 | if (grid[i] == NULL) 30 | { 31 | for (i--; i >= 0; i--) 32 | free(grid[i]); 33 | free(grid); 34 | return (NULL); 35 | } 36 | } 37 | 38 | for (i = 0; i < height; i++) 39 | for (j = 0; j < width; j++) 40 | grid[i][j] = 0; 41 | 42 | return (grid); 43 | } 44 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * free_grid - frees a 2 dimensional grid. 6 | * @grid: multidimensional array of integers. 7 | * @height: height of the grid. 8 | * 9 | * Return: no return 10 | */ 11 | void free_grid(int **grid, int height) 12 | { 13 | if (grid != NULL && height != 0) 14 | { 15 | for (; height >= 0; height--) 16 | free(grid[height]); 17 | free(grid); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | char *create_array(unsigned int size, char c); 5 | char *_strdup(char *str); 6 | char *str_concat(char *s1, char *s2); 7 | int **alloc_grid(int width, int height); 8 | void free_grid(int **grid, int height); 9 | char *argstostr(int ac, char **av); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * malloc_checked - allocates memory using malloc 4 | * @b: size passed in bytes 5 | * Return: nothing 6 | */ 7 | void *malloc_checked(unsigned int b) 8 | { 9 | void *p = malloc(b); 10 | 11 | if (p == NULL) 12 | exit(98); 13 | 14 | return (p); 15 | } 16 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * string_nconcat - function that concatenates two strings 4 | * @s1: string 1 5 | * @s2: string 2 6 | * @n: bytes of @s2 to add to @s1 to be a new string 7 | * Return: new string followed by the first @n bytes 8 | * of string 2 @s2 or NULL 9 | */ 10 | char *string_nconcat(char *s1, char *s2, unsigned int n) 11 | { 12 | unsigned int k, m, i, j; 13 | char *str; 14 | 15 | /*treat NULL as empty string*/ 16 | if (s1 == NULL) 17 | s1 = ""; 18 | if (s2 == NULL) 19 | s2 = ""; 20 | 21 | k = m = 0; 22 | /*length of both s1 & s2 respectively*/ 23 | while (s1[k] != '\0') 24 | k++; 25 | while (s2[m] != '\0') 26 | m++; 27 | /*set n to length of s2*/ 28 | if (n >= m) 29 | n = m; 30 | 31 | str = (char *) malloc((k + n + 1) * sizeof(char)); 32 | if (str == NULL) 33 | return (NULL); 34 | 35 | /*add s1 to str*/ 36 | for (i = 0; s1[i] != '\0'; i++) 37 | str[i] = s1[i]; 38 | /*add s2 n bytes to str*/ 39 | for (j = 0; j < n && s2[j] != '\0'; j++) 40 | { 41 | str[i] = s2[j]; 42 | i++; 43 | } 44 | str[i] = '\0'; 45 | return (str); 46 | } 47 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * _realloc - reallocate a memory block using malloc and free 4 | * 5 | * @ptr: pointer to previous memory 6 | * @old_size: old memory array size 7 | * @new_size: new memory array size 8 | * Return: pointer to new memory 9 | * NULL if it fails 10 | */ 11 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 12 | { 13 | char *new_ptr, *temp_ptr; 14 | unsigned int i; 15 | 16 | if (new_size == old_size) 17 | return (ptr); 18 | 19 | if (ptr == NULL) 20 | { 21 | new_ptr = malloc(new_size); 22 | if (new_ptr == NULL) 23 | return (NULL); 24 | free(ptr); 25 | return (new_ptr); 26 | } 27 | 28 | if (new_size == 0 && ptr != NULL) 29 | { 30 | free(ptr); 31 | return (NULL); 32 | } 33 | 34 | new_ptr = malloc(new_size); 35 | if (new_ptr == NULL) 36 | return (NULL); 37 | 38 | temp_ptr = ptr; 39 | 40 | for (i = 0; i < old_size; i++) 41 | new_ptr[i] = temp_ptr[i]; 42 | 43 | free(ptr); 44 | return (new_ptr); 45 | } 46 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * *_memset - fills memory with a constant byte 5 | * @s: memory area to be filled 6 | * @b: char to copy 7 | * @n: number of times to copy b 8 | * Return: pointer to the memory area s 9 | */ 10 | char *_memset(char *s, char b, unsigned int n) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | s[i] = b; 17 | } 18 | return (s); 19 | } 20 | 21 | /** 22 | * *_calloc - allocates memory for an array 23 | * @i: number of elements in the array 24 | * @size: size of each element 25 | * 26 | * Return: pointer to allocated memory 27 | */ 28 | void *_calloc(unsigned int i, unsigned int size) 29 | { 30 | char *ptr; 31 | 32 | if (i == 0 || size == 0) 33 | return (NULL); 34 | 35 | ptr = malloc(size * i); 36 | 37 | if (ptr == NULL) 38 | return (NULL); 39 | 40 | _memset(ptr, 0, i * size); 41 | 42 | return (ptr); 43 | } 44 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | /** 4 | * *array_range - creates an array of integers 5 | * @min: minimum range of values stored 6 | * @max: maximum range of values stored and number of elements 7 | * Return: pointer to the new array 8 | */ 9 | int *array_range(int min, int max) 10 | { 11 | int *ptr; 12 | int i, size; 13 | 14 | if (min > max) 15 | return (NULL); 16 | 17 | size = max - min + 1; 18 | 19 | ptr = malloc(sizeof(int) * size); 20 | 21 | if (ptr == NULL) 22 | return (NULL); 23 | 24 | for (i = 0; min <= max; i++) 25 | ptr[i] = min++; 26 | 27 | return (ptr); 28 | } 29 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | * **0.** 2 | * [0-malloc_checked.c](./0-malloc_checked.c): C function that returns a 3 | pointer to a newly-allocated space in memory using `malloc`. 4 | * If `malloc` fails, the function causes normal process termination with a status value 5 | of `98`. 6 | 7 | * **1.** 8 | * [1-string_nconcat.c](./1-string_nconcat.c): C function that returns a pointer to a 9 | newly-allocated space in memory containing the concatenation of two strings. 10 | * The returned pointer contains `s1` followed by the first `n` bytes 11 | of `s2`, null-terminated. 12 | * If `n` is greater than or equal to the length of `s2`, the entire string `s2` is used. 13 | * If `NULL` is passed, the function treats the parameter as an empty string. 14 | * If the function fails - returns `NULL`. 15 | 16 | * **2.** 17 | * [2-calloc.c](./2-calloc.c): C function that returns a pointer to a newly-allocated space 18 | in memory for an array, using `malloc`. 19 | * Allocates memory for an array of `nmemb` elements of `size` bytes each. 20 | * The memory is set to zero. 21 | * If `nmemb` = 0, `size` = `0`, or the function fail - returns `NULL`. 22 | 23 | * **3,** 24 | * [3-array_range.c](./3-array_range.c): C function that returns a pointer to a 25 | newly-allocated space in memory containing an array of integers. 26 | * The array contains all the values from parameters `min` to `max`, inclusive, 27 | ordered from `min` to `max`. 28 | * If `min > max` or the function fails - returns `NULL`. 29 | 30 | * **4.** 31 | * [100-realloc.c](./100-realloc.c): C function that reallocates a memory block using 32 | `malloc` and `free`. 33 | * The parameter `ptr` is a pointer to the memory previously allocated with 34 | a call to `malloc: malloc(old_size)`. 35 | * The paramter `old_size` is the size, in bytes, of the allocated space for `ptr`. 36 | * The paramter `new_size` is the new size, in bytes, of the new memory block. 37 | * The contens of `ptr` are copied to the newly-allocated space in the range from the 38 | start of `ptr` up to the minimum of `old_size` and `new_size`. 39 | * If `new_size` > `old_size`, the "added" memory is not initialized. 40 | * If `new_size` == `old_size`, the function returns `ptr`. 41 | * If `ptr` is `NULL`, the call is equivalent to `malloc(new_size)` for all values of 42 | `old_size` and `new_size`. 43 | * If `new_size` = 0 and `ptr` is not `NULL`, the call is equivalent to 44 | `free(ptr)` and the function returns `NULL`. 45 | 46 | 47 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 5 | int *array_range(int min, int max); 6 | void *_calloc(unsigned int i, unsigned int size); 7 | char *string_nconcat(char *s1, char *s2, unsigned int n); 8 | void *malloc_checked(unsigned int b); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _OBJECT_LIKE_MACRO_ 2 | #define _OBJECT_LIKE_MACRO_ 3 | 4 | #define SIZE 1024 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef _PI_ 2 | #define _PI_ 3 | 4 | #define PI 3.14159265359 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints the name of the file. 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("%s\n", __FILE__); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x0D-preprocessor/3-function_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_LIKE_MACRO_ 2 | #define _FUNCTION_LIKE_MACRO_ 3 | 4 | #define ABS(x) ((x) < (0) ? ((x) * (-1)) : (x)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUM_LIKE_MACRO_ 2 | #define _SUM_LIKE_MACRO_ 3 | 4 | #define SUM(x, y) ((x) + (y)) 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # C - Preprocessor 2 | 3 | ## General objectives: 4 | What are macros and how to use them 5 | What are the most common predefined macros 6 | How to include guard your header files 7 | 8 | ## Tests : 9 | 10 | * [tests](./tests): Folder of test files. Provided by Holberton School. 11 | 12 | ## Tasks : 13 | 14 | * **0.** 15 | * [0-object_like_macro.h](./0-object_like_macro.h): Header file that defines a 16 | macro named `SIZE` as an abbreviation for the token `1024`. 17 | 18 | * **1.** 19 | * [1-pi.h](./1-pi.h): Header file that defines a macro named `PI` as an abbreviation 20 | for the token `3.14159265359`. 21 | 22 | * **2.** 23 | * [2-main.c](./2-main.c): C program that prints the name of the file it was 24 | compiled from followed by a new line. 25 | 26 | * **3.** 27 | * [3-function_like_macro.h](./3-function_like_macro.h): Header file that defines a 28 | function-like macro `ABS(x)` that computes the absolute value of a number `x`. 29 | 30 | * **4.** 31 | * [4-sum.h](./4-sum.h): Header file that defines a function-like macro `SUM(x, y)` 32 | that computes the sum of the numbers `x` and `y`. 33 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/ 2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | print_dog(&my_dog); 17 | return (0); 18 | } -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | 3 | /** 4 | * init_dog - initializes a variable of type struct dog. 5 | * @d: struct dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: no return. 11 | */ 12 | void init_dog(struct dog *d, char *name, float age, char *owner) 13 | { 14 | if (d) 15 | { 16 | d->name = name; 17 | d->age = age; 18 | d->owner = owner; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | struct dog my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | print_dog(&my_dog); 17 | return (0); 18 | } -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * print_dog - prints a struct dog. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (d) 13 | { 14 | if (!(d->name)) 15 | printf("Name: (nil)\n"); 16 | else 17 | printf("Name: %s\n", d->name); 18 | 19 | printf("Age: %f\n", d->age); 20 | 21 | if (!(d->owner)) 22 | printf("Owner: (nil)\n"); 23 | else 24 | printf("Owner: %s\n", d->owner); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dog.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | dog_t my_dog; 12 | 13 | my_dog.name = "Poppy"; 14 | my_dog.age = 3.5; 15 | my_dog.owner = "Bob"; 16 | printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); 17 | return (0); 18 | } -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * new_dog - creates a new dog. 6 | * @name: name of the dog. 7 | * @age: age of the dog. 8 | * @owner: owner of the dog. 9 | * 10 | * Return: struct dog. 11 | * if fails, returns NULL. 12 | */ 13 | dog_t *new_dog(char *name, float age, char *owner) 14 | { 15 | dog_t *p_dog; 16 | int i, lname, lowner; 17 | 18 | p_dog = malloc(sizeof(*p_dog)); 19 | if (p_dog == NULL || !(name) || !(owner)) 20 | { 21 | free(p_dog); 22 | return (NULL); 23 | } 24 | 25 | for (lname = 0; name[lname]; lname++) 26 | ; 27 | 28 | for (lowner = 0; owner[lowner]; lowner++) 29 | ; 30 | 31 | p_dog->name = malloc(lname + 1); 32 | p_dog->owner = malloc(lowner + 1); 33 | 34 | if (!(p_dog->name) || !(p_dog->owner)) 35 | { 36 | free(p_dog->owner); 37 | free(p_dog->name); 38 | free(p_dog); 39 | return (NULL); 40 | } 41 | 42 | for (i = 0; i < lname; i++) 43 | p_dog->name[i] = name[i]; 44 | p_dog->name[i] = '\0'; 45 | 46 | p_dog->age = age; 47 | 48 | for (i = 0; i < lowner; i++) 49 | p_dog->owner[i] = owner[i]; 50 | p_dog->owner[i] = '\0'; 51 | 52 | return (p_dog); 53 | } 54 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | 4 | /** 5 | * free_dog - frees dogs. 6 | * @d: struct dog. 7 | * 8 | * Return: no return. 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d) 13 | { 14 | free(d->name); 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # C - Structures, typedef 2 | 3 | In this project, I learned about using structures and `typedef` in C. 4 | 5 | ## Header File :file_folder: 6 | 7 | * [dog.h](./dog.h): Header file containing definitions and prototypes for all types 8 | and functions written in the project. 9 | 10 | | Type/File | Defintion/Prototype | 11 | | --------------- | ------------------------------------------------------------------------ | 12 | | `struct dog` |
  • `char *name`
  • `float age`
  • `char *owner`
| 13 | | `typedef dog_t` | `struct dog` | 14 | | `1-init_dog.c` | `void init_dog(struct dog *d, char *name, float age, char *owner);` | 15 | | `2-print_dog.c` | `void print_dog(struct dog *d);` | 16 | | `4-new_dog.c` | `dog_t *new_dog(char *name, float age, char *owner);` | 17 | | `5-free_dog.c` | `void free_dog(dog_t *d);` | 18 | 19 | ## Tasks :page_with_curl: 20 | 21 | * **0. Django** 22 | * [dog.h](./dog.h): Header file that defines a new type `struct dog` with the 23 | following elements: 24 | * `char *name` 25 | * `float age` 26 | * `char *owner` 27 | 28 | * **1. A dog is the only thing on earth that loves you more than you love yourself** 29 | * [1-init_dog.c](./1-init_dog.c): C function that initializes a variable of type `struct dog`. 30 | 31 | * **2. A dog will teach you unconditional love. If you can have that in your life, things won't be too bad** 32 | * [2-print_dog.c](./2-print_dog.c): C function that prints a `struct dog`. 33 | * If an element of `d` is `NULL`, the function prints `(nil)` instead of the element. 34 | * If `d` is `NULL`, the function prints nothing. 35 | 36 | * **3. Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to read** 37 | * [dog.h](./dog.h): Header file that defines a new type `dog_t` as a new name for the 38 | type `struct dog`. 39 | 40 | * **4. A door is what a dog is perpetually on the wrong side of** 41 | * [4-new_dog.c](./4-new_dog.c): C function that creates a dog. 42 | * Returns `NULL` if the function fails. 43 | 44 | * **5. How many legs does a dog have if you call his tail a leg? Four. Saying that a tail is a leg doesn't make it a leg** 45 | * [5-free_dog.c](./5-free_dog.c): C function that frees dogs. 46 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x0E-structures_typedef/c -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOG_H_ 2 | #define _DOG_H_ 3 | 4 | /** 5 | * dog_t - Typedef for struct dog 6 | */ 7 | typedef struct dog dog_t; 8 | 9 | /** 10 | * struct dog - struct that stores some information of a dog 11 | * @name: name of the dog 12 | * @age: age of the dog 13 | * @owner: owner of the dog 14 | * 15 | * Description: struct called "dog" that stores its name, its age 16 | * and the name of its owner. 17 | */ 18 | struct dog 19 | { 20 | char *name; 21 | float age; 22 | char *owner; 23 | }; 24 | 25 | void init_dog(struct dog *d, char *name, float age, char *owner); 26 | void print_dog(struct dog *d); 27 | dog_t *new_dog(char *name, float age, char *owner); 28 | void free_dog(dog_t *d); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_name_as_is - prints a name as is 6 | * @name: name of the person 7 | * 8 | * Return: Nothing. 9 | */ 10 | void print_name_as_is(char *name) 11 | { 12 | printf("Hello, my name is %s\n", name); 13 | } 14 | 15 | /** 16 | * print_name_uppercase - print a name in uppercase 17 | * @name: name of the person 18 | * 19 | * Return: Nothing. 20 | */ 21 | void print_name_uppercase(char *name) 22 | { 23 | unsigned int i; 24 | 25 | printf("Hello, my uppercase name is "); 26 | i = 0; 27 | while (name[i]) 28 | { 29 | if (name[i] >= 'a' && name[i] <= 'z') 30 | { 31 | putchar(name[i] + 'A' - 'a'); 32 | } 33 | else 34 | { 35 | putchar(name[i]); 36 | } 37 | i++; 38 | } 39 | } 40 | 41 | /** 42 | * main - check the code 43 | * 44 | * Return: Always 0. 45 | */ 46 | int main(void) 47 | { 48 | print_name("Bob", print_name_as_is); 49 | print_name("Bob Dylan", print_name_uppercase); 50 | printf("\n"); 51 | return (0); 52 | } -------------------------------------------------------------------------------- /0x0F-function_pointers/0-print_name.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * print_name - prints a name as is. 5 | * @name: input name. 6 | * @f: function pointer. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_name(char *name, void (*f)(char *)) 11 | { 12 | if (name && f) 13 | f(name); 14 | } 15 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-array_iterator.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * array_iterator - executes a function given as a 5 | * parameter on each element of an array. 6 | * @array: input integer array. 7 | * @size: size of the array. 8 | * @action: pointer to the function. 9 | * 10 | * Return: no return. 11 | */ 12 | void array_iterator(int *array, size_t size, void (*action)(int)) 13 | { 14 | unsigned int i; 15 | 16 | if (array && action) 17 | for (i = 0; i < size; i++) 18 | action(array[i]); 19 | } 20 | -------------------------------------------------------------------------------- /0x0F-function_pointers/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "function_pointers.h" 3 | 4 | /** 5 | * print_elem - prints an integer 6 | * @elem: the integer to print 7 | * 8 | * Return: Nothing. 9 | */ 10 | void print_elem(int elem) 11 | { 12 | printf("%d\n", elem); 13 | } 14 | 15 | /** 16 | * print_elem_hex - prints an integer, in hexadecimal 17 | * @elem: the integer to print 18 | * 19 | * Return: Nothing. 20 | */ 21 | void print_elem_hex(int elem) 22 | { 23 | printf("0x%x\n", elem); 24 | } 25 | 26 | /** 27 | * main - check the code 28 | * 29 | * Return: Always 0. 30 | */ 31 | int main(void) 32 | { 33 | int array[5] = {0, 98, 402, 1024, 4096}; 34 | 35 | array_iterator(array, 5, &print_elem); 36 | array_iterator(array, 5, &print_elem_hex); 37 | return (0); 38 | } -------------------------------------------------------------------------------- /0x0F-function_pointers/100-main_opcodes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - check the code for Holberton School students. 6 | * @argc: argument count. 7 | * @argv: argument vector. 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(int argc, char *argv[]) 12 | { 13 | char *opc = (char *) main; 14 | int i, nbytes; 15 | 16 | if (argc != 2) 17 | { 18 | printf("Error\n"); 19 | exit(1); 20 | } 21 | 22 | nbytes = atoi(argv[1]); 23 | 24 | if (nbytes < 0) 25 | { 26 | printf("Error\n"); 27 | exit(2); 28 | } 29 | 30 | for (i = 0; i < nbytes; i++) 31 | { 32 | printf("%02x", opc[i] & 0xFF); 33 | if (i != nbytes - 1) 34 | printf(" "); 35 | } 36 | 37 | printf("\n"); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x0F-function_pointers/2-int_index.c: -------------------------------------------------------------------------------- 1 | #include "function_pointers.h" 2 | 3 | /** 4 | * int_index - searches for an integer 5 | * @array: input integer array. 6 | * @size: size of the array. 7 | * @comp: pointer to the function to be used 8 | * to compare values. 9 | * 10 | * Return: index of the first eement for which the comp 11 | * function does not return 0. If no elements matches, 12 | * return -1. If size <= 0, return -1. 13 | */ 14 | int int_index(int *array, int size, int (*comp)(int)) 15 | { 16 | int i; 17 | 18 | if (array && comp) 19 | { 20 | if (size <= 0) 21 | return (-1); 22 | 23 | for (i = 0; i < size; i++) 24 | if (comp(array[i])) 25 | return (i); 26 | } 27 | 28 | return (-1); 29 | } 30 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-calc.h: -------------------------------------------------------------------------------- 1 | #ifndef _CALC_H_ 2 | #define _CALC_H_ 3 | 4 | #include 5 | #include 6 | /** 7 | * struct op - Struct op 8 | * 9 | * @op: The operator 10 | * @f: The function associated 11 | */ 12 | typedef struct op 13 | { 14 | char *op; 15 | int (*f)(int a, int b); 16 | } op_t; 17 | 18 | int op_add(int a, int b); 19 | int op_sub(int a, int b); 20 | int op_mul(int a, int b); 21 | int op_div(int a, int b); 22 | int op_mod(int a, int b); 23 | int (*get_op_func(char *c))(int, int); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-get_op_func.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * get_op_func - selects the correct function to perform 5 | * the operation asked by the user. 6 | * @c: char operator. 7 | * 8 | * Return: pointer to the function that corresponds to the operator. 9 | */ 10 | int (*get_op_func(char *c))(int, int) 11 | { 12 | op_t ops[] = { 13 | {"+", op_add}, 14 | {"-", op_sub}, 15 | {"*", op_mul}, 16 | {"/", op_div}, 17 | {"%", op_mod}, 18 | {NULL, NULL} 19 | }; 20 | int i = 0; 21 | 22 | while (i < 10) 23 | { 24 | if (c[0] == ops->op[i]) 25 | break; 26 | i++; 27 | } 28 | 29 | return (ops[i / 2].f); 30 | } 31 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-main.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * main - check the code for Holberton School students. 5 | * @argc: argument count. 6 | * @argv: argument vector. 7 | * 8 | * Return: Always 0. 9 | */ 10 | int main(int argc, char *argv[]) 11 | { 12 | int a, b; 13 | int (*operation)(int, int); 14 | 15 | if (argc != 4) 16 | { 17 | printf("Error\n"); 18 | exit(98); 19 | } 20 | if (argv[2][1]) 21 | { 22 | printf("Error\n"); 23 | exit(99); 24 | } 25 | 26 | operation = get_op_func(argv[2]); 27 | 28 | if (operation == NULL) 29 | { 30 | printf("Error\n"); 31 | exit(99); 32 | } 33 | 34 | a = atoi(argv[1]); 35 | b = atoi(argv[3]); 36 | 37 | printf("%d\n", operation(a, b)); 38 | return (0); 39 | } 40 | -------------------------------------------------------------------------------- /0x0F-function_pointers/3-op_functions.c: -------------------------------------------------------------------------------- 1 | #include "3-calc.h" 2 | 3 | /** 4 | * op_add - adds two numbers. 5 | * @a: first number. 6 | * @b: second number. 7 | * 8 | * Return: add. 9 | */ 10 | int op_add(int a, int b) 11 | { 12 | return (a + b); 13 | } 14 | 15 | /** 16 | * op_sub - subctracts two numbers. 17 | * @a: first number. 18 | * @b: second number. 19 | * 20 | * Return: difference. 21 | */ 22 | int op_sub(int a, int b) 23 | { 24 | return (a - b); 25 | } 26 | 27 | /** 28 | * op_mul - multiplies two numbers. 29 | * @a: first number. 30 | * @b: second number. 31 | * 32 | * Return: multiplication. 33 | */ 34 | int op_mul(int a, int b) 35 | { 36 | return (a * b); 37 | } 38 | 39 | /** 40 | * op_div - divides two numbers. 41 | * @a: first number. 42 | * @b: second number. 43 | * 44 | * Return: division. 45 | */ 46 | int op_div(int a, int b) 47 | { 48 | if (b == 0) 49 | { 50 | printf("Error\n"); 51 | exit(100); 52 | } 53 | return (a / b); 54 | } 55 | 56 | /** 57 | * op_mod - calculates the module of two numbers. 58 | * @a: first number. 59 | * @b: second number. 60 | * 61 | * Return: remainder of the division. 62 | */ 63 | int op_mod(int a, int b) 64 | { 65 | if (b == 0) 66 | { 67 | printf("Error\n"); 68 | exit(100); 69 | } 70 | return (a % b); 71 | } 72 | -------------------------------------------------------------------------------- /0x0F-function_pointers/README.md: -------------------------------------------------------------------------------- 1 | # Function pointers 2 | Project to learn about function pointers in **C language**. 3 | 4 | * C files are compiled using `gcc -Wall -pedantic -Werror -Wextra -std=gnu89 ` 5 | * C files are written according to the C90 standard 6 | * Tested on Ubuntu 14.04 LTS 7 | 8 | ## Files 9 | All of the following files are programs written in C: 10 | 11 | 12 | 13 | `0-print_name.c` :Prints a name 14 | `1-array_iterator.c` : Executes a function given as a parameter on each element of an array 15 | `2-int_index.c` : Searches for an integer 16 | `3-calc.h` : Header file that contains all function prototypes and data structures 17 | arithmetic operations 18 | `3-get_op_func.c` : Function pointer that returns the pointer of the arithmetic functions 19 | `100-main_opcodes.c`: Prints the opcodes of its own main function 20 | -------------------------------------------------------------------------------- /0x0F-function_pointers/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x0F-function_pointers/a -------------------------------------------------------------------------------- /0x0F-function_pointers/function_pointers.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTION_POINTERS_ 2 | #define _FUNCTION_POINTERS_ 3 | 4 | #include 5 | 6 | void print_name(char *name, void (*f)(char *)); 7 | void array_iterator(int *array, size_t size, void (*action)(int)); 8 | int int_index(int *array, int size, int (*comp)(int)); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x10-variadic_functions/ 3-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_all("ceis", 'B', 3, "stSchool"); 11 | return (0); 12 | } -------------------------------------------------------------------------------- /0x10-variadic_functions/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "variadic_functions.h" 3 | 4 | /** 5 | * main - check the code 6 | * 7 | * Return: Always 0. 8 | */ 9 | int main(void) 10 | { 11 | int sum; 12 | 13 | sum = sum_them_all(2, 98, 1024); 14 | printf("%d\n", sum); 15 | sum = sum_them_all(4, 98, 1024, 402, -1024); 16 | printf("%d\n", sum); 17 | return (0); 18 | } -------------------------------------------------------------------------------- /0x10-variadic_functions/0-sum_them_all.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 0-sum_them_all.c 3 | * Auth: Tolulope Fakunle 4 | */ 5 | 6 | #include "variadic_functions.h" 7 | #include 8 | 9 | /** 10 | * sum_them_all - Returns the sum of all its paramters. 11 | * @n: The number of paramters passed to the function. 12 | * @...: A variable number of paramters to calculate the sum of. 13 | * 14 | * Return: If n == 0 - 0. 15 | * Otherwise - the sum of all parameters. 16 | */ 17 | int sum_them_all(const unsigned int n, ...) 18 | { 19 | va_list valist; 20 | unsigned int index, sum = 0; 21 | 22 | va_start(valist, n); 23 | 24 | for (index = 0; index < n; index++) 25 | sum += va_arg(valist, int); 26 | 27 | va_end(valist); 28 | 29 | return (sum); 30 | } 31 | -------------------------------------------------------------------------------- /0x10-variadic_functions/1-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_numbers(", ", 4, 0, 98, -1024, 402); 11 | return (0); 12 | } -------------------------------------------------------------------------------- /0x10-variadic_functions/1-print_numbers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 1-print_numbers.c 3 | * Auth: Tolulope Fakunle 4 | */ 5 | 6 | #include "variadic_functions.h" 7 | #include 8 | #include 9 | 10 | /** 11 | * print_numbers - Prints numbers, followed by a new line. 12 | * @separator: The string to be printed between numbers. 13 | * @n: The number of integers passed to the function. 14 | * @...: A variable number of numbers to be printed. 15 | */ 16 | void print_numbers(const char *separator, const unsigned int n, ...) 17 | { 18 | va_list valist; 19 | unsigned int index; 20 | 21 | va_start(valist, n); 22 | 23 | for (index = 0; index < n; index++) 24 | { 25 | printf("%d", va_arg(valist, int)); 26 | 27 | if (index != (n - 1) && separator != NULL) 28 | printf("%s", separator); 29 | } 30 | 31 | printf("\n"); 32 | 33 | va_end(valist); 34 | } 35 | -------------------------------------------------------------------------------- /0x10-variadic_functions/2-main.c: -------------------------------------------------------------------------------- 1 | #include "variadic_functions.h" 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | print_strings(", ", 2, "Jay", "Django"); 11 | return (0); 12 | } -------------------------------------------------------------------------------- /0x10-variadic_functions/2-print_strings.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 2-print_strings.c 3 | * Auth: Tolulope Fakunle 4 | */ 5 | 6 | #include "variadic_functions.h" 7 | #include 8 | #include 9 | 10 | /** 11 | * print_strings - Prints strings, followed by a new line. 12 | * @separator: The string to be printed between strings. 13 | * @n: The number of strings passed to the function. 14 | * @...: A variable number of strings to be printed. 15 | * 16 | * Description: If separator is NULL, it is not printed. 17 | * If one of the strings if NULL, (nil) is printed instead. 18 | */ 19 | void print_strings(const char *separator, const unsigned int n, ...) 20 | { 21 | va_list strings; 22 | char *str; 23 | unsigned int i; 24 | 25 | va_start(strings, n); 26 | 27 | for (i = 0; i < n; i++) 28 | { 29 | str = va_arg(strings, char *); 30 | 31 | if (str == NULL) 32 | printf("(nil)"); 33 | else 34 | printf("%s", str); 35 | 36 | if (i != (n - 1) && separator != NULL) 37 | printf("%s", separator); 38 | } 39 | 40 | printf("\n"); 41 | 42 | va_end(strings); 43 | } 44 | -------------------------------------------------------------------------------- /0x10-variadic_functions/3-print_all.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 3-print_all.c 3 | * Auth: Tolulope Fakunle 4 | */ 5 | 6 | #include "variadic_functions.h" 7 | #include 8 | #include 9 | 10 | void print_char(va_list arg); 11 | void print_int(va_list arg); 12 | void print_float(va_list arg); 13 | void print_string(va_list arg); 14 | void print_all(const char * const format, ...); 15 | 16 | /** 17 | * print_char - Prints a char. 18 | * @arg: A list of arguments pointing to 19 | * the character to be printed. 20 | */ 21 | void print_char(va_list arg) 22 | { 23 | char letter; 24 | 25 | letter = va_arg(arg, int); 26 | printf("%c", letter); 27 | } 28 | 29 | /** 30 | * print_int - Prints an int. 31 | * @arg: A list of arguments pointing to 32 | * the integer to be printed. 33 | */ 34 | void print_int(va_list arg) 35 | { 36 | int num; 37 | 38 | num = va_arg(arg, int); 39 | printf("%d", num); 40 | } 41 | 42 | /** 43 | * print_float - Prints a float. 44 | * @arg: A list of arguments pointing to 45 | * the float to be printed. 46 | */ 47 | void print_float(va_list arg) 48 | { 49 | float num; 50 | 51 | num = va_arg(arg, double); 52 | printf("%f", num); 53 | } 54 | 55 | /** 56 | * print_string - Prints a string. 57 | * @arg: A list of arguments pointing to 58 | * the string to be printed. 59 | */ 60 | void print_string(va_list arg) 61 | { 62 | char *str; 63 | 64 | str = va_arg(arg, char *); 65 | 66 | if (str == NULL) 67 | { 68 | printf("(nil)"); 69 | return; 70 | } 71 | 72 | printf("%s", str); 73 | } 74 | 75 | /** 76 | * print_all - Prints anything, followed by a new line. 77 | * @format: A string of characters representing the argument types. 78 | * @...: A variable number of arguments to be printed. 79 | * 80 | * Description: Any argument not of type char, int, float, 81 | * or char * is ignored. 82 | * If a string argument is NULL, (nil) is printed instead. 83 | */ 84 | void print_all(const char * const format, ...) 85 | { 86 | va_list args; 87 | int i = 0, j = 0; 88 | char *separator = ""; 89 | printer_t funcs[] = { 90 | {"c", print_char}, 91 | {"i", print_int}, 92 | {"f", print_float}, 93 | {"s", print_string} 94 | }; 95 | 96 | va_start(args, format); 97 | 98 | while (format && (*(format + i))) 99 | { 100 | j = 0; 101 | 102 | while (j < 4 && (*(format + i) != *(funcs[j].symbol))) 103 | j++; 104 | 105 | if (j < 4) 106 | { 107 | printf("%s", separator); 108 | funcs[j].print(args); 109 | separator = ", "; 110 | } 111 | 112 | i++; 113 | } 114 | 115 | printf("\n"); 116 | 117 | va_end(args); 118 | } 119 | -------------------------------------------------------------------------------- /0x10-variadic_functions/README.md: -------------------------------------------------------------------------------- 1 | # C - Variadic functions 2 | 3 | In this project, I learned about using variadic functions with the `va_start`, 4 | `va_arg`, and `va_end` macros as well as why and how to use the `const` type qualifier in C. 5 | 6 | | 7 | 8 | ## Tasks : 9 | 10 | * **0. Beauty is variable, ugliness is constant** 11 | * [0-sum_them_all.c](./0-sum_them_all.c): C function that returns the sum of 12 | all its paramters. 13 | * If the parameter `n == 0`, the function returns `0`. 14 | 15 | * **1. To be is to be the value of a variable** 16 | * [1-print_numbers.c](./1-print_numbers.c): C function that prints numbers, 17 | followed by a new line. 18 | * The paramter `separator` is the string to be printed between numbers. 19 | * The paramter `n` is the number of integers passed to the function. 20 | * If `separator` is `NULL`, it is not printed. 21 | 22 | * **2. One woman's constant is another woman's variable** 23 | * [2-print_strings.c](./2-print_strings.c): C function that prints strings, 24 | followed by a new line. 25 | * The parameter `separator` is the string to be printed between the strings. 26 | * The parameter `n` is the number of strings passed to the function. 27 | * If `separator` is `NULL`, it is not printed. 28 | * If one of the strings is `NULL`, the function prints `(nil)` instead. 29 | 30 | * **3. To be is a to be the value of a variable** 31 | * [3-print_all.c](./3-print_all.c): C function that prints anything. 32 | * Usage: `print_all(", ", 2, "Jay", "Django");` 33 | * The paramter `format` is a list of types of arguments passed to the function 34 | (`c`:`char`, `i`:`int`, `f`:`float`, `s`:`char *` (if the string is 35 | `NULL`, `(nil)` is printed instead)) 36 | * Any other character is ignored. 37 | * A new line is printed at the end of the function. 38 | 39 | -------------------------------------------------------------------------------- /0x10-variadic_functions/variadic_functions.h: -------------------------------------------------------------------------------- 1 | #ifndef VARIADIC_FUNCTIONS_H 2 | #define VARIADIC_FUNCTIONS_H 3 | 4 | /* 5 | * File: variadic_functions.h 6 | * Auth: Tolulope Fakunle 7 | * Desc: Header file containing prototypes for all functions 8 | * used in the 0x0F-variadic_functions directory. 9 | */ 10 | 11 | #include 12 | 13 | /** 14 | * struct printer - A new struct type defining a printer. 15 | * @symbol: A symbol representing a data type. 16 | * @print: A function pointer to a function that prints 17 | * a data type corresponding to symbol. 18 | */ 19 | typedef struct printer 20 | { 21 | char *symbol; 22 | void (*print)(va_list arg); 23 | 24 | } printer_t; 25 | 26 | int sum_them_all(const unsigned int n, ...); 27 | void print_numbers(const char *separator, const unsigned int n, ...); 28 | void print_strings(const char *separator, const unsigned int n, ...); 29 | void print_all(const char * const format, ...); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = print_list(head); 30 | printf("-> %lu elements\n", n); 31 | 32 | printf("\n"); 33 | free(new->str); 34 | new->str = NULL; 35 | n = print_list(head); 36 | printf("-> %lu elements\n", n); 37 | 38 | free(new); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * print_list - prints all the elements of a list_t list. 4 | * @list: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t print_list(const list_t *list) 9 | { 10 | size_t num; 11 | 12 | num = 0; 13 | while (list != NULL) 14 | { 15 | if (list->str == NULL) 16 | printf("[%d] %s\n", 0, "(nil)"); 17 | else 18 | printf("[%d] %s\n", list->len, list->str); 19 | list = list->next; 20 | num++; 21 | } 22 | return (num); 23 | } 24 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * list_len - returns then number of elements in a list. 4 | * @list: singly linked list. 5 | * Return: number of elements in the list. 6 | */ 7 | 8 | size_t list_len(const list_t *list) 9 | { 10 | size_t number; 11 | 12 | number = 0; 13 | while (list != NULL) 14 | { 15 | list = list->next; 16 | number++; 17 | } 18 | return (number); 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | list_t *new; 15 | list_t hello = {"World", 5, NULL}; 16 | size_t n; 17 | 18 | head = &hello; 19 | new = malloc(sizeof(list_t)); 20 | if (new == NULL) 21 | { 22 | printf("Error\n"); 23 | return (1); 24 | } 25 | new->str = strdup("Hello"); 26 | new->len = 5; 27 | new->next = head; 28 | head = new; 29 | n = list_len(head); 30 | printf("-> %lu elements\n", n); 31 | free(new->str); 32 | free(new); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * beforemain - function executed before main 5 | * Return: no return. 6 | */ 7 | 8 | void __attribute__ ((constructor)) beforemain() 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - check the code 5 | * 6 | * Return: Always 0. 7 | */ 8 | int main(void) 9 | { 10 | printf("(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.)\n"); 11 | return (0); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | /** 3 | * add_node - adds a new node at the beginning 4 | * of a list_t list. 5 | * @head: head of the linked list. 6 | * @str: string to store in the list. 7 | * Return: address of the head. 8 | */ 9 | 10 | list_t *add_node(list_t **head, const char *str) 11 | { 12 | list_t *new; 13 | size_t nchar; 14 | 15 | new = malloc(sizeof(list_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | 19 | new->str = strdup(str); 20 | 21 | for (nchar = 0; str[nchar]; nchar++) 22 | ; 23 | 24 | new->len = nchar; 25 | new->next = *head; 26 | *head = new; 27 | 28 | return (*head); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node(&head, "Alexandro"); 17 | add_node(&head, "Asaia"); 18 | add_node(&head, "Augustin"); 19 | add_node(&head, "Bennett"); 20 | add_node(&head, "Bilal"); 21 | add_node(&head, "Chandler"); 22 | add_node(&head, "Damian"); 23 | add_node(&head, "Daniel"); 24 | add_node(&head, "Dora"); 25 | add_node(&head, "Electra"); 26 | add_node(&head, "Gloria"); 27 | add_node(&head, "Joe"); 28 | add_node(&head, "John"); 29 | add_node(&head, "John"); 30 | add_node(&head, "Josquin"); 31 | add_node(&head, "Kris"); 32 | add_node(&head, "Marine"); 33 | add_node(&head, "Mason"); 34 | add_node(&head, "Praylin"); 35 | add_node(&head, "Rick"); 36 | add_node(&head, "Rick"); 37 | add_node(&head, "Rona"); 38 | add_node(&head, "Siphan"); 39 | add_node(&head, "Sravanthi"); 40 | add_node(&head, "Steven"); 41 | add_node(&head, "Tasneem"); 42 | add_node(&head, "William"); 43 | add_node(&head, "Zee"); 44 | print_list(head); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_node_end - adds a new node at the end 5 | * of a list_t list. 6 | * @head: head of the linked list. 7 | * @str: string to store in the list. 8 | * Return: address of the head. 9 | */ 10 | 11 | list_t *add_node_end(list_t **head, const char *str) 12 | { 13 | list_t *new, *temp; 14 | size_t l; 15 | 16 | new = malloc(sizeof(list_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->str = strdup(str); 21 | 22 | for (l = 0; str[l]; l++) 23 | ; 24 | 25 | new->len = l; 26 | new->next = NULL; 27 | temp = *head; 28 | 29 | if (temp == NULL) 30 | { 31 | *head = new; 32 | } 33 | else 34 | { 35 | while (temp->next != NULL) 36 | temp = temp->next; 37 | temp->next = new; 38 | } 39 | 40 | return (*head); 41 | } 42 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Anne"); 17 | add_node_end(&head, "Colton"); 18 | add_node_end(&head, "Corbin"); 19 | add_node_end(&head, "Daniel"); 20 | add_node_end(&head, "Danton"); 21 | add_node_end(&head, "David"); 22 | add_node_end(&head, "Gary"); 23 | add_node_end(&head, "Holden"); 24 | add_node_end(&head, "Ian"); 25 | add_node_end(&head, "Ian"); 26 | add_node_end(&head, "Jay"); 27 | add_node_end(&head, "Jennie"); 28 | add_node_end(&head, "Jimmy"); 29 | add_node_end(&head, "Justin"); 30 | add_node_end(&head, "Kalson"); 31 | add_node_end(&head, "Kina"); 32 | add_node_end(&head, "Matthew"); 33 | add_node_end(&head, "Max"); 34 | add_node_end(&head, "Michael"); 35 | add_node_end(&head, "Ntuj"); 36 | add_node_end(&head, "Philip"); 37 | add_node_end(&head, "Richard"); 38 | add_node_end(&head, "Samantha"); 39 | add_node_end(&head, "Stuart"); 40 | add_node_end(&head, "Swati"); 41 | add_node_end(&head, "Timothy"); 42 | add_node_end(&head, "Victor"); 43 | add_node_end(&head, "Walton"); 44 | print_list(head); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-free_list.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_list - frees a list 5 | * @head: head of the linked list. 6 | * Return: no return. 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *curre; 12 | 13 | while ((curre = head) != NULL) 14 | { 15 | head = head->next; 16 | free(curre->str); 17 | free(curre); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "lists.h" 5 | 6 | /** 7 | * main - check the code 8 | * 9 | * Return: Always 0. 10 | */ 11 | int main(void) 12 | { 13 | list_t *head; 14 | 15 | head = NULL; 16 | add_node_end(&head, "Bob"); 17 | add_node_end(&head, "&"); 18 | add_node_end(&head, "Kris"); 19 | add_node_end(&head, "love"); 20 | add_node_end(&head, "asm"); 21 | print_list(head); 22 | free_list(head); 23 | head = NULL; 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # C - Singly linked lists 2 | 3 | In this project,we practiced building and using singly linked lists 4 | in C while learning when and why to use linked lists versus arrays. 5 | 6 | ## Header File :file_folder: 7 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct list_s - singly linked list 10 | * @str: string - (malloc'ed string) 11 | * @len: length of the string 12 | * @next: points to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for Holberton project 16 | */ 17 | typedef struct list_s 18 | { 19 | char *str; 20 | unsigned int len; 21 | struct list_s *next; 22 | } list_t; 23 | 24 | size_t print_list(const list_t *list); 25 | size_t list_len(const list_t *list); 26 | list_t *add_node(list_t **head, const char *str); 27 | list_t *add_node_end(list_t **head, const char *str); 28 | void free_list(list_t *head); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/0-print_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_listint - prints all the elements of a list. 5 | * @h: head of a list. 6 | * 7 | * Return: numbers of nodes. 8 | */ 9 | size_t print_listint(const listint_t *h) 10 | { 11 | size_t nodes = 0; 12 | 13 | while (h != NULL) 14 | { 15 | printf("%d\n", h->n); 16 | h = h->next; 17 | nodes++; 18 | } 19 | return (nodes); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/1-listint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * listint_len - returns the number of elements in 5 | * a linked list. 6 | * @h: head of a list. 7 | * 8 | * Return: numbers of nodes. 9 | */ 10 | size_t listint_len(const listint_t *h) 11 | { 12 | size_t nods = 0; 13 | 14 | while (h != NULL) 15 | { 16 | h = h->next; 17 | nods++; 18 | } 19 | return (nods); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/10-delete_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_nodeint_at_index - deletes the node at index 5 | * of a linked list. 6 | * @head: head of a list. 7 | * @index: index of the list where the node is 8 | * deleted. 9 | * 10 | * Return: 1 if it succeeded, -1 if it failed. 11 | */ 12 | int delete_nodeint_at_index(listint_t **head, unsigned int index) 13 | { 14 | unsigned int i; 15 | listint_t *prev; 16 | listint_t *next; 17 | 18 | prev = *head; 19 | 20 | if (index != 0) 21 | { 22 | for (i = 0; i < index - 1 && prev != NULL; i++) 23 | { 24 | prev = prev->next; 25 | } 26 | } 27 | 28 | if (prev == NULL || (prev->next == NULL && index != 0)) 29 | { 30 | return (-1); 31 | } 32 | 33 | next = prev->next; 34 | 35 | if (index != 0) 36 | { 37 | prev->next = next->next; 38 | free(next); 39 | } 40 | else 41 | { 42 | free(prev); 43 | *head = next; 44 | } 45 | 46 | return (1); 47 | } 48 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/100-reverse_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * reverse_listint - reverses a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: pointer to the first node. 8 | */ 9 | listint_t *reverse_listint(listint_t **head) 10 | { 11 | listint_t *p; 12 | listint_t *n; 13 | 14 | p = NULL; 15 | n = NULL; 16 | 17 | while (*head != NULL) 18 | { 19 | n = (*head)->next; 20 | (*head)->next = p; 21 | p = *head; 22 | *head = n; 23 | } 24 | 25 | *head = p; 26 | return (*head); 27 | } 28 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/101-print_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * print_listint_safe - prints a linked list. 28 | * @head: head of a list. 29 | * 30 | * Return: number of nodes in the list. 31 | */ 32 | size_t print_listint_safe(const listint_t *head) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | 37 | hptr = NULL; 38 | while (head != NULL) 39 | { 40 | new = malloc(sizeof(listp_t)); 41 | 42 | if (new == NULL) 43 | exit(98); 44 | 45 | new->p = (void *)head; 46 | new->next = hptr; 47 | hptr = new; 48 | 49 | add = hptr; 50 | 51 | while (add->next != NULL) 52 | { 53 | add = add->next; 54 | if (head == add->p) 55 | { 56 | printf("-> [%p] %d\n", (void *)head, head->n); 57 | free_listp(&hptr); 58 | return (nnodes); 59 | } 60 | } 61 | 62 | printf("[%p] %d\n", (void *)head, head->n); 63 | head = head->next; 64 | nnodes++; 65 | } 66 | 67 | free_listp(&hptr); 68 | return (nnodes); 69 | } 70 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/102-free_listint_safe.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listp2 - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listp2(listp_t **head) 10 | { 11 | listp_t *temp; 12 | listp_t *curr; 13 | 14 | if (head != NULL) 15 | { 16 | curr = *head; 17 | while ((temp = curr) != NULL) 18 | { 19 | curr = curr->next; 20 | free(temp); 21 | } 22 | *head = NULL; 23 | } 24 | } 25 | 26 | /** 27 | * free_listint_safe - frees a linked list. 28 | * @h: head of a list. 29 | * 30 | * Return: size of the list that was freed. 31 | */ 32 | size_t free_listint_safe(listint_t **h) 33 | { 34 | size_t nnodes = 0; 35 | listp_t *hptr, *new, *add; 36 | listint_t *curr; 37 | 38 | hptr = NULL; 39 | while (*h != NULL) 40 | { 41 | new = malloc(sizeof(listp_t)); 42 | 43 | if (new == NULL) 44 | exit(98); 45 | 46 | new->p = (void *)*h; 47 | new->next = hptr; 48 | hptr = new; 49 | 50 | add = hptr; 51 | 52 | while (add->next != NULL) 53 | { 54 | add = add->next; 55 | if (*h == add->p) 56 | { 57 | *h = NULL; 58 | free_listp2(&hptr); 59 | return (nnodes); 60 | } 61 | } 62 | 63 | curr = *h; 64 | *h = (*h)->next; 65 | free(curr); 66 | nnodes++; 67 | } 68 | 69 | *h = NULL; 70 | free_listp2(&hptr); 71 | return (nnodes); 72 | } 73 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/103-find_loop.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * find_listint_loop - finds the loop in a linked list. 5 | * @head: head of a list. 6 | * 7 | * Return: the address of the node where the loop starts. 8 | */ 9 | listint_t *find_listint_loop(listint_t *head) 10 | { 11 | listint_t *p2; 12 | listint_t *prev; 13 | 14 | p2 = head; 15 | prev = head; 16 | while (head && p2 && p2->next) 17 | { 18 | head = head->next; 19 | p2 = p2->next->next; 20 | 21 | if (head == p2) 22 | { 23 | head = prev; 24 | prev = p2; 25 | while (1) 26 | { 27 | p2 = prev; 28 | while (p2->next != head && p2->next != prev) 29 | { 30 | p2 = p2->next; 31 | } 32 | if (p2->next == head) 33 | break; 34 | 35 | head = head->next; 36 | } 37 | return (p2->next); 38 | } 39 | } 40 | 41 | return (NULL); 42 | } 43 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/2-add_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint - add a new node at the beginning 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint(listint_t **head, const int n) 12 | { 13 | listint_t *New; 14 | 15 | New = malloc(sizeof(listint_t)); 16 | 17 | if (New == NULL) 18 | return (NULL); 19 | 20 | New->n = n; 21 | New->next = *head; 22 | *head = New; 23 | 24 | return (*head); 25 | } 26 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/3-add_nodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_nodeint_end - add a new node at the end 5 | * of a linked list 6 | * @head: head of a list. 7 | * @n: n element. 8 | * 9 | * Return: address of the new element. NUll if it failed. 10 | */ 11 | listint_t *add_nodeint_end(listint_t **head, const int n) 12 | { 13 | listint_t *New; 14 | listint_t *tmp; 15 | 16 | (void)tmp; 17 | 18 | New = malloc(sizeof(listint_t)); 19 | 20 | if (New == NULL) 21 | return (NULL); 22 | 23 | New->n = n; 24 | New->next = NULL; 25 | tmp = *head; 26 | if (*head == NULL) 27 | { 28 | *head = New; 29 | } 30 | else 31 | { 32 | while (tmp->next != NULL) 33 | { 34 | tmp = tmp->next; 35 | } 36 | tmp->next = New; 37 | } 38 | 39 | return (*head); 40 | } 41 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/4-free_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint - frees a linked list 5 | * @head: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint(listint_t *head) 10 | { 11 | listint_t *temp; 12 | 13 | while ((temp = head) != NULL) 14 | { 15 | head = head->next; 16 | free(temp); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/5-free_listint2.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_listint2 - frees a linked list 5 | * @h: head of a list. 6 | * 7 | * Return: no return. 8 | */ 9 | void free_listint2(listint_t **h) 10 | { 11 | listint_t *temp; 12 | listint_t *current; 13 | 14 | if (h != NULL) 15 | { 16 | current = *h; 17 | while ((temp = current) != NULL) 18 | { 19 | current = current->next; 20 | free(temp); 21 | } 22 | *h = NULL; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/6-pop_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * pop_listint - deletes the head node of 5 | * a linked list 6 | * @h: head of a list. 7 | * 8 | * Return: head node's data. 9 | */ 10 | int pop_listint(listint_t **head) 11 | { 12 | int h_node; 13 | listint_t *head; 14 | listint_t *current; 15 | 16 | if (*head == NULL) 17 | return (0); 18 | 19 | current = *head; 20 | 21 | h_node = current->n; 22 | 23 | head = current->next; 24 | 25 | free(current); 26 | 27 | *head = head; 28 | 29 | return (h_node); 30 | } 31 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/7-get_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_nodeint_at_index - returns the nth node of a linked list 5 | * @h: head of a list. 6 | * @index: index of the node. 7 | * 8 | * Return: nth node. If node does not exist, returns NULL. 9 | */ 10 | listint_t *get_nodeint_at_index(listint_t *h, unsigned int index) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; i < index && h != NULL; i++) 15 | { 16 | h = h->next; 17 | } 18 | 19 | return (h); 20 | } 21 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/8-sum_listint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_listint - returns the sum of all the data (n) of 5 | * a linked list. 6 | * @h: head of a list. 7 | * 8 | * Return: sum of all the data (n). 9 | */ 10 | int sum_listint(listint_t *h) 11 | { 12 | int sum; 13 | 14 | sum = 0; 15 | while (h != NULL) 16 | { 17 | sum += h->n; 18 | h = h->next; 19 | } 20 | 21 | return (sum); 22 | } 23 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/9-insert_nodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_nodeint_at_index - inserts a new node 5 | * at a given position. 6 | * @head: head of a list. 7 | * @idx: index of the list where the new node is 8 | * added. 9 | * @n: integer element. 10 | * 11 | * Return: the address of the new node, or NULL if it 12 | * failed. 13 | */ 14 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n) 15 | { 16 | unsigned int i; 17 | listint_t *new; 18 | listint_t *h; 19 | 20 | h = *head; 21 | 22 | if (idx != 0) 23 | { 24 | for (i = 0; i < idx - 1 && h != NULL; i++) 25 | { 26 | h = h->next; 27 | } 28 | } 29 | 30 | if (h == NULL && idx != 0) 31 | return (NULL); 32 | 33 | new = malloc(sizeof(listint_t)); 34 | if (new == NULL) 35 | return (NULL); 36 | 37 | new->n = n; 38 | 39 | if (idx == 0) 40 | { 41 | new->next = *head; 42 | *head = new; 43 | } 44 | else 45 | { 46 | new->next = h->next; 47 | h->next = new; 48 | } 49 | 50 | return (new); 51 | } 52 | -------------------------------------------------------------------------------- /0x13-more_singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * @n: integer 10 | * @next: points to the next node 11 | * 12 | * Description: singly linked list node structure 13 | * for Holberton project 14 | */ 15 | typedef struct listint_s 16 | { 17 | int n; 18 | struct listint_s *next; 19 | } listint_t; 20 | 21 | /** 22 | * struct listp_s - singly linked list 23 | * @p: pointers of nodes 24 | * @next: points to the next node 25 | * 26 | * Description: singly linked list of pointers 27 | */ 28 | typedef struct listp_s 29 | { 30 | void *p; 31 | struct listp_s *next; 32 | } listp_t; 33 | 34 | size_t print_listint(const listint_t *h); 35 | size_t listint_len(const listint_t *h); 36 | listint_t *add_nodeint(listint_t **head, const int n); 37 | listint_t *add_nodeint_end(listint_t **head, const int n); 38 | void free_listint(listint_t *head); 39 | void free_listint2(listint_t **head); 40 | int pop_listint(listint_t **head); 41 | listint_t *get_nodeint_at_index(listint_t *head, unsigned int index); 42 | int sum_listint(listint_t *head); 43 | listint_t *insert_nodeint_at_index(listint_t **head, unsigned int idx, int n); 44 | int delete_nodeint_at_index(listint_t **head, unsigned int index); 45 | listint_t *reverse_listint(listint_t **head); 46 | size_t print_listint_safe(const listint_t *head); 47 | size_t free_listint_safe(listint_t **h); 48 | listint_t *find_listint_loop(listint_t *head); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/0-binary_to_uint.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * binary_to_uint - converts a binary number to an 5 | * unsigned int. 6 | * @b: binary. 7 | * 8 | * Return: unsigned int. 9 | */ 10 | unsigned int binary_to_uint(const char *b) 11 | { 12 | unsigned int i; 13 | int len, base2; 14 | 15 | if (!b) 16 | return (0); 17 | 18 | i = 0; 19 | 20 | for (len = 0; b[len] != '\0'; len++) 21 | ; 22 | 23 | for (len--, base2 = 1; len >= 0; len--, base2 *= 2) 24 | { 25 | if (b[len] != '0' && b[len] != '1') 26 | { 27 | return (0); 28 | } 29 | 30 | if (b[len] & 1) 31 | { 32 | i += base2; 33 | } 34 | } 35 | 36 | return (i); 37 | } 38 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/1-print_binary.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_binary - prints the binary representation 5 | * of a number. 6 | * @n: unsigned long int. 7 | * 8 | * Return: no return. 9 | */ 10 | void print_binary(unsigned long int n) 11 | { 12 | if (n >> 0) 13 | { 14 | if (n >> 1) 15 | print_binary(n >> 1); 16 | _putchar((n & 1) + '0'); 17 | } 18 | else 19 | { 20 | _putchar('0'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/100-get_endianness.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_endianness - checks the endianness 5 | * 6 | * Return: 0 if big endian, 1 if little endian 7 | */ 8 | int get_endianness(void) 9 | { 10 | unsigned int d; 11 | char *c; 12 | 13 | d = 1; 14 | c = (char *) &d; 15 | 16 | return ((int)*c); 17 | } 18 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/101-password: -------------------------------------------------------------------------------- 1 | Hol 2 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/2-get_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * get_bit - returns the value of a bit at a given 5 | * index. 6 | * @n: unsigned long int input. 7 | * @index: index of the bit. 8 | * 9 | * Return: value of the bit. 10 | */ 11 | int get_bit(unsigned long int n, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (n == 0 && index < 64) 16 | return (0); 17 | 18 | for (i = 0; i <= 63; n >>= 1, i++) 19 | { 20 | if (index == i) 21 | { 22 | return (n & 1); 23 | } 24 | } 25 | 26 | return (-1); 27 | } 28 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/3-set_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_bit - sets the value of a bit to 1. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int set_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int L; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | L = 1 << index; 19 | *n = (*n | L); 20 | 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/4-clear_bit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * clear_bit - sets the value of a bit to 0. 5 | * at a given index. 6 | * @n: pointer of an unsigned long int. 7 | * @index: index of the bit. 8 | * 9 | * Return: 1 if it worked, -1 if it didn't. 10 | */ 11 | int clear_bit(unsigned long int *n, unsigned int index) 12 | { 13 | unsigned int l; 14 | 15 | if (index > 63) 16 | return (-1); 17 | 18 | l = 1 << index; 19 | 20 | if (*n & l) 21 | *n ^= l; 22 | 23 | return (1); 24 | } 25 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/5-flip_bits.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * flip_bits - returns the number of bits you would 5 | * need to flip to get from one number to another 6 | * @n: number one. 7 | * @m: number two. 8 | * 9 | * Return: number of bits. 10 | */ 11 | unsigned int flip_bits(unsigned long int n, unsigned long int m) 12 | { 13 | unsigned int bit; 14 | 15 | for (bit = 0; n || m; n >>= 1, m >>= 1) 16 | { 17 | if ((n & 1) != (m & 1)) 18 | bit++; 19 | } 20 | 21 | return (bit); 22 | } 23 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/README.md: -------------------------------------------------------------------------------- 1 | # C - Bit manipulation 2 | 3 | In this project, I learned how to manipulate bits and use the 4 | bitwise operators `>>`, `<<`, `&`, `|`, and `^` in C. 5 | 6 | ## Header File :file_folder: 7 | 8 | * [main.h](./main.h): Header file containing prototypes for all 9 | functions written in the project. 10 | 11 | 12 | ## Tasks :page_with_curl: 13 | 14 | * **0. 0** 15 | * [0-binary_to_uint.c](./0-binary_to_uint.c): C function that converts a binary number 16 | to an `unsigned int`. 17 | * The parameter `b` is a pointer to a string of `0` and `1` characters. 18 | * If `b` is `NULL` or there are one or more characters in `b` that are 19 | not `0` or `1` - returns `0`. 20 | * Otherwise - returns the converted number. 21 | 22 | * **1. 1** 23 | * [1-print_binary.c](./1-print_binary.c): C function that prints the binary representation 24 | of a number. 25 | 26 | * **2. 10** 27 | * [2-get_bit.c](./2-get_bit.c): C function that returns the value of a bit at a 28 | given index. 29 | * Indices start at `0`. 30 | * If an error occurs - returns `-1`. 31 | * Otherwise - returns the value of the bit at the given index. 32 | 33 | * **3. 11** 34 | * [3-set_bit.c](./3-set_bit.c): C function that sets the value of a bit at a given index 35 | to `1`. 36 | * If an error occurs - returns `-1`. 37 | * Otherwise - returns `1`. 38 | 39 | * **4. 100** 40 | * [4-clear_bit.c](./4-clear_bit.c): C function that sets the value of a bit at 41 | a given index to `0`. 42 | * If an error occurs - returns `-1`. 43 | * Otherwise - returns `1`. 44 | 45 | * **5. 101** 46 | * [5-flip_bits.c](./5-flip_bits.c): C function that returns the number of bits needed 47 | to be flipped to get from one number to another. 48 | 49 | * **6. Endianness** 50 | * [100-get_endianness.c](./100-get_endianness.c): C function that checks the endianness. 51 | * If big-endian - returns `0`. 52 | * If little-endian - returns `1`. 53 | 54 | * **7. Crackme3** 55 | * [101-password](./101-password): File containing the password for the 56 | [crackme3](https://github.com/holbertonschool/0x13.c) executable. 57 | -------------------------------------------------------------------------------- /0x14-bit_manipulation/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | unsigned int binary_to_uint(const char *b); 5 | void print_binary(unsigned long int n); 6 | int _putchar(char c); 7 | int get_bit(unsigned long int n, unsigned int index); 8 | int set_bit(unsigned long int *n, unsigned int index); 9 | int clear_bit(unsigned long int *n, unsigned int index); 10 | unsigned int flip_bits(unsigned long int n, unsigned long int m); 11 | int get_endianness(void); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /0x15-file_io/0-read_textfile.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * read_textfile - reads a text file and prints the letters 5 | * @filename: filename. 6 | * @letters: numbers of letters printed. 7 | * 8 | * Return: numbers of letters printed. It fails, returns 0. 9 | */ 10 | ssize_t read_textfile(const char *filename, size_t letters) 11 | { 12 | int fd; 13 | ssize_t nread, nwrite; 14 | char *BUFF; 15 | 16 | if (!filename) 17 | return (0); 18 | 19 | fd = open(filename, O_RDONLY); 20 | 21 | if (fd == -1) 22 | return (0); 23 | 24 | BUFF = malloc(sizeof(char) * (letters)); 25 | if (!BUFF) 26 | return (0); 27 | 28 | nread = read(fd, BUFF, letters); 29 | nwrite = write(STDOUT_FILENO, BUFF, nread); 30 | 31 | close(fd); 32 | 33 | free(BUFF); 34 | 35 | return (nwrite); 36 | } 37 | -------------------------------------------------------------------------------- /0x15-file_io/1-create_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * create_file - creates a file 5 | * @filename: filename. 6 | * @text_content: content writed in the file. 7 | * 8 | * Return: 1 if it success. -1 if it fails. 9 | */ 10 | int create_file(const char *filename, char *text_content) 11 | { 12 | int fd; 13 | int lett; 14 | int rwrite; 15 | 16 | if (!filename) 17 | return (-1); 18 | 19 | fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); 20 | 21 | if (fd == -1) 22 | return (-1); 23 | 24 | if (!text_content) 25 | text_content = ""; 26 | 27 | for (lett = 0; text_content[lett]; lett++) 28 | ; 29 | 30 | rwrite = write(fd, text_content, lett); 31 | 32 | if (rwrite == -1) 33 | return (-1); 34 | 35 | close(fd); 36 | 37 | return (1); 38 | } 39 | -------------------------------------------------------------------------------- /0x15-file_io/2-append_text_to_file.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * append_text_to_file - appends text at the end of a file 5 | * @filename: filename. 6 | * @text_content: added content. 7 | * 8 | * Return: 1 if the file exists. -1 if the fails does not exist 9 | * or if it fails. 10 | */ 11 | int append_text_to_file(const char *filename, char *text_content) 12 | { 13 | int fd; 14 | int lett; 15 | int writee; 16 | 17 | if (!filename) 18 | return (-1); 19 | 20 | fd = open(filename, O_WRONLY | O_APPEND); 21 | 22 | if (fd == -1) 23 | return (-1); 24 | 25 | if (text_content) 26 | { 27 | for (lett = 0; text_content[lett]; lett++) 28 | ; 29 | 30 | writee = write(fd, text_content, lett); 31 | 32 | if (writee == -1) 33 | return (-1); 34 | } 35 | 36 | close(fd); 37 | 38 | return (1); 39 | } 40 | -------------------------------------------------------------------------------- /0x15-file_io/3-cp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * error_file - checks if files can be opened. 6 | * @file_from: file_from. 7 | * @file_to: file_to. 8 | * @argv: arguments vector. 9 | * Return: no return. 10 | */ 11 | void error_file(int file_from, int file_to, char *argv[]) 12 | { 13 | if (file_from == -1) 14 | { 15 | dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]); 16 | exit(98); 17 | } 18 | if (file_to == -1) 19 | { 20 | dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]); 21 | exit(99); 22 | } 23 | } 24 | 25 | /** 26 | * main - check the code for Holberton School students. 27 | * @argc: number of arguments. 28 | * @argv: arguments vector. 29 | * Return: Always 0. 30 | */ 31 | int main(int argc, char *argv[]) 32 | { 33 | int file_from, file_to, err_close; 34 | ssize_t chars, nwr; 35 | char buf[1024]; 36 | 37 | if (argc != 3) 38 | { 39 | dprintf(STDERR_FILENO, "%s\n", "Usage: cp file_from file_to"); 40 | exit(97); 41 | } 42 | 43 | file_from = open(argv[1], O_RDONLY); 44 | file_to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0664); 45 | error_file(file_from, file_to, argv); 46 | 47 | chars = 1024; 48 | while (chars == 1024) 49 | { 50 | chars = read(file_from, buf, 1024); 51 | if (chars == -1) 52 | error_file(-1, 0, argv); 53 | nwr = write(file_to, buf, chars); 54 | if (nwr == -1) 55 | error_file(0, -1, argv); 56 | } 57 | 58 | err_close = close(file_from); 59 | if (err_close == -1) 60 | { 61 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 62 | exit(100); 63 | } 64 | 65 | err_close = close(file_to); 66 | if (err_close == -1) 67 | { 68 | dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from); 69 | exit(100); 70 | } 71 | return (0); 72 | } 73 | -------------------------------------------------------------------------------- /0x15-file_io/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | ssize_t read_textfile(const char *filename, size_t letters); 11 | int create_file(const char *filename, char *text_content); 12 | int append_text_to_file(const char *filename, char *text_content); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * print_dlistint - prints all elements of a dlistint_t list. 6 | * @h: head of a doubly linked list 7 | * Return: the number of nodes 8 | */ 9 | 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | const dlistint_t *ptr = h; 13 | size_t n = 0; 14 | 15 | while (ptr && ptr->prev) 16 | ptr = ptr->prev; 17 | while (ptr) 18 | { 19 | printf("%d\n", ptr->n); 20 | n++; 21 | ptr = ptr->next; 22 | } 23 | return (n); 24 | } 25 | -------------------------------------------------------------------------------- /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 | 9 | size_t dlistint_len(const dlistint_t *h) 10 | { 11 | size_t len = 0; 12 | int index = 0; 13 | 14 | for (index = 0; h; index++) 15 | { 16 | len += 1; 17 | h = h->next; 18 | } 19 | return (len); 20 | } 21 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_dnodeint - add new element at beginiing of list 6 | * @head: pointer 7 | * @n: element in list 8 | * Return: new Element 9 | */ 10 | 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | 15 | new = malloc(sizeof(dlistint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | new->prev = NULL; 19 | new->next = *head; 20 | new->n = n; 21 | if (*head != NULL) 22 | (*head)->prev = new; 23 | *head = new; 24 | return (new); 25 | } 26 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * add_dnodeint_end - add element at the end of the list 6 | * @head: list 7 | * @n: element in list 8 | * Return: new element/NULL if failed 9 | */ 10 | 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *_new, *_start; 14 | 15 | _start = *head; 16 | _new = malloc(sizeof(dlistint_t)); 17 | if (_new == NULL) 18 | return (NULL); 19 | if (*head == NULL) 20 | { 21 | _new->next = NULL; 22 | _new->n = n; 23 | _new->prev = NULL; 24 | *head = _new; 25 | return (_new); 26 | } 27 | while (_start->next) 28 | { 29 | _start = _start->next; 30 | } 31 | _start->next = _new; 32 | _new->prev = _start; 33 | _new->next = NULL; 34 | _new->n = n; 35 | return (_new); 36 | } 37 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | #include 3 | 4 | /** 5 | * free_dlistint - free a list 6 | * @head: list 7 | * Return: Void 8 | */ 9 | 10 | void free_dlistint(dlistint_t *head) 11 | { 12 | dlistint_t *start, *check_is; 13 | 14 | start = head; 15 | while (start != NULL) 16 | { 17 | check_is = start->next; 18 | free(start); 19 | start = check_is; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /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 | 10 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 11 | { 12 | unsigned int len; 13 | 14 | for (len = 0; len < index && head->next; len++) 15 | { 16 | head = head->next; 17 | } 18 | if (len < index) 19 | return (NULL); 20 | return (head); 21 | } 22 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - sum of elements in list 5 | * @head: list 6 | * Return: int 7 | */ 8 | 9 | int sum_dlistint(dlistint_t *head) 10 | { 11 | int sum = 0; 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 2 | #include 3 | #include "lists.h" 4 | /** 5 | * create_dnode - creates new node 6 | * @n: data of node 7 | * @prev: link to prev node 8 | * @next: link to next node 9 | * Return: pointer to new node 10 | */ 11 | dlistint_t *create_dnode(int n, dlistint_t *prev, dlistint_t *next) 12 | { 13 | dlistint_t *new; 14 | 15 | new = malloc(sizeof(dlistint_t)); 16 | if (new == NULL) 17 | return (NULL); 18 | new->n = n; 19 | new->prev = prev; 20 | new->next = next; 21 | return (new); 22 | } 23 | /** 24 | * insert_dnodeint_at_index - inserts a new node at a given position 25 | * @h: head of doubly-linked list 26 | * @idx: index for insertion of new node 27 | * @n: data for new node 28 | * Return: address of new node or NULL if error 29 | */ 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 31 | { 32 | dlistint_t *current = *h, *prev = NULL; 33 | unsigned int count = 0; 34 | 35 | if (!h) 36 | return (NULL); 37 | if (idx == 0) /* insert at list beginning*/ 38 | { 39 | if (!*h) 40 | *h = create_dnode(n, NULL, NULL); /*first node*/ 41 | else 42 | { 43 | (*h)->prev = create_dnode(n, NULL, *h); 44 | *h = (*h)->prev; 45 | } 46 | return (*h); 47 | } 48 | for (current = *h; current && (count < idx); current = current->next, count++) 49 | { 50 | prev = current; 51 | } 52 | if ((count == idx) && (current == NULL)) 53 | { 54 | prev->next = create_dnode(n, prev, NULL); 55 | return (prev->next); 56 | } 57 | if ((count < idx) && (current == NULL))/* check if idx too high*/ 58 | return (NULL); 59 | if (prev != NULL) 60 | { /*inserting in middle of list*/ 61 | prev->next = create_dnode(n, prev, current); 62 | current->prev = prev->next; 63 | return (prev->next); 64 | } 65 | return (NULL); /*should never run*/ 66 | } 67 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - function that deletes the node at index of a, 5 | * linked list. 6 | * @head: pointer to pointer to the h of linked list. 7 | * @index: index of the node that should be deleted. Index starts at 0. 8 | * 9 | * Return: if it succeeded, -1 if it failed 10 | */ 11 | 12 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 13 | { 14 | dlistint_t *h1; 15 | dlistint_t *h2; 16 | unsigned int i; 17 | 18 | h1 = *head; 19 | 20 | if (h1 != NULL) 21 | while (h1->prev != NULL) 22 | h1 = h1->prev; 23 | i = 0; 24 | 25 | while (h1 != NULL) 26 | { 27 | if (i == index) 28 | { 29 | if (i == 0) 30 | { 31 | *head = h1->next; 32 | if (*head != NULL) 33 | (*head)->prev = NULL; 34 | } 35 | else 36 | { 37 | h2->next = h1->next; 38 | 39 | if (h1->next != NULL) 40 | h1->next->prev = h2; 41 | } 42 | free(h1); 43 | return (1); 44 | } 45 | h2 = h1; 46 | h1 = h1->next; 47 | i++; 48 | } 49 | return (-1); 50 | } -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x17-doubly_linked_lists 2 | This repo contains ALX codes for C doubly linked lists 3 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef LISTS_H 2 | #define LISTS_H 3 | 4 | #include 5 | 6 | typedef struct dlistint_s 7 | { 8 | int n; 9 | struct dlistint_s *prev; 10 | struct dlistint_s *next; 11 | } dlistint_t; 12 | 13 | size_t print_dlistint(const dlistint_t *h); 14 | size_t dlistint_len(const dlistint_t *h); 15 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 16 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 17 | void free_dlistint(dlistint_t *head); 18 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 19 | int sum_dlistint(dlistint_t *head); 20 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 21 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * main - check the code 7 | * 8 | * Return: Always EXIT_SUCCESS. 9 | */ 10 | int main(void) 11 | { 12 | printf("%d\n", _strlen("My Dyn Lib")); 13 | return (EXIT_SUCCESS); 14 | } -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c -fPIC *.c 3 | gcc -shared *.o -o liball.so 4 | rm *.o 5 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | Dynamic libraries 2 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _abs - compute absolute value of integer 5 | * @n: integer argument 6 | * Return: absolute value 7 | */ 8 | 9 | int _abs(int n) 10 | { 11 | if (n > 0) 12 | return (n); 13 | else if (n < 0) 14 | return (-n); 15 | 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _atoi - convert a string to an integer. 4 | * @s: pointer to string 5 | * 6 | * Return: integer value of string, or 0 if no digits are found 7 | */ 8 | int _atoi(char *s) 9 | { 10 | int sign = 1; 11 | int result = 0; 12 | while (*s) 13 | { 14 | if (*s == '-') 15 | sign = -sign; 16 | else if (*s >= '0' && *s <= '9') 17 | result = result * 10 + (*s - '0'); 18 | else if (result > 0) 19 | break; 20 | s++; 21 | } 22 | return (sign *result); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - checks for alphabet character 5 | * @c: character to check 6 | * Return: 1 if lower or uppercase, 0 if not alphabet character 7 | */ 8 | 9 | int _isalpha(int c) 10 | { 11 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 12 | { 13 | return (1); 14 | } 15 | else 16 | { 17 | return (0); 18 | } 19 | _putchar('\n'); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit 0 through 9 5 | * @c: digit to be checked 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isdigit(int c) 10 | { 11 | if (c >= '0' && c <= '9') 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - find lower caracter 5 | * 6 | * @c: the charactere to check 7 | * 8 | * Return: 1 if true. 9 | **/ 10 | 11 | int _islower(int c) 12 | { 13 | if (c >= 'a' && c <= 'z') 14 | return (1); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: character to be checked 6 | * Return: 1 if true, 0 if false 7 | */ 8 | 9 | int _isupper(int c) 10 | { 11 | if (c >= 'A' && c <= 'Z') 12 | return (1); 13 | 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/liball.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x18-dynamic_libraries/liball.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omazoz/alx-low_level_programming/b7ce895e6bd47ac6646199f46d0e3325b6413a7c/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | #include 5 | int _putchar(char c); 6 | int _islower(int c); 7 | int _isalpha(int c); 8 | int _abs(int n); 9 | int _isupper(int c); 10 | int _isdigit(int c); 11 | int _strlen(char *s); 12 | void _puts(char *s); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | char *_strcat(char *dest, char *src); 16 | char *_strncat(char *dest, char *src, int n); 17 | char *_strncpy(char *dest, char *src, int n); 18 | int _strcmp(char *s1, char *s2); 19 | char *_memset(char *s, char b, unsigned int n); 20 | char *_memcpy(char *dest, char *src, unsigned int n); 21 | char *_strchr(char *s, char c); 22 | unsigned int _strspn(char *s, char *accept); 23 | char *_strpbrk(char *s, char *accept); 24 | char *_strstr(char *haystack, char *needle); 25 | #endif 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | for (i = 0; i < n; i++) 13 | *(dest + i) = *(src + i); 14 | 15 | return (dest); 16 | } 17 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Entry point 5 | * lets print a character 6 | * Return: 1 (SUCCESS) 7 | */ 8 | 9 | int _putchar(char c) 10 | { 11 | return (write(1, &c, 1)); 12 | } 13 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - Prints a string 5 | * @str: The string to print 6 | * 7 | * Return: void 8 | */ 9 | void _puts(char *str) 10 | { 11 | for (; *str != '\0'; str++) 12 | { 13 | _putchar(*str); 14 | } 15 | 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *_strcat - function that concatenates two strings. 4 | *@dest: pointer to destination string. 5 | *@src: pointer to source string. 6 | * 7 | *Return: pointer to destination string. 8 | */ 9 | char *_strcat(char *dest, char *src) 10 | { 11 | int length, i; 12 | 13 | length = 0; 14 | while (dest[length] != '\0') 15 | { 16 | length++; 17 | } 18 | for (i = 0; src[i] != '\0'; i++, length++) 19 | { 20 | dest[length] = src[i]; 21 | } 22 | dest[length] = '\0'; 23 | return (dest); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare two strings 5 | * @s1: one string 6 | * @s2: one string 7 | * Return: int that tells num spaces in between 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0; 13 | 14 | while (*(s1 + i) == *(s2 + i) && *(s1 + i)) 15 | i++; 16 | 17 | if (*(s2 + i)) 18 | return (*(s1 + i)-*(s2 + i)); 19 | else 20 | return (0); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcpy - Copy a string 5 | * @dest: Destination value 6 | * @src: Source value 7 | * 8 | * Return: the pointer to dest 9 | */ 10 | char *_strcpy(char *dest, char *src) 11 | { 12 | int i; 13 | 14 | for (i = 0; src[i] != '\0'; i++) 15 | { 16 | dest[i] = src[i]; 17 | } 18 | 19 | dest[i++] = '\0'; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - Returns the length of a string 5 | * @s: String to count 6 | * 7 | * Return: String length 8 | */ 9 | int _strlen(char *s) 10 | { 11 | int i = 0; 12 | 13 | for (; *s != '\0'; s++) 14 | { 15 | i++; 16 | } 17 | 18 | return (i); 19 | } 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strncat - concatenates n bytes of two strings 4 | * @dest: pointer destination 5 | * @src: pointer source 6 | * @n: number of bytes 7 | * Return: void 8 | */ 9 | 10 | char *_strncat(char *dest, char *src, int n) 11 | { 12 | 13 | int dest_len, i; 14 | 15 | for (dest_len = 0; dest[dest_len] != '\0'; dest_len++) 16 | ; 17 | 18 | for (i = 0; i < n && src[i] != '\0'; i++) 19 | dest[dest_len + i] = src[i]; 20 | 21 | /*should end with a end of string char*/ 22 | dest[dest_len + i] = '\0'; 23 | 24 | return (dest); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * *_strncpy - find the length of a string 4 | * @dest: pointer to the string 5 | * @src: pointer 6 | * @n: int 7 | * Return: destination 8 | */ 9 | 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < n && src[i] != '\0'; i++) 16 | dest[i] = src[i]; 17 | 18 | for ( ; i < n; i++) 19 | dest[i] = '\0'; 20 | 21 | return (dest); 22 | } 23 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @s2: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in s2, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *s2) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(s2 + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(s2 + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s1: initial segment. 6 | * @s2: s2ed bytes. 7 | * Return: the number of s2ed bytes. 8 | */ 9 | unsigned int _strspn(char *s1, char *s2) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s1 + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(s2 + j) != '\0'; j++) 17 | { 18 | if (*(s1 + i) == *(s2 + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * str2 in the string str1. 6 | * @str1: entire string. 7 | * @str2: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *str1, char *str2) 12 | { 13 | char *bstr1; 14 | char *pstr2; 15 | 16 | while (*str1 != '\0') 17 | { 18 | bstr1 = str1; 19 | pstr2 = str2; 20 | 21 | while (*str1 != '\0' && *pstr2 != '\0' && *str1 == *pstr2) 22 | { 23 | str1++; 24 | pstr2++; 25 | } 26 | if (!*pstr2) 27 | return (bstr1); 28 | str1 = bstr1 + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - Creates a hash table. 5 | * @size: The size of the array. 6 | * 7 | * Return: If an error occurs - NULL. 8 | * Otherwise - a pointer to the new hash table. 9 | */ 10 | hash_table_t *hash_table_create(unsigned long int size) 11 | { 12 | hash_table_t *hat; 13 | unsigned long int i; 14 | 15 | hat = malloc(sizeof(hash_table_t)); 16 | if (hat == NULL) 17 | return (NULL); 18 | 19 | hat->size = size; 20 | hat->array = malloc(sizeof(hash_node_t *) * size); 21 | if (hat->array == NULL) 22 | return (NULL); 23 | for (i = 0; i < size; i++) 24 | hat->array[i] = NULL; 25 | 26 | return (hat); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - Hash function implementing the djb2 algorithm. 5 | * @str: The string to hash. 6 | * 7 | * Return: The calculated hash. 8 | */ 9 | unsigned long int hash_djb2(const unsigned char *str) 10 | { 11 | unsigned long int hat; 12 | int c; 13 | 14 | hat = 5381; 15 | while ((c = *str++)) 16 | hat = ((hat << 5) + hat) + c; /* the hash * 33 + c */ 17 | 18 | return (hat); 19 | } 20 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - Get the index at which a key/value 5 | * pair should be stored in array of a hash table. 6 | * @key: The key to get the index of. 7 | * @size: The size of the array of the hash table. 8 | * 9 | * Return: The index of the key. 10 | * Description: Uses the djb2 algorithm. 11 | */ 12 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 13 | { 14 | return (hash_djb2(key) % size); 15 | } 16 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_set - Add or update an element in a hash table. 5 | * @ht: A pointer to the hash table. 6 | * @key: The key to add - cannot be an empty string. 7 | * @value: The value associated with key. 8 | * 9 | * Return: Upon failure - 0. 10 | * Otherwise - 1. 11 | */ 12 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 13 | { 14 | hash_node_t *new; 15 | char *val_copy; 16 | unsigned long int index, i; 17 | 18 | if (ht == NULL || key == NULL || *key == '\0' || value == NULL) 19 | return (0); 20 | 21 | val_copy = strdup(value); 22 | if (val_copy == NULL) 23 | return (0); 24 | 25 | index = key_index((const unsigned char *)key, ht->size); 26 | for (i = index; ht->array[i]; i++) 27 | { 28 | if (strcmp(ht->array[i]->key, key) == 0) 29 | { 30 | free(ht->array[i]->value); 31 | ht->array[i]->value = val_copy; 32 | return (1); 33 | } 34 | } 35 | 36 | new = malloc(sizeof(hash_node_t)); 37 | if (new == NULL) 38 | { 39 | free(val_copy); 40 | return (0); 41 | } 42 | new->key = strdup(key); 43 | if (new->key == NULL) 44 | { 45 | free(new); 46 | return (0); 47 | } 48 | new->value = val_copy; 49 | new->next = ht->array[index]; 50 | ht->array[index] = new; 51 | 52 | return (1); 53 | } 54 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - Retrieve the value associated with 5 | * a key in a hash table. 6 | * @ht: A pointer to the hash table. 7 | * @key: The key to get the value of. 8 | * 9 | * Return: If the key cannot be matched - NULL. 10 | * Otherwise - the value associated with key in ht. 11 | */ 12 | char *hash_table_get(const hash_table_t *ht, const char *key) 13 | { 14 | hash_node_t *node; 15 | unsigned long int i; 16 | 17 | if (ht == NULL || key == NULL || *key == '\0') 18 | return (NULL); 19 | 20 | i = key_index((const unsigned char *)key, ht->size); 21 | if (i >= ht->size) 22 | return (NULL); 23 | 24 | node = ht->array[i]; 25 | while (node && strcmp(node->key, key) != 0) 26 | node = node->next; 27 | 28 | return ((node == NULL) ? NULL : node->value); 29 | } 30 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - Prints a hash table. 5 | * @ht: A pointer to the hash table to print. 6 | * 7 | * Description: Key/value pairs are printed in the order 8 | * they appear in the array of the hash table. 9 | */ 10 | void hash_table_print(const hash_table_t *ht) 11 | { 12 | hash_node_t *node; 13 | unsigned long int i; 14 | unsigned char comma = 0; 15 | 16 | if (ht == NULL) 17 | return; 18 | 19 | printf("{"); 20 | for (i = 0; i < ht->size; i++) 21 | { 22 | if (ht->array[i] != NULL) 23 | { 24 | if (comma == 1) 25 | printf(", "); 26 | 27 | node = ht->array[i]; 28 | while (node != NULL) 29 | { 30 | printf("'%s': '%s'", node->key, node->value); 31 | node = node->next; 32 | if (node != NULL) 33 | printf(", "); 34 | } 35 | comma = 1; 36 | } 37 | } 38 | printf("}\n"); 39 | } 40 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - Deletes a hash table. 5 | * @ht: A pointer to a hash table. 6 | */ 7 | void hash_table_delete(hash_table_t *hat) 8 | { 9 | hash_table_t *head = hat; 10 | hash_node_t *node, *tmp; 11 | unsigned long int i; 12 | 13 | for (i = 0; i < hat->size; i++) 14 | { 15 | if (hat->array[i] != NULL) 16 | { 17 | node = hat->array[i]; 18 | while (node != NULL) 19 | { 20 | tmp = node->next; 21 | free(node->key); 22 | free(node->value); 23 | free(node); 24 | node = tmp; 25 | } 26 | } 27 | } 28 | free(head->array); 29 | free(head); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | ## Hash tables 2 | 3 | ### Learning Objectives 4 | 5 | At the end of the project, you are expected to be able to explain to anyone, without the help of Google: 6 | 7 | #### General 8 | 9 | - What is a hash function 10 | - What makes a good hash function 11 | - What is a hash table, how do they work and how to use them 12 | - What is a collision and what are the main ways of dealing with collisions in the context of a hash table 13 | - What are the advantages and drawbacks of using hash tables 14 | - What are the most common use cases of hash tables 15 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | printf("%p\n", (void *)ht); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | char *s; 14 | unsigned long int hash_table_array_size; 15 | 16 | hash_table_array_size = 1024; 17 | s = "cisfun"; 18 | printf("%lu\n", hash_djb2((unsigned char *)s)); 19 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 20 | s = "Don't forget to tweet today"; 21 | printf("%lu\n", hash_djb2((unsigned char *)s)); 22 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 23 | s = "98"; 24 | printf("%lu\n", hash_djb2((unsigned char *)s)); 25 | printf("%lu\n", key_index((unsigned char *)s, hash_table_array_size)); 26 | return (EXIT_SUCCESS); 27 | } 28 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_set(ht, "betty", "holberton"); 17 | return (EXIT_SUCCESS); 18 | } 19 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | char *value; 15 | 16 | ht = hash_table_create(1024); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_set(ht, "c", "isfun"); 25 | 26 | value = hash_table_get(ht, "python"); 27 | printf("%s:%s\n", "python", value); 28 | value = hash_table_get(ht, "Jennie"); 29 | printf("%s:%s\n", "Jennie", value); 30 | value = hash_table_get(ht, "N"); 31 | printf("%s:%s\n", "N", value); 32 | value = hash_table_get(ht, "Asterix"); 33 | printf("%s:%s\n", "Asterix", value); 34 | value = hash_table_get(ht, "Betty"); 35 | printf("%s:%s\n", "Betty", value); 36 | value = hash_table_get(ht, "98"); 37 | printf("%s:%s\n", "98", value); 38 | value = hash_table_get(ht, "c"); 39 | printf("%s:%s\n", "c", value); 40 | value = hash_table_get(ht, "javascript"); 41 | printf("%s:%s\n", "javascript", value); 42 | return (EXIT_SUCCESS); 43 | } 44 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | 15 | ht = hash_table_create(1024); 16 | hash_table_print(ht); 17 | hash_table_set(ht, "c", "fun"); 18 | hash_table_set(ht, "python", "awesome"); 19 | hash_table_set(ht, "Jennie", "and Jay love asm"); 20 | hash_table_set(ht, "N", "queens"); 21 | hash_table_set(ht, "Asterix", "Obelix"); 22 | hash_table_set(ht, "Betty", "Holberton"); 23 | hash_table_set(ht, "98", "Battery Street"); 24 | hash_table_print(ht); 25 | return (EXIT_SUCCESS); 26 | } 27 | -------------------------------------------------------------------------------- /0x1A-hash_tables/tests/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../hash_tables.h" 5 | 6 | /** 7 | * main - check the code for ALX-Africa Cohort 5 Students. 8 | * 9 | * Return: Always EXIT_SUCCESS. 10 | */ 11 | int main(void) 12 | { 13 | hash_table_t *ht; 14 | char *key; 15 | char *value; 16 | 17 | ht = hash_table_create(1024); 18 | hash_table_set(ht, "c", "fun"); 19 | hash_table_set(ht, "python", "awesome"); 20 | hash_table_set(ht, "Jennie", "and Jay love asm"); 21 | hash_table_set(ht, "N", "queens"); 22 | hash_table_set(ht, "Asterix", "Obelix"); 23 | hash_table_set(ht, "Betty", "Holberton"); 24 | hash_table_set(ht, "98", "Battery Streetz"); 25 | key = strdup("Tim"); 26 | value = strdup("Britton"); 27 | hash_table_set(ht, key, value); 28 | key[0] = '\0'; 29 | value[0] = '\0'; 30 | free(key); 31 | free(value); 32 | hash_table_set(ht, "98", "Battery Street"); 33 | hash_table_set(ht, "hetairas", "Jennie"); 34 | hash_table_set(ht, "hetairas", "Jennie Z"); 35 | hash_table_set(ht, "mentioner", "Jennie"); 36 | hash_table_set(ht, "hetairas", "Jennie Z Chu"); 37 | hash_table_print(ht); 38 | hash_table_delete(ht); 39 | return (EXIT_SUCCESS); 40 | } 41 | -------------------------------------------------------------------------------- /0x1C-makefiles/0-Makefile: -------------------------------------------------------------------------------- 1 | all: main.c school.c 2 | gcc main.c school.c -o school -------------------------------------------------------------------------------- /0x1C-makefiles/1-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | all: $(SRC) 4 | $(CC) $(SRC) -o school 5 | -------------------------------------------------------------------------------- /0x1C-makefiles/100-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | CFLAGS = -Wall -Werror -Wextra -pedantic 6 | 7 | .PHONY: all clean oclean fclean re 8 | 9 | all: m.h $(OBJ) 10 | $(CC) $(OBJ) -o $(NAME) 11 | 12 | clean: 13 | $(RM) *~ $(NAME) 14 | 15 | oclean: 16 | $(RM) $(OBJ) 17 | 18 | fclean: clean oclean 19 | 20 | re: fclean all 21 | -------------------------------------------------------------------------------- /0x1C-makefiles/2-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | 6 | all: $(OBJ) 7 | $(CC) $(OBJ) -o $(NAME) 8 | -------------------------------------------------------------------------------- /0x1C-makefiles/3-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | 7 | all: $(OBJ) 8 | $(CC) $(OBJ) -o $(NAME) 9 | 10 | clean: 11 | $(RM) *~ $(NAME) 12 | 13 | oclean: 14 | $(RM) $(OBJ) 15 | 16 | fclean: clean oclean 17 | 18 | re: fclean all 19 | -------------------------------------------------------------------------------- /0x1C-makefiles/4-Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | SRC = main.c school.c 3 | OBJ = $(SRC:.c=.o) 4 | NAME = school 5 | RM = rm -f 6 | CFLAGS = -Wall -Werror -Wextra -pedantic 7 | 8 | all: $(OBJ) 9 | $(CC) $(OBJ) -o $(NAME) 10 | 11 | clean: 12 | $(RM) *~ $(NAME) 13 | 14 | oclean: 15 | $(RM) $(OBJ) 16 | 17 | fclean: clean oclean 18 | 19 | re: fclean all 20 | -------------------------------------------------------------------------------- /0x1C-makefiles/5-island_perimeter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Defines an island perimeter measuring function.""" 3 | 4 | 5 | def island_perimeter(grid): 6 | """Return the perimiter of an island. 7 | 8 | The grid represents water by 0 and land by 1. 9 | 10 | Args: 11 | grid (list): A list of list of integers representing an island. 12 | Returns: 13 | The perimeter of the island defined in grid. 14 | """ 15 | width = len(grid[0]) 16 | height = len(grid) 17 | edges = 0 18 | size = 0 19 | 20 | for i in range(height): 21 | for j in range(width): 22 | if grid[i][j] == 1: 23 | size += 1 24 | if (j > 0 and grid[i][j - 1] == 1): 25 | edges += 1 26 | if (i > 0 and grid[i - 1][j] == 1): 27 | edges += 1 28 | return size * 4 - edges * 2 29 | -------------------------------------------------------------------------------- /0x1C-makefiles/m.h: -------------------------------------------------------------------------------- 1 | #ifndef __M_H__ 2 | #define __M_H__ 3 | 4 | #include 5 | #include 6 | 7 | void print_school(void); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /0x1C-makefiles/main.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * main - Entry point 5 | * 6 | * Return: Always 7 | */ 8 | int main(void) 9 | { 10 | print_school(); 11 | return (EXIT_SUCCESS); 12 | } 13 | -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_school 5 | * 6 | * Return: Nothing 7 | */ 8 | void print_school(void) 9 | { 10 | printf("j#0000000000000000000000000000000000000\n"); 11 | printf("j#000000000000000000@Q**g00000000000000\n"); 12 | printf("j#0000000000000000*]++]4000000000000000\n"); 13 | printf("j#000000000000000k]++]++*N#000000000000\n"); 14 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 15 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 16 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 17 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 18 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 19 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 20 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 21 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 22 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 23 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 24 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 25 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 26 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 27 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 28 | printf("j#00000000000w]++++]*0##000000000000000\n"); 29 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 30 | printf("j#00000000000000000we]+]Q00000000000000\n"); 31 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 32 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 33 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 34 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-makefiles/tests/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/.gitignore: -------------------------------------------------------------------------------- 1 | main.c 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | /** 3 | * linear_search - function that uses linear search algorithm 4 | * to find a specific value in a data set 5 | * @array: the data set to search in 6 | * @size: size of the array 7 | * @value: the value to be found in the array 8 | * Return: the index of the value found, Otherwise -1 9 | */ 10 | int linear_search(int *array, size_t size, int value) 11 | { 12 | size_t i = 0; 13 | 14 | if (!size || !array) 15 | return (-1); 16 | while (i < size) 17 | { 18 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 19 | if (array[i] == value) 20 | return (i); 21 | i++; 22 | } 23 | return (-1); 24 | } 25 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * print_arr - function to print the content of the array 5 | * @array: the array to print 6 | * @left: the index to start from 7 | * @right: the index of the last item to be print 8 | */ 9 | 10 | void print_arr(int *array, size_t left, size_t right) 11 | { 12 | if (left <= right) 13 | { 14 | printf("Searching in array: %d", array[left]); 15 | left++; 16 | } 17 | while (left <= right) 18 | { 19 | printf(", %d", array[left]); 20 | left++; 21 | } 22 | printf("\n"); 23 | } 24 | /** 25 | * binary_search - Function that uses the binary search algorithm 26 | * to search for a value in a data set 27 | * @array: the data set 28 | * @size: the size of the array 29 | * @value: the value to be searched in the array 30 | * Return: the index of the value found, Otherwise -1 31 | */ 32 | int binary_search(int *array, size_t size, int value) 33 | { 34 | size_t mid, right_, left_; 35 | 36 | if (!size || !array) 37 | return (-1); 38 | left_ = 0; 39 | right_ = size - 1; 40 | while (left_ <= right_) 41 | { 42 | mid = (left_ + right_) / 2; 43 | print_arr(array, left_, right_); 44 | if (array[mid] == value) 45 | return (mid); 46 | else if (array[mid] > value) 47 | right_ = mid - 1; 48 | else 49 | left_ = mid + 1; 50 | } 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /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 2 | 3 | This project is about search algorithms in C. 4 | 5 | ## Files 6 | 7 | - [search_algos.h](./search_algos.h) - Header file with prototypes of functions and data structures used in this project. 8 | 9 | - [0-linear.c](./0-linear.c) - Function that searches for a value in an array of integers using the Linear search algorithm. 10 | 11 | - [1-binary.c](./1-binary.c) - Function that searches for a value in a sorted array of integers using the Binary search algorithm. 12 | 13 | - [2-O](./2-O) - Time complexity (worst case) of a linear search in an array of size n. 14 | 15 | - [3-O](./3-O) - Space complexity (worst case) of an iterative linear search algorithm in an array of size n. 16 | 17 | - [4-O](./4-O) - Time complexity (worst case) of a binary search in an array of size n. 18 | 19 | - [5-O](./5-O) - Space complexity (worst case) of a binary search in an array of size n. 20 | 21 | - [6-O](./6-O) - Space complexity of this function / algorithm: 22 | 23 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef SEARCH_ALGOS_H 2 | #define SEARCH_ALGOS_H 3 | 4 | #include 5 | 6 | int linear_search(int *array, size_t size, int value); 7 | int binary_search(int *array, size_t size, int value); 8 | 9 | #endif 10 | --------------------------------------------------------------------------------