├── 0x00-hello_world ├── 0-preprocessor ├── 1-compiler ├── 100-intel ├── 101-quote.c ├── 2-assembler ├── 3-name ├── 4-puts.c ├── 5-printf.c ├── 6-size.c └── README.md ├── 0x01-variables_if_else_while ├── 0-positive_or_negative.c ├── 1-last_digit.c ├── 100-print_comb3.c ├── 101-print_comb4.c ├── 102-print_comb5.c ├── 2-print_alphabet.c ├── 3-print_alphabets.c ├── 4-print_alphabt.c ├── 5-print_numbers.c ├── 6-print_numberz.c ├── 7-print_tebahpla.c ├── 8-print_base16.c ├── 9-print_comb.c └── README.md ├── 0x02-functions_nested_loops ├── 0-putchar.c ├── 1-alphabet.c ├── 10-add.c ├── 100-times_table.c ├── 101-natural.c ├── 102-fibonacci.c ├── 103-fibonacci.c ├── 104-fibonacci.c ├── 2-print_alphabet_x10.c ├── 3-islower.c ├── 4-isalpha.c ├── 5-sign.c ├── 6-abs.c ├── 7-print_last_digit.c ├── 8-24_hours.c ├── 9-times_table.c ├── README.md └── main.h ├── 0x03-debugging ├── 0-main.c ├── 1-main.c ├── 2-largest_number.c ├── 3-print_remaining_days.c ├── README.md └── main.h ├── 0x04-more_functions_nested_loops ├── 0-isupper.c ├── 1-isdigit.c ├── 10-print_triangle.c ├── 100-prime_factor.c ├── 101-print_number.c ├── 2-mul.c ├── 3-print_numbers.c ├── 4-print_most_numbers.c ├── 5-more_numbers.c ├── 6-print_line.c ├── 7-print_diagonal.c ├── 8-print_square.c ├── 9-fizz_buzz.c ├── README.md └── main.h ├── 0x05-pointers_arrays_strings ├── 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 ├── README.md └── main.h ├── 0x06-pointers_arrays_strings ├── 0-strcat.c ├── 1-strncat.c ├── 100-rot13.c ├── 102-magic.c ├── 103-infinite_add.c ├── 104-print_buffer.c ├── 2-strncpy.c ├── 3-strcmp.c ├── 4-rev_array.c ├── 5-string_toupper.c ├── 6-cap_string.c ├── 7-leet.c ├── README.md └── main.h ├── 0x07-pointers_arrays_strings ├── 0-memset.c ├── 1-memcpy.c ├── 100-set_string.c ├── 101-crackme_password ├── 2-strchr.c ├── 3-strspn.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 7-print_chessboard.c ├── 8-print_diagsums.c ├── README.md └── main.h ├── 0x08-recursion ├── 0-puts_recursion.c ├── 1-print_rev_recursion.c ├── 100-is_palindrome.c ├── 101-wildcmp.c ├── 2-strlen_recursion.c ├── 3-factorial.c ├── 4-pow_recursion.c ├── 5-sqrt_recursion.c ├── 6-is_prime_number.c ├── README.md └── main.h ├── 0x09-static_libraries ├── 0-isupper.c ├── 0-isupper.o ├── 0-memset.c ├── 0-memset.o ├── 0-strcat.c ├── 0-strcat.o ├── 1-isdigit.c ├── 1-isdigit.o ├── 1-memcpy.c ├── 1-memcpy.o ├── 1-strncat.c ├── 1-strncat.o ├── 100-atoi.c ├── 100-atoi.o ├── 2-strchr.c ├── 2-strchr.o ├── 2-strlen.c ├── 2-strlen.o ├── 2-strncpy.c ├── 2-strncpy.o ├── 3-islower.c ├── 3-islower.o ├── 3-puts.c ├── 3-puts.o ├── 3-strcmp.c ├── 3-strcmp.o ├── 3-strspn.c ├── 3-strspn.o ├── 4-isalpha.c ├── 4-isalpha.o ├── 4-strpbrk.c ├── 4-strpbrk.o ├── 5-strstr.c ├── 5-strstr.o ├── 6-abs.c ├── 6-abs.o ├── 9-strcpy.c ├── 9-strcpy.o ├── README.md ├── _putchar.c ├── _putchar.o ├── create_static_lib.sh ├── libmy.a ├── main.c ├── main.h └── quote ├── 0x0A-argc_argv ├── 0-whatsmyname.c ├── 1-args.c ├── 100-change.c ├── 2-args.c ├── 3-mul.c ├── 4-add.c └── README.md ├── 0x0B-malloc_free ├── .100-argstostr.c.swp ├── 0-create_array.c ├── 1-strdup.c ├── 100-argstostr.c ├── 101-strtow.c ├── 2-str_concat.c ├── 3-alloc_grid.c ├── 4-free_grid.c ├── README.md └── main.h ├── 0x0C-more_malloc_free ├── 0-malloc_checked.c ├── 1-string_nconcat.c ├── 100-realloc.c ├── 101-mul.c ├── 2-calloc.c ├── 3-array_range.c ├── README.md └── main.h ├── 0x0D-preprocessor ├── 0-object_like_macro.h ├── 1-pi.h ├── 2-main.c ├── 3-function_like_macro.h ├── 4-sum.h └── README.md ├── 0x0E-structures_typedef ├── 1-init_dog.c ├── 2-print_dog.c ├── 4-new_dog.c ├── 5-free_dog.c ├── README.md └── dog.h ├── 0x12-singly_linked_lists ├── .gitignore ├── 0-print_list.c ├── 1-list_len.c ├── 100-first.c ├── 101-hello_holberton.asm ├── 2-add_node.c ├── 3-add_node_end.c ├── 4-free_list.c ├── README.md └── lists.h ├── 0x17-doubly_linked_lists ├── 0-print_dlistint.c ├── 1-dlistint_len.c ├── 100-password ├── 102-result ├── 103-keygen.c ├── 2-add_dnodeint.c ├── 3-add_dnodeint_end.c ├── 4-free_dlistint.c ├── 5-get_dnodeint.c ├── 6-sum_dlistint.c ├── 7-insert_dnodeint.c ├── 8-delete_dnodeint.c ├── README.md └── lists.h ├── 0x18-dynamic_libraries ├── 0-isupper.c ├── 0-main.c ├── 0-memset.c ├── 0-strcat.c ├── 1-create_dynamic_lib.sh ├── 1-isdigit.c ├── 1-main.c ├── 1-memcpy.c ├── 1-strncat.c ├── 100-atoi.c ├── 101-make_me_win.sh ├── 2-strchr.c ├── 2-strlen.c ├── 2-strncpy.c ├── 3-islower.c ├── 3-puts.c ├── 3-strcmp.c ├── 3-strspn.c ├── 4-isalpha.c ├── 4-strpbrk.c ├── 5-strstr.c ├── 6-abs.c ├── 9-strcpy.c ├── README.md ├── _putchar.c ├── libdynamic.so └── main.h ├── 0x1A-hash_tables ├── 0-hash_table_create.c ├── 1-djb2.c ├── 100-sorted_hash_table.c ├── 2-key_index.c ├── 3-hash_table_set.c ├── 4-hash_table_get.c ├── 5-hash_table_print.c ├── 6-hash_table_delete.c ├── README.md └── hash_tables.h ├── 0x1C-makefiles ├── 0-Makefile ├── 1-Makefile ├── 100-Makefile ├── 2-Makefile ├── 3-Makefile ├── 4-Makefile ├── 5-island_perimeter.py ├── README.md ├── a.out ├── images │ └── makefile.gif ├── m.h ├── main.c ├── makefile.gif ├── school.c └── tests │ └── 5-main.py ├── 0x1E-search_algorithms ├── 0-linear.c ├── 1-binary.c ├── 1-binary_recursive.c ├── 100-jump.c ├── 101-O ├── 102-interpolation.c ├── 103-exponential.c ├── 104-advanced_binary.c ├── 105-jump_list.c ├── 106-linear_skip.c ├── 107-O ├── 108-O ├── 2-O ├── 3-O ├── 4-O ├── 5-O ├── 6-O ├── README.md ├── listint │ ├── create_list.c │ ├── free_list.c │ └── print_list.c ├── search_algos.h ├── skiplist │ ├── create_skiplist.c │ ├── free_skiplist.c │ └── print_skiplist.c └── tests │ ├── 0-main.c │ ├── 1-main.c │ ├── 100-main.c │ ├── 102-main.c │ ├── 103-main.c │ ├── 104-main.c │ ├── 105-main.c │ └── 106-main.c ├── README.md └── references ├── An Introduction to the C Programming Language and Software Design.pdf ├── DynamicMemoryAllocation.pdf ├── EverythingYouNeedToKnowToStartwithC.pdf ├── Learn-c-the-hard-way.pdf ├── ModernC.pdf ├── Programming_in_C_4th_Edition_by_Stephen_Kochan.pdf ├── Recursion.pdf ├── Structures.pdf ├── structs.pdf └── typedef.pdf /0x00-hello_world/0-preprocessor: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -E $CFILE -o c 3 | -------------------------------------------------------------------------------- /0x00-hello_world/1-compiler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -c $CFILE\ 3 | -------------------------------------------------------------------------------- /0x00-hello_world/100-intel: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S -masm=intel $CFILE\ 3 | -------------------------------------------------------------------------------- /0x00-hello_world/101-quote.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | *main - Prints a quote 6 | * 7 | *Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | write(2, "and that piece of art is useful\" - Dora Korpar, 2015-10-19\n", 59); 12 | return (1); 13 | } 14 | -------------------------------------------------------------------------------- /0x00-hello_world/2-assembler: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -S $CFILE\ 3 | -------------------------------------------------------------------------------- /0x00-hello_world/3-name: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc $CFILE -o cisfun\ 3 | -------------------------------------------------------------------------------- /0x00-hello_world/4-puts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - Entry point 5 | * 6 | *Return: Always 0 (Success) 7 | */ 8 | int main(void) 9 | { 10 | puts("\"Programming is like building a multilingual puzzle"); 11 | return (0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /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 | 14 | -------------------------------------------------------------------------------- /0x00-hello_world/6-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | *main - prints the size of various types 5 | *Return: 0 6 | */ 7 | int main(void) 8 | { 9 | printf("Size of a char: %d byte(s)\n", sizeof(char)); 10 | printf("Size of an int: %d byte(s)\n", sizeof(int)); 11 | printf("Size of a long int: %d byte(s)\n", sizeof(long int)); 12 | printf("Size of a long long int: %d byte(s)\n", sizeof(long long int)); 13 | printf("Size of a float: %d byte(s)\n", sizeof(float)); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x00-hello_world/README.md: -------------------------------------------------------------------------------- 1 | C project task 2 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/0-positive_or_negative.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - assign a random number to int n 6 | * each time it executes and prints it 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | int n; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | if (n > 0) 17 | printf("%d is positive\n", n); 18 | else if (n == 0) 19 | printf("%d is zero\n", n); 20 | else 21 | printf("%d is negative\n", n); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/1-last_digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /** 5 | * main - main block 6 | * Description: Print statements based on the last digit of the random number. 7 | * Return: 0 8 | */ 9 | int main(void) 10 | { 11 | int n; 12 | int m; 13 | 14 | srand(time(0)); 15 | n = rand() - RAND_MAX / 2; 16 | m = n % 10; 17 | 18 | if (m > 5) 19 | printf("Last digit of %i is %i and is greater than 5\n", 20 | n, m); 21 | else if (m == 0) 22 | printf("Last digit of %i is %i and is 0\n", n, m); 23 | else 24 | printf("Last digit of %i is %i and is less than 6 and not 0\n", 25 | n, m); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/100-print_comb3.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible combinations of two digits. 5 | * Numbers must be separated by commas and a space. 6 | * 01 and 10 are considered as the same combination of the two digits. 7 | * Print only the smallest combination of two digits. 8 | * Numbers should be printed in ascending order, with two digits. 9 | * You can only use `putchar`. 10 | * You can only use `putchar` up to 5 times. 11 | * You are not allowed to use any variable of type `char`. 12 | * Return: 0 13 | */ 14 | int main(void) 15 | { 16 | int i, j, k; 17 | 18 | i = 0; 19 | 20 | while (i < 100) 21 | { 22 | j = i % 10; /* singles digit */ 23 | k = i / 10; /* doubles digit */ 24 | 25 | if (k < j) 26 | { 27 | putchar(k + '0'); 28 | putchar(j + '0'); 29 | 30 | if (i < 89) 31 | { 32 | putchar(44); 33 | putchar(32); 34 | } 35 | } 36 | 37 | i++; 38 | } 39 | putchar('\n'); 40 | 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/101-print_comb4.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible different combinations of 3 digits. 5 | * Numbers must be separated by commas and a space. 6 | * The 3 digits must be different. 7 | * 012, 120, 102, 021, 201, 210 are considered the same combination. 8 | * print only the smallest combination of 3 digits. 9 | * Numbers should be printed in ascending order. 10 | * You can only use `putchar` to print to console. 11 | * You can only use `putchar` up to 6 times. 12 | * You are not allowed to use any variables of type `char`. 13 | * Return: 0 14 | */ 15 | int main(void) 16 | { 17 | int i, j, k, l; 18 | 19 | for (i = 0; i < 1000; i++) 20 | { 21 | j = i / 100; /* hundreds */ 22 | k = (i / 10) % 10; /* tens */ 23 | l = i % 10; /* singles */ 24 | 25 | if (j < k && k < l) 26 | { 27 | putchar(j + '0'); 28 | putchar(k + '0'); 29 | putchar(l + '0'); 30 | 31 | if (i < 700) 32 | { 33 | putchar(44); 34 | putchar(32); 35 | } 36 | } 37 | } 38 | putchar('\n'); 39 | 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/102-print_comb5.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible combinations of two two-digit numbers. 5 | * Numbers should range from 0 to 99. 6 | * The two numbers should be separated by a space. 7 | * All numbers should be printed with two digits. 1 should be printed as 01. 8 | * Combination of numbers must be separated by a comma followed by a space. 9 | * Combinations of numbers should be printed in ascending order. 10 | * `00 01` and `01 00` are considered as the same combination. 11 | * You can only use `putchar` to print to console. 12 | * You can only use `putchar` up to 8 times. 13 | * You are not allowed to use any variable of type `char`. 14 | * Return: 0 15 | */ 16 | int main(void) 17 | { 18 | int i, j; 19 | int a, b, c, d; 20 | 21 | for (i = 0; i < 100; i++) 22 | { 23 | a = i / 10; /* doubles fnum */ 24 | b = i % 10; /* singles fnum */ 25 | 26 | for (j = 0; j < 100; j++) 27 | { 28 | c = j / 10; /* doubles snum */ 29 | d = j % 10; /* singles snum */ 30 | 31 | if (a < c || (a == c && b < d)) 32 | { 33 | putchar(a + '0'); 34 | putchar(b + '0'); 35 | putchar(32); 36 | putchar(c + '0'); 37 | putchar(d + '0'); 38 | 39 | if (!(a == 9 && b == 8)) 40 | { 41 | putchar(44); 42 | putchar(32); 43 | } 44 | } 45 | } 46 | } 47 | putchar(10); 48 | 49 | return (0); 50 | } 51 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/2-print_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Use `putchar` function to print the alphabet in lowercase. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c = 'a'; 10 | 11 | while (c <= 'z') 12 | { 13 | putchar(c); 14 | c++; 15 | } 16 | putchar('\n'); 17 | 18 | return (0); 19 | } 20 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/3-print_alphabets.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Use `putchar` to print lowercase and then uppercase alphabet. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c = 'a'; 10 | 11 | while (c <= 'z') 12 | { 13 | putchar(c); 14 | c++; 15 | } 16 | 17 | c = 'A'; 18 | 19 | while (c <= 'Z') 20 | { 21 | putchar(c); 22 | c++; 23 | } 24 | 25 | putchar('\n'); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/4-print_alphabt.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Use `putchar` to print all letters but the letter 'q' and 'e'. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c = 'a'; 10 | 11 | while (c <= 'z') 12 | { 13 | if (c != 'q' && c != 'e') 14 | putchar(c); 15 | c++; 16 | } 17 | putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/5-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all numbers of base 10, starting from 0. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | int i; 10 | 11 | for (i = 0; i < 10; i++) 12 | { 13 | printf("%i", i); 14 | } 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/6-print_numberz.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all numbers of base 10, starting from 0. 5 | * You are not allowed to use any variable of type char. 6 | * You can only use `putchar` to print to console. 7 | * You can only use `putchar` twice. 8 | * Return: 0 9 | */ 10 | int main(void) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < 10; i++) 15 | { 16 | putchar(i + '0'); 17 | } 18 | putchar('\n'); 19 | 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/7-print_tebahpla.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print the alphabet in reverse order in lowercase. 5 | * Return: 0 6 | */ 7 | int main(void) 8 | { 9 | char c; 10 | 11 | for (c = 'z'; c >= 'a'; c--) 12 | { 13 | putchar(c); 14 | } 15 | putchar('\n'); 16 | 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/8-print_base16.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all numbers of base64 in lowercase. 5 | * You can only use `putchar`, and only 3 times. 6 | * Return: 0 7 | */ 8 | int main(void) 9 | { 10 | char c; 11 | int i; 12 | 13 | for (i = 0; i < 10; i++) 14 | { 15 | putchar(i + '0'); 16 | } 17 | for (c = 'a'; c < 'g'; c++) 18 | { 19 | putchar(c); 20 | } 21 | putchar('\n'); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/9-print_comb.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - main block 4 | * Description: Print all possible combinations fo single-digit numbers. 5 | * Numbers must be separated by commas and a space. 6 | * You can only use `putchar` to print to console 7 | * You can only use `putchar` up to four times. 8 | * You are not allowed to use any variable of type `char`. 9 | * Return: 0 10 | */ 11 | int main(void) 12 | { 13 | int i = 0; 14 | 15 | while (i < 10) 16 | { 17 | putchar(i + '0'); 18 | if (i < 9) 19 | { 20 | putchar(44); 21 | putchar(32); 22 | } 23 | i++; 24 | } 25 | putchar('\n'); 26 | 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /0x01-variables_if_else_while/README.md: -------------------------------------------------------------------------------- 1 | # C - Variables, if, else, while 2 | 3 | In this project, I learned about using arithmetic, logical, relational, and boolean 4 | operators in C. Additionally, I practiced declaring and defining variables of type 5 | `char`, `int`, and `unsigned int`, using `if` and `if...else` statements, and 6 | implementing `while` loops. 7 | 8 | ## Tasks :page_with_curl: 9 | 10 | * **0. Positive anything is better than negative nothing** 11 | * [0-positive_or_negative.c](./0-positive_or_negative.c): C program that prints whether 12 | a randomly generated number is positive or negative, followed by a new line: 13 | * Completion of [this source code](https://github.com/holbertonschool/0x01.c/blob/master/0-positive_or_negative_c). 14 | * Stores a different random number every time the program is run. 15 | * Prints the number followed by `is positive` if the number is greater than 0, `is zero` if the number is zero, or `is negative` if the number is less than 0. 16 | 17 | * **1. The last digit** 18 | * [1-last_digit.c](./1-last_digit.c): C program that prints the last digit of a 19 | randomly generated number, followed by a new line: 20 | * Completion of [this source code](https://github.com/holbertonschool/0x01.c/blob/master/1-last_digit_c). 21 | * Stores a different value every time the program is run. 22 | * Prints the string `Last digit of` [number] `is` [last_digit] `and is` followed 23 | by `greater than 5` if the digit is greater than 5, `0` if the digit is 0, or 24 | `less than 6 and not 0` if the digit is less than 6 and not 0. 25 | 26 | * **2. I sometimes suffer from insomnia. And when I can't fall asleep, I play what I call the alphabet game** 27 | * [2-print_alphabet.c](./2-print_alphabet.c): C program that prints the alphabet in 28 | lowercase, followed by a new line, using only the `putchar` function exactly twice. 29 | 30 | * **3. When I was having that alphabet soup, I never thought that it would pay off** 31 | * [3-print_alphabets.c](./3-print_alphabets.c): C program that prints the alphabet in 32 | lowercase, then in uppercase, followed by a new line, using only the `putchar` 33 | function exactly three times. 34 | 35 | * **4. alphABET** 36 | * [4-print_alphabt.c](./4-print_alphabt.c): C program that prints the alphabet in lowercase 37 | except for the letters `q` and `e`, followed by a new line, using only the `putchar` 38 | function exactly twice. 39 | 40 | * **5. Numbers** 41 | * [5-print_numbers.c](./5-print_numbers.c): C program that prints all single digit numbers 42 | of base 10 starting from `0`, followed by a new line. 43 | 44 | * **6. Numberz** 45 | * [6-print_numberz.c](./6-print_numberz.c): C program that prints all single digit numbers 46 | of base 10 starting from `0`, followed by a new line, using only the `putchar` function 47 | exactly twice without any variables of type `char`. 48 | 49 | * **7. Smile in the mirror** 50 | * [7-print_tebahpla.c](./7-print_tebahpla.c): C program that prints the lowercase alphabet 51 | in reverse, followed by a new line, using only the `putchar` function exactly twice. 52 | 53 | * **8. Hexadecimal** 54 | * [8-print_base16.c](./8-print_base16.c): C program that prints all the numbers of base 55 | 16 in lowercase, followed by a new line, using only the `putchar` function exactly three 56 | times. 57 | 58 | * **9. Patience, persistence and perspiration make an unbeatable combination for success** 59 | * [9-print_comb.c](./9-print_comb.c): C program that prints all possible combinations of 60 | single-digit numbers, using only the `putchar` function exactly four times without any 61 | variables of type `char`: 62 | * Numbers are separated by `,`, followed by a space. 63 | * Numbers are printed in ascending order. 64 | 65 | * **10. 00...99** 66 | * [10-print_comb2.c](./10-print_comb2.c): C program that prints the numbers from `00` 67 | to `99` using only the `putchar` function exactly five times without any variables of 68 | type `char`: 69 | * Numbers are separated by `,`, followed by a space. 70 | * Numbers are printed in ascending order, with two digits. 71 | 72 | * **11. Inventing is a combination of brains and materials. The more brains you use, the less material you need** 73 | * [100-print_comb3.c](./100-print_comb3.c): C program that prints all possible different 74 | combinations of two digits using only the `putchar` function exactly five times without any 75 | variables of type `char`: 76 | * Numbers are separated by `,`, followed by a space. 77 | * The two digits are different. 78 | * `01` and `10` are considered the same combination of the two digits `0` and `1`. 79 | * Prints only the smallest combination of two digits. 80 | * Numbers are printed in ascending order. 81 | 82 | * **12. The success combination in business is: Do what you do better... and: do more of what you do...** 83 | * [101-print_comb4.c](./101-print_comb4.c): C program that prints all possible different 84 | combinations of three digits using only the `putchar` function exactly six times without 85 | any variables of type `char`: 86 | * Numbers are separated by `,`, followed by a space. 87 | * The three digits are different. 88 | * `012`, `120`, `102`, `021`, `201` and `210` are considered the same combination of the three digits `0`, `1` and `2`. 89 | * Prints only the smallest combination of three digits. 90 | * Numbers are printed in ascending order. 91 | 92 | * **13. Software is eating the World** 93 | * [102-print_comb5.c](./102-print_comb5.c): C program that prints all possible combinations 94 | of two-digit numbers using only the `putchar` function exactly eight times without any 95 | variables of type `char`: 96 | * Numbers range from `0` to `99`. 97 | * Two numbers are separated by a space. 98 | * Numbers are printed with two digits [ie. `1` is printed as `01`]. 99 | * `00 01` and `01 00` are considered the same as the combination of of the numbers `0` and `1`. 100 | * Combinations of numbers are separated by `,`, followed by a space. 101 | * Combinations of numbers are printed in ascending order. 102 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/0-putchar.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | *main - Entry point 4 | * 5 | *Return: nothing, return void 6 | */ 7 | int main(void) 8 | { 9 | _putchar('_'); 10 | _putchar('p'); 11 | _putchar('u'); 12 | _putchar('t'); 13 | _putchar('c'); 14 | _putchar('h'); 15 | _putchar('a'); 16 | _putchar('r'); 17 | _putchar('\n'); 18 | 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/1-alphabet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | *print_alphabet - print alphabet lowercase 5 | * 6 | *Return: Always 0 7 | */ 8 | 9 | void print_alphabet(void) 10 | { 11 | char alphabet; 12 | 13 | for (alphabet = 'a'; alphabet <= 'z'; alphabet++) 14 | { 15 | _putchar(alphabet); 16 | } 17 | 18 | _putchar('\n'); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/10-add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * add -prints add two numbers 6 | * @i: print int i 7 | * @k: print int k 8 | * Return: Always 0. 9 | */ 10 | 11 | int add(int i, int k) 12 | { 13 | return (i + k); 14 | } 15 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/100-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_times_table - Prints the times table of the input, 5 | * starting with 0 6 | * @n: The value of the times table to be printed. 7 | */ 8 | void print_times_table(int n) 9 | { 10 | int num, mult, prod; 11 | 12 | if (n >= 0 && n <= 15) 13 | { 14 | for (num = 0; num <= n; num++) 15 | { 16 | _putchar('0'); 17 | for (mult = 1; mult <= n; mult++) { 18 | _putchar(','); 19 | _putchar(' '); 20 | prod = num * mult; 21 | if (prod <= 99) _putchar(' '); 22 | if (prod <= 9) _putchar(' '); 23 | if (prod >= 100) 24 | { 25 | _putchar((prod / 100) + '0'); 26 | _putchar(((prod / 10)) % 10 + '0'); 27 | } 28 | else if (prod <= 99 && prod >= 10) 29 | { 30 | _putchar((prod / 10) + '0'); 31 | } 32 | _putchar((prod % 10) + '0'); 33 | } 34 | _putchar('\n'); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/101-natural.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - check for multiples of 3 and 5 4 | * 5 | * Return: 0 always 6 | */ 7 | int main(void) 8 | { 9 | int x = 1024, y, sum = 0; 10 | 11 | for (y = 0; y < x; y++) 12 | { 13 | if ((y % 3 == 0) || (y % 5 == 0)) 14 | { 15 | sum = sum + y; 16 | } 17 | } 18 | printf("%d\n", sum); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/102-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print first 50 fibonacci 4 | * 5 | * Return: 0 always. 6 | */ 7 | int main(void) 8 | { 9 | long int i, x = 1, y = 2, sum = 0; 10 | 11 | for (i = 0; i < 49; i++) 12 | { 13 | printf("%ld, ", x); 14 | sum = x + y; 15 | x = y; 16 | y = sum; 17 | if (i == 48) 18 | printf("%ld\n", x); 19 | } 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/103-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | /** 3 | * main - print first 50 fibonacci 4 | * 5 | * Return: 0 always. 6 | */ 7 | int main(void) 8 | { 9 | long int i, x = 1, y = 2, sum = 0, tSum = 0; 10 | 11 | for (i = 0; i < 49; i++) 12 | { 13 | if ((y % 2 == 0) && (y <= 4000000)) 14 | { 15 | tSum = tSum + y; 16 | } 17 | sum = x + y; 18 | x = y; 19 | y = sum; 20 | 21 | } 22 | printf("%ld\n", tSum); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/104-fibonacci.c: -------------------------------------------------------------------------------- 1 | 42 lines (35 sloc) 642 Bytes 2 | 3 | #include 4 | /** 5 | * main - finds and prints the first 98 Fibonacci numbers, 6 | * starting with 1 and 2 7 | * followed by a new line 8 | * Return: ALways 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | unsigned long int i, j, k, j1, j2, k1, k2; 13 | 14 | j = 1; 15 | k = 2; 16 | 17 | printf("%lu", j); 18 | 19 | for (i = 1; i < 91; i++) 20 | { 21 | printf(", %lu", k); 22 | k = k + j; 23 | j = k - j; 24 | } 25 | 26 | j1 = j / 1000000000; 27 | j2 = j % 1000000000; 28 | k1 = k / 1000000000; 29 | k2 = k % 1000000000; 30 | 31 | for (i = 92; i < 99; ++i) 32 | { 33 | printf(", %lu", k1 + (k2 / 1000000000)); 34 | printf("%lu", k2 % 1000000000); 35 | k1 = k1 + j1; 36 | j1 = k1 - j1; 37 | k2 = k2 + j2; 38 | j2 = k2 - j2; 39 | } 40 | 41 | printf("\n"); 42 | return(0); 43 | } 44 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/2-print_alphabet_x10.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_alphabet_x10 - print alphabet 5 | * 6 | * Return: Always 0. 7 | */ 8 | void print_alphabet_x10(void) 9 | { 10 | int alphabet; 11 | int count; 12 | 13 | count = 0; 14 | while (count < 10) 15 | { 16 | for (alphabet = 'a' ; alphabet <= 'z'; alphabet++) 17 | { 18 | _putchar(alphabet); 19 | } 20 | 21 | count++; 22 | _putchar('\n'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - Short description, single line 5 | * @c: contains value to be compared 6 | * Return: Always 0. 7 | */ 8 | int _islower(int c) 9 | 10 | { 11 | 12 | if (c > 'a' && c < 'z') 13 | { 14 | return (1); 15 | } 16 | 17 | else 18 | { 19 | return (0); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isalpha - Return 1 if c is a letter. lowercase or uppercase 5 | * 6 | * @c: The int to print 7 | * Return: Always 0. 8 | */ 9 | 10 | int _isalpha(int c) 11 | 12 | { 13 | 14 | if ((c > 'a' && c < 'z') || (c > 'A' && c < 'Z')) 15 | { 16 | return (1); 17 | } 18 | 19 | else 20 | { 21 | return (0); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/5-sign.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_sign - return 0 letter not lowercase, 1 letter lowercase 5 | * 6 | * @n: the int to print 7 | * Return: Always 0. 8 | */ 9 | int print_sign(int n) 10 | { 11 | if (n > 0) 12 | { 13 | _putchar ('+'); 14 | return (1); 15 | } 16 | 17 | else if (n == 0) 18 | { 19 | _putchar ('0'); 20 | return (0); 21 | } 22 | 23 | else 24 | { 25 | _putchar ('-'); 26 | return (-1); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _abs - value absolute 6 | * 7 | * @n: The int to print 8 | * Return: Always 0. 9 | */ 10 | 11 | int _abs(int n) 12 | { 13 | 14 | if (n < 0) 15 | { 16 | return (n * (-1)); 17 | } 18 | 19 | else if (n == 0) 20 | { 21 | return (0); 22 | } 23 | 24 | else 25 | { 26 | return (n); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/7-print_last_digit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_last_digit - last digit 5 | * 6 | * @n: The int to print 7 | * Return: Always 0. 8 | */ 9 | 10 | int print_last_digit(int n) 11 | 12 | { 13 | int last_digit; 14 | 15 | if (n < 0) 16 | { 17 | last_digit = (-1 * (n % 10)); 18 | _putchar (last_digit + '0'); 19 | return (last_digit); 20 | } 21 | 22 | else 23 | { 24 | last_digit = (n % 10); 25 | _putchar (last_digit + '0'); 26 | return (last_digit); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/8-24_hours.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * jack_bauer -prints every minute of the day 5 | * 6 | * Return: Always 0. 7 | */ 8 | 9 | void jack_bauer(void) 10 | 11 | { 12 | int a; 13 | int b; 14 | 15 | for (a = 0; a <= 23; a++) 16 | { 17 | for (b = 0; b <= 59; b++) 18 | { 19 | _putchar (a / 10 + '0'); 20 | _putchar (a % 10 + '0'); 21 | _putchar (':'); 22 | _putchar (b / 10 + '0'); 23 | _putchar (b % 10 + '0'); 24 | _putchar ('\n'); 25 | 26 | } 27 | 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/9-times_table.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * times_table -prints tables 4 | * 5 | * Return: Always 0. 6 | */ 7 | void times_table(void) 8 | { 9 | int a; 10 | int b; 11 | int c; 12 | for (a = 0; a <= 9; a++) 13 | { 14 | for (b = 0; b <= 9; b++) 15 | { 16 | c = a * b; 17 | if ((c / 10) == 0) 18 | { 19 | if (b == 0) 20 | { 21 | _putchar ('0'); 22 | } 23 | if (b != 0) 24 | { 25 | _putchar (' '); 26 | _putchar ((c % 10) + '0'); 27 | } 28 | if (b < 9) 29 | { 30 | _putchar(','); 31 | _putchar (' '); 32 | } 33 | } 34 | else 35 | { 36 | _putchar ((c / 10) + '0'); 37 | _putchar ((c % 10) + '0'); 38 | if (b < 9) 39 | { 40 | _putchar(','); 41 | _putchar (' '); 42 | } 43 | } 44 | } 45 | _putchar ('\n'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | my readme for 0x02-functions nestedd loop 2 | -------------------------------------------------------------------------------- /0x02-functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void print_alphabet(void); 3 | void print_alphabet_x10(void); 4 | int _islower(int c); 5 | int _isalpha(int c); 6 | int print_sign(int n); 7 | int _abs(int); 8 | int print_last_digit(int); 9 | void jack_bauer(void); 10 | void times_table(void); 11 | int add(int, int); 12 | void print_to_98(int n); 13 | -------------------------------------------------------------------------------- /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 = 98; 13 | positive_or_negative(i); 14 | 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x03-debugging/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - causes an infinite loop 5 | * Return: 0 6 | */ 7 | 8 | int main(void) 9 | { 10 | int i; 11 | 12 | printf("Infinite loop incoming :(\n"); 13 | 14 | i = 0; 15 | 16 | /**while (i < 10) 17 | *{ 18 | * putchar(i); 19 | }*/ 20 | 21 | printf("Infinite loop avoided! \\o/\n"); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x03-debugging/2-largest_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * largest_number - returns the largest of 3 numbers 5 | * @a: first integer 6 | * @b: second integer 7 | * @c: third integer 8 | * Return: largest number 9 | */ 10 | 11 | int largest_number(int a, int b, int c) 12 | { 13 | int largest; 14 | 15 | if (a >= b && b >= c) 16 | { 17 | largest = a; 18 | } 19 | else if (b >= a && a >= c) 20 | { 21 | largest = b; 22 | } 23 | else 24 | { 25 | largest = c; 26 | } 27 | return (largest); 28 | } 29 | -------------------------------------------------------------------------------- /0x03-debugging/3-print_remaining_days.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "main.h" 3 | 4 | /** 5 | * print_remaining_days - takes a date and prints how many days are 6 | * left in the year, taking leap years into account 7 | * @month: month in number format 8 | * @day: day of month 9 | * @year: year 10 | * Return: void 11 | **/ 12 | 13 | void print_remaining_days(int month, int day, int year) 14 | { 15 | if ((year % 4 == 0 || year % 400 == 0) && !(year % 100 == 0)) 16 | { 17 | if (month >= 2 && day >= 60) 18 | { 19 | day++; 20 | } 21 | 22 | printf("Day of the year: %d\n", day); 23 | printf("Remaining days: %d\n", 366 - day); 24 | } 25 | else 26 | { 27 | if (month == 2 && day == 60) 28 | { 29 | printf("Invalid date: %02d/%02d/%04d\n", month, day - 31, year); 30 | } 31 | else 32 | { 33 | printf("Day of the year: %d\n", day); 34 | printf("Remaining days: %d\n", 365 - day); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /0x03-debugging/README.md: -------------------------------------------------------------------------------- 1 | debugging task 2 | -------------------------------------------------------------------------------- /0x03-debugging/main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | #include 5 | 6 | void positive_or_negative(int i); 7 | int largest_number(int a, int b, int c); 8 | int convert_day(int month, int day); 9 | void print_remaining_days(int month, int day, int year); 10 | 11 | #endif /* MAIN_H */ 12 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/0-isupper.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 0-isupper.c 3 | */ 4 | 5 | #include "main.h" 6 | 7 | /** 8 | * _isupper - Checks for uppercase characters. 9 | * @c: The character to be checked. 10 | * 11 | * Return: 1 if character is uppercase, 0 otherwise. 12 | */ 13 | int _isupper(int c) 14 | { 15 | if (c >= 'A' && c <= 'Z') 16 | return (1); 17 | 18 | else 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks if parameter is a number between 0 to 9. 5 | * @c: input number. 6 | * Return: 1 if is a number (0 to 9), 0 in other case. 7 | */ 8 | int _isdigit(int c) 9 | { 10 | if (c >= 48 && c <= 57) 11 | { 12 | return (1); 13 | } 14 | else 15 | { 16 | return (0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/10-print_triangle.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_triangle - prints a triangle. 5 | * @size: numbers of lines. 6 | * Return: no return. 7 | */ 8 | void print_triangle(int size) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < size; i++) 13 | { 14 | for (j = 1; j < (size - i); j++) 15 | _putchar(' '); 16 | for (j--; j < size; j++) 17 | _putchar(35); 18 | if (i < (size - 1)) 19 | _putchar('\n'); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/100-prime_factor.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * main - prints largest prime factor. 6 | * Return: Always 0. 7 | */ 8 | 9 | int main(void) 10 | { 11 | long int n, fp; 12 | 13 | n = 612852475143; 14 | for (fp = 2; fp <= n; fp++) 15 | { 16 | if (n % fp == 0) 17 | { 18 | n /= fp; 19 | fp--; 20 | } 21 | } 22 | printf("%ld\n", fp); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/101-print_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_number - prints an integer. 5 | * @n: input integer. 6 | * Return: no return. 7 | */ 8 | void print_number(int n) 9 | { 10 | unsigned int m, d, count; 11 | 12 | if (n < 0) 13 | { 14 | _putchar(45); 15 | m = n * -1; 16 | } 17 | else 18 | { 19 | m = n; 20 | } 21 | 22 | d = m; 23 | count = 1; 24 | 25 | while (d > 9) 26 | { 27 | d /= 10; 28 | count *= 10; 29 | } 30 | 31 | for (; count >= 1; count /= 10) 32 | { 33 | _putchar(((m / count) % 10) + 48); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/2-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * mul - multiplies two integers. 5 | * @a: first number. 6 | * @b: second number. 7 | * Return: multiplication. 8 | */ 9 | int mul(int a, int b) 10 | { 11 | return (a * b); 12 | } 13 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/3-print_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_numbers - prints numbers between 0 to 9. 5 | * Return: no return. 6 | */ 7 | void print_numbers(void) 8 | { 9 | int ch; 10 | 11 | for (ch = 48; ch < 58; ch++) 12 | { 13 | _putchar(ch); 14 | } 15 | _putchar('\n'); 16 | } 17 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/4-print_most_numbers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: 4-print_most_numbers.c 3 | */ 4 | 5 | #include "main.h" 6 | 7 | /** 8 | * print_most_numbers - Prints the numbers from 0-9 except for 2 and 4. 9 | */ 10 | void print_most_numbers(void) 11 | { 12 | int num; 13 | 14 | for (num = 0; num <= 9; num++) 15 | { 16 | if (num != 2 && num != 4) 17 | _putchar((num % 10) + '0'); 18 | } 19 | 20 | _putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/5-more_numbers.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * more_numbers - prints numbers between 0 to 14 5 | * 10 times. 6 | * Return: no return. 7 | */ 8 | void more_numbers(void) 9 | { 10 | int i, ch; 11 | 12 | for (i = 0; i < 10; i++) 13 | { 14 | for (ch = 0; ch < 15; ch++) 15 | { 16 | if (ch >= 10) 17 | _putchar((ch / 10) + 48); 18 | _putchar((ch % 10) + 48); 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 - prints straight line n times. 5 | * @n: times straight line is printed. 6 | * Return: no return. 7 | */ 8 | void print_line(int n) 9 | { 10 | int i; 11 | 12 | for (i = 0; i < n; i++) 13 | { 14 | _putchar(95); 15 | } 16 | _putchar('\n'); 17 | } 18 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/7-print_diagonal.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_diagonal - prints diagonal line n times. 5 | * @n: times diagonal line is printed. 6 | * Return: no return. 7 | */ 8 | void print_diagonal(int n) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < n; i++) 13 | { 14 | for (j = 0; j < i; j++) 15 | { 16 | _putchar(' '); 17 | } 18 | _putchar(92); 19 | if (i < (n - 1)) 20 | _putchar('\n'); 21 | } 22 | _putchar('\n'); 23 | } 24 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/8-print_square.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_square - prints hashes squares. 5 | * @size: size of the square. 6 | * Return: no return. 7 | */ 8 | void print_square(int size) 9 | { 10 | int i, j; 11 | 12 | for (i = 0; i < size; i++) 13 | { 14 | for (j = 0; j < size; j++) 15 | { 16 | _putchar(35); 17 | } 18 | if (i != size - 1) 19 | _putchar('\n'); 20 | } 21 | _putchar('\n'); 22 | } 23 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/9-fizz_buzz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - prints Buzz each numbers of 3 and 5. 5 | * Return: Always 0. 6 | */ 7 | int main(void) 8 | { 9 | int n; 10 | 11 | n = 1; 12 | printf("%d", n); 13 | for (n = 2; n <= 100; n++) 14 | { 15 | if ((n % 3 == 0) && (n % 5 == 0)) 16 | { 17 | printf(" FizzBuzz"); 18 | } 19 | else if (n % 3 == 0) 20 | { 21 | printf(" Fizz"); 22 | } 23 | else if (n % 5 == 0) 24 | { 25 | printf(" Buzz"); 26 | } 27 | else 28 | { 29 | printf(" %d", n); 30 | } 31 | } 32 | printf("\n"); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/README.md: -------------------------------------------------------------------------------- 1 | # C - More functions, more nested loops 2 | 3 | In this project, I continued to learn about using nested loops and writing functions in C. 4 | 5 | ## Header File :file_folder: 6 | 7 | * [main.h](./main.h): Header file containing prototypes for all functions written in the project. 8 | 9 | | File | Prototype | 10 | | ------------------------ | -------------------------------- | 11 | | `0-isupper.c` | `int _isupper(int c);` | 12 | | `1-isdigit.c` | `int _isdigit(int c);` | 13 | | `2-mul.c` | `int mul(int a, int b);` | 14 | | `3-print_numbers.c` | `void print_numbers(void);` | 15 | | `4-print_most_numbers.c` | `void print_most_numbers(void);` | 16 | | `5-more_numbers.c` | `void more_numbers(void);` | 17 | | `6-print_line.c` | `void print_line(int n);` | 18 | | `7-print_diagonal.c` | `void print_diagonal(int n);` | 19 | | `8-print_square.c` | `void print_square(int size);` | 20 | | `10-print_triangle.c` | `void print_triangle(int size);` | 21 | | `101-print_number.c` | `void print_number(int n);` | 22 | 23 | ## Tasks :page_with_curl: 24 | 25 | * **0. isupper** 26 | * [0-isupper.c](./0-isupper.c): C function that checks for uppercase characters. Returns 27 | `1` if the character is uppercase, `0` otherwise. 28 | 29 | * **1. isdigit** 30 | * [1-isdigit.c](./1-isdigit.c): C function that checks for a digit (`0` through `9`). 31 | Returns `1` if the character is a digit, `0` otherwise. 32 | 33 | * **2. Collaboration is multiplication** 34 | * [2-mul.c](./2-mul.c): C function that multiplies two integers. Returns the value of 35 | the multiplication. 36 | 37 | * **3. The numbers speak for themselves** 38 | * [3-print_numbers.c](./3-print_numbers.c): C function that prints the numbers `0` to 39 | `9`, followed by a new line. 40 | 41 | * **4. I believe in numbers and signs** 42 | * [4-print_most_numbers](./4-print_most_numbers.c): C function that prints the numbers 43 | `0` to `9` except for `2` and `4`, followed by a new line. 44 | 45 | * **5. Numbers constitute the only universal language** 46 | * [5-more_numbers.c](./5-more_numbers.c): C function that prints the numbers `0` to 47 | `14`, followed by a new line, ten times. 48 | 49 | * **6. The shortest distance between two points is a straight line** 50 | * [6-print_line.c](./6-print_line.c): C function that draws a straight line in the terminal 51 | using the character `_`, followed by a new line. 52 | * If the function receives length input of zero or less, only a new line is printed. 53 | 54 | * **7. I feel like I am diagonally parked in a parallel universe** 55 | * [7-print_diagonal.c](./7-print_diagonal.c): C function that draws a diagonal 56 | line in the terminal using the `\` character, followed by a new line. 57 | * If the function receives length input of zero or less, only a new line is printed. 58 | 59 | * **8. You are so much sunshine in every square inch** 60 | * [8-print_square.c](./8-print_square.c): C function that prints a square using the `#` 61 | character, followed by a new line. 62 | * If the function receives size input of zero or less, only a new line is printed. 63 | 64 | * **9. Fizz-Buzz** 65 | * [9-fizz_buzz.c](./9-fizz_buzz.c): C program that prints the numbers from `1` to 66 | `100`, but for multiples of three, `Fizz` is printed instead of the number, for 67 | multiples of five, `Buzz`, and for multiples of both three and five, `FizzBuzz`. 68 | * Each number or word is separated by a space. 69 | 70 | * **10. Triangles** 71 | * [10-print_triangle.c](./10-print_triangle.c): C function that prints a triangle using 72 | the `#` character, followed by a new line. 73 | * If the function receives size input of zero or less, only a new line is printed. 74 | 75 | * **11. The problem of distinguishing prime numbers from composite numbers and of resolving the latter into their prime factors is known to be one of the most important and useful in arithmetic** 76 | * [100-prime_factor.c](./100-prime_factor.c): C program that prints the largest prime factor 77 | of the number `612852475143`, followed by a new line. 78 | 79 | * **12. Numbers have life; they're not just symbols on paper** 80 | * [101-print_number.c](./101-print_number.c): C function that prints an integer without 81 | using `long`, arrays, pointers, or hard-coded special values. 82 | -------------------------------------------------------------------------------- /0x04-more_functions_nested_loops/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | int _isupper(int c); 5 | int _isdigit(int c); 6 | int mul(int a, int b); 7 | void print_numbers(void); 8 | int _putchar(char c); 9 | void print_most_numbers(void); 10 | void more_numbers(void); 11 | void print_line(int n); 12 | void print_diagonal(int n); 13 | void print_square(int size); 14 | void print_triangle(int size); 15 | void print_number(int n); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/0-reset_to_98.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reset_to_98 - updates the value *n points to. 5 | * @n: the number to modify. 6 | */ 7 | 8 | void reset_to_98(int *n) 9 | { 10 | *n = 98; 11 | } 12 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/1-swap.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * swap_int - swap the value of two integers. 5 | * 6 | * @a: first integer. 7 | * @b: second integer. 8 | * 9 | */ 10 | 11 | void swap_int(int *a, int *b) 12 | { 13 | int d; 14 | 15 | d = 0; 16 | d = *a; 17 | *a = *b; 18 | *b = d; 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - convert a string into an integer. 5 | * 6 | * @s: the string to use. 7 | * Return: integer. 8 | */ 9 | 10 | int _atoi(char *s) 11 | { 12 | int sign = 1, i = 0; 13 | unsigned int res = 0; 14 | 15 | while (!(s[i] <= '9' && s[i] >= '0') && s[i] != '\0') 16 | { 17 | if (s[i] == '-') 18 | sign *= -1; 19 | i++; 20 | } 21 | while (s[i] <= '9' && (s[i] >= '0' && s[i] != '\0')) 22 | { 23 | res = (res * 10) + (s[i] - '0'); 24 | i++; 25 | } 26 | res *= sign; 27 | return (res); 28 | } 29 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/101-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * main - print password. 8 | * 9 | * Return: 0. 10 | */ 11 | 12 | int main(void) 13 | { 14 | int ascii = 2772, i = 0, j, random; 15 | char password[100]; 16 | time_t t; 17 | 18 | 19 | srand((int) time(&t)); 20 | while (ascii > 126) 21 | { 22 | random = rand() % 126; 23 | password[i] = random; 24 | ascii -= random; 25 | i++; 26 | } 27 | if (ascii > 0) 28 | password[i] = ascii; 29 | else 30 | { 31 | i--; 32 | } 33 | for (j = 0; j <= i; j++) 34 | { 35 | printf("%c", password[j]); 36 | } 37 | return (0); 38 | } 39 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - calculate the length of a string. 5 | * 6 | * @s: the string to calculate it's length. 7 | * Return: length of a string. 8 | */ 9 | 10 | int _strlen(char *s) 11 | { 12 | int i; 13 | int length = 0; 14 | 15 | for (i = 0; s[i] != '\0'; i++) 16 | { 17 | length++; 18 | } 19 | return (length); 20 | } 21 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string followed by a new line.. 5 | * 6 | * @str: the string to print. 7 | * 8 | */ 9 | 10 | void _puts(char *str) 11 | { 12 | int i; 13 | 14 | for (i = 0; str[i] != '\0'; i++) 15 | { 16 | _putchar(str[i]); 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/4-print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - print a string in reverse. 5 | * 6 | * @s: the string to print in reverse. 7 | * 8 | */ 9 | 10 | void print_rev(char *s) 11 | { 12 | int i = 0; 13 | int length; 14 | 15 | for (length = 0; s[length] != '\0'; length++) 16 | { 17 | } 18 | 19 | for (i = length - 1; i >= 0; i--) 20 | { 21 | _putchar(s[i]); 22 | } 23 | _putchar('\n'); 24 | } 25 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/5-rev_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rev_string - reverse a string. 5 | * 6 | * @s: the string to be reversed. 7 | */ 8 | 9 | void rev_string(char *s) 10 | { 11 | int length, j, i; 12 | char v1, v2; 13 | 14 | for (length = 0; s[length] != '\0'; length++) 15 | { 16 | } 17 | 18 | j = length - 1; 19 | i = 0; 20 | 21 | while (j > i) 22 | { 23 | v1 = s[i]; 24 | v2 = s[j]; 25 | s[i] = v2; 26 | s[j] = v1; 27 | j--; 28 | i++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | C pointers,arrays and strings 2 | -------------------------------------------------------------------------------- /0x05-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | #ifndef main 2 | 3 | int _putchar(char c); 4 | void reset_to_98(int *n); 5 | void swap_int(int *a, int *b); 6 | int _strlen(char *s); 7 | void _puts(char *str); 8 | void print_rev(char *s); 9 | void rev_string(char *s); 10 | void puts2(char *str); 11 | void puts_half(char *str); 12 | void print_array(int *a, int n); 13 | char *_strcpy(char *dest, char *src); 14 | int _atoi(char *s); 15 | 16 | #define main 17 | #endif 18 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcat - concatinate 2 strings. 5 | * @dest: first string. 6 | * @src: second string. 7 | * Return: string. 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i = 0, j = 0; 13 | 14 | while (dest[i] != '\0') 15 | i++; 16 | 17 | while (src[j] != '\0') 18 | { 19 | dest[i] = src[j]; 20 | i++; 21 | j++; 22 | } 23 | 24 | dest[i] = '\0'; 25 | 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncat - concatinate 2 strings. 5 | * @dest: first string. 6 | * @src: second string. 7 | * @n: the number of bytes to use from src. 8 | * Return: string. 9 | */ 10 | 11 | char *_strncat(char *dest, char *src, int n) 12 | { 13 | int i = 0, k = 0; 14 | 15 | while (dest[i] != '\0') 16 | i++; 17 | 18 | while (src[k] != '\0' && n > k) 19 | { 20 | dest[i] = src[k]; 21 | k++; 22 | i++; 23 | } 24 | if(n > 0) 25 | { 26 | dest[i] = '\0'; 27 | } 28 | 29 | return (dest); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/100-rot13.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * rot13 - encoding a string using rot13. 5 | * @s: the string to be encoded to rot13. 6 | * Return: the string s encoded to rot13. 7 | */ 8 | 9 | char *rot13(char *s) 10 | { 11 | int i = 0, j = 0; 12 | char string_rot13[] = "NnOoPpQqRrSsTtUuVvWwXxYyZzAaBbCcDdEeFfGgHhIiJjKkLlMm"; 13 | char string_alpha[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; 14 | 15 | while (s[i] != '\0') 16 | { 17 | do { 18 | if (s[i] == string_alpha[j]) 19 | { 20 | s[i] = string_rot13[j]; 21 | break; 22 | } 23 | j++; 24 | } while (string_alpha[j] != '\0'); 25 | j = 0; 26 | i++; 27 | } 28 | 29 | return (s); 30 | } 31 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/102-magic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n; 6 | int a[5]; 7 | int *p; 8 | 9 | a[2] = 1024; 10 | p = &n; 11 | /* 12 | * write your line of code here... 13 | * Remember: 14 | * - you are not allowed to use a 15 | * - you are not allowed to modify p 16 | * - only one statement 17 | * - you are not allowed to code anything else than this line of code 18 | */ 19 | *(p + 5) = 98; 20 | /* ...so that this prints 98\n */ 21 | printf("a[2] = %d\n", a[2]); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/103-infinite_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * infinite_add - add two numbers. 5 | * 6 | * @n1: first number. 7 | * @n2: second number. 8 | * @r: result. 9 | * @size_r: result size. 10 | * Return: the addition of n1 and n2. 11 | */ 12 | 13 | char *infinite_add(char *n1, char *n2, char *r, int size_r) 14 | { 15 | int add = 0, len1, len2, i, j; 16 | 17 | for (len1 = 0; n1[len1]; len1++) 18 | ; 19 | for (len2 = 0; n2[len2]; len2++) 20 | ; 21 | if (len1 > size_r || len2 > size_r) 22 | return (0); 23 | len1--; 24 | len2--; 25 | size_r--; 26 | for (i = 0; i < size_r; i++, len1--, len2--) 27 | { 28 | if (len1 >= 0) 29 | add += n1[len1] - '0'; 30 | if (len2 >= 0) 31 | add += n2[len2] - '0'; 32 | if (len1 < 0 && len2 < 0 && add == 0) 33 | break; 34 | r[i] = add % 10 + '0'; 35 | add /= 10; 36 | } 37 | r[i] = '\0'; 38 | if (len1 >= 0 || len2 >= 0 || add) 39 | return (0); 40 | for (i--, j = 0; i > j; i--, j++) 41 | { 42 | add = r[i]; 43 | r[i] = r[j]; 44 | r[j] = add; 45 | } 46 | return (r); 47 | } 48 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/104-print_buffer.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_buffer - Print a buffer 10 bytes per line. 6 | * @b: Buffer address. 7 | * @size: Number of characters to be printed. 8 | */ 9 | void print_buffer(char *b, int size) 10 | { 11 | int j, k, l; 12 | 13 | if (size <= 0) 14 | printf("\n"); 15 | else 16 | { 17 | for (j = 0; j < size; j += 10) 18 | { 19 | printf("%.8x:", j); 20 | for (k = j; k < j + 10; k++) 21 | { 22 | if (k % 2 == 0) 23 | printf(" "); 24 | if (k < size) 25 | printf("%.2x", *(b + k)); 26 | else 27 | printf(" "); 28 | } 29 | printf(" "); 30 | for (l = j; l < j + 10; l++) 31 | { 32 | if (l >= size) 33 | break; 34 | if (*(b + l) < 32 || *(b + l) > 126) 35 | printf("%c", '.'); 36 | else 37 | printf("%c", *(b + l)); 38 | } 39 | printf("\n"); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strncpy - copy src into dest. 5 | * @dest: first string. 6 | * @src: second string. 7 | * @n: the number of bytes to use from src. 8 | * Return: string. 9 | */ 10 | 11 | char *_strncpy(char *dest, char *src, int n) 12 | { 13 | int i = 0, k = 0; 14 | 15 | while (n > k) 16 | { 17 | if (src[k] == '\0') 18 | { 19 | for (; k < n; k++) 20 | { 21 | dest[i] = '\0'; 22 | i++; 23 | } 24 | } 25 | else 26 | { 27 | dest[i] = src[k]; 28 | k++; 29 | i++; 30 | } 31 | } 32 | 33 | return (dest); 34 | } 35 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strcmp - compare two strings. 5 | * @s1: string 1. 6 | * @s2: string 2. 7 | * Return: int. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, cmp = 0; 13 | 14 | while (s1[i] != '\0' && s2[i] != '\0' && cmp == 0) 15 | { 16 | cmp = s1[i] - s2[i]; 17 | i++; 18 | } 19 | 20 | return (cmp); 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/4-rev_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * reverse_array - reverse an array. 5 | * @a: array. 6 | * @n: array's length. 7 | */ 8 | 9 | void reverse_array(int *a, int n) 10 | { 11 | int i, j, tmp; 12 | 13 | j = n - 1; 14 | for (i = 0; i < n / 2; i++) 15 | { 16 | tmp = a[i]; 17 | a[i] = a[j]; 18 | a[j] = tmp; 19 | j--; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/5-string_toupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * string_toupper - changes all lowercase letters of a string to uppercase. 5 | * @s: string to modify. 6 | * 7 | * Return: s modified. 8 | */ 9 | 10 | char *string_toupper(char *s) 11 | { 12 | int i = 0; 13 | 14 | while (s[i] != '\0') 15 | { 16 | if (s[i] > 96 && s[i] < 123) 17 | { 18 | s[i] -= 32; 19 | } 20 | i++; 21 | } 22 | 23 | return (s); 24 | } 25 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/6-cap_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * cap_string - capitalizes all words of a string. 5 | * @s: string to use. 6 | * 7 | * Return: string. 8 | */ 9 | 10 | char *cap_string(char *s) 11 | { 12 | int i = 1, j, check; 13 | char a[] = {',', ';', '.', '!', '?', '"', '(', ')', '{', '}', '\n', '\t', ' '}; 14 | 15 | if (s[0] > 96 && s[0] < 123) 16 | s[0] -= 32; 17 | 18 | while (s[i] != '\0') 19 | { 20 | if (s[i] > 96 && s[i] < 123) 21 | { 22 | j = 0; 23 | check = 0; 24 | while (check == 0 && j < 13) 25 | { 26 | if (s[i - 1] == a[j]) 27 | { 28 | check = 1; 29 | } 30 | j++; 31 | } 32 | if (check == 1) 33 | { 34 | s[i] -= 32; 35 | } 36 | } 37 | i++; 38 | } 39 | return (s); 40 | } 41 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/7-leet.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * leet - encodes a string into 1337. 5 | * @s: string to encode. 6 | * Return: the encoded string. 7 | */ 8 | 9 | char *leet(char *s) 10 | { 11 | int i = 0, j = 0; 12 | char array_leet[] = {'4', '3', '1', '0', '7'}; 13 | char array_up[] = {'A', 'E', 'L', 'O', 'T'}; 14 | char array_low[] = {'a', 'e', 'l', 'o', 't'}; 15 | 16 | while (s[i] != '\0') 17 | { 18 | for (j = 0; j < 5; j++) 19 | { 20 | if (s[i] == array_low[j] || s[i] == array_up[j]) 21 | s[i] = array_leet[j]; 22 | } 23 | i++; 24 | } 25 | 26 | return (s); 27 | } 28 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | more pointers, arrays and strings 2 | -------------------------------------------------------------------------------- /0x06-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | char *_strcat(char *dest, char *src); 2 | char *_strncat(char *dest, char *src, int n); 3 | char *_strncpy(char *dest, char *src, int n); 4 | int _strcmp(char *s1, char *s2); 5 | void reverse_array(int *a, int n); 6 | char *string_toupper(char *); 7 | char *cap_string(char *); 8 | char *rot13(char *); 9 | void print_number(int n); 10 | char *infinite_add(char *n1, char *n2, char *r, int size_r); 11 | void print_buffer(char *b, int size); 12 | 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memset - fills string with constant char b upto n bytes 5 | * @s: input pointer to string 6 | * @b: constant char 7 | * @n: number of bytes 8 | * Return: pointer to s string 9 | */ 10 | 11 | char *_memset(char *s, char b, unsigned int n) 12 | { 13 | unsigned int i = 0; 14 | 15 | while (i < n) 16 | { 17 | s[i] = b; 18 | i++; 19 | } 20 | return (s); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _memcpy - input 5 | * @dest: input pointer to string destination 6 | * @src: input pointer to source string 7 | * @n: number of bytes 8 | * Return: pointer to destination string 9 | */ 10 | 11 | char *_memcpy(char *dest, char *src, unsigned int n) 12 | { 13 | unsigned int i = 0; 14 | 15 | while (i < n) 16 | { 17 | *(dest + i) = *(src + i); 18 | i++; 19 | } 20 | return (dest); 21 | } 22 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/100-set_string.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * set_string - a function that sets the value of a pointer to a char. 5 | * @s: double pointer to a string 6 | * @to: pointer to the string to set s to 7 | * Return: Description of the returned value 8 | */ 9 | void set_string(char **s, char *to) 10 | { 11 | *s = to; 12 | } 13 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/101-crackme_password: -------------------------------------------------------------------------------- 1 | abc123 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strchr - locates a character in a string 5 | * @s: string input 6 | * @c: character to find 7 | * Return: pointer to first occurence of c character 8 | */ 9 | 10 | char *_strchr(char *s, char c) 11 | { 12 | unsigned int i; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | if (s[i] == c) 16 | break; 17 | return (s[i] == c ? (s + i) : '\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring 5 | * @s: input string to search for substring 6 | * @accept: characters that prefix substring must include 7 | * Return: length of prefix substring 8 | */ 9 | 10 | unsigned int _strspn(char *s, char *accept) 11 | { 12 | unsigned int i, j, a_len = 0, len = 0; 13 | 14 | while (accept[a_len] != '\0') 15 | a_len++; 16 | for (i = 0; s[i] != '\0'; i++) 17 | for (j = 0; j < a_len; j++) 18 | if (s[i] == accept[j]) 19 | len++, j = a_len; 20 | else 21 | if (j == a_len - 1) 22 | goto exit; 23 | exit: return (len); 24 | } 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - finds first matching char in string 5 | * @s: input string to search for matching char 6 | * @accept: characters that could be matched 7 | * Return: pointer to matching char 8 | */ 9 | 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; s[i] != '\0'; i++) 15 | for (j = 0; accept[j] != '\0'; j++) 16 | if (s[i] == accept[j]) 17 | goto exit; 18 | exit: return (s[i] != '\0' ? s + i : '\0'); 19 | } 20 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - returns pointer to first char of matching substring 5 | * @haystack: string to find substring in 6 | * @needle: substring to find match of 7 | * Return: pointer to first char of matching substring 8 | */ 9 | 10 | char *_strstr(char *haystack, char *needle) 11 | { 12 | int k; 13 | 14 | while (*haystack != '\0') 15 | { 16 | k = 0; 17 | while (*haystack == *needle && *haystack != '\0' && *needle != '\0') 18 | haystack++, needle++, k++; 19 | if (*needle == '\0') 20 | return (haystack - k); 21 | haystack -= (k - 1), needle -= k; 22 | } 23 | return ('\0'); 24 | } 25 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/7-print_chessboard.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_chessboard - a function that prints a chessboard 5 | * @a: an array input to print 6 | * Return: Nothing 7 | */ 8 | void print_chessboard(char (*a)[8]) 9 | { 10 | int i = 0, j; 11 | 12 | for (; i < 8; i++) 13 | { 14 | for (j = 0; j < 8; j++) 15 | _putchar(a[i][j]); 16 | _putchar('\n'); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/8-print_diagsums.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_diagsums - prints sum of #'s in diagnols of square 6 | * @a: input square array 7 | * @size: size of one dimension in array 8 | * Return: void 9 | */ 10 | 11 | void print_diagsums(int *a, int size) 12 | { 13 | int i, j, sum1 = 0, sum2 = 0; 14 | 15 | for (j = 0, i = size - 1; j < (size * size); j += size + 1, i += size - 1) 16 | sum1 += a[j], sum2 += a[i]; 17 | printf("%d, %d\n", sum1, sum2); 18 | } 19 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/README.md: -------------------------------------------------------------------------------- 1 | even more pointer, arrays and strings. 2 | -------------------------------------------------------------------------------- /0x07-pointers_arrays_strings/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *_memset(char *s, char b, unsigned int n); 3 | char *_memcpy(char *dest, char *src, unsigned int n); 4 | char *_strchr(char *s, char c); 5 | unsigned int _strspn(char *s, char *accept); 6 | char *_strpbrk(char *s, char *accept); 7 | char *_strstr(char *haystack, char *needle); 8 | void print_chessboard(char (*a)[8]); 9 | void print_diagsums(int *a, int size); 10 | void set_string(char **s, char *to); 11 | -------------------------------------------------------------------------------- /0x08-recursion/0-puts_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts_recursion - Prints a string followed by a new line 5 | * @s: string 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | 11 | void _puts_recursion(char *s) 12 | { 13 | if (*s == 0) 14 | { 15 | _putchar('\n'); 16 | return; 17 | } 18 | _putchar(*s); 19 | _puts_recursion(s + 1); 20 | } 21 | -------------------------------------------------------------------------------- /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 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | void _print_rev_recursion(char *s) 11 | { 12 | if (*s != '\0') 13 | { 14 | _print_rev_recursion(s + 1); 15 | _putchar(*s); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/100-is_palindrome.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * palind2 - obtains length of a 5 | * @a: string 6 | * @l: integer to count length 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int palind2(char *a, int l) 12 | { 13 | if (*a == 0) 14 | return (l - 1); 15 | return (palind2(a + 1, l + 1)); 16 | } 17 | /** 18 | * palind3 - compares string vs string reverse 19 | * @a: string 20 | * @l: length 21 | * 22 | * Return: On success 1. 23 | * On error, -1 is returned, and errno is set appropriately. 24 | */ 25 | 26 | int palind3(char *a, int l) 27 | { 28 | if (*a != *(a + l)) 29 | return (0); 30 | else if (*a == 0) 31 | return (1); 32 | return (palind3(a + 1, l - 2)); 33 | } 34 | /** 35 | * is_palindrome - checks if a string is a palindrome 36 | * @s: string to evaluate 37 | * 38 | * Return: On success 1. 39 | * On error, -1 is returned, and errno is set appropriately. 40 | */ 41 | int is_palindrome(char *s) 42 | { 43 | int l; 44 | 45 | l = palind2(s, 0); 46 | return (palind3(s, l)); 47 | } 48 | -------------------------------------------------------------------------------- /0x08-recursion/101-wildcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * wildcmp - compares two strings and returns 1 if the strings 5 | * can be considered identical, otherwise return 0. 6 | * @s1: string to compare to 7 | * @s2: string with wild character 8 | * 9 | * Return: On success 1. 10 | * On error, -1 is returned, and errno is set appropriately. 11 | */ 12 | int wildcmp(char *s1, char *s2) 13 | { 14 | if (*s1 == '\0' && *s2 == '\0') 15 | return (1); 16 | 17 | if (*s1 == *s2) 18 | return (wildcmp(s1 + 1, s2 + 1)); 19 | 20 | if (*s2 == '*') 21 | { 22 | if (*s2 == '*' && *(s2 + 1) != '\0' && *s1 == '\0') 23 | return (0); 24 | if (wildcmp(s1, s2 + 1) || wildcmp(s1 + 1, s2)) 25 | return (1); 26 | } 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/2-strlen_recursion.c: -------------------------------------------------------------------------------- 1 | nclude "main.h" 2 | 3 | /** 4 | * _strlen_recursion - Returns Length of String 5 | * @s: string 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | 11 | int _strlen_recursion(char *s) 12 | { 13 | if (*s != '\0') 14 | { 15 | return (1 + _strlen_recursion(s + 1)); 16 | } 17 | return (0); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/3-factorial.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * factorial - gets factorial of n 5 | * @n: integer 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int factorial(int n) 11 | { 12 | if (n < 0) 13 | return (-1); 14 | if (n == 0) 15 | return (1); 16 | return (n * factorial(n - 1)); 17 | } 18 | -------------------------------------------------------------------------------- /0x08-recursion/4-pow_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _pow_recursion - raises x to the power of y 5 | * @x: Number Integer 6 | * @y: Power 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int _pow_recursion(int x, int y) 12 | { 13 | if (y < 0) 14 | return (-1); 15 | if (y == 0) 16 | return (1); 17 | return (x * _pow_recursion(x, y - 1)); 18 | } 19 | -------------------------------------------------------------------------------- /0x08-recursion/5-sqrt_recursion.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * sqrt2 - Makes possible to evaluate from 1 to n 5 | * @a: same number as n 6 | * @b: number that iterates from 1 to n 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int sqrt2(int a, int b) 12 | { 13 | if (b * b == a) 14 | return (b); 15 | else if (b * b > a) 16 | return (-1); 17 | return (sqrt2(a, b + 1)); 18 | } 19 | /** 20 | * _sqrt_recursion - returns the natural square root of n 21 | * @n: Number Integer 22 | * 23 | * Return: On success 1. 24 | * On error, -1 is returned, and errno is set appropriately. 25 | */ 26 | int _sqrt_recursion(int n) 27 | { 28 | return (sqrt2(n, 1)); 29 | } 30 | -------------------------------------------------------------------------------- /0x08-recursion/6-is_prime_number.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prime2 - Makes possible to evaluate from 1 to n 5 | * @a: same number as n 6 | * @b: number that iterates from 1 to n 7 | * 8 | * Return: On success 1. 9 | * On error, -1 is returned, and errno is set appropriately. 10 | */ 11 | int prime2(int a, int b) 12 | { 13 | if (a == b) 14 | return (1); 15 | else if (a % b == 0) 16 | return (0); 17 | return (prime2(a, b + 1)); 18 | } 19 | /** 20 | * is_prime_number - checks if a number is prime 21 | * @n: Number Integer 22 | * 23 | * Return: On success 1. 24 | * On error, -1 is returned, and errno is set appropriately. 25 | */ 26 | int is_prime_number(int n) 27 | { 28 | if (n <= 1) 29 | return (0); 30 | return (prime2(n, 2)); 31 | } 32 | -------------------------------------------------------------------------------- /0x08-recursion/README.md: -------------------------------------------------------------------------------- 1 | # recursor 2 | 3 | tasks on alx recursion projection 4 | -------------------------------------------------------------------------------- /0x08-recursion/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | void _puts_recursion(char *s); 3 | void _print_rev_recursion(char *s); 4 | int _strlen_recursion(char *s); 5 | int factorial(int n); 6 | int _pow_recursion(int x, int y); 7 | int _sqrt_recursion(int n); 8 | int is_prime_number(int n); 9 | int is_palindrome(char *s); 10 | int wildcmp(char *s1, char *s2); 11 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-isupper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/0-isupper.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memset - fills memory with a constant byte, 4 | * @s: memory area. 5 | * @b: constant byte. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memset(char *s, char b, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(s + i) = b; 15 | 16 | return (s); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-memset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/0-memset.o -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - function commute srtings 5 | * @dest: param pointer to a char 6 | * @src: param pointer to a char 7 | * Return: return value of dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | while (src[j] != '\0') 23 | { 24 | dest[i] = src[j]; 25 | j++; 26 | i++; 27 | } 28 | 29 | dest[i] = '\0'; 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/0-strcat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/0-strcat.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-isdigit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/1-isdigit.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-memcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/1-memcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes used from src. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/1-strncat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/1-strncat.o -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer. 5 | * @s: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | if (*(s + count) == '-') 17 | pn *= -1; 18 | 19 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 20 | { 21 | if (size > 0) 22 | m *= 10; 23 | size++; 24 | } 25 | count++; 26 | } 27 | 28 | for (i = count - size; i < count; i++) 29 | { 30 | oi = oi + ((*(s + i) - 48) * m); 31 | m /= 10; 32 | } 33 | return (oi * pn); 34 | } 35 | -------------------------------------------------------------------------------- /0x09-static_libraries/100-atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/100-atoi.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strchr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/2-strchr.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input stringeturn. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strlen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/2-strlen.o -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x09-static_libraries/2-strncpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/2-strncpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - prints 1 or 0 depending on input 5 | * @c: first parameter 6 | * 7 | * Description: prints all lowercase letters 8 | * Return: Always(0). 9 | */ 10 | 11 | int _islower(int c) 12 | { 13 | return (c >= 'a' && c <= 'z'); 14 | } 15 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-islower.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/3-islower.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, 5 | * @str: pointer to the string to print 6 | * Return: void 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i]) 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-puts.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/3-puts.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equals, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strcmp.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/3-strcmp.o -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x09-static_libraries/3-strspn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/3-strspn.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - Check if character is a alphabet character. 4 | * @c: type int character 5 | * Return: 1 if letter, lowercase or uppercase, and 0 otherwise 6 | */ 7 | 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 11 | return (1); 12 | 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-isalpha.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/4-isalpha.o -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/4-strpbrk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/4-strpbrk.o -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x09-static_libraries/5-strstr.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/5-strstr.o -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - compute the absolute value of an integer 4 | * @n: int type number 5 | * Return: absolute value of @n 6 | */ 7 | int _abs(int n) 8 | { 9 | if (n < 0) 10 | { 11 | return (n * -1); 12 | } 13 | else 14 | { 15 | return (n); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x09-static_libraries/6-abs.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/6-abs.o -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcpy - copies the string pointed to by src, 5 | * including the terminating null byte, to the 6 | * buffer pointed to by dest. 7 | * @dest: destination. 8 | * @src: source. 9 | * Return: the pointer to dest. 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int count = 0; 14 | 15 | while (count >= 0) 16 | { 17 | *(dest + count) = *(src + count); 18 | if (*(src + count) == '\0') 19 | break; 20 | count++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x09-static_libraries/9-strcpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/9-strcpy.o -------------------------------------------------------------------------------- /0x09-static_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C - Static libraries 2 | 3 | In this project, I learned what static libraries are and practiced creating and 4 | using them with `ar`, `ranlib`, and `nm`. 5 | 6 | ## Tasks :page_with_curl: 7 | 8 | * **0. A library is not a luxury but one of the necessities of life** 9 | * [libmy.a](./libmy.a): C Static library containing all the functions 10 | listed below : 11 | * `int _putchar(char c);` 12 | * `int _islower(int c);` 13 | * `int _isalpha(int c);` 14 | * `int _abs(int n);` 15 | * `int _isupper(int c);` 16 | * `int _isdigit(int c);` 17 | * `int _strlen(char *s);` 18 | * `void _puts(char *s);` 19 | * `char *_strcpy(char *dest, char *src);` 20 | * `int _atoi(char *s);` 21 | * `char *_strcat(char *dest, char *src);` 22 | * `char *_strncat(char *dest, char *src, int n);` 23 | * `char *_strncpy(char *dest, char *src, int n);` 24 | * `int _strcmp(char *s1, char *s2);` 25 | * `char *_memset(char *s, char b, unsigned int n);` 26 | * `char *_memcpy(char *dest, char *src, unsigned int n);` 27 | * `char *_strchr(char *s, char c);` 28 | * `unsigned int _strspn(char *s, char *accept);` 29 | * `char *_strpbrk(char *s, char *accept);` 30 | * `char *_strstr(char *haystack, char *needle);` 31 | 32 | * [main.h](./main.h): Header file containing the prototypes of all functions 33 | included in `libmy.a`. 34 | 35 | * **1. Without libraries what have we? We have no past and no future** 36 | * [create_static_lib.sh](./create_static_lib.sh): Bash script that creates a static 37 | library called `liball.a` from all the `.c` files in the current directory. 38 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x09-static_libraries/_putchar.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/_putchar.o -------------------------------------------------------------------------------- /0x09-static_libraries/create_static_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -Wall -pedantic -Werror -Wextra -c *.c 3 | ar -rc liball.a *.o 4 | ranlib liball.a 5 | -------------------------------------------------------------------------------- /0x09-static_libraries/libmy.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/libmy.a -------------------------------------------------------------------------------- /0x09-static_libraries/main.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | int main(void) 4 | { 5 | _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); 6 | return (0); 7 | } 8 | -------------------------------------------------------------------------------- /0x09-static_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef FILE_MAIN 2 | #define FILE_MAIN 3 | 4 | /*a list that was given from the task 0 */ 5 | 6 | int _putchar(char c); 7 | int _islower(int c); 8 | int _isalpha(int c); 9 | int _abs(int n); 10 | int _isupper(int c); 11 | int _isdigit(int c); 12 | int _strlen(char *s); 13 | void _puts(char *s); 14 | char *_strcpy(char *dest, char *src); 15 | int _atoi(char *s); 16 | char *_strcat(char *dest, char *src); 17 | char *_strncat(char *dest, char *src, int n); 18 | char *_strncpy(char *dest, char *src, int n); 19 | int _strcmp(char *s1, char *s2); 20 | char *_memset(char *s, char b, unsigned int n); 21 | char *_memcpy(char *dest, char *src, unsigned int n); 22 | char *_strchr(char *s, char c); 23 | unsigned int _strspn(char *s, char *accept); 24 | char *_strpbrk(char *s, char *accept); 25 | char *_strstr(char *haystack, char *needle); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /0x09-static_libraries/quote: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x09-static_libraries/quote -------------------------------------------------------------------------------- /0x0A-argc_argv/0-whatsmyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * @argc: Counts the number of parameters that go into main 8 | * @argv: Pointer of array of pointers containing strings entering main 9 | * Return: Always 0 (Success) 10 | */ 11 | 12 | int main(int argc, char **argv) 13 | { 14 | if (argc > 0) 15 | printf("%s\n", argv[0]); 16 | return (0); 17 | } 18 | -------------------------------------------------------------------------------- /0x0A-argc_argv/1-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * @argc: Counts the number of parameters that go into main 8 | * @argv: Pointer of array of pointers containing strings entering main 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char **argv) 12 | { 13 | (void) argv; 14 | printf("%i\n", argc - 1); 15 | return (0); 16 | } 17 | -------------------------------------------------------------------------------- /0x0A-argc_argv/100-change.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | /** 7 | * _isnumber - checks if string is a number 8 | * @s: string 9 | * 10 | * Return: On success 1. 11 | * If not a number, 0 is returned. 12 | */ 13 | int _isnumber(char *s) 14 | { 15 | int i, check, d; 16 | 17 | i = 0, d = 0, check = 1; 18 | if (*s == '-') 19 | i++; 20 | for (; *(s + i) != 0; i++) 21 | { 22 | d = isdigit(*(s + i)); 23 | if (d == 0) 24 | { 25 | check = 0; 26 | break; 27 | } 28 | } 29 | return (check); 30 | } 31 | /** 32 | * main - Entry point 33 | * 34 | * @argc: Counts the number of parameters that go into main 35 | * @argv: Pointer of array of pointers containing strings entering main 36 | * Return: Always 0 (Success) 37 | */ 38 | int main(int argc, char **argv) 39 | { 40 | int j, ex, coins, cents, d; 41 | int c[5] = {25, 10, 5, 2, 1}; 42 | 43 | ex = 1, j = 0, coins = 0; 44 | if (argc == 2) 45 | { 46 | if (_isnumber(argv[1])) 47 | { 48 | ex = 0, cents = atoi(argv[1]); 49 | if (cents >= 0) 50 | { 51 | while (cents != 0) 52 | { 53 | d = cents / c[j]; 54 | if (d == 0) 55 | { 56 | j++; 57 | } 58 | else 59 | { 60 | coins += d; 61 | cents -= (d * c[j]); 62 | } 63 | } 64 | } 65 | } 66 | } 67 | if (ex == 0) 68 | printf("%i\n", coins); 69 | else 70 | printf("%s\n", "Error"); 71 | return (ex); 72 | } 73 | -------------------------------------------------------------------------------- /0x0A-argc_argv/2-args.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * @argc: Counts the number of parameters that go into main 8 | * @argv: Pointer of array of pointers containing strings entering main 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char **argv) 12 | { 13 | int i; 14 | 15 | if (argc > 0) 16 | { 17 | for (i = 0; i < argc; i++) 18 | { 19 | printf("%s\n", argv[i]); 20 | } 21 | } 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /0x0A-argc_argv/3-mul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * @argc: Counts the number of parameters that go into main 8 | * @argv: Pointer of array of pointers containing strings entering main 9 | * Return: Always 0 (Success) 10 | */ 11 | int main(int argc, char **argv) 12 | { 13 | int n, ex; 14 | 15 | ex = 0; 16 | if (argc != 3) 17 | { 18 | printf("%s\n", "Error"); 19 | ex = 1; 20 | } 21 | else 22 | { 23 | n = atoi(argv[1]) * atoi(argv[2]); 24 | printf("%i\n", n); 25 | } 26 | return (ex); 27 | } 28 | -------------------------------------------------------------------------------- /0x0A-argc_argv/4-add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * _isnumber - checks if string is a number 7 | * @s: string 8 | * 9 | * Return: On success 1. 10 | * If not a number, 0 is returned. 11 | */ 12 | 13 | int _isnumber(char *s) 14 | { 15 | int i, check, d; 16 | 17 | i = 0, d = 0, check = 1; 18 | if (*s == '-') 19 | i++; 20 | for (; *(s + i) != 0; i++) 21 | { 22 | d = isdigit(*(s + i)); 23 | if (d == 0) 24 | { 25 | check = 0; 26 | break; 27 | } 28 | } 29 | return (check); 30 | } 31 | /** 32 | * main - Entry point 33 | * 34 | * @argc: Counts the number of parameters that go into main 35 | * @argv: Pointer of array of pointers containing strings entering main 36 | * Return: Always 0 (Success) 37 | */ 38 | int main(int argc, char **argv) 39 | { 40 | int i, n, ex; 41 | 42 | ex = 0, n = 0; 43 | if (argc > 1) 44 | { 45 | for (i = 1; i < argc; i++) 46 | { 47 | if (_isnumber(argv[i])) 48 | n += atoi(argv[i]); 49 | else 50 | ex = 1; 51 | } 52 | } 53 | if (ex == 0) 54 | printf("%i\n", n); 55 | else 56 | printf("%s\n", "Error"); 57 | return (ex); 58 | } 59 | -------------------------------------------------------------------------------- /0x0A-argc_argv/README.md: -------------------------------------------------------------------------------- 1 | # 0x0A-argc_argv 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/.100-argstostr.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x0B-malloc_free/.100-argstostr.c.swp -------------------------------------------------------------------------------- /0x0B-malloc_free/0-create_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * create_array - creates an array of chars, and initializes it with 7 | * a specific char 8 | * @size: size of array 9 | * @c: character to initialize with 10 | * 11 | * Return: Pointer 12 | */ 13 | char *create_array(unsigned int size, char c) 14 | { 15 | unsigned int i; 16 | char *s; 17 | 18 | if (size <= 0) 19 | return (0); 20 | 21 | s = malloc(sizeof(char) * size); 22 | 23 | if (s == 0) 24 | return (0); 25 | 26 | for (i = 0; i < size; i++) 27 | *(s + i) = c; 28 | 29 | *(s + i) = '\0'; 30 | 31 | return (s); 32 | } 33 | -------------------------------------------------------------------------------- /0x0B-malloc_free/1-strdup.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * _strdup - allocate a string. 6 | * @str: string. 7 | * Return: pointer to string if success. 8 | */ 9 | 10 | char *_strdup(char *str) 11 | { 12 | int i; 13 | char *str1; 14 | 15 | if (str == NULL) 16 | return (NULL); 17 | 18 | for (i = 0; str[i]; i++) 19 | ; 20 | i++; 21 | str1 = malloc(sizeof(char) * i); 22 | 23 | if (str1 == NULL) 24 | return (NULL); 25 | 26 | for (i = 0; str[i] != '\0'; i++) 27 | str1[i] = str[i]; 28 | str1[i] = '\0'; 29 | return (str1); 30 | } 31 | -------------------------------------------------------------------------------- /0x0B-malloc_free/100-argstostr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * argstostr - concatenates all the arguments of your program 6 | * @ac: argument count in main 7 | * @av: arguments passed to main 8 | * 9 | * Return: Pointer 10 | */ 11 | 12 | char *argstostr(int ac, char **av) 13 | { 14 | char *s; 15 | int l, lt, i, j, k; 16 | 17 | if (ac == 0 || av == NULL) 18 | return (0); 19 | l = 0, k = 0; 20 | for (i = 0; i < ac; i++) 21 | { 22 | lt = 0; 23 | while (av[i][lt]) 24 | lt++; 25 | l += lt + 1; 26 | } 27 | s = malloc((l + 1) * sizeof(char)); 28 | 29 | if (s == 0) 30 | return (0); 31 | 32 | for (j = 0; j < ac; j++) 33 | { 34 | lt = 0; 35 | while (av[j][lt]) 36 | { 37 | *(s + k) = av[j][lt]; 38 | k++; 39 | lt++; 40 | } 41 | *(s + k) = '\n'; 42 | k++; 43 | } 44 | *(s + k) = '\0'; 45 | 46 | return (s); 47 | } 48 | -------------------------------------------------------------------------------- /0x0B-malloc_free/101-strtow.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | /** 5 | * _wcount - counts number of words 6 | * @sw: string 7 | * 8 | * Return: int 9 | */ 10 | int _wcount(char *sw) 11 | { 12 | int l, wc; 13 | 14 | l = 0, wc = 0; 15 | if (*(sw + l) == ' ') 16 | l++; 17 | while (*(sw + l)) 18 | { 19 | if (*(sw + l) == ' ' && *(sw + l - 1) != ' ') 20 | wc++; 21 | if (*(sw + l) != ' ' && *(sw + l + 1) == 0) 22 | wc++; 23 | l++; 24 | } 25 | return (wc); 26 | } 27 | /** 28 | * _trspace - Moves adress to remove trailig whitespaces 29 | * @st: string 30 | * 31 | * Return: Pointer 32 | */ 33 | char *_trspace(char *st) 34 | { 35 | while (*st == ' ') 36 | st++; 37 | return (st); 38 | } 39 | /** 40 | * strtow - splits a string into words 41 | * @str: string 42 | * 43 | * Return: Double Pointer 44 | */ 45 | char **strtow(char *str) 46 | { 47 | char **s, *ts; 48 | int l, l2, wc, i, j, fr, k; 49 | 50 | if (str == NULL || *str == 0) 51 | return (0); 52 | fr = 0; 53 | wc = _wcount(str); 54 | if (wc == 0) 55 | return (0); 56 | s = malloc((wc + 1) * sizeof(char *)); 57 | if (s == 0) 58 | return (0); 59 | ts = _trspace(str); 60 | for (i = 0; i < wc; i++) 61 | { 62 | l = 0; 63 | while (*(ts + l) != ' ' && *(ts + l) != 0) 64 | l++; 65 | s[i] = malloc((l + 1) * sizeof(char)); 66 | if (s[i] == 0) 67 | { 68 | fr = 1; 69 | break; 70 | } 71 | for (j = 0, l2 = 0; l2 < l; l2++, j++) 72 | s[i][j] = *(ts + l2); 73 | s[i][j] = '\0'; 74 | ts = _trspace(ts + l); 75 | } 76 | s[i] = NULL; 77 | if (fr == 1) 78 | { 79 | for (k = 0; k <= i; k++) 80 | free(s[k]); 81 | free(s); 82 | } 83 | return (s); 84 | } 85 | -------------------------------------------------------------------------------- /0x0B-malloc_free/2-str_concat.c: -------------------------------------------------------------------------------- 1 | # include "main.h" 2 | # include 3 | 4 | /** 5 | * str_concat - concat 2 strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * Return: pointer to string. 9 | */ 10 | 11 | char *str_concat(char *s1, char *s2) 12 | { 13 | char *s; 14 | int i = 0, j = 0, k = 0; 15 | 16 | if (s1 != NULL) 17 | for (; s1[i]; i++) 18 | ; 19 | if (s2 != NULL) 20 | for (; s2[j]; j++) 21 | ; 22 | 23 | s = malloc(sizeof(char) * (i + j + 1)); 24 | if (s == NULL) 25 | return (NULL); 26 | 27 | while (k < i) 28 | { 29 | s[k] = s1[k]; 30 | k++; 31 | } 32 | 33 | while (k < i + j) 34 | { 35 | s[k] = s2[k - i]; 36 | k++; 37 | } 38 | s[k] = '\0'; 39 | return (s); 40 | } 41 | -------------------------------------------------------------------------------- /0x0B-malloc_free/3-alloc_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * alloc_grid - returns a pointer to a 2 dimensional array of integers 7 | * @width: columns 8 | * @height: rows 9 | * 10 | * Return: Double Pointer 11 | */ 12 | 13 | int **alloc_grid(int width, int height) 14 | { 15 | int **s; 16 | int i, j, k, fr; 17 | 18 | fr = 0; 19 | if (width <= 0 || height <= 0) 20 | return (0); 21 | 22 | s = malloc(height * sizeof(int *)); 23 | if (s == 0) 24 | return (0); 25 | for (i = 0; i < height; i++) 26 | { 27 | *(s + i) = malloc(width * sizeof(int)); 28 | if (*(s + i) == 0) 29 | { 30 | fr = 1; 31 | break; 32 | } 33 | for (j = 0; j < width; j++) 34 | { 35 | s[i][j] = 0; 36 | } 37 | } 38 | if (fr == 1) 39 | { 40 | for (k = 0; k <= i; k++) 41 | { 42 | free(*(s + k)); 43 | } 44 | free(s); 45 | } 46 | return (s); 47 | } 48 | -------------------------------------------------------------------------------- /0x0B-malloc_free/4-free_grid.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | 5 | /** 6 | * free_grid - frees a 2 dimensional grid previously created by 7 | * alloc_grid function. 8 | * @grid: matrix double pointer 9 | * @height: rows (pointer) 10 | * 11 | * Return: Nothing 12 | */ 13 | 14 | void free_grid(int **grid, int height) 15 | { 16 | int i; 17 | 18 | for (i = 0; i < height; i++) 19 | { 20 | free(*(grid + i)); 21 | } 22 | free(grid); 23 | } 24 | -------------------------------------------------------------------------------- /0x0B-malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # Malloc in C programming 2 | -------------------------------------------------------------------------------- /0x0B-malloc_free/main.h: -------------------------------------------------------------------------------- 1 | int _putchar(char c); 2 | char *create_array(unsigned int size, char c); 3 | char *_strdup(char *str); 4 | char *str_concat(char *s1, char *s2); 5 | int **alloc_grid(int width, int height); 6 | void free_grid(int **grid, int height); 7 | char *argstostr(int ac, char **av); 8 | char **strtow(char *str); 9 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/0-malloc_checked.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * malloc_checked - allocates memory. 5 | * @b: amount of bytes. 6 | * 7 | * Return: pointer to the allocated memory. 8 | * if malloc fails, status value is equal to 98. 9 | */ 10 | void *malloc_checked(unsigned int b) 11 | { 12 | char *p; 13 | 14 | p = malloc(b); 15 | if (p == NULL) 16 | exit(98); 17 | return (p); 18 | } 19 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/1-string_nconcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * string_nconcat - concatenates two strings. 6 | * @s1: first string. 7 | * @s2: second string. 8 | * @n: amount of bytes. 9 | * 10 | * Return: pointer to the allocated memory. 11 | * if malloc fails, status value is equal to 98. 12 | */ 13 | char *string_nconcat(char *s1, char *s2, unsigned int n) 14 | { 15 | char *sout; 16 | unsigned int ls1, ls2, lsout, i; 17 | 18 | if (s1 == NULL) 19 | s1 = ""; 20 | 21 | if (s2 == NULL) 22 | s2 = ""; 23 | 24 | for (ls1 = 0; s1[ls1] != '\0'; ls1++) 25 | ; 26 | 27 | for (ls2 = 0; s2[ls2] != '\0'; ls2++) 28 | ; 29 | 30 | if (n > ls2) 31 | n = ls2; 32 | 33 | lsout = ls1 + n; 34 | 35 | sout = malloc(lsout + 1); 36 | 37 | if (sout == NULL) 38 | return (NULL); 39 | 40 | for (i = 0; i < lsout; i++) 41 | if (i < ls1) 42 | sout[i] = s1[i]; 43 | else 44 | sout[i] = s2[i - ls1]; 45 | 46 | sout[i] = '\0'; 47 | 48 | return (sout); 49 | } 50 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/100-realloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _realloc - reallocates a memory block. 5 | * @ptr: pointer to the memory previously allocated. 6 | * @old_size: size, in bytes, of the allocated space of ptr. 7 | * @new_size: new size, in bytes, of the new memory block. 8 | * 9 | * Return: ptr. 10 | * if new_size == old_size, returns ptr without changes. 11 | * if malloc fails, returns NULL. 12 | */ 13 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size) 14 | { 15 | if (new_size == 0 && ptr != NULL) 16 | { 17 | free(ptr); 18 | return (NULL); 19 | } 20 | 21 | if (ptr == NULL) 22 | ptr = malloc(new_size); 23 | 24 | if (new_size == old_size) 25 | return (ptr); 26 | 27 | free(ptr); 28 | ptr = malloc(new_size); 29 | 30 | return (ptr); 31 | } 32 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/101-mul.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | #include 4 | #include 5 | 6 | /** 7 | * _is_zero - determines if any number is zero 8 | * @argv: argument vector. 9 | * 10 | * Return: no return. 11 | */ 12 | void _is_zero(char *argv[]) 13 | { 14 | int i, isn1 = 1, isn2 = 1; 15 | 16 | for (i = 0; argv[1][i]; i++) 17 | if (argv[1][i] != '0') 18 | { 19 | isn1 = 0; 20 | break; 21 | } 22 | 23 | for (i = 0; argv[2][i]; i++) 24 | if (argv[2][i] != '0') 25 | { 26 | isn2 = 0; 27 | break; 28 | } 29 | 30 | if (isn1 == 1 || isn2 == 1) 31 | { 32 | printf("0\n"); 33 | exit(0); 34 | } 35 | } 36 | 37 | /** 38 | * _initialize_array - set memery to zero in a new array 39 | * @ar: char array. 40 | * @lar: length of the char array. 41 | * 42 | * Return: pointer of a char array. 43 | */ 44 | char *_initialize_array(char *ar, int lar) 45 | { 46 | int i = 0; 47 | 48 | for (i = 0; i < lar; i++) 49 | ar[i] = '0'; 50 | ar[lar] = '\0'; 51 | return (ar); 52 | } 53 | 54 | /** 55 | * _checknum - determines length of the number 56 | * and checks if number is in base 10. 57 | * @argv: arguments vector. 58 | * @n: row of the array. 59 | * 60 | * Return: length of the number. 61 | */ 62 | int _checknum(char *argv[], int n) 63 | { 64 | int ln; 65 | 66 | for (ln = 0; argv[n][ln]; ln++) 67 | if (!isdigit(argv[n][ln])) 68 | { 69 | printf("Error\n"); 70 | exit(98); 71 | } 72 | 73 | return (ln); 74 | } 75 | 76 | /** 77 | * main - Entry point. 78 | * program that multiplies two positive numbers. 79 | * @argc: number of arguments. 80 | * @argv: arguments vector. 81 | * 82 | * Return: 0 - success. 83 | */ 84 | int main(int argc, char *argv[]) 85 | { 86 | int ln1, ln2, lnout, add, addl, i, j, k, ca; 87 | char *nout; 88 | 89 | if (argc != 3) 90 | printf("Error\n"), exit(98); 91 | ln1 = _checknum(argv, 1), ln2 = _checknum(argv, 2); 92 | _is_zero(argv), lnout = ln1 + ln2, nout = malloc(lnout + 1); 93 | if (nout == NULL) 94 | printf("Error\n"), exit(98); 95 | nout = _initialize_array(nout, lnout); 96 | k = lnout - 1, i = ln1 - 1, j = ln2 - 1, ca = addl = 0; 97 | for (; k >= 0; k--, i--) 98 | { 99 | if (i < 0) 100 | { 101 | if (addl > 0) 102 | { 103 | add = (nout[k] - '0') + addl; 104 | if (add > 9) 105 | nout[k - 1] = (add / 10) + '0'; 106 | nout[k] = (add % 10) + '0'; 107 | } 108 | i = ln1 - 1, j--, addl = 0, ca++, k = lnout - (1 + ca); 109 | } 110 | if (j < 0) 111 | { 112 | if (nout[0] != '0') 113 | break; 114 | lnout--; 115 | free(nout), nout = malloc(lnout + 1), nout = _initialize_array(nout, lnout); 116 | k = lnout - 1, i = ln1 - 1, j = ln2 - 1, ca = addl = 0; 117 | } 118 | if (j >= 0) 119 | { 120 | add = ((argv[1][i] - '0') * (argv[2][j] - '0')) + (nout[k] - '0') + addl; 121 | addl = add / 10, nout[k] = (add % 10) + '0'; 122 | } 123 | } 124 | printf("%s\n", nout); 125 | return (0); 126 | } 127 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/2-calloc.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _calloc - allocates memory for an array. 5 | * @nmemb: number of elements. 6 | * @size: size of bytes. 7 | * 8 | * Return: pointer to the allocated memory. 9 | * if nmemb or size is 0, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | void *_calloc(unsigned int nmemb, unsigned int size) 13 | { 14 | char *p; 15 | unsigned int i; 16 | 17 | if (nmemb == 0 || size == 0) 18 | return (NULL); 19 | 20 | p = malloc(nmemb * size); 21 | 22 | if (p == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; i < (nmemb * size); i++) 26 | p[i] = 0; 27 | 28 | return (p); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/3-array_range.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * array_range - creates an array of integers. 5 | * @min: minimum value. 6 | * @max: maximum value. 7 | * 8 | * Return: pointer to the newly created array. 9 | * if man > mix, returns NULL. 10 | * if malloc fails, returns NULL. 11 | */ 12 | int *array_range(int min, int max) 13 | { 14 | int *ar; 15 | int i; 16 | 17 | if (min > max) 18 | return (NULL); 19 | 20 | ar = malloc(sizeof(*ar) * ((max - min) + 1)); 21 | 22 | if (ar == NULL) 23 | return (NULL); 24 | 25 | for (i = 0; min <= max; i++, min++) 26 | ar[i] = min; 27 | 28 | return (ar); 29 | } 30 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/README.md: -------------------------------------------------------------------------------- 1 | # C - More malloc, free 2 | 3 | In this project, I learned about using `exit`, `calloc`, and `realloc` in C. 4 | 5 | ## Header File :file_folder: 6 | 7 | * [main.h](./main.h): Header file containing prototypes for all functions written in 8 | the project. 9 | 10 | | File | Prototype | 11 | | -------------------- | -------------------------------------------------------------------------- | 12 | | `0-malloc_checked.c` | `void *malloc_checked(unsigned int b);` | 13 | | `1-string_nconcat.c` | `char *string_nconcat(char *s1, char *s2, unsigned int n);` | 14 | | `2-calloc.c` | `char *string_nconcat(char *s1, char *s2, unsigned int n);` | 15 | | `3-array_range.c` | `int *array_range(int min, int max);` | 16 | | `100-realloc.c` | `void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size);` | 17 | 18 | ## Tasks :page_with_curl: 19 | 20 | * **0. Trust no one** 21 | * [0-malloc_checked.c](./0-malloc_checked.c): C function that returns a 22 | pointer to a newly-allocated space in memory using `malloc`. 23 | * If `malloc` fails, the function causes normal process termination with a status value 24 | of `98`. 25 | 26 | * **1. string_nconcat** 27 | * [1-string_nconcat.c](./1-string_nconcat.c): C function that returns a pointer to a 28 | newly-allocated space in memory containing the concatenation of two strings. 29 | * The returned pointer contains `s1` followed by the first `n` bytes 30 | of `s2`, null-terminated. 31 | * If `n` is greater than or equal to the length of `s2`, the entire string `s2` is used. 32 | * If `NULL` is passed, the function treats the parameter as an empty string. 33 | * If the function fails - returns `NULL`. 34 | 35 | * **2. _calloc** 36 | * [2-calloc.c](./2-calloc.c): C function that returns a pointer to a newly-allocated space 37 | in memory for an array, using `malloc`. 38 | * Allocates memory for an array of `nmemb` elements of `size` bytes each. 39 | * The memory is set to zero. 40 | * If `nmemb` = 0, `size` = `0`, or the function fail - returns `NULL`. 41 | 42 | * **3. array_range** 43 | * [3-array_range.c](./3-array_range.c): C function that returns a pointer to a 44 | newly-allocated space in memory containing an array of integers. 45 | * The array contains all the values from parameters `min` to `max`, inclusive, 46 | ordered from `min` to `max`. 47 | * If `min > max` or the function fails - returns `NULL`. 48 | 49 | * **4. _realloc** 50 | * [100-realloc.c](./100-realloc.c): C function that reallocates a memory block using 51 | `malloc` and `free`. 52 | * The parameter `ptr` is a pointer to the memory previously allocated with 53 | a call to `malloc: malloc(old_size)`. 54 | * The paramter `old_size` is the size, in bytes, of the allocated space for `ptr`. 55 | * The paramter `new_size` is the new size, in bytes, of the new memory block. 56 | * The contens of `ptr` are copied to the newly-allocated space in the range from the 57 | start of `ptr` up to the minimum of `old_size` and `new_size`. 58 | * If `new_size` > `old_size`, the "added" memory is not initialized. 59 | * If `new_size` == `old_size`, the function returns `ptr`. 60 | * If `ptr` is `NULL`, the call is equivalent to `malloc(new_size)` for all values of 61 | `old_size` and `new_size`. 62 | * If `new_size` = 0 and `ptr` is not `NULL`, the call is equivalent to 63 | `free(ptr)` and the function returns `NULL`. 64 | 65 | * **5. We must accept finite disappointment, but never lose infinite hope** 66 | * [101-mul.c](./101-mul.c): C program that multiplies two positive numbers. 67 | * Usage: `mul num1 num2`. 68 | * The function assumes `num1` and `num2` are passed in base 10. 69 | * Prints the result followed by a new line. 70 | * If the number of arguments is incorrect or either of `num1` or `num2` 71 | contains non-digits, the function prints `Error` followed by a new line and 72 | exits with a status of `98`. 73 | -------------------------------------------------------------------------------- /0x0C-more_malloc_free/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | void *malloc_checked(unsigned int b); 5 | char *string_nconcat(char *s1, char *s2, unsigned int n); 6 | void *_calloc(unsigned int nmemb, unsigned int size); 7 | int *array_range(int min, int max); 8 | void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /0x0D-preprocessor/0-object_like_macro.h: -------------------------------------------------------------------------------- 1 | #ifndef SIZE 2 | #define SIZE 1024 3 | #endif 4 | -------------------------------------------------------------------------------- /0x0D-preprocessor/1-pi.h: -------------------------------------------------------------------------------- 1 | #ifndef PI 2 | #define PI 3.14159265359 3 | #endif 4 | -------------------------------------------------------------------------------- /0x0D-preprocessor/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * main - Print the the name of the file it was compiled from. 5 | * Return: success (0). 6 | */ 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 ABS_H 2 | #define ABS_H 3 | #define ABS(a) (((a) < 0) ? (-(a)) : (a)) 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/4-sum.h: -------------------------------------------------------------------------------- 1 | #ifndef SUM_H 2 | #define SUM_H 3 | #define SUM(x, y) (x + y) 4 | #endif 5 | -------------------------------------------------------------------------------- /0x0D-preprocessor/README.md: -------------------------------------------------------------------------------- 1 | # C Preprocessors 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/1-init_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * init_dog - Initialize a variable of type struct dog. 5 | * @d: Variable to initialize. 6 | * @name: Dog's name. 7 | * @age: Dog's age. 8 | * @owner: Dog's owner. 9 | */ 10 | void init_dog(struct dog *d, char *name, float age, char *owner) 11 | { 12 | if (d != NULL) 13 | { 14 | d->name = name; 15 | d->age = age; 16 | d->owner = owner; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/2-print_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * print_dog - prints a struct dog 5 | * @d: Pointer to struct 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | void print_dog(struct dog *d) 11 | { 12 | if (d != 0) 13 | { 14 | if ((*d).name != 0) 15 | printf("Name: %s\n", (*d).name); 16 | else 17 | printf("Name: (nil)\n"); 18 | printf("Age: %f\n", (*d).age); 19 | if ((*d).owner != 0) 20 | printf("Owner: %s\n", (*d).owner); 21 | else 22 | printf("Owner: (nil)\n"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/4-new_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * _strdup - returns a pointer to a newly allocated space in memory, which 5 | * contains a copy of the string given as a parameter. 6 | * @str: string to copy 7 | * 8 | * Return: Pointer 9 | */ 10 | char *_strdup(char *str) 11 | { 12 | int l, i; 13 | char *s; 14 | 15 | if (str == NULL) 16 | return (0); 17 | 18 | l = 0; 19 | while (*(str + l)) 20 | l++; 21 | 22 | s = malloc(sizeof(char) * l + 1); 23 | 24 | if (s == 0) 25 | return (0); 26 | 27 | for (i = 0; i <= l; i++) 28 | { 29 | *(s + i) = *(str + i); 30 | } 31 | return (s); 32 | } 33 | /** 34 | * new_dog - creates a new dog 35 | * @name: name of dog 36 | * @age: age of dog 37 | * @owner: owner of dog 38 | * 39 | * Return: On success 1. 40 | * On error, -1 is returned, and errno is set appropriately. 41 | */ 42 | dog_t *new_dog(char *name, float age, char *owner) 43 | { 44 | dog_t *new_dog; 45 | 46 | new_dog = malloc(sizeof(struct dog)); 47 | 48 | if (new_dog == 0 || name == 0 || owner == 0) 49 | return (0); 50 | 51 | new_dog->name = _strdup(name); 52 | if (new_dog->name == 0) 53 | { 54 | free(new_dog); 55 | return (0); 56 | } 57 | new_dog->age = age; 58 | new_dog->owner = _strdup(owner); 59 | if (new_dog->owner == 0) 60 | { 61 | free(new_dog); 62 | free(new_dog->name); 63 | return (0); 64 | } 65 | return (new_dog); 66 | } 67 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/5-free_dog.c: -------------------------------------------------------------------------------- 1 | #include "dog.h" 2 | #include 3 | /** 4 | * free_dog - frees memory of structure dogs 5 | * @d: pointer of structure 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | void free_dog(dog_t *d) 11 | { 12 | if (d != 0) 13 | { 14 | free(d->name); 15 | free(d->owner); 16 | free(d); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/README.md: -------------------------------------------------------------------------------- 1 | # structures & typeofdef in c 2 | -------------------------------------------------------------------------------- /0x0E-structures_typedef/dog.h: -------------------------------------------------------------------------------- 1 | #ifndef STRUCTS 2 | #define STRUCTS 3 | /** 4 | * struct dog - description for a pet 5 | * @name: pet name 6 | * @age: pet age 7 | * @owner: owner 8 | * 9 | * Description: Longer description 10 | */ 11 | typedef struct dog 12 | { 13 | char *name; 14 | float age; 15 | char *owner; 16 | } dog_t; 17 | #endif 18 | #ifndef _FUNCTIONS_H 19 | #define _FUNCTIONS_H 20 | int _putchar(char c); 21 | void init_dog(struct dog *d, char *name, float age, char *owner); 22 | void print_dog(struct dog *d); 23 | dog_t *new_dog(char *name, float age, char *owner); 24 | void free_dog(dog_t *d); 25 | #endif 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/.gitignore: -------------------------------------------------------------------------------- 1 | # ignore all files that start with test- 2 | test 3 | test-* 4 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/0-print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "lists.h" 6 | 7 | /** 8 | * print_list - prints all elements of a list 9 | * @h: takes head of the list 10 | * Return: number of nodes in list 11 | */ 12 | size_t print_list(const list_t *h) 13 | { 14 | size_t n; 15 | 16 | n = 0; 17 | 18 | while (h != NULL) 19 | { 20 | if (h->str == NULL) 21 | printf("[%d] %s\n", 0, "(nil)"); 22 | else 23 | printf("[%d] %s\n", h->len, h->str); 24 | h = h->next; 25 | n++; 26 | } 27 | 28 | return (n); 29 | } 30 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/1-list_len.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "lists.h" 6 | 7 | /** 8 | * list_len - returns no. of elements in a list 9 | * @h: takes head of the list 10 | * Return: number of nodes in list 11 | */ 12 | size_t list_len(const list_t *h) 13 | { 14 | size_t n; 15 | 16 | n = 0; 17 | 18 | while (h != NULL) 19 | { 20 | h = h->next; 21 | n++; 22 | } 23 | 24 | return (n); 25 | } 26 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/100-first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * before_main - function executed before main 5 | * Return: void. 6 | */ 7 | 8 | void __attribute__ ((constructor)) before_main(void) 9 | { 10 | printf("You're beat! and yet, you must allow"); 11 | printf(",\nI bore my house upon my back!\n"); 12 | } 13 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/101-hello_holberton.asm: -------------------------------------------------------------------------------- 1 | SECTION .data 2 | msg: db "Hello, Holberton", 0 3 | fmt: db "%s", 10, 0 4 | 5 | SECTION .text 6 | extern printf 7 | global main 8 | main: 9 | mov esi, msg 10 | mov edi, fmt 11 | mov eax, 0 12 | call printf 13 | 14 | mov eax, 0 15 | ret 16 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/2-add_node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "lists.h" 6 | 7 | /** 8 | * add_node - prints all elements of a list 9 | * @head: takes head of the list 10 | * @str: string data 11 | * Return: number of nodes in list 12 | */ 13 | list_t *add_node(list_t **head, const char *str) 14 | { 15 | list_t *new; 16 | 17 | new = malloc(sizeof(list_t)); 18 | 19 | if (new == NULL) 20 | return (NULL); 21 | 22 | new->str = strdup(str); 23 | new->len = strlen(str); 24 | new->next = *head; 25 | *head = new; 26 | 27 | return (*head); 28 | } 29 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/3-add_node_end.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "lists.h" 6 | 7 | /** 8 | * add_node_end - prints all elements of a list 9 | * @head: takes head of the list 10 | * @str: string data 11 | * Return: number of nodes in list 12 | */ 13 | list_t *add_node_end(list_t **head, const char *str) 14 | { 15 | list_t *new, *temp; 16 | 17 | new = malloc(sizeof(list_t)); 18 | 19 | if (new == NULL) 20 | return (NULL); 21 | 22 | new->str = strdup(str); 23 | new->len = strlen(str); 24 | new->next = NULL; 25 | 26 | temp = *head; 27 | 28 | if (temp == NULL) 29 | *head = new; 30 | else 31 | { 32 | while (temp->next != NULL) 33 | { 34 | temp = temp->next; 35 | } 36 | temp->next = new; 37 | } 38 | 39 | return (*head); 40 | } 41 | -------------------------------------------------------------------------------- /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: void. 7 | */ 8 | 9 | void free_list(list_t *head) 10 | { 11 | list_t *curr; 12 | 13 | while ((curr = head) != NULL) 14 | { 15 | head = head->next; 16 | free(curr->str); 17 | free(curr); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # 0x12-singly_linked_lists 2 | -------------------------------------------------------------------------------- /0x12-singly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_H_ 2 | #define _LISTS_H_ 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int _putchar(char c); 10 | 11 | /** 12 | * struct list_s - singly linked list 13 | * @str: string - (malloc'ed string) 14 | * @len: length of the string 15 | * @next: points to the next node 16 | * 17 | * Description: singly linked list node structure 18 | */ 19 | typedef struct list_s 20 | { 21 | char *str; 22 | unsigned int len; 23 | struct list_s *next; 24 | } list_t; 25 | 26 | size_t print_list(const list_t *h); 27 | size_t list_len(const list_t *h); 28 | list_t *add_node(list_t **head, const char *str); 29 | list_t *add_node_end(list_t **head, const char *str); 30 | void free_list(list_t *head); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/0-print_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * print_dlistint - prints all the elements of a 5 | * dlistint_t list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t print_dlistint(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | printf("%d\n", h->n); 25 | count++; 26 | h = h->next; 27 | } 28 | 29 | return (count); 30 | } 31 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/1-dlistint_len.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * dlistint_len - returns the number of elements in 5 | * a double linked list 6 | * 7 | * @h: head of the list 8 | * Return: the number of nodes 9 | */ 10 | size_t dlistint_len(const dlistint_t *h) 11 | { 12 | int count; 13 | 14 | count = 0; 15 | 16 | if (h == NULL) 17 | return (count); 18 | 19 | while (h->prev != NULL) 20 | h = h->prev; 21 | 22 | while (h != NULL) 23 | { 24 | count++; 25 | h = h->next; 26 | } 27 | 28 | return (count); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/100-password: -------------------------------------------------------------------------------- 1 | en C Pyfo neZ 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/102-result: -------------------------------------------------------------------------------- 1 | 906609 -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/103-keygen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /** 6 | * f4 - finds the biggest number 7 | * 8 | * @usrn: username 9 | * @len: length of username 10 | * Return: the biggest number 11 | */ 12 | int f4(char *usrn, int len) 13 | { 14 | int ch; 15 | int vch; 16 | unsigned int rand_num; 17 | 18 | ch = *usrn; 19 | vch = 0; 20 | 21 | while (vch < len) 22 | { 23 | if (ch < usrn[vch]) 24 | ch = usrn[vch]; 25 | vch += 1; 26 | } 27 | 28 | srand(ch ^ 14); 29 | rand_num = rand(); 30 | 31 | return (rand_num & 63); 32 | } 33 | 34 | /** 35 | * f5 - multiplies each char of username 36 | * 37 | * @usrn: username 38 | * @len: length of username 39 | * Return: multiplied char 40 | */ 41 | int f5(char *usrn, int len) 42 | { 43 | int ch; 44 | int vch; 45 | 46 | ch = vch = 0; 47 | 48 | while (vch < len) 49 | { 50 | ch = ch + usrn[vch] * usrn[vch]; 51 | vch += 1; 52 | } 53 | 54 | return (((unsigned int)ch ^ 239) & 63); 55 | } 56 | 57 | /** 58 | * f6 - generates a random char 59 | * 60 | * @usrn: username 61 | * Return: a random char 62 | */ 63 | int f6(char *usrn) 64 | { 65 | int ch; 66 | int vch; 67 | 68 | ch = vch = 0; 69 | 70 | while (vch < *usrn) 71 | { 72 | ch = rand(); 73 | vch += 1; 74 | } 75 | 76 | return (((unsigned int)ch ^ 229) & 63); 77 | } 78 | 79 | /** 80 | * main - Entry point 81 | * 82 | * @argc: arguments count 83 | * @argv: arguments vector 84 | * Return: Always 0 85 | */ 86 | int main(int argc, char **argv) 87 | { 88 | char keygen[7]; 89 | int len, ch, vch; 90 | long alph[] = { 91 | 0x3877445248432d41, 0x42394530534e6c37, 0x4d6e706762695432, 92 | 0x74767a5835737956, 0x2b554c59634a474f, 0x71786636576a6d34, 93 | 0x723161513346655a, 0x6b756f494b646850 }; 94 | (void) argc; 95 | 96 | for (len = 0; argv[1][len]; len++) 97 | ; 98 | /* ----------- f1 ----------- */ 99 | keygen[0] = ((char *)alph)[(len ^ 59) & 63]; 100 | /* ----------- f2 ----------- */ 101 | ch = vch = 0; 102 | while (vch < len) 103 | { 104 | ch = ch + argv[1][vch]; 105 | vch = vch + 1; 106 | } 107 | keygen[1] = ((char *)alph)[(ch ^ 79) & 63]; 108 | /* ----------- f3 ----------- */ 109 | ch = 1; 110 | vch = 0; 111 | while (vch < len) 112 | { 113 | ch = argv[1][vch] * ch; 114 | vch = vch + 1; 115 | } 116 | keygen[2] = ((char *)alph)[(ch ^ 85) & 63]; 117 | /* ----------- f4 ----------- */ 118 | keygen[3] = ((char *)alph)[f4(argv[1], len)]; 119 | /* ----------- f5 ----------- */ 120 | keygen[4] = ((char *)alph)[f5(argv[1], len)]; 121 | /* ----------- f6 ----------- */ 122 | keygen[5] = ((char *)alph)[f6(argv[1])]; 123 | keygen[6] = '\0'; 124 | for (ch = 0; keygen[ch]; ch++) 125 | printf("%c", keygen[ch]); 126 | return (0); 127 | } 128 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/2-add_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint - adds a new node at the beginning 5 | * of a dlistint_t list 6 | * 7 | * @head: head of the list 8 | * @n: value of the element 9 | * Return: the address of the new element 10 | */ 11 | dlistint_t *add_dnodeint(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *new; 14 | dlistint_t *h; 15 | 16 | new = malloc(sizeof(dlistint_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->prev = NULL; 22 | h = *head; 23 | 24 | if (h != NULL) 25 | { 26 | while (h->prev != NULL) 27 | h = h->prev; 28 | } 29 | 30 | new->next = h; 31 | 32 | if (h != NULL) 33 | h->prev = new; 34 | 35 | *head = new; 36 | 37 | return (new); 38 | } 39 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/3-add_dnodeint_end.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * add_dnodeint_end - adds a new node at the end 5 | * of a dlistint_t list 6 | * 7 | * @head: head of the list 8 | * @n: value of the element 9 | * Return: the address of the new element 10 | */ 11 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n) 12 | { 13 | dlistint_t *h; 14 | dlistint_t *new; 15 | 16 | new = malloc(sizeof(dlistint_t)); 17 | if (new == NULL) 18 | return (NULL); 19 | 20 | new->n = n; 21 | new->next = NULL; 22 | 23 | h = *head; 24 | 25 | if (h != NULL) 26 | { 27 | while (h->next != NULL) 28 | h = h->next; 29 | h->next = new; 30 | } 31 | else 32 | { 33 | *head = new; 34 | } 35 | 36 | new->prev = h; 37 | 38 | return (new); 39 | } 40 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/4-free_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * free_dlistint - frees a dlistint_t list 5 | * 6 | * @head: head of the list 7 | * Return: no return 8 | */ 9 | void free_dlistint(dlistint_t *head) 10 | { 11 | dlistint_t *tmp; 12 | 13 | if (head != NULL) 14 | while (head->prev != NULL) 15 | head = head->prev; 16 | 17 | while ((tmp = head) != NULL) 18 | { 19 | head = head->next; 20 | free(tmp); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/5-get_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * get_dnodeint_at_index - returns the 5 | * nth node of a dlistint_t linked list 6 | * 7 | * @head: head of the list 8 | * @index: index of the nth node 9 | * Return: nth node 10 | */ 11 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index) 12 | { 13 | unsigned int i; 14 | 15 | if (head == NULL) 16 | return (NULL); 17 | 18 | while (head->prev != NULL) 19 | head = head->prev; 20 | 21 | i = 0; 22 | 23 | while (head != NULL) 24 | { 25 | if (i == index) 26 | break; 27 | head = head->next; 28 | i++; 29 | } 30 | 31 | return (head); 32 | } 33 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/6-sum_dlistint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * sum_dlistint - returns the sum of all the data (n) 5 | * of a doubly linked list 6 | * 7 | * @head: head of the list 8 | * Return: sum of the data 9 | */ 10 | int sum_dlistint(dlistint_t *head) 11 | { 12 | int sum; 13 | 14 | sum = 0; 15 | 16 | if (head != NULL) 17 | { 18 | while (head->prev != NULL) 19 | head = head->prev; 20 | 21 | while (head != NULL) 22 | { 23 | sum += head->n; 24 | head = head->next; 25 | } 26 | } 27 | 28 | return (sum); 29 | } 30 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/7-insert_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * insert_dnodeint_at_index - inserts a new node at 5 | * a given position 6 | * 7 | * @h: head of the list 8 | * @idx: index of the new node 9 | * @n: value of the new node 10 | * Return: the address of the new node, or NULL if it failed 11 | */ 12 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n) 13 | { 14 | dlistint_t *new; 15 | dlistint_t *head; 16 | unsigned int i; 17 | 18 | new = NULL; 19 | if (idx == 0) 20 | new = add_dnodeint(h, n); 21 | else 22 | { 23 | head = *h; 24 | i = 1; 25 | if (head != NULL) 26 | while (head->prev != NULL) 27 | head = head->prev; 28 | while (head != NULL) 29 | { 30 | if (i == idx) 31 | { 32 | if (head->next == NULL) 33 | new = add_dnodeint_end(h, n); 34 | else 35 | { 36 | new = malloc(sizeof(dlistint_t)); 37 | if (new != NULL) 38 | { 39 | new->n = n; 40 | new->next = head->next; 41 | new->prev = head; 42 | head->next->prev = new; 43 | head->next = new; 44 | } 45 | } 46 | break; 47 | } 48 | head = head->next; 49 | i++; 50 | } 51 | } 52 | 53 | return (new); 54 | } 55 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/8-delete_dnodeint.c: -------------------------------------------------------------------------------- 1 | #include "lists.h" 2 | 3 | /** 4 | * delete_dnodeint_at_index - deletes the node at index of a 5 | * dlistint_t linked list 6 | * 7 | * @head: head of the list 8 | * @index: index of the new node 9 | * Return: 1 if it succeeded, -1 if it failed 10 | */ 11 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index) 12 | { 13 | dlistint_t *h1; 14 | dlistint_t *h2; 15 | unsigned int i; 16 | 17 | h1 = *head; 18 | 19 | if (h1 != NULL) 20 | while (h1->prev != NULL) 21 | h1 = h1->prev; 22 | 23 | i = 0; 24 | 25 | while (h1 != NULL) 26 | { 27 | if (i == index) 28 | { 29 | if (i == 0) 30 | { 31 | *head = h1->next; 32 | if (*head != NULL) 33 | (*head)->prev = NULL; 34 | } 35 | else 36 | { 37 | h2->next = h1->next; 38 | 39 | if (h1->next != NULL) 40 | h1->next->prev = h2; 41 | } 42 | 43 | free(h1); 44 | return (1); 45 | } 46 | h2 = h1; 47 | h1 = h1->next; 48 | i++; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/README.md: -------------------------------------------------------------------------------- 1 | # Doubly linked lists 2 | -------------------------------------------------------------------------------- /0x17-doubly_linked_lists/lists.h: -------------------------------------------------------------------------------- 1 | #ifndef _LISTS_ 2 | #define _LISTS_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct dlistint_s - doubly linked list 9 | * @n: integer 10 | * @prev: points to the previous node 11 | * @next: points to the next node 12 | * 13 | * Description: doubly linked list node structure 14 | * for Holberton project 15 | */ 16 | typedef struct dlistint_s 17 | { 18 | int n; 19 | struct dlistint_s *prev; 20 | struct dlistint_s *next; 21 | } dlistint_t; 22 | 23 | size_t print_dlistint(const dlistint_t *h); 24 | size_t dlistint_len(const dlistint_t *h); 25 | dlistint_t *add_dnodeint(dlistint_t **head, const int n); 26 | dlistint_t *add_dnodeint_end(dlistint_t **head, const int n); 27 | void free_dlistint(dlistint_t *head); 28 | dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index); 29 | int sum_dlistint(dlistint_t *head); 30 | dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n); 31 | int delete_dnodeint_at_index(dlistint_t **head, unsigned int index); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/0-isupper.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isupper - checks for uppercase character 5 | * @c: the character to be checked 6 | * Return: 1 if c is uppercase, 0 otherwise 7 | */ 8 | int _isupper(int c) 9 | { 10 | return (c >= 'A' && c <= 'Z'); 11 | } 12 | -------------------------------------------------------------------------------- /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 | } 15 | -------------------------------------------------------------------------------- /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/0-strcat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * *_strcat - function commute srtings 5 | * @dest: param pointer to a char 6 | * @src: param pointer to a char 7 | * Return: return value of dest 8 | */ 9 | 10 | char *_strcat(char *dest, char *src) 11 | { 12 | int i; 13 | int j; 14 | 15 | i = 0; 16 | j = 0; 17 | 18 | while (dest[i] != '\0') 19 | { 20 | i++; 21 | } 22 | while (src[j] != '\0') 23 | { 24 | dest[i] = src[j]; 25 | j++; 26 | i++; 27 | } 28 | 29 | dest[i] = '\0'; 30 | return (dest); 31 | } 32 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-create_dynamic_lib.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc *.c -c -fPIC 3 | gcc *.o -shared -o liball.so 4 | export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH 5 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-isdigit.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _isdigit - checks for a digit (0 through 9) 5 | * @c: int to be checked 6 | * Return: 1 if c is a digit, 0 otherwise 7 | */ 8 | int _isdigit(int c) 9 | { 10 | return (c >= '0' && c <= '9'); 11 | } 12 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int add(int a, int b); 4 | int sub(int a, int b); 5 | int mul(int a, int b); 6 | int div(int a, int b); 7 | int mod(int a, int b); 8 | 9 | int add(int a, int b) 10 | { 11 | return a + b; 12 | } 13 | 14 | int sub(int a, int b) 15 | { 16 | return a - b; 17 | } 18 | int mul(int a, int b) 19 | { 20 | return a * b; 21 | } 22 | int div(int a, int b) 23 | { 24 | return a / b; 25 | } 26 | int mod(int a, int b) 27 | { 28 | return a % b; 29 | } 30 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-memcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _memcpy - copies memory area, 4 | * @dest: destination memory area. 5 | * @src: source memory area. 6 | * @n: bytes filled. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_memcpy(char *dest, char *src, unsigned int n) 10 | { 11 | unsigned int i; 12 | 13 | for (i = 0; i < n; i++) 14 | *(dest + i) = *(src + i); 15 | 16 | return (dest); 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/1-strncat.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncat - concatenates two strings, 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes used from src. 7 | * Return: the pointer to dest. 8 | */ 9 | char *_strncat(char *dest, char *src, int n) 10 | { 11 | int count = 0, count2 = 0; 12 | 13 | while (*(dest + count) != '\0') 14 | { 15 | count++; 16 | } 17 | 18 | while (count2 < n) 19 | { 20 | *(dest + count) = *(src + count2); 21 | if (*(src + count2) == '\0') 22 | break; 23 | count++; 24 | count2++; 25 | } 26 | return (dest); 27 | } 28 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/100-atoi.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _atoi - converts a string to an integer. 5 | * @s: input string. 6 | * Return: integer. 7 | */ 8 | int _atoi(char *s) 9 | { 10 | unsigned int count = 0, size = 0, oi = 0, pn = 1, m = 1, i; 11 | 12 | while (*(s + count) != '\0') 13 | { 14 | if (size > 0 && (*(s + count) < '0' || *(s + count) > '9')) 15 | break; 16 | if (*(s + count) == '-') 17 | pn *= -1; 18 | 19 | if ((*(s + count) >= '0') && (*(s + count) <= '9')) 20 | { 21 | if (size > 0) 22 | m *= 10; 23 | size++; 24 | } 25 | count++; 26 | } 27 | 28 | for (i = count - size; i < count; i++) 29 | { 30 | oi = oi + ((*(s + i) - 48) * m); 31 | m /= 10; 32 | } 33 | return (oi * pn); 34 | } 35 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/101-make_me_win.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget -P /tmp/ https://raw.github.com/luischaparroc/holbertonschool-low_level_programming/master/0x18-dynamic_libraries/nrandom.so 3 | export LD_PRELOAD=/tmp/nrandom.so 4 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strchr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strchr - locates a character in a string, 4 | * @s: string. 5 | * @c: character. 6 | * Return: the pointer to the first occurrence of the character c. 7 | */ 8 | char *_strchr(char *s, char c) 9 | { 10 | unsigned int i = 0; 11 | 12 | for (; *(s + i) != '\0'; i++) 13 | if (*(s + i) == c) 14 | return (s + i); 15 | if (*(s + i) == c) 16 | return (s + i); 17 | return ('\0'); 18 | } 19 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strlen.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strlen - returns the length of a string. 5 | * @s: input stringeturn. 6 | * Return: length of a string. 7 | */ 8 | int _strlen(char *s) 9 | { 10 | int count = 0; 11 | 12 | while (*(s + count) != '\0') 13 | count++; 14 | return (count); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/2-strncpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strncpy - copies a string 4 | * @dest: destination. 5 | * @src: source. 6 | * @n: amount of bytes from src. 7 | * Return: the pointer to dest. 8 | */ 9 | 10 | char *_strncpy(char *dest, char *src, int n) 11 | { 12 | int i; 13 | 14 | for (i = 0; i < n && src[i] != '\0'; i++) 15 | dest[i] = src[i]; 16 | for ( ; i < n; i++) 17 | dest[i] = '\0'; 18 | 19 | return (dest); 20 | } 21 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-islower.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _islower - prints 1 or 0 depending on input 5 | * @c: first parameter 6 | * 7 | * Description: prints all lowercase letters 8 | * Return: Always(0). 9 | */ 10 | 11 | int _islower(int c) 12 | { 13 | return (c >= 'a' && c <= 'z'); 14 | } 15 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-puts.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _puts - prints a string, followed by a new line, 5 | * @str: pointer to the string to print 6 | * Return: void 7 | */ 8 | 9 | void _puts(char *str) 10 | { 11 | int i = 0; 12 | 13 | while (str[i]) 14 | { 15 | _putchar(str[i]); 16 | i++; 17 | } 18 | _putchar('\n'); 19 | } 20 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strcmp.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _strcmp - compares two strings 4 | * @s1: first string. 5 | * @s2: second string. 6 | * Return: 0 if s1 and s2 are equals, 7 | * another number if not. 8 | */ 9 | 10 | int _strcmp(char *s1, char *s2) 11 | { 12 | int i = 0, op = 0; 13 | 14 | while (op == 0) 15 | { 16 | if ((*(s1 + i) == '\0') && (*(s2 + i) == '\0')) 17 | break; 18 | op = *(s1 + i) - *(s2 + i); 19 | i++; 20 | } 21 | 22 | return (op); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/3-strspn.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strspn - gets the length of a prefix substring. 5 | * @s: initial segment. 6 | * @accept: accepted bytes. 7 | * Return: the number of accepted bytes. 8 | */ 9 | unsigned int _strspn(char *s, char *accept) 10 | { 11 | unsigned int i, j, bool; 12 | 13 | for (i = 0; *(s + i) != '\0'; i++) 14 | { 15 | bool = 1; 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | { 20 | bool = 0; 21 | break; 22 | } 23 | } 24 | if (bool == 1) 25 | break; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-isalpha.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _isalpha - Check if character is a alphabet character. 4 | * @c: type int character 5 | * Return: 1 if letter, lowercase or uppercase, and 0 otherwise 6 | */ 7 | 8 | int _isalpha(int c) 9 | { 10 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 11 | return (1); 12 | 13 | else 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/4-strpbrk.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strpbrk - searches a string for any of a set of bytes. 5 | * @s: first string. 6 | * @accept: second string. 7 | * Return: a pointer to the byte in s that matches one of the 8 | * bytes in accept, or NULL if no such byte is found. 9 | */ 10 | char *_strpbrk(char *s, char *accept) 11 | { 12 | unsigned int i, j; 13 | 14 | for (i = 0; *(s + i) != '\0'; i++) 15 | { 16 | for (j = 0; *(accept + j) != '\0'; j++) 17 | { 18 | if (*(s + i) == *(accept + j)) 19 | return (s + i); 20 | } 21 | } 22 | return ('\0'); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/5-strstr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _strstr - finds the first occurrence of the substring. 5 | * needle in the string haystack. 6 | * @haystack: entire string. 7 | * @needle: substring. 8 | * Return: pointer to the beginning of located substring or 9 | * NULL if the substring is not found. 10 | */ 11 | char *_strstr(char *haystack, char *needle) 12 | { 13 | char *bhaystack; 14 | char *pneedle; 15 | 16 | while (*haystack != '\0') 17 | { 18 | bhaystack = haystack; 19 | pneedle = needle; 20 | 21 | while (*haystack != '\0' && *pneedle != '\0' && *haystack == *pneedle) 22 | { 23 | haystack++; 24 | pneedle++; 25 | } 26 | if (!*pneedle) 27 | return (bhaystack); 28 | haystack = bhaystack + 1; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/6-abs.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * _abs - compute the absolute value of an integer 4 | * @n: int type number 5 | * Return: absolute value of @n 6 | */ 7 | int _abs(int n) 8 | { 9 | if (n < 0) 10 | { 11 | return (n * -1); 12 | } 13 | else 14 | { 15 | return (n); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/9-strcpy.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * _strcpy - copies the string pointed to by src, 5 | * including the terminating null byte, to the 6 | * buffer pointed to by dest. 7 | * @dest: destination. 8 | * @src: source. 9 | * Return: the pointer to dest. 10 | */ 11 | char *_strcpy(char *dest, char *src) 12 | { 13 | int count = 0; 14 | 15 | while (count >= 0) 16 | { 17 | *(dest + count) = *(src + count); 18 | if (*(src + count) == '\0') 19 | break; 20 | count++; 21 | } 22 | return (dest); 23 | } 24 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/README.md: -------------------------------------------------------------------------------- 1 | # C - Dynamic libraries 2 | In this project, learned about using dynamic libraries in C, including how to create them and how to use them with `$LD_LIBRARY_PATH`, `nm`, `ldd`, and `ldconfig`. 3 | 4 | ### Tasks 5 | * 0. A library is not a luxury but one of the necessities of life 6 | 7 | * libdynamic.so: C dynamic library containing all the functions listed below: 8 | 9 | * `int _putchar(char c);` 10 | * `int _islower(int c);` 11 | * `int _isalpha(int c);` 12 | * `int _abs(int n);` 13 | * `int _isupper(int c);` 14 | * `int _isdigit(int c);` 15 | * `int _strlen(char *s);` 16 | * `void _puts(char *s);` 17 | * `char *_strcpy(char *dest, char *src);` 18 | * `int _atoi(char *s);` 19 | * `char *_strcat(char *dest, char *src);` 20 | * `char *_strncat(char *dest, char *src, int n);` 21 | * `char *_strncpy(char *dest, char *src, int n);` 22 | * `int _strcmp(char *s1, char *s2);` 23 | * `char *_memset(char *s, char b, unsigned int n);` 24 | * `char *_memcpy(char *dest, char *src, unsigned int n);` 25 | * `char *_strchr(char *s, char c);` 26 | * `unsigned int _strspn(char *s, char *accept);` 27 | * `char *_strpbrk(char *s, char *accept);` 28 | * `char *_strstr(char *haystack, char *needle);` 29 | * main.h: Header file containing the prototypes of all functions included in `libdynamic.so`. 30 | 31 | * __1. Without libraries what have we? We have no past and no future__ 32 | 33 | 1-create\_dynamic\_lib.sh: Bash script that creates a dynamic library called `liball.so` from all the `.c` files in the current directory. 34 | * __2. Let's call C functions from Python__ 35 | 36 | * 100-operations.so: C dynamic library containing basic C mathematical operation functions that can be called from Python. 37 | * Includes: 38 | * `int add(int a, int b);` 39 | * `int sub(int a, int b);` 40 | * `int mul(int a, int b);` 41 | * `int div(int a, int b);` 42 | * `int mod(int a, int b);` 43 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/_putchar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * _putchar - writes the character c to stdout 5 | * @c: The character to print 6 | * 7 | * Return: On success 1. 8 | * On error, -1 is returned, and errno is set appropriately. 9 | */ 10 | int _putchar(char c) 11 | { 12 | return (write(1, &c, 1)); 13 | } 14 | -------------------------------------------------------------------------------- /0x18-dynamic_libraries/libdynamic.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x18-dynamic_libraries/libdynamic.so -------------------------------------------------------------------------------- /0x18-dynamic_libraries/main.h: -------------------------------------------------------------------------------- 1 | #ifndef __MAIN_H__ 2 | #define __MAIN_H__ 3 | 4 | int _putchar(char c); 5 | int _islower(int c); 6 | int _isalpha(int c); 7 | int _abs(int n); 8 | int _isupper(int c); 9 | int _isdigit(int c); 10 | int _strlen(char *s); 11 | void _puts(char *s); 12 | char *_strcpy(char *dest, char *src); 13 | int _atoi(char *s); 14 | char *_strcat(char *dest, char *src); 15 | char *_strncat(char *dest, char *src, int n); 16 | char *_strncpy(char *dest, char *src, int n); 17 | int _strcmp(char *s1, char *s2); 18 | char *_memset(char *s, char b, unsigned int n); 19 | char *_memcpy(char *dest, char *src, unsigned int n); 20 | char *_strchr(char *s, char c); 21 | unsigned int _strspn(char *s, char *accept); 22 | char *_strpbrk(char *s, char *accept); 23 | char *_strstr(char *haystack, char *needle); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /0x1A-hash_tables/0-hash_table_create.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_create - creates a hash table with a given size 5 | * 6 | * @size: size of the hash table 7 | * Return: the created hash table, or NULL if function fails 8 | */ 9 | hash_table_t *hash_table_create(unsigned long int size) 10 | { 11 | hash_table_t *table; 12 | hash_node_t **array; 13 | unsigned long int i; 14 | 15 | table = malloc(sizeof(hash_table_t)); 16 | if (table == NULL) 17 | return (NULL); 18 | 19 | array = malloc(sizeof(hash_node_t *) * size); 20 | if (array == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | array[i] = NULL; 25 | 26 | table->array = array; 27 | table->size = size; 28 | 29 | return (table); 30 | } 31 | -------------------------------------------------------------------------------- /0x1A-hash_tables/1-djb2.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_djb2 - function that generates a random number through 5 | * a string given (hash number) 6 | * 7 | * @str: string 8 | * Return: hash number 9 | */ 10 | unsigned long int hash_djb2(const unsigned char *str) 11 | { 12 | unsigned long int hash; 13 | int c; 14 | 15 | hash = 5381; 16 | while ((c = *str++)) 17 | { 18 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 19 | } 20 | return (hash); 21 | } 22 | -------------------------------------------------------------------------------- /0x1A-hash_tables/100-sorted_hash_table.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * shash_table_create - creates a shash table with a given size 5 | * 6 | * @size: size of the shash table 7 | * Return: the created shash table, or NULL if function fails 8 | */ 9 | shash_table_t *shash_table_create(unsigned long int size) 10 | { 11 | shash_table_t *table; 12 | shash_node_t **array; 13 | unsigned long int i; 14 | 15 | table = malloc(sizeof(shash_table_t)); 16 | if (table == NULL) 17 | return (NULL); 18 | 19 | array = malloc(sizeof(shash_node_t *) * size); 20 | if (array == NULL) 21 | return (NULL); 22 | 23 | for (i = 0; i < size; i++) 24 | array[i] = NULL; 25 | 26 | table->array = array; 27 | table->size = size; 28 | table->shead = NULL; 29 | table->stail = NULL; 30 | 31 | return (table); 32 | } 33 | 34 | /** 35 | * add_n_shash - adds a node at the beginning of a shash at a given index 36 | * 37 | * @h: head of the shash linked list 38 | * @key: key of the shash 39 | * @value: value to store 40 | * Return: created node 41 | */ 42 | shash_node_t *add_n_shash(shash_node_t **h, const char *key, const char *value) 43 | { 44 | shash_node_t *tmp; 45 | 46 | tmp = *h; 47 | 48 | while (tmp != NULL) 49 | { 50 | if (strcmp(key, tmp->key) == 0) 51 | { 52 | free(tmp->value); 53 | tmp->value = strdup(value); 54 | return (tmp); 55 | } 56 | tmp = tmp->next; 57 | } 58 | 59 | tmp = malloc(sizeof(shash_node_t)); 60 | 61 | if (tmp == NULL) 62 | return (NULL); 63 | 64 | tmp->key = strdup(key); 65 | tmp->value = strdup(value); 66 | tmp->next = *h; 67 | 68 | *h = tmp; 69 | 70 | return (tmp); 71 | } 72 | 73 | /** 74 | * add_i_shash - adds a node on the DLL of the shash table 75 | * 76 | * @ht: pointer to the table 77 | * @new: new node to add 78 | * Return: no return 79 | */ 80 | void add_i_shash(shash_table_t *ht, shash_node_t *new) 81 | { 82 | shash_node_t *tmp1, *tmp2; 83 | int ret; 84 | 85 | tmp1 = tmp2 = ht->shead; 86 | 87 | while (tmp1 != NULL) 88 | { 89 | ret = strcmp(new->key, tmp1->key); 90 | if (ret == 0) 91 | { 92 | return; 93 | } 94 | else if (ret < 0) 95 | { 96 | new->sprev = tmp1->sprev; 97 | 98 | if (tmp1->sprev) 99 | tmp1->sprev->snext = new; 100 | else 101 | ht->shead = new; 102 | 103 | tmp1->sprev = new; 104 | new->snext = tmp1; 105 | 106 | return; 107 | } 108 | tmp2 = tmp1; 109 | tmp1 = tmp1->snext; 110 | } 111 | 112 | new->sprev = tmp2; 113 | new->snext = NULL; 114 | 115 | if (ht->shead) 116 | tmp2->snext = new; 117 | else 118 | ht->shead = new; 119 | 120 | ht->stail = new; 121 | } 122 | 123 | /** 124 | * shash_table_set - adds a hash (key, value) to a given shash table 125 | * 126 | * @ht: pointer to the shash table 127 | * @key: key of the shash 128 | * @value: value to store 129 | * Return: 1 if successes, 0 if fails 130 | */ 131 | int shash_table_set(shash_table_t *ht, const char *key, const char *value) 132 | { 133 | unsigned long int k_index; 134 | shash_node_t *new; 135 | 136 | if (ht == NULL) 137 | return (0); 138 | 139 | if (key == NULL || *key == '\0') 140 | return (0); 141 | 142 | k_index = key_index((unsigned char *)key, ht->size); 143 | 144 | new = add_n_shash(&(ht->array[k_index]), key, value); 145 | 146 | if (new == NULL) 147 | return (0); 148 | 149 | add_i_shash(ht, new); 150 | 151 | return (1); 152 | } 153 | 154 | /** 155 | * shash_table_get - retrieves a value associated with a key 156 | * 157 | * @ht: pointer to the shash table 158 | * @key: key of the shash 159 | * Return: value of the shash. 160 | */ 161 | char *shash_table_get(const shash_table_t *ht, const char *key) 162 | { 163 | unsigned long int k_index; 164 | shash_node_t *tmp; 165 | 166 | if (ht == NULL) 167 | return (NULL); 168 | 169 | if (key == NULL || *key == '\0') 170 | return (NULL); 171 | 172 | k_index = key_index((unsigned char *)key, ht->size); 173 | 174 | tmp = ht->array[k_index]; 175 | 176 | while (tmp != NULL) 177 | { 178 | if (strcmp(tmp->key, key) == 0) 179 | return (tmp->value); 180 | tmp = tmp->next; 181 | } 182 | 183 | return (NULL); 184 | } 185 | 186 | /** 187 | * shash_table_print - prints the keys and values of the shash table 188 | * 189 | * @ht: pointer to the shash table 190 | * Return: no return 191 | */ 192 | void shash_table_print(const shash_table_t *ht) 193 | { 194 | shash_node_t *tmp; 195 | char *sep; 196 | 197 | if (ht == NULL) 198 | return; 199 | 200 | printf("{"); 201 | sep = ""; 202 | 203 | tmp = ht->shead; 204 | 205 | while (tmp != NULL) 206 | { 207 | printf("%s'%s': '%s'", sep, tmp->key, tmp->value); 208 | sep = ", "; 209 | tmp = tmp->snext; 210 | } 211 | 212 | printf("}\n"); 213 | } 214 | 215 | /** 216 | * shash_table_print_rev - prints the keys and values of the shash table 217 | * in reverse 218 | * 219 | * @ht: pointer to the shash table 220 | * Return: no return 221 | */ 222 | void shash_table_print_rev(const shash_table_t *ht) 223 | { 224 | shash_node_t *tmp; 225 | char *sep; 226 | 227 | if (ht == NULL) 228 | return; 229 | 230 | printf("{"); 231 | sep = ""; 232 | 233 | tmp = ht->stail; 234 | 235 | while (tmp != NULL) 236 | { 237 | printf("%s'%s': '%s'", sep, tmp->key, tmp->value); 238 | sep = ", "; 239 | tmp = tmp->sprev; 240 | } 241 | 242 | printf("}\n"); 243 | } 244 | 245 | /** 246 | * shash_table_delete - deletes a shash table 247 | * 248 | * @ht: pointer to the shash table 249 | * Return: no return 250 | */ 251 | void shash_table_delete(shash_table_t *ht) 252 | { 253 | unsigned long int i; 254 | shash_node_t *tmp1; 255 | shash_node_t *tmp2; 256 | 257 | if (ht == NULL) 258 | return; 259 | 260 | for (i = 0; i < ht->size; i++) 261 | { 262 | tmp1 = ht->array[i]; 263 | while ((tmp2 = tmp1) != NULL) 264 | { 265 | tmp1 = tmp1->next; 266 | free(tmp2->key); 267 | free(tmp2->value); 268 | free(tmp2); 269 | } 270 | } 271 | free(ht->array); 272 | free(ht); 273 | } 274 | -------------------------------------------------------------------------------- /0x1A-hash_tables/2-key_index.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * key_index - function that generates a distributed index 5 | * according to a given hash 6 | * 7 | * @key: key passed 8 | * @size: size of the hash tables 9 | * Return: key index 10 | */ 11 | unsigned long int key_index(const unsigned char *key, unsigned long int size) 12 | { 13 | if (key == NULL || size == 0) 14 | return (0); 15 | return (hash_djb2(key) % size); 16 | } 17 | -------------------------------------------------------------------------------- /0x1A-hash_tables/3-hash_table_set.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * add_n_hash - adds a node at the beginning of a hash at a given index 5 | * 6 | * @head: head of the hash linked list 7 | * @key: key of the hash 8 | * @value: value to store 9 | * Return: head of the hash 10 | */ 11 | hash_node_t *add_n_hash(hash_node_t **head, const char *key, const char *value) 12 | { 13 | hash_node_t *tmp; 14 | 15 | tmp = *head; 16 | 17 | while (tmp != NULL) 18 | { 19 | if (strcmp(key, tmp->key) == 0) 20 | { 21 | free(tmp->value); 22 | tmp->value = strdup(value); 23 | return (*head); 24 | } 25 | tmp = tmp->next; 26 | } 27 | 28 | tmp = malloc(sizeof(hash_node_t)); 29 | 30 | if (tmp == NULL) 31 | return (NULL); 32 | 33 | tmp->key = strdup(key); 34 | tmp->value = strdup(value); 35 | tmp->next = *head; 36 | *head = tmp; 37 | 38 | return (*head); 39 | } 40 | 41 | /** 42 | * hash_table_set - adds a hash (key, value) to a given hash table 43 | * 44 | * @ht: pointer to the hash table 45 | * @key: key of the hash 46 | * @value: value to store 47 | * Return: 1 if successes, 0 if fails 48 | */ 49 | int hash_table_set(hash_table_t *ht, const char *key, const char *value) 50 | { 51 | unsigned long int k_index; 52 | 53 | if (ht == NULL) 54 | return (0); 55 | 56 | if (key == NULL || *key == '\0') 57 | return (0); 58 | 59 | k_index = key_index((unsigned char *)key, ht->size); 60 | 61 | if (add_n_hash(&(ht->array[k_index]), key, value) == NULL) 62 | return (0); 63 | 64 | return (1); 65 | } 66 | -------------------------------------------------------------------------------- /0x1A-hash_tables/4-hash_table_get.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_get - retrieves a value associated with a key 5 | * 6 | * @ht: pointer to the hash table 7 | * @key: key of the hash 8 | * Return: value of the hash. 9 | */ 10 | char *hash_table_get(const hash_table_t *ht, const char *key) 11 | { 12 | unsigned long int k_index; 13 | hash_node_t *tmp; 14 | 15 | if (ht == NULL) 16 | return (NULL); 17 | 18 | if (key == NULL || *key == '\0') 19 | return (NULL); 20 | 21 | k_index = key_index((unsigned char *)key, ht->size); 22 | 23 | tmp = ht->array[k_index]; 24 | 25 | while (tmp != NULL) 26 | { 27 | if (strcmp(tmp->key, key) == 0) 28 | return (tmp->value); 29 | tmp = tmp->next; 30 | } 31 | 32 | return (NULL); 33 | } 34 | -------------------------------------------------------------------------------- /0x1A-hash_tables/5-hash_table_print.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_print - prints the keys and values of the hash table 5 | * 6 | * @ht: pointer to the hash table 7 | * Return: no return 8 | */ 9 | void hash_table_print(const hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *tmp; 13 | char *sep; 14 | 15 | if (ht == NULL) 16 | return; 17 | 18 | printf("{"); 19 | sep = ""; 20 | 21 | for (i = 0; i < ht->size; i++) 22 | { 23 | tmp = ht->array[i]; 24 | while (tmp != NULL) 25 | { 26 | printf("%s'%s': '%s'", sep, tmp->key, tmp->value); 27 | sep = ", "; 28 | tmp = tmp->next; 29 | } 30 | } 31 | printf("}\n"); 32 | } 33 | -------------------------------------------------------------------------------- /0x1A-hash_tables/6-hash_table_delete.c: -------------------------------------------------------------------------------- 1 | #include "hash_tables.h" 2 | 3 | /** 4 | * hash_table_delete - deletes a hash table 5 | * 6 | * @ht: pointer to the hash table 7 | * Return: no return 8 | */ 9 | void hash_table_delete(hash_table_t *ht) 10 | { 11 | unsigned long int i; 12 | hash_node_t *tmp1; 13 | hash_node_t *tmp2; 14 | 15 | if (ht == NULL) 16 | return; 17 | 18 | for (i = 0; i < ht->size; i++) 19 | { 20 | tmp1 = ht->array[i]; 21 | while ((tmp2 = tmp1) != NULL) 22 | { 23 | tmp1 = tmp1->next; 24 | free(tmp2->key); 25 | free(tmp2->value); 26 | free(tmp2); 27 | } 28 | } 29 | free(ht->array); 30 | free(ht); 31 | } 32 | -------------------------------------------------------------------------------- /0x1A-hash_tables/README.md: -------------------------------------------------------------------------------- 1 | # Hash Tables 2 | -------------------------------------------------------------------------------- /0x1A-hash_tables/hash_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef __HASH__ 2 | #define __HASH__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * * struct shash_node_s - Node of a sorted hash table 10 | * * 11 | * * @key: The key, string 12 | * * The key is unique in the HashTable 13 | * * @value: The value corresponding to a key 14 | * * @next: A pointer to the next node of the List 15 | * * @sprev: A pointer to the previous element of the sorted linked list 16 | * * @snext: A pointer to the next element of the sorted linked list 17 | * */ 18 | typedef struct shash_node_s 19 | { 20 | char *key; 21 | char *value; 22 | struct shash_node_s *next; 23 | struct shash_node_s *sprev; 24 | struct shash_node_s *snext; 25 | } shash_node_t; 26 | 27 | /** 28 | * * struct shash_table_s - Sorted hash table data structure 29 | * * 30 | * * @size: The size of the array 31 | * * @array: An array of size @size 32 | * * Each cell of this array is a pointer to the first node of a linked list, 33 | * * because we want our HashTable to use a Chaining collision handling 34 | * * @shead: A pointer to the first element of the sorted linked list 35 | * * @stail: A pointer to the last element of the sorted linked list 36 | * */ 37 | typedef struct shash_table_s 38 | { 39 | unsigned long int size; 40 | shash_node_t **array; 41 | shash_node_t *shead; 42 | shash_node_t *stail; 43 | } shash_table_t; 44 | 45 | /** 46 | * * struct hash_node_s - Node of a hash table 47 | * * 48 | * * @key: The key, string 49 | * * The key is unique in the HashTable 50 | * * @value: The value corresponding to a key 51 | * * @next: A pointer to the next node of the List 52 | * */ 53 | typedef struct hash_node_s 54 | { 55 | char *key; 56 | char *value; 57 | struct hash_node_s *next; 58 | } hash_node_t; 59 | 60 | /** 61 | * * struct hash_table_s - Hash table data structure 62 | * * 63 | * * @size: The size of the array 64 | * * @array: An array of size @size 65 | * * Each cell of this array is a pointer to the first node of a linked list, 66 | * * because we want our HashTable to use a Chaining collision handling 67 | * */ 68 | typedef struct hash_table_s 69 | { 70 | unsigned long int size; 71 | hash_node_t **array; 72 | } hash_table_t; 73 | 74 | /* 0-hash_table_create.c */ 75 | hash_table_t *hash_table_create(unsigned long int size); 76 | 77 | /* 1-djb2.c */ 78 | unsigned long int hash_djb2(const unsigned char *str); 79 | 80 | /* 2-key_index.c */ 81 | unsigned long int key_index(const unsigned char *key, unsigned long int size); 82 | 83 | /* 3-hash_table_set.c */ 84 | hash_node_t *add_n_hash(hash_node_t **, const char *, const char *); 85 | int hash_table_set(hash_table_t *ht, const char *key, const char *value); 86 | 87 | /* 4-hash_table_get.c */ 88 | char *hash_table_get(const hash_table_t *ht, const char *key); 89 | 90 | /* 5-hash_table_print.c */ 91 | void hash_table_print(const hash_table_t *ht); 92 | 93 | /* 6-hash_table_delete.c */ 94 | void hash_table_delete(hash_table_t *ht); 95 | 96 | /* 100-sorted_hash_table.c */ 97 | shash_table_t *shash_table_create(unsigned long int size); 98 | shash_node_t *add_n_shash(shash_node_t **, const char *, const char *); 99 | void add_i_shash(shash_table_t *ht, shash_node_t *new); 100 | int shash_table_set(shash_table_t *ht, const char *key, const char *value); 101 | char *shash_table_get(const shash_table_t *ht, const char *key); 102 | void shash_table_print(const shash_table_t *ht); 103 | void shash_table_print_rev(const shash_table_t *ht); 104 | void shash_table_delete(shash_table_t *ht); 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /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/README.md: -------------------------------------------------------------------------------- 1 | # C - Makefiles 2 | 3 | In this project, I practiced writing Makefiles. 4 | 5 | ![makefile](./images/makefile.gif) 6 | 7 | ## Tests :heavy_check_mark: 8 | 9 | * [tests](./tests): Folder of test files. 10 | 11 | ## Helper Files 12 | 13 | * [school.c](./school.c): C function that displays a seahorse in text. 14 | Used for Makefile practice purposes throughout project. 15 | 16 | * [main.c](./main.c): Main C function that runs the function defined in 17 | [school.c](./school.c). 18 | 19 | ## Header File :file_folder: 20 | 21 | * [m.h](./m.h): Header file defining the function prototype used in `school.c`. 22 | 23 | ## Tasks 24 | 25 | * **0. make -f 0-Makefile** 26 | * [0-Makefile](./0-Makefile): Makefile that creates an executable `school` based on 27 | [school.c](./school.c) and [main.c](./main.c). Includes: 28 | * Rule `all` that builds the executable. 29 | 30 | * **1. make -f 1-Makefile** 31 | * [1-Makefile](./1-Makefile): Makefile that creates an executable `school` based on 32 | [school.c](./school.c) and [main.c](./main.c). Builds on [0-Makefile](./0-Makefile) 33 | with: 34 | * Variable `CC` that defines the compiler to be used. 35 | * Variable `SRC` that defines the `.c` files to compile. 36 | * The `all` rule only recompiles updated source files. 37 | 38 | * **2. make -f 2-Makefile** 39 | * [2-Makefile](./2-Makefile): Makefile that creates an executable `school` based on 40 | [school.c](./school.c) and [main.c](./main.c). Builds on [1-Makefile](./1-Makefile) 41 | with: 42 | * Variable `OBJ` that defines the `.o` files to compile. 43 | * Variable `NAME` that defines the name of the executable. 44 | 45 | * **3. make -f 3-Makefile** 46 | * [3-Makefile](./3-Makefile): Makefile that creates an executable `school` based on 47 | [school.c](./school.c) and [main.c](./main.c). Builds on [2-Makefile](./2-Makefile) 48 | with: 49 | * Rule `clean` that deletes all Emacs/Vim temporary files as well as the 50 | executable. 51 | * Rule `oclean` that deletes the object files. 52 | * Rule `fclean` that deltes all of the temporary files, executable, and 53 | object files. 54 | * Rule `re` that forces recompilation of all source files. 55 | * Variable `RM` that defines the command to delete files. 56 | 57 | * **4. A complete Makefile** 58 | * [4-Makefile](./4-Makefile): Makefile that creates an executable `school` based on 59 | [school.c](./school.c) and [main.c](./main.c). Builds on [3-Makefile](./3-Makefile) 60 | with: 61 | * Variable `CFLAGS` that defines the compiler flags `-Wall -Werror -Wextra 62 | -pedantic`. 63 | 64 | * **5. Island Perimeter** 65 | * [5-island_perimeter.py](./5-island_perimeter.py): Python function that returns the 66 | perimeter of an island defined in a grid. 67 | * Prototype: `def island_perimeter(grid):` 68 | * The parameter `grid` is a list of a list of integers. 69 | * Water is represented by `0`. 70 | * Land is represented by `1`. 71 | * Each element of the lists represents a cell square of side length 1. 72 | * Grid cells are connected horizontally/verticaly (not diagonally). 73 | * The grid is rectangular, with a width and height not exceeding 100. 74 | * The grid is completely surrounded by water, and there is either exactly 75 | one island or nothing. 76 | * The island does not contain lakes (water inside disconnected from 77 | surrounding land). 78 | 79 | * **6. make -f 100-Makefile** 80 | * [100-Makefile](./100-Makefile): Makefile that creates an executable `school` based on 81 | [school.c](./school.c) and [main.c](./main.c). Builds on [4-Makefile](./4-Makefile) 82 | with: 83 | * Does not define the variable `RM`. 84 | * Never uses the string `$(CFLAGS)`. 85 | * Does not compile if the header `m.h` is missing. 86 | * Works even if there are existing files of the same name as any of the 87 | Makefile rules in the current directory. 88 | -------------------------------------------------------------------------------- /0x1C-makefiles/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x1C-makefiles/a.out -------------------------------------------------------------------------------- /0x1C-makefiles/images/makefile.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x1C-makefiles/images/makefile.gif -------------------------------------------------------------------------------- /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/makefile.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/0x1C-makefiles/makefile.gif -------------------------------------------------------------------------------- /0x1C-makefiles/school.c: -------------------------------------------------------------------------------- 1 | #include "m.h" 2 | 3 | /** 4 | * print_school 5 | * 6 | * Return: Nothing 7 | */ 8 | void print_school(void) 9 | { 10 | printf("j#0000000000000000000000000000000000000\n"); 11 | printf("j#000000000000000000@Q**g00000000000000\n"); 12 | printf("j#0000000000000000*]++]4000000000000000\n"); 13 | printf("j#000000000000000k]++]++*N#000000000000\n"); 14 | printf("j#0000000000000*C+++]++]++]J*0000000000\n"); 15 | printf("j#00000000000@+]++qwwwp=]++++]*00000000\n"); 16 | printf("j#0000000000*+++]q#0000k+]+]++]4#000000\n"); 17 | printf("j#00000000*C+]+]w#0000*]+++]+]++0000000\n"); 18 | printf("j#0000we+]wW000***C++]++]+]++++40000000\n"); 19 | printf("j#000000000*C+]+]]+]++]++]++]+q#0000000\n"); 20 | printf("j#0000000*]+]+++++++]++]+++]+++J0000000\n"); 21 | printf("j#000000C++]=]+]+]+]++]++]+]+]+]=000000\n"); 22 | printf("j#00000k+]++]+++]+]++qwW0000000AgW00000\n"); 23 | printf("j#00000k++]++]+]+++qW#00000000000000000\n"); 24 | printf("j#00000A]++]++]++]++J**0000000000000000\n"); 25 | printf("j#000000e]++]+++]++]++]J000000000000000\n"); 26 | printf("j#0000000A]++]+]++]++]++000000000000000\n"); 27 | printf("j#000000000w]++]+]++]+qW#00000000000000\n"); 28 | printf("j#00000000000w]++++]*0##000000000000000\n"); 29 | printf("j#0000000000000Ag]+]++*0000000000000000\n"); 30 | printf("j#00000000000000000we]+]Q00000000000000\n"); 31 | printf("j#0000000000000@@+wgdA]+J00000000000000\n"); 32 | printf("j#0000000000000k?qwgdC=]4#0000000000000\n"); 33 | printf("j#00000000000000w]+]++qw#00000000000000\n"); 34 | printf("\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); 35 | } 36 | -------------------------------------------------------------------------------- /0x1C-makefiles/tests/5-main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """ 3 | 5-main 4 | """ 5 | island_perimeter = __import__('5-island_perimeter').island_perimeter 6 | 7 | if __name__ == "__main__": 8 | grid = [ 9 | [0, 0, 0, 0, 0, 0], 10 | [0, 1, 0, 0, 0, 0], 11 | [0, 1, 0, 0, 0, 0], 12 | [0, 1, 1, 1, 0, 0], 13 | [0, 0, 0, 0, 0, 0] 14 | ] 15 | print(island_perimeter(grid)) 16 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/0-linear.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_search - searches for a value in an array of 5 | * integers using the Linear search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: Always EXIT_SUCCESS 11 | */ 12 | int linear_search(int *array, size_t size, int value) 13 | { 14 | int i; 15 | 16 | if (array == NULL) 17 | return (-1); 18 | 19 | for (i = 0; i < (int)size; i++) 20 | { 21 | printf("Value checked array[%u] = [%d]\n", i, array[i]); 22 | if (value == array[i]) 23 | return (i); 24 | } 25 | return (-1); 26 | } 27 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search - Searches for a value in a sorted array 5 | * of integers using binary search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @size: The number of elements in the array. 8 | * @value: The value to search for. 9 | * 10 | * Return: If the value is not present or the array is NULL, -1. 11 | * Otherwise, the index where the value is located. 12 | * 13 | * Description: Prints the [sub]array being searched after each change. 14 | */ 15 | int binary_search(int *array, size_t size, int value) 16 | { 17 | size_t i, left, right; 18 | 19 | if (array == NULL) 20 | return (-1); 21 | 22 | for (left = 0, right = size - 1; right >= left;) 23 | { 24 | printf("Searching in array: "); 25 | for (i = left; i < right; i++) 26 | printf("%d, ", array[i]); 27 | printf("%d\n", array[i]); 28 | 29 | i = left + (right - left) / 2; 30 | if (array[i] == value) 31 | return (i); 32 | if (array[i] > value) 33 | right = i - 1; 34 | else 35 | left = i + 1; 36 | } 37 | 38 | return (-1); 39 | } 40 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/1-binary_recursive.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * binary_search_recursive - Searches recursively for a value in a sorted 5 | * array of integers using binary search. 6 | * @array: A pointer to the first element of the [sub]array to search. 7 | * @left: The starting index of the [sub]array to search. 8 | * @right: The ending index of the [sub]array to search. 9 | * @value: The value to search for. 10 | * 11 | * Return: If the value is not present, -1. 12 | * Otherwise, the index where the value is located. 13 | * 14 | * Description: Prints the [sub]array being searched after each change. 15 | */ 16 | int binary_search_recursive(int *array, size_t left, size_t right, int value) 17 | { 18 | size_t i; 19 | 20 | if (right < left) 21 | return (-1); 22 | 23 | printf("Searching in array: "); 24 | for (i = left; i < right; i++) 25 | printf("%d, ", array[i]); 26 | printf("%d\n", array[i]); 27 | 28 | i = left + (right - left) / 2; 29 | if (array[i] == value) 30 | return (i); 31 | if (array[i] > value) 32 | return (binary_search_recursive(array, left, i - 1, value)); 33 | return (binary_search_recursive(array, i + 1, right, value)); 34 | } 35 | 36 | /** 37 | * binary_search - Searches for a value in a sorted array 38 | * of integers using binary search. 39 | * @array: A pointer to the first element of the array to search. 40 | * @size: The number of elements in the array. 41 | * @value: The value to search for. 42 | * 43 | * Return: If the value is not present or the array is NULL, -1. 44 | * Otherwise, the index where the value is located. 45 | * 46 | * Description: Prints the [sub]array being searched after each change. 47 | */ 48 | int binary_search(int *array, size_t size, int value) 49 | { 50 | if (array == NULL) 51 | return (-1); 52 | 53 | return (binary_search_recursive(array, 0, size - 1, value)); 54 | } 55 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/100-jump.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_search - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int jump_search(int *array, size_t size, int value) 14 | { 15 | int index, m, k, prev; 16 | 17 | if (array == NULL || size == 0) 18 | return (-1); 19 | 20 | m = (int)sqrt((double)size); 21 | k = 0; 22 | prev = index = 0; 23 | 24 | do { 25 | printf("Value checked array[%d] = [%d]\n", index, array[index]); 26 | 27 | if (array[index] == value) 28 | return (index); 29 | k++; 30 | prev = index; 31 | index = k * m; 32 | } while (index < (int)size && array[index] < value); 33 | 34 | printf("Value found between indexes [%d] and [%d]\n", prev, index); 35 | 36 | for (; prev <= index && prev < (int)size; prev++) 37 | { 38 | printf("Value checked array[%d] = [%d]\n", prev, array[prev]); 39 | if (array[prev] == value) 40 | return (prev); 41 | } 42 | 43 | return (-1); 44 | } 45 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/101-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/102-interpolation.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * interpolation_search - searches for a value in an array of 5 | * integers using the Interpolation search algorithm 6 | * 7 | * @array: input array 8 | * @size: size of the array 9 | * @value: value to search in 10 | * Return: index of the number 11 | */ 12 | int interpolation_search(int *array, size_t size, int value) 13 | { 14 | size_t pos, low, high; 15 | double f; 16 | 17 | if (array == NULL) 18 | return (-1); 19 | 20 | low = 0; 21 | high = size - 1; 22 | 23 | while (size) 24 | { 25 | f = (double)(high - low) / (array[high] - array[low]) * (value - array[low]); 26 | pos = (size_t)(low + f); 27 | printf("Value checked array[%d]", (int)pos); 28 | 29 | if (pos >= size) 30 | { 31 | printf(" is out of range\n"); 32 | break; 33 | } 34 | else 35 | { 36 | printf(" = [%d]\n", array[pos]); 37 | } 38 | 39 | if (array[pos] == value) 40 | return ((int)pos); 41 | 42 | if (array[pos] < value) 43 | low = pos + 1; 44 | else 45 | high = pos - 1; 46 | 47 | if (low == high) 48 | break; 49 | } 50 | 51 | return (-1); 52 | } 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/103-exponential.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * _binary_search - Searches for a value in a sorted array 5 | * of integers using binary search. 6 | * @array: A pointer to the first element of the array to search. 7 | * @left: The starting index of the [sub]array to search. 8 | * @right: The ending index of the [sub]array to search. 9 | * @value: The value to search for. 10 | * 11 | * Return: If the value is not present or the array is NULL, -1. 12 | * Otherwise, the index where the value is located. 13 | * 14 | * Description: Prints the [sub]array being searched after each change. 15 | */ 16 | int _binary_search(int *array, size_t left, size_t right, int value) 17 | { 18 | size_t i; 19 | 20 | if (array == NULL) 21 | return (-1); 22 | 23 | while (right >= left) 24 | { 25 | printf("Searching in array: "); 26 | for (i = left; i < right; i++) 27 | printf("%d, ", array[i]); 28 | printf("%d\n", array[i]); 29 | 30 | i = left + (right - left) / 2; 31 | if (array[i] == value) 32 | return (i); 33 | if (array[i] > value) 34 | right = i - 1; 35 | else 36 | left = i + 1; 37 | } 38 | 39 | return (-1); 40 | } 41 | 42 | /** 43 | * exponential_search - Searches for a value in a sorted array 44 | * of integers using exponential search. 45 | * @array: A pointer to the first element of the array to search. 46 | * @size: The number of elements in the array. 47 | * @value: The value to search for. 48 | * 49 | * Return: If the value is not present or the array is NULL, -1. 50 | * Otherwise, the index where the value is located. 51 | * 52 | * Description: Prints a value every time it is compared in the array. 53 | */ 54 | int exponential_search(int *array, size_t size, int value) 55 | { 56 | size_t i = 0, right; 57 | 58 | if (array == NULL) 59 | return (-1); 60 | 61 | if (array[0] != value) 62 | { 63 | for (i = 1; i < size && array[i] <= value; i = i * 2) 64 | printf("Value checked array[%ld] = [%d]\n", i, array[i]); 65 | } 66 | 67 | right = i < size ? i : size - 1; 68 | printf("Value found between indexes [%ld] and [%ld]\n", i / 2, right); 69 | return (_binary_search(array, i / 2, right, value)); 70 | } 71 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/104-advanced_binary.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * rec_search - searches for a value in an array of 5 | * integers using the Binary search algorithm 6 | * 7 | * 8 | * @array: input array 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | int rec_search(int *array, size_t size, int value) 14 | { 15 | size_t half = size / 2; 16 | size_t i; 17 | 18 | if (array == NULL || size == 0) 19 | return (-1); 20 | 21 | printf("Searching in array"); 22 | 23 | for (i = 0; i < size; i++) 24 | printf("%s %d", (i == 0) ? ":" : ",", array[i]); 25 | 26 | printf("\n"); 27 | 28 | if (half && size % 2 == 0) 29 | half--; 30 | 31 | if (value == array[half]) 32 | { 33 | if (half > 0) 34 | return (rec_search(array, half + 1, value)); 35 | return ((int)half); 36 | } 37 | 38 | if (value < array[half]) 39 | return (rec_search(array, half + 1, value)); 40 | 41 | half++; 42 | return (rec_search(array + half, size - half, value) + half); 43 | } 44 | 45 | /** 46 | * advanced_binary - calls to rec_search to return 47 | * the index of the number 48 | * 49 | * @array: input array 50 | * @size: size of the array 51 | * @value: value to search in 52 | * Return: index of the number 53 | */ 54 | int advanced_binary(int *array, size_t size, int value) 55 | { 56 | int index; 57 | 58 | index = rec_search(array, size, value); 59 | 60 | if (index >= 0 && array[index] != value) 61 | return (-1); 62 | 63 | return (index); 64 | } 65 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/105-jump_list.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | #include 3 | 4 | /** 5 | * jump_list - searches for a value in an array of 6 | * integers using the Jump search algorithm 7 | * 8 | * @list: input list 9 | * @size: size of the array 10 | * @value: value to search in 11 | * Return: index of the number 12 | */ 13 | listint_t *jump_list(listint_t *list, size_t size, int value) 14 | { 15 | size_t index, k, m; 16 | listint_t *prev; 17 | 18 | if (list == NULL || size == 0) 19 | return (NULL); 20 | 21 | m = (size_t)sqrt((double)size); 22 | index = 0; 23 | k = 0; 24 | 25 | do { 26 | prev = list; 27 | k++; 28 | index = k * m; 29 | 30 | while (list->next && list->index < index) 31 | list = list->next; 32 | 33 | if (list->next == NULL && index != list->index) 34 | index = list->index; 35 | 36 | printf("Value checked at index [%d] = [%d]\n", (int)index, list->n); 37 | 38 | } while (index < size && list->next && list->n < value); 39 | 40 | printf("Value found between indexes "); 41 | printf("[%d] and [%d]\n", (int)prev->index, (int)list->index); 42 | 43 | for (; prev && prev->index <= list->index; prev = prev->next) 44 | { 45 | printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n); 46 | if (prev->n == value) 47 | return (prev); 48 | } 49 | 50 | return (NULL); 51 | } 52 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/106-linear_skip.c: -------------------------------------------------------------------------------- 1 | #include "search_algos.h" 2 | 3 | /** 4 | * linear_skip - searches for a value in a skip list 5 | * 6 | * @list: input list 7 | * @value: value to search in 8 | * Return: index of the number 9 | */ 10 | skiplist_t *linear_skip(skiplist_t *list, int value) 11 | { 12 | skiplist_t *go; 13 | 14 | if (list == NULL) 15 | return (NULL); 16 | 17 | go = list; 18 | 19 | do { 20 | list = go; 21 | go = go->express; 22 | printf("Value checked at index "); 23 | printf("[%d] = [%d]\n", (int)go->index, go->n); 24 | } while (go->express && go->n < value); 25 | 26 | if (go->express == NULL) 27 | { 28 | list = go; 29 | while (go->next) 30 | go = go->next; 31 | } 32 | 33 | printf("Value found between indexes "); 34 | printf("[%d] and [%d]\n", (int)list->index, (int)go->index); 35 | 36 | while (list != go->next) 37 | { 38 | printf("Value checked at index [%d] = [%d]\n", (int)list->index, list->n); 39 | if (list->n == value) 40 | return (list); 41 | list = list->next; 42 | } 43 | 44 | return (NULL); 45 | } 46 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/107-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/108-O: -------------------------------------------------------------------------------- 1 | O(sqrt(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/2-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/3-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/4-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/5-O: -------------------------------------------------------------------------------- 1 | O(1) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/6-O: -------------------------------------------------------------------------------- 1 | O(nm) 2 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/create_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | void free_list(listint_t *list); 5 | 6 | /** 7 | * create_list - Creates a single linked list 8 | * 9 | * @array: Pointer to the array to use to fill the list 10 | * @size: Size of the array 11 | * 12 | * Return: A pointer to the head of the created list (NULL on failure) 13 | */ 14 | listint_t *create_list(int *array, size_t size) 15 | { 16 | listint_t *list; 17 | listint_t *node; 18 | 19 | list = NULL; 20 | while (array && size--) 21 | { 22 | node = malloc(sizeof(*node)); 23 | if (!node) 24 | { 25 | free_list(list); 26 | return (NULL); 27 | } 28 | node->n = array[size]; 29 | node->index = size; 30 | node->next = list; 31 | list = node; 32 | } 33 | return (list); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/free_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | /** 5 | * free_list - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_list(listint_t *list) 10 | { 11 | listint_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_list(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/listint/print_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | /** 5 | * print_list - Prints the content of a listint_t 6 | * 7 | * @list: Pointer to the head of the list 8 | */ 9 | void print_list(const listint_t *list) 10 | { 11 | printf("List :\n"); 12 | while (list) 13 | { 14 | printf("Index[%lu] = [%d]\n", list->index, list->n); 15 | list = list->next; 16 | } 17 | printf("\n"); 18 | } 19 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/search_algos.h: -------------------------------------------------------------------------------- 1 | #ifndef _SORTING_ALGOS_H_ 2 | #define _SORTING_ALGOS_H_ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * struct listint_s - singly linked list 9 | * 10 | * @n: Integer 11 | * @index: Index of the node in the list 12 | * @next: Pointer to the next node 13 | * 14 | * Description: singly linked list node structure 15 | * for ALX project 16 | */ 17 | typedef struct listint_s 18 | { 19 | int n; 20 | size_t index; 21 | struct listint_s *next; 22 | } listint_t; 23 | 24 | /** 25 | * struct skiplist_s - Singly linked list with an express lane 26 | * 27 | * @n: Integer 28 | * @index: Index of the node in the list 29 | * @next: Pointer to the next node 30 | * @express: Pointer to the next node in the express lane 31 | * 32 | * Description: singly linked list node structure with an express lane 33 | * for Holberton project 34 | */ 35 | typedef struct skiplist_s 36 | { 37 | int n; 38 | size_t index; 39 | struct skiplist_s *next; 40 | struct skiplist_s *express; 41 | } skiplist_t; 42 | 43 | int linear_search(int *array, size_t size, int value); 44 | int binary_search(int *array, size_t size, int value); 45 | int jump_search(int *array, size_t size, int value); 46 | int interpolation_search(int *array, size_t size, int value); 47 | int exponential_search(int *array, size_t size, int value); 48 | int advanced_binary(int *array, size_t size, int value); 49 | listint_t *jump_list(listint_t *list, size_t size, int value); 50 | skiplist_t *linear_skip(skiplist_t *list, int value); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/create_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | void free_skiplist(skiplist_t *list); 6 | 7 | /** 8 | * init_express - Initializes the express lane of the linked list 9 | * 10 | * @list: Pointer to the head node of the list 11 | * @size: Number of nodes in the list 12 | */ 13 | void init_express(skiplist_t *list, size_t size) 14 | { 15 | const size_t step = sqrt(size); 16 | size_t i; 17 | skiplist_t *save; 18 | 19 | for (save = list, i = 0; i < size; ++i, list = list->next) 20 | { 21 | if (i % step == 0) 22 | { 23 | save->express = list; 24 | save = list; 25 | } 26 | } 27 | } 28 | 29 | /** 30 | * create_skiplist - Create a single linked list 31 | * 32 | * @array: Pointer to the array used to fill the list 33 | * @size: Size of the array 34 | * 35 | * Return: A pointer to the head of the created list (NULL on failure) 36 | */ 37 | skiplist_t *create_skiplist(int *array, size_t size) 38 | { 39 | skiplist_t *list; 40 | skiplist_t *node; 41 | size_t save_size; 42 | 43 | list = NULL; 44 | save_size = size; 45 | while (array && size--) 46 | { 47 | node = malloc(sizeof(*node)); 48 | if (!node) 49 | { 50 | free_skiplist(list); 51 | return (NULL); 52 | } 53 | node->n = array[size]; 54 | node->index = size; 55 | node->express = NULL; 56 | node->next = list; 57 | list = node; 58 | } 59 | init_express(list, save_size); 60 | return (list); 61 | } 62 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/free_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../search_algos.h" 3 | 4 | /** 5 | * free_skiplist - Deallocates a singly linked list 6 | * 7 | * @list: Pointer to the linked list to be freed 8 | */ 9 | void free_skiplist(skiplist_t *list) 10 | { 11 | skiplist_t *node; 12 | 13 | if (list) 14 | { 15 | node = list->next; 16 | free(list); 17 | free_skiplist(node); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/skiplist/print_skiplist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * print_skiplist - dump the content of a skiplist_t 7 | * 8 | * @list: Pointer to the head of the list 9 | * 10 | * Return: void 11 | */ 12 | void print_skiplist(const skiplist_t *list) 13 | { 14 | const skiplist_t *node; 15 | 16 | printf("List :\n"); 17 | for (node = list; node; node = node->next) 18 | { 19 | printf("Index[%lu] = [%d]\n", node->index, node->n); 20 | } 21 | printf("\nExpress lane :\n"); 22 | for (node = list; node; node = node->express) 23 | { 24 | printf("Index[%lu] = [%d]\n", node->index, node->n); 25 | } 26 | printf("\n"); 27 | } 28 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); 19 | printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); 18 | printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); 19 | printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 6, jump_search(array, size, 6)); 18 | printf("Found %d at index: %d\n\n", 1, jump_search(array, size, 1)); 19 | printf("Found %d at index: %d\n", 999, jump_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 3, interpolation_search(array, size, 3)); 18 | printf("Found %d at index: %d\n\n", 7, interpolation_search(array, size, 7)); 19 | printf("Found %d at index: %d\n", 999, interpolation_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 54, 61, 62, 76, 99 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 62, exponential_search(array, size, 62)); 18 | printf("Found %d at index: %d\n\n", 3, exponential_search(array, size, 3)); 19 | printf("Found %d at index: %d\n", 999, exponential_search(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/104-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always EXIT_SUCCESS 9 | */ 10 | int main(void) 11 | { 12 | int array[] = { 13 | 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 14 | }; 15 | size_t size = sizeof(array) / sizeof(array[0]); 16 | 17 | printf("Found %d at index: %d\n\n", 8, advanced_binary(array, size, 8)); 18 | printf("Found %d at index: %d\n\n", 5, advanced_binary(array, size, 5)); 19 | printf("Found %d at index: %d\n", 999, advanced_binary(array, size, 999)); 20 | return (EXIT_SUCCESS); 21 | } 22 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/105-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | listint_t *create_list(int *array, size_t size); 6 | void print_list(const listint_t *list); 7 | void free_list(listint_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | listint_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_list(array, size); 23 | print_list(list); 24 | 25 | res = jump_list(list, size, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = jump_list(list, size, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = jump_list(list, size, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_list(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /0x1E-search_algorithms/tests/106-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../search_algos.h" 4 | 5 | skiplist_t *create_skiplist(int *array, size_t size); 6 | void print_skiplist(const skiplist_t *list); 7 | void free_skiplist(skiplist_t *list); 8 | 9 | /** 10 | * main - Entry point 11 | * 12 | * Return: Always EXIT_SUCCESS 13 | */ 14 | int main(void) 15 | { 16 | skiplist_t *list, *res; 17 | int array[] = { 18 | 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 19 | }; 20 | size_t size = sizeof(array) / sizeof(array[0]); 21 | 22 | list = create_skiplist(array, size); 23 | print_skiplist(list); 24 | 25 | res = linear_skip(list, 53); 26 | printf("Found %d at index: %lu\n\n", 53, res->index); 27 | res = linear_skip(list, 2); 28 | printf("Found %d at index: %lu\n\n", 2, res->index); 29 | res = linear_skip(list, 999); 30 | printf("Found %d at index: %p\n", 999, (void *) res); 31 | 32 | free_skiplist(list); 33 | return (EXIT_SUCCESS); 34 | } 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # C is FUN 😊 4 | 5 | >Learning C right now, been wanting to get on this. It's going to be FUN !!. 6 | 7 |

8 | 9 |

10 | 11 | ## About 12 | 13 | - This repository consists of all the C programming projects done with [ALX Africa](https://www.alxafrica.com/) Full stack Software Engineering course in partnership with [Holberton School](https://www.holbertonschool.com/) - now aquired by the **AFRICA LEADERSHIP GROUP**(ALG, ALX, ALU) by [Ajiboye Adeleye](https://www.github.com/Adeleye080). 14 | - All main.c files are prewritten by the school. We build functions that produce a specific output while also taking into consideration edge cases. 15 | 16 | --- 17 | 18 | ## Resource 19 | 20 | - C [Books and PDF's](./references) to check out and use as a reference. 21 | 22 | --- 23 | 24 | - [0x00](./0x00-hello_world) : Hello, World. 25 | - [0x01](./0x01-variables_if_else_while) : Variables,if,else,while. 26 | - [0x02](./0x02-functions_nested_loops) : Functions, nested loops. 27 | - [0x03](./0x03-debugging) : Debugging. 28 | - [0x04](./0x04-more_functions_nested_loops) : More Functions and nested loops. 29 | - [0x05](./0x05-pointers_arrays_strings) : Pointers, arrays and strings. 30 | - [0x06](./0x06-pointers_arrays_strings) : More pointers, arrays and strings. 31 | - [0x07](./0x07-pointers_arrays_strings) : Even more pointers, arrays and strings. 32 | - [0x08](./0x08-recursion) : Recursion. 33 | - [0x09](./0x09-static_libraries) : Static libraries. 34 | - [0x0A](./0x0A-argc_argv) : argc, argv[]. 35 | - [0x0B](./0x0B-malloc_free) : Malloc, free. 36 | - [0x0C](./0x0C-more_malloc_free) : More malloc, free. 37 | - [0x0D](./0x0D-preprocessor) : Preprocessor. 38 | - [0x0E](./0x0E-structures_typedef) : Structures, typedef. 39 | - [0x0F](./0x0F-function_pointers) : Function pointers. 40 | - [0x10](./0x10-variadic_functions) : Variadic functions. 41 | - [0x11](https://github.com/Adeleye080/printf) : Write our own printf project. 42 | - [0x12](./0x12-singly_linked_lists) : Singly linked lists. 43 | - [0x13](./0x13-more_singly_linked_lists) : More singly linked lists. 44 | - [0x14](./0x14-bit_manipulation) : Bit manipulation. 45 | - [0x15](./0x15-file_io) : File I/O. 46 | - [0x1A](./0x1A-hash_tables) : Hash tables 47 | - [0x1C](./0x1C-makefiles) : Makefiles 48 | - [0x1E](./0x1E-search_algorithms) : Search Algorithms 49 | 50 | --- 51 | 52 | ### Author 53 | - Ajiboye Adeleye 54 | - Github 55 | - LinkedIn 56 | - e-mail 57 | - Twitter 58 | 59 | 60 | --- 61 | ### Acknowledgements :pray: 62 | All of the work in this project was conducted as part of the ALX-SE program's curriculum. ALX Africa is an **ALG** online full-stack software engineering school that uses project-based peer learning to educate students for careers in the IT industry. Visit this website for further information. 63 | -------------------------------------------------------------------------------- /references/An Introduction to the C Programming Language and Software Design.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/An Introduction to the C Programming Language and Software Design.pdf -------------------------------------------------------------------------------- /references/DynamicMemoryAllocation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/DynamicMemoryAllocation.pdf -------------------------------------------------------------------------------- /references/EverythingYouNeedToKnowToStartwithC.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/EverythingYouNeedToKnowToStartwithC.pdf -------------------------------------------------------------------------------- /references/Learn-c-the-hard-way.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/Learn-c-the-hard-way.pdf -------------------------------------------------------------------------------- /references/ModernC.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/ModernC.pdf -------------------------------------------------------------------------------- /references/Programming_in_C_4th_Edition_by_Stephen_Kochan.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/Programming_in_C_4th_Edition_by_Stephen_Kochan.pdf -------------------------------------------------------------------------------- /references/Recursion.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/Recursion.pdf -------------------------------------------------------------------------------- /references/Structures.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/Structures.pdf -------------------------------------------------------------------------------- /references/structs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/structs.pdf -------------------------------------------------------------------------------- /references/typedef.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Adeleye080/low_level_programming/a9de6ee9d7f42910f0d2b142b4a5924e882a1d2b/references/typedef.pdf --------------------------------------------------------------------------------